Search in sources :

Example 6 with Refund

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

the class BaseStripeManager method refund.

// https://stripe.com/docs/api#create_refund
boolean refund(Transaction transaction, PurchaseContext purchaseContext, Integer amountToRefund) {
    Optional<Integer> amount = Optional.ofNullable(amountToRefund);
    String chargeId = transaction.getTransactionId();
    try {
        String amountOrFull = amount.map(p -> MonetaryUtil.formatCents(p, transaction.getCurrency())).orElse("full");
        log.info("Stripe: trying to do a refund for payment {} with amount: {}", chargeId, amountOrFull);
        Map<String, Object> params = new HashMap<>();
        params.put("charge", chargeId);
        amount.ifPresent(a -> params.put("amount", a));
        if (transaction.getPlatformFee() > 0 && isConnectEnabled(new PaymentContext(purchaseContext))) {
            params.put("refund_application_fee", true);
        }
        Optional<RequestOptions> requestOptionsOptional = options(purchaseContext);
        if (requestOptionsOptional.isPresent()) {
            RequestOptions options = requestOptionsOptional.get();
            Refund r = Refund.create(params, options);
            boolean pending = PENDING.equals(r.getStatus());
            if (SUCCEEDED.equals(r.getStatus()) || pending) {
                log.info("Stripe: refund for payment {} {} for amount: {}", chargeId, pending ? "registered" : "executed with success", amountOrFull);
                return true;
            } else {
                log.warn("Stripe: was not able to refund payment with id {}, returned status is not 'succeded' but {}", chargeId, r.getStatus());
                return false;
            }
        }
        return false;
    } catch (StripeException e) {
        log.warn("Stripe: was not able to refund payment with id " + chargeId, e);
        return false;
    }
}
Also used : Transaction(alfio.model.transaction.Transaction) RequestOptions(com.stripe.net.RequestOptions) java.util(java.util) PaymentProxy(alfio.model.transaction.PaymentProxy) PaymentResult(alfio.manager.support.PaymentResult) PurchaseContext(alfio.model.PurchaseContext) PurchaseContextType(alfio.model.PurchaseContext.PurchaseContextType) UnaryOperator(java.util.function.UnaryOperator) ConfigurationManager(alfio.manager.system.ConfigurationManager) PaymentInformation(alfio.model.PaymentInformation) Charge(com.stripe.model.Charge) Profiles(org.springframework.core.env.Profiles) Configurable(alfio.model.Configurable) Refund(com.stripe.model.Refund) TicketRepository(alfio.repository.TicketRepository) Predicate(java.util.function.Predicate) FeeCalculator(alfio.manager.support.FeeCalculator) PaymentMethod(alfio.model.transaction.PaymentMethod) Stripe(com.stripe.Stripe) ErrorsCode(alfio.util.ErrorsCode) ConfigurationRepository(alfio.repository.system.ConfigurationRepository) PaymentContext(alfio.model.transaction.PaymentContext) MonetaryUtil(alfio.util.MonetaryUtil) Environment(org.springframework.core.env.Environment) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) ConfigurationPathLevel(alfio.model.system.ConfigurationPathLevel) com.stripe.exception(com.stripe.exception) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) BalanceTransaction(com.stripe.model.BalanceTransaction) Webhook(com.stripe.net.Webhook) RequestOptions(com.stripe.net.RequestOptions) PaymentContext(alfio.model.transaction.PaymentContext) Refund(com.stripe.model.Refund)

Example 7 with Refund

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

the class RefundTest method testChargeRefundListAndRetrievePerCallAPIKey.

@Test
public void testChargeRefundListAndRetrievePerCallAPIKey() throws StripeException {
    Charge ch = Charge.create(defaultChargeParams);
    ch = ch.refund();
    Map<String, Object> listParams = new HashMap<String, Object>();
    listParams.put("count", 1);
    Refund created = ch.getRefunds().all(listParams, Stripe.apiKey).getData().get(0);
    Refund retrieved = ch.getRefunds().retrieve(created.getId(), Stripe.apiKey);
    assertEquals(created.getId(), retrieved.getId());
}
Also used : Refund(com.stripe.model.Refund) HashMap(java.util.HashMap) Charge(com.stripe.model.Charge) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 8 with Refund

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

the class RefundTest method testChargeRefundCreate.

@Test
public void testChargeRefundCreate() throws StripeException {
    Charge ch = Charge.create(defaultChargeParams);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("amount", 10);
    ChargeRefundCollection refunds = ch.getRefunds();
    Refund created = refunds.create(params);
    Refund retrieved = ch.getRefunds().retrieve(created.getId());
    assertEquals(created.getId(), retrieved.getId());
}
Also used : ChargeRefundCollection(com.stripe.model.ChargeRefundCollection) Refund(com.stripe.model.Refund) HashMap(java.util.HashMap) Charge(com.stripe.model.Charge) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 9 with Refund

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

the class RefundTest method testChargeRefundListAndRetrieve.

@Test
public void testChargeRefundListAndRetrieve() throws StripeException {
    Charge ch = Charge.create(defaultChargeParams);
    ch = ch.refund();
    Map<String, Object> listParams = new HashMap<String, Object>();
    listParams.put("count", 1);
    Refund created = ch.getRefunds().all(listParams).getData().get(0);
    Refund retrieved = ch.getRefunds().retrieve(created.getId());
    assertEquals(created.getId(), retrieved.getId());
}
Also used : Refund(com.stripe.model.Refund) HashMap(java.util.HashMap) Charge(com.stripe.model.Charge) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Example 10 with Refund

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

the class RefundTest method testUpdate.

@Test
public void testUpdate() throws StripeException {
    Refund refund = new Refund();
    refund.setId("re_foo");
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("reason", "fraudulent");
    refund.update(params);
    verifyPost(Refund.class, "https://api.stripe.com/v1/refunds/re_foo", params);
    verifyNoMoreInteractions(networkMock);
}
Also used : Refund(com.stripe.model.Refund) HashMap(java.util.HashMap) BaseStripeTest(com.stripe.BaseStripeTest) Test(org.junit.Test)

Aggregations

Refund (com.stripe.model.Refund)10 Charge (com.stripe.model.Charge)8 Test (org.junit.Test)8 BaseStripeFunctionalTest (com.stripe.BaseStripeFunctionalTest)7 HashMap (java.util.HashMap)7 ChargeRefundCollection (com.stripe.model.ChargeRefundCollection)3 BalanceTransaction (com.stripe.model.BalanceTransaction)2 RequestOptions (com.stripe.net.RequestOptions)2 LinkedList (java.util.LinkedList)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 PaymentInformation (alfio.model.PaymentInformation)1 PurchaseContext (alfio.model.PurchaseContext)1 PurchaseContextType (alfio.model.PurchaseContext.PurchaseContextType)1 ConfigurationKeys (alfio.model.system.ConfigurationKeys)1 ConfigurationPathLevel (alfio.model.system.ConfigurationPathLevel)1 PaymentContext (alfio.model.transaction.PaymentContext)1