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);
}
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";
}
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());
}
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);
}
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());
}
Aggregations