Search in sources :

Example 21 with Charge

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

the class RefundTest method testChargeRefund.

@Test
public void testChargeRefund() throws StripeException {
    Charge createdCharge = Charge.create(defaultChargeParams);
    Charge refundedCharge = createdCharge.refund();
    assertTrue(refundedCharge.getRefunded());
    ChargeRefundCollection refunds = refundedCharge.getRefunds();
    assertTrue(refunds.getData() instanceof List);
    assertEquals(1, refunds.getData().size());
    assertTrue(refunds.getData().get(0) instanceof Refund);
}
Also used : ChargeRefundCollection(com.stripe.model.ChargeRefundCollection) Refund(com.stripe.model.Refund) Charge(com.stripe.model.Charge) List(java.util.List) LinkedList(java.util.LinkedList) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 22 with Charge

use of com.stripe.model.Charge in project alf.io by alfio-event.

the class BaseStripeManager method getInfo.

Optional<PaymentInformation> getInfo(Transaction transaction, PurchaseContext purchaseContext) {
    try {
        Optional<RequestOptions> requestOptionsOptional = options(purchaseContext);
        if (requestOptionsOptional.isPresent()) {
            RequestOptions options = requestOptionsOptional.get();
            Charge charge = Charge.retrieve(transaction.getTransactionId(), options);
            String paidAmount = MonetaryUtil.formatCents(charge.getAmount(), charge.getCurrency());
            String refundedAmount = MonetaryUtil.formatCents(charge.getAmountRefunded(), charge.getCurrency());
            List<BalanceTransaction.Fee> fees = retrieveBalanceTransaction(charge.getBalanceTransaction(), options).getFeeDetails();
            return Optional.of(new PaymentInformation(paidAmount, refundedAmount, getFeeAmount(fees, "stripe_fee"), getFeeAmount(fees, "application_fee")));
        }
        return Optional.empty();
    } catch (StripeException e) {
        return Optional.empty();
    }
}
Also used : RequestOptions(com.stripe.net.RequestOptions) Charge(com.stripe.model.Charge) PaymentInformation(alfio.model.PaymentInformation)

Example 23 with Charge

use of com.stripe.model.Charge in project alf.io by alfio-event.

the class StripeManagerTest method successFlow.

@Test
public void successFlow() {
    BaseStripeManager baseStripeManager = new BaseStripeManager(configurationManager, configurationRepository, ticketRepository, mock(Environment.class)) {

        @Override
        protected Optional<Charge> charge(PaymentSpecification spec, Map<String, Object> chargeParams) {
            return Optional.of(new Charge() {

                {
                    setId(paymentId);
                    setDescription("description");
                }
            });
        }
    };
    StripeCreditCardManager stripeCreditCardManager = new StripeCreditCardManager(transactionRepository, baseStripeManager, TestUtil.clockProvider());
    PaymentSpecification spec = new PaymentSpecification("", new StripeCreditCardToken(""), 100, event, "", customerName);
    PaymentResult result = stripeCreditCardManager.doPayment(spec);
    assertEquals(result, PaymentResult.successful(paymentId));
}
Also used : PaymentResult(alfio.manager.support.PaymentResult) StripeCreditCardToken(alfio.model.transaction.token.StripeCreditCardToken) Charge(com.stripe.model.Charge) Environment(org.springframework.core.env.Environment) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 24 with Charge

use of com.stripe.model.Charge in project alf.io by alfio-event.

the class StripeManagerTest method internalError.

@Test
public void internalError() {
    BaseStripeManager baseStripeManager = new BaseStripeManager(configurationManager, configurationRepository, ticketRepository, mock(Environment.class)) {

        @Override
        protected Optional<Charge> charge(PaymentSpecification spec, Map<String, Object> chargeParams) {
            return Optional.of(new Charge() {

                {
                    setId(paymentId);
                    setDescription("description");
                }
            });
        }
    };
    StripeCreditCardManager stripeCreditCardManager = new StripeCreditCardManager(transactionRepository, baseStripeManager, TestUtil.clockProvider());
    when(event.getCurrency()).thenReturn("CHF");
    when(transactionRepository.insert(anyString(), isNull(), anyString(), any(ZonedDateTime.class), anyInt(), eq("CHF"), anyString(), anyString(), anyLong(), anyLong(), eq(Transaction.Status.COMPLETE), anyMap())).thenThrow(new NullPointerException());
    PaymentSpecification spec = new PaymentSpecification("", new StripeCreditCardToken(""), 100, event, "", customerName);
    Assertions.assertThrows(IllegalStateException.class, () -> stripeCreditCardManager.doPayment(spec));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) StripeCreditCardToken(alfio.model.transaction.token.StripeCreditCardToken) Charge(com.stripe.model.Charge) Environment(org.springframework.core.env.Environment) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 25 with Charge

use of com.stripe.model.Charge in project alf.io by alfio-event.

the class StripeManager method getInfo.

Optional<PaymentInformation> getInfo(Transaction transaction, Event event) {
    try {
        Optional<RequestOptions> requestOptionsOptional = options(event);
        if (requestOptionsOptional.isPresent()) {
            RequestOptions options = requestOptionsOptional.get();
            Charge charge = Charge.retrieve(transaction.getTransactionId(), options);
            String paidAmount = MonetaryUtil.formatCents(charge.getAmount());
            String refundedAmount = MonetaryUtil.formatCents(charge.getAmountRefunded());
            List<Fee> fees = retrieveBalanceTransaction(charge.getBalanceTransaction(), options).getFeeDetails();
            return Optional.of(new PaymentInformation(paidAmount, refundedAmount, getFeeAmount(fees, "stripe_fee"), getFeeAmount(fees, "application_fee")));
        }
        return Optional.empty();
    } catch (StripeException e) {
        return Optional.empty();
    }
}
Also used : RequestOptions(com.stripe.net.RequestOptions) Fee(com.stripe.model.Fee) Charge(com.stripe.model.Charge) PaymentInformation(alfio.model.PaymentInformation)

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