/** * This method processes the pending payment using the configured payment gateway (at the time of * writing, only STRIPE) and returns a PaymentResult. In order to preserve the consistency of the * payment, when a non-gateway Exception is thrown, it rethrows an IllegalStateException * * @param reservationId * @param gatewayToken * @param price * @param event * @param email * @param customerName * @param billingAddress * @return PaymentResult * @throws java.lang.IllegalStateException if there is an error after charging the credit card */ public PaymentResult processPayment( String reservationId, String gatewayToken, int price, Event event, String email, CustomerName customerName, String billingAddress) { try { final Charge charge = stripeManager.chargeCreditCard( gatewayToken, price, event, reservationId, email, customerName.getFullName(), billingAddress); log.info("transaction {} paid: {}", reservationId, charge.getPaid()); transactionRepository.insert( charge.getId(), reservationId, ZonedDateTime.now(), price, event.getCurrency(), charge.getDescription(), PaymentProxy.STRIPE.name()); return PaymentResult.successful(charge.getId()); } catch (Exception e) { if (e instanceof StripeException) { return PaymentResult.unsuccessful(stripeManager.handleException((StripeException) e)); } throw new IllegalStateException(e); } }
/** * Ensure the provided parameter for API key is actually being used. All other PerCallAPIKey * methods assume this part works. * * @throws StripeException */ @Test public void testPerCallAPIUsage() throws StripeException { Charge createdCharge = Charge.create(defaultChargeParams, Stripe.apiKey); assertFalse(createdCharge.getRefunded()); try { Charge.create(defaultChargeParams, "INVALID_KEY_HERE"); fail(); } catch (Exception e) { } }
@Test public void testChargeRetrievePerCallAPIKey() throws StripeException { Charge createdCharge = Charge.create(defaultChargeParams, Stripe.apiKey); Charge retrievedCharge = Charge.retrieve(createdCharge.getId(), Stripe.apiKey); assertEquals(createdCharge.getCreated(), retrievedCharge.getCreated()); assertEquals(createdCharge.getId(), retrievedCharge.getId()); }
@Test public void testChargeListPerCallAPIKey() throws StripeException { Map<String, Object> listParams = new HashMap<String, Object>(); listParams.put("count", 1); List<Charge> charges = Charge.all(listParams, Stripe.apiKey).getData(); assertEquals(charges.size(), 1); }
@Test public void testTokenUsePerCallAPIKey() throws StripeException { Token createdToken = Token.create(defaultTokenParams, Stripe.apiKey); Map<String, Object> chargeWithTokenParams = new HashMap<String, Object>(); chargeWithTokenParams.put("amount", 199); chargeWithTokenParams.put("currency", "usd"); chargeWithTokenParams.put("card", createdToken.getId()); Charge.create(chargeWithTokenParams, Stripe.apiKey); Token retrievedToken = Token.retrieve(createdToken.getId(), Stripe.apiKey); assertTrue(retrievedToken.getUsed()); }
@Test(expected = CardException.class) public void testInvalidCardPerCallAPIKey() throws StripeException { Map<String, Object> invalidChargeParams = new HashMap<String, Object>(); invalidChargeParams.putAll(defaultChargeParams); Map<String, Object> invalidCardParams = new HashMap<String, Object>(); invalidCardParams.put("number", "4242424242424241"); invalidCardParams.put("exp_month", 12); invalidCardParams.put("exp_year", 2015); invalidChargeParams.put("card", invalidCardParams); Charge.create(invalidChargeParams, Stripe.apiKey); }
@Test public void testChargeCreate() throws StripeException { Charge createdCharge = Charge.create(defaultChargeParams); assertFalse(createdCharge.getRefunded()); assertEquals(1, createdCharge.getFeeDetails().size()); Fee fee = createdCharge.getFeeDetails().get(0); assertEquals("stripe_fee", fee.getType()); assertEquals(createdCharge.getFee(), fee.getAmount()); assertEquals(createdCharge.getCurrency(), fee.getCurrency()); assertEquals(null, fee.getApplication()); }
@Test public void testChargePartialRefundPerCallAPIKey() throws StripeException { Charge createdCharge = Charge.create(defaultChargeParams); Map<String, Object> refundParams = new HashMap<String, Object>(); final Integer REFUND_AMOUNT = 50; refundParams.put("amount", REFUND_AMOUNT); Charge refundedCharge = createdCharge.refund(refundParams, Stripe.apiKey); assertFalse(refundedCharge.getRefunded()); assertEquals(refundedCharge.getAmountRefunded(), REFUND_AMOUNT); }
@Test public void testChargeCapture() throws StripeException { HashMap<String, Object> options = (HashMap<String, Object>) defaultChargeParams.clone(); options.put("capture", false); Charge created = Charge.create(options); assertFalse(created.getCaptured()); Charge captured = created.capture(); assertTrue(captured.getCaptured()); }
@Test public void testChargeRefundPerCallAPIKey() throws StripeException { Charge createdCharge = Charge.create(defaultChargeParams, Stripe.apiKey); Charge refundedCharge = createdCharge.refund(Stripe.apiKey); assertTrue(refundedCharge.getRefunded()); }
@Test public void testChargeCreatePerCallAPIKey() throws StripeException { Charge createdCharge = Charge.create(defaultChargeParams, Stripe.apiKey); assertFalse(createdCharge.getRefunded()); }
@Test public void testChargeRefund() throws StripeException { Charge createdCharge = Charge.create(defaultChargeParams); Charge refundedCharge = createdCharge.refund(); assertTrue(refundedCharge.getRefunded()); }