Search in sources :

Example 1 with Charge

use of com.stripe.model.Charge 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 Charge

use of com.stripe.model.Charge in project tutorials by eugenp.

the class ChargeController method charge.

@PostMapping("/charge")
public String charge(ChargeRequest chargeRequest, Model model) throws StripeException {
    chargeRequest.setDescription("Example charge");
    chargeRequest.setCurrency(Currency.EUR);
    Charge charge = paymentsService.charge(chargeRequest);
    model.addAttribute("id", charge.getId());
    model.addAttribute("status", charge.getStatus());
    model.addAttribute("chargeId", charge.getId());
    model.addAttribute("balance_transaction", charge.getBalanceTransaction());
    return "result";
}
Also used : Charge(com.stripe.model.Charge) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with Charge

use of com.stripe.model.Charge in project stripe-java by stripe.

the class ChargeTest method testChargeRetrieve.

@Test
public void testChargeRetrieve() throws StripeException {
    Charge createdCharge = Charge.create(defaultChargeParams);
    Charge retrievedCharge = Charge.retrieve(createdCharge.getId());
    assertEquals(createdCharge.getCreated(), retrievedCharge.getCreated());
    assertEquals(createdCharge.getId(), retrievedCharge.getId());
    assertNotNull(retrievedCharge.getSource());
    assertEquals("card", retrievedCharge.getSource().getObject());
    Card card = (Card) retrievedCharge.getSource();
    assertEquals("4242", card.getLast4());
    // BT Checks:
    assertNotNull(retrievedCharge.getBalanceTransaction());
    assertNull(retrievedCharge.getBalanceTransactionObject());
}
Also used : Charge(com.stripe.model.Charge) Card(com.stripe.model.Card) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 4 with Charge

use of com.stripe.model.Charge in project stripe-java by stripe.

the class ChargeTest method testChargeCreateWithShippingDetails.

@Test
public void testChargeCreateWithShippingDetails() throws StripeException {
    ShippingDetails shippingDetails = new ShippingDetails();
    shippingDetails.setName("name");
    shippingDetails.setPhone("123-456-7890");
    Address address = new Address().setCity("Washington").setCountry("USA").setLine1("1600 Pennsylvania Ave.").setLine2("line 2 address").setPostalCode("20500").setState("D.C.");
    shippingDetails.setAddress(address);
    Map<String, Object> params = ImmutableMap.<String, Object>builder().putAll(defaultChargeParams).put("shipping", ImmutableMap.builder().put("address", ImmutableMap.builder().put("line1", address.getLine1()).put("line2", address.getLine2()).put("city", address.getCity()).put("country", address.getCountry()).put("postal_code", address.getPostalCode()).put("state", address.getState()).build()).put("name", shippingDetails.getName()).put("phone", shippingDetails.getPhone()).build()).build();
    Charge createdCharge = Charge.create(params);
    assertEquals(createdCharge.getShipping(), shippingDetails);
}
Also used : Address(com.stripe.model.Address) ShippingDetails(com.stripe.model.ShippingDetails) Charge(com.stripe.model.Charge) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 5 with Charge

use of com.stripe.model.Charge in project stripe-java by stripe.

the class ChargeTest method testChargeCapture.

@Test
public void testChargeCapture() throws StripeException {
    Map<String, Object> options = new HashMap<String, Object>(defaultChargeParams);
    options.put("capture", false);
    Charge created = Charge.create(options);
    assertFalse(created.getCaptured());
    Charge captured = created.capture();
    assertTrue(captured.getCaptured());
}
Also used : HashMap(java.util.HashMap) Charge(com.stripe.model.Charge) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Aggregations

Charge (com.stripe.model.Charge)49 BaseStripeFunctionalTest (com.stripe.BaseStripeFunctionalTest)36 Test (org.junit.Test)36 HashMap (java.util.HashMap)19 RequestOptions (com.stripe.net.RequestOptions)12 Refund (com.stripe.model.Refund)8 Dispute (com.stripe.model.Dispute)7 Card (com.stripe.model.Card)5 EvidenceSubObject (com.stripe.model.EvidenceSubObject)5 Environment (org.springframework.core.env.Environment)5 PaymentResult (alfio.manager.support.PaymentResult)4 PaymentInformation (alfio.model.PaymentInformation)4 StripeException (com.stripe.exception.StripeException)3 BalanceTransaction (com.stripe.model.BalanceTransaction)3 ConfigurationManager (alfio.manager.system.ConfigurationManager)2 PurchaseContext (alfio.model.PurchaseContext)2 ConfigurationKeys (alfio.model.system.ConfigurationKeys)2 StripeCreditCardToken (alfio.model.transaction.token.StripeCreditCardToken)2 ConfigurationRepository (alfio.repository.system.ConfigurationRepository)2 Stripe (com.stripe.Stripe)2