Search in sources :

Example 1 with RequestOptions

use of com.stripe.net.RequestOptions in project alf.io by alfio-event.

the class StripeManager method chargeCreditCard.

/**
 * After client side integration with stripe widget, our server receives the stripeToken
 * StripeToken is a single-use token that allows our server to actually charge the credit card and
 * get money on our account.
 * <p>
 * as documented in https://stripe.com/docs/tutorials/charges
 *
 * @return
 * @throws StripeException
 */
Optional<Charge> chargeCreditCard(String stripeToken, long amountInCent, Event event, String reservationId, String email, String fullName, String billingAddress) throws StripeException {
    int tickets = ticketRepository.countTicketsInReservation(reservationId);
    Map<String, Object> chargeParams = new HashMap<>();
    chargeParams.put("amount", amountInCent);
    FeeCalculator.getCalculator(event, configurationManager).apply(tickets, amountInCent).ifPresent(fee -> chargeParams.put("application_fee", fee));
    chargeParams.put("currency", event.getCurrency());
    chargeParams.put("card", stripeToken);
    chargeParams.put("description", String.format("%d ticket(s) for event %s", tickets, event.getDisplayName()));
    Map<String, String> initialMetadata = new HashMap<>();
    initialMetadata.put("reservationId", reservationId);
    initialMetadata.put("email", email);
    initialMetadata.put("fullName", fullName);
    if (StringUtils.isNotBlank(billingAddress)) {
        initialMetadata.put("billingAddress", billingAddress);
    }
    chargeParams.put("metadata", initialMetadata);
    Optional<RequestOptions> opt = options(event);
    if (!opt.isPresent()) {
        return Optional.empty();
    }
    RequestOptions options = opt.get();
    Charge charge = Charge.create(chargeParams, options);
    if (charge.getBalanceTransactionObject() == null) {
        try {
            charge.setBalanceTransactionObject(retrieveBalanceTransaction(charge.getBalanceTransaction(), options));
        } catch (Exception e) {
            log.warn("can't retrieve balance transaction", e);
        }
    }
    return Optional.of(charge);
}
Also used : RequestOptions(com.stripe.net.RequestOptions) Charge(com.stripe.model.Charge)

Example 2 with RequestOptions

use of com.stripe.net.RequestOptions in project stripe-java by stripe.

the class Subscription method deleteDiscount.

/**
 * Deletes the subscription's discount.
 *
 * @deprecated Use {@link #deleteDiscount(RequestOptions)} instead.
 */
@Deprecated
public void deleteDiscount(String apiKey) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {
    RequestOptions result = null;
    if (apiKey != null) {
        result = RequestOptions.builder().setApiKey(apiKey).build();
    }
    deleteDiscount(result);
}
Also used : RequestOptions(com.stripe.net.RequestOptions)

Example 3 with RequestOptions

use of com.stripe.net.RequestOptions in project stripe-java by stripe.

the class IdempotentTest method testNotIdempotentWhenUnset.

@Test
public void testNotIdempotentWhenUnset() throws CardException, APIException, AuthenticationException, InvalidRequestException, APIConnectionException {
    RequestOptions options = RequestOptions.builder().build();
    Charge firstCharge = Charge.create(defaultChargeParams, options);
    Charge secondCharge = Charge.create(defaultChargeParams, options);
    assertNotSame(firstCharge.getId(), secondCharge.getId());
}
Also used : RequestOptions(com.stripe.net.RequestOptions) Charge(com.stripe.model.Charge) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 4 with RequestOptions

use of com.stripe.net.RequestOptions in project stripe-java by stripe.

the class IdempotentTest method testDefaultOptionsHaveUnsetIdempotentRequest.

@Test
public void testDefaultOptionsHaveUnsetIdempotentRequest() throws CardException, APIException, AuthenticationException, InvalidRequestException, APIConnectionException {
    RequestOptions options = RequestOptions.getDefault();
    Charge firstCharge = Charge.create(defaultChargeParams, options);
    Charge secondCharge = Charge.create(defaultChargeParams, options);
    assertNotSame(firstCharge.getId(), secondCharge.getId());
}
Also used : RequestOptions(com.stripe.net.RequestOptions) Charge(com.stripe.model.Charge) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 5 with RequestOptions

use of com.stripe.net.RequestOptions in project stripe-java by stripe.

the class RelayTest method testSKUCreateReadUpdate.

@Test
public void testSKUCreateReadUpdate() throws StripeException {
    final RequestOptions relayRequestOptions = RequestOptions.builder().setApiKey("sk_test_JieJALRz7rPz7boV17oMma7a").build();
    Map<String, Object> productCreateParams = new HashMap<String, Object>();
    String productId = "my_first_product_" + UUID.randomUUID();
    productCreateParams.put("id", productId);
    productCreateParams.put("type", "good");
    productCreateParams.put("name", "Watermelon");
    productCreateParams.put("attributes[]", "size");
    Product.create(productCreateParams, relayRequestOptions);
    Map<String, Object> skuCreateParams = new HashMap<String, Object>();
    String skuId = "my_first_sku_" + UUID.randomUUID();
    skuCreateParams.put("id", skuId);
    skuCreateParams.put("product", productId);
    skuCreateParams.put("attributes", ImmutableMap.of("size", "large"));
    skuCreateParams.put("price", 100);
    skuCreateParams.put("currency", "usd");
    skuCreateParams.put("inventory", ImmutableMap.of("type", "infinite"));
    SKU created = SKU.create(skuCreateParams, relayRequestOptions);
    assertEquals(skuId, created.getId());
    assertEquals(productId, created.getProduct());
    assertEquals("large", created.getAttributes().get("size"));
    assertEquals("infinite", created.getInventory().getType());
    SKU retrieved = SKU.retrieve(skuId, relayRequestOptions);
    assertEquals("large", retrieved.getAttributes().get("size"));
    SKU updated = retrieved.update(ImmutableMap.<String, Object>of("price", 200), relayRequestOptions);
    assertEquals((Integer) 200, updated.getPrice());
}
Also used : RequestOptions(com.stripe.net.RequestOptions) HashMap(java.util.HashMap) SKU(com.stripe.model.SKU) DeletedSKU(com.stripe.model.DeletedSKU) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Aggregations

RequestOptions (com.stripe.net.RequestOptions)29 Test (org.junit.Test)19 BaseStripeFunctionalTest (com.stripe.BaseStripeFunctionalTest)12 Charge (com.stripe.model.Charge)12 HashMap (java.util.HashMap)11 BaseStripeTest (com.stripe.BaseStripeTest)5 RequestOptionsBuilder (com.stripe.net.RequestOptions.RequestOptionsBuilder)4 PaymentInformation (alfio.model.PaymentInformation)3 DeletedProduct (com.stripe.model.DeletedProduct)2 DeletedSKU (com.stripe.model.DeletedSKU)2 Product (com.stripe.model.Product)2 Refund (com.stripe.model.Refund)2 SKU (com.stripe.model.SKU)2 FeeCalculator (alfio.manager.support.FeeCalculator)1 PaymentResult (alfio.manager.support.PaymentResult)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 UserManager (alfio.manager.user.UserManager)1 Configurable (alfio.model.Configurable)1 PurchaseContext (alfio.model.PurchaseContext)1 PurchaseContextType (alfio.model.PurchaseContext.PurchaseContextType)1