Search in sources :

Example 1 with Stripe

use of com.stripe.Stripe 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 2 with Stripe

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

the class StripeWebhookPaymentManager method parseTransactionPayload.

@Override
public Optional<TransactionWebhookPayload> parseTransactionPayload(String body, String signature, Map<String, String> additionalInfo, PaymentContext paymentContext) {
    try {
        var stripeEvent = Webhook.constructEvent(body, signature, getWebhookSignatureKey(paymentContext.getConfigurationLevel()));
        String eventType = stripeEvent.getType();
        if (eventType.startsWith("charge.")) {
            return deserializeObject(stripeEvent).map(obj -> new StripeChargeTransactionWebhookPayload(eventType, (Charge) obj));
        } else if (eventType.startsWith("payment_intent.")) {
            return deserializeObject(stripeEvent).map(obj -> new StripePaymentIntentWebhookPayload(eventType, (PaymentIntent) obj));
        }
        return Optional.empty();
    } catch (Exception e) {
        log.error("got exception while handling stripe webhook", e);
        return Optional.empty();
    }
}
Also used : alfio.repository(alfio.repository) java.util(java.util) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) ZonedDateTime(java.time.ZonedDateTime) PaymentResult(alfio.manager.support.PaymentResult) PurchaseContext(alfio.model.PurchaseContext) ConfigurationManager(alfio.manager.system.ConfigurationManager) JsonParser(com.google.gson.JsonParser) PaymentInformation(alfio.model.PaymentInformation) Charge(com.stripe.model.Charge) alfio.model.transaction.capabilities(alfio.model.transaction.capabilities) PaymentIntent(com.stripe.model.PaymentIntent) alfio.model.transaction(alfio.model.transaction) StripeChargeTransactionWebhookPayload(alfio.model.transaction.webhook.StripeChargeTransactionWebhookPayload) StripePaymentIntentWebhookPayload(alfio.model.transaction.webhook.StripePaymentIntentWebhookPayload) StripeException(com.stripe.exception.StripeException) Stripe(com.stripe.Stripe) ConfigurationRepository(alfio.repository.system.ConfigurationRepository) Audit(alfio.model.Audit) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) StringReader(java.io.StringReader) StripeSCACreditCardToken(alfio.model.transaction.token.StripeSCACreditCardToken) Environment(org.springframework.core.env.Environment) STRIPE_MANAGER_TYPE_KEY(alfio.manager.payment.BaseStripeManager.STRIPE_MANAGER_TYPE_KEY) TicketReservation(alfio.model.TicketReservation) Log4j2(lombok.extern.log4j.Log4j2) PaymentWebhookResult(alfio.manager.support.PaymentWebhookResult) StripeObject(com.stripe.model.StripeObject) ClockProvider(alfio.util.ClockProvider) EXTERNAL_PROCESSING_PAYMENT(alfio.model.TicketReservation.TicketReservationStatus.EXTERNAL_PROCESSING_PAYMENT) ConfigurationKeys(alfio.model.system.ConfigurationKeys) WAITING_EXTERNAL_CONFIRMATION(alfio.model.TicketReservation.TicketReservationStatus.WAITING_EXTERNAL_CONFIRMATION) BalanceTransaction(com.stripe.model.BalanceTransaction) Webhook(com.stripe.net.Webhook) Transactional(org.springframework.transaction.annotation.Transactional) StripeChargeTransactionWebhookPayload(alfio.model.transaction.webhook.StripeChargeTransactionWebhookPayload) StripePaymentIntentWebhookPayload(alfio.model.transaction.webhook.StripePaymentIntentWebhookPayload) Charge(com.stripe.model.Charge) StripeException(com.stripe.exception.StripeException)

Aggregations

PaymentResult (alfio.manager.support.PaymentResult)2 ConfigurationManager (alfio.manager.system.ConfigurationManager)2 PaymentInformation (alfio.model.PaymentInformation)2 PurchaseContext (alfio.model.PurchaseContext)2 ConfigurationKeys (alfio.model.system.ConfigurationKeys)2 ConfigurationRepository (alfio.repository.system.ConfigurationRepository)2 Stripe (com.stripe.Stripe)2 BalanceTransaction (com.stripe.model.BalanceTransaction)2 Charge (com.stripe.model.Charge)2 Webhook (com.stripe.net.Webhook)2 java.util (java.util)2 Log4j2 (lombok.extern.log4j.Log4j2)2 Environment (org.springframework.core.env.Environment)2 STRIPE_MANAGER_TYPE_KEY (alfio.manager.payment.BaseStripeManager.STRIPE_MANAGER_TYPE_KEY)1 FeeCalculator (alfio.manager.support.FeeCalculator)1 PaymentWebhookResult (alfio.manager.support.PaymentWebhookResult)1 ConfigurationLevel (alfio.manager.system.ConfigurationLevel)1 UserManager (alfio.manager.user.UserManager)1 Audit (alfio.model.Audit)1 Configurable (alfio.model.Configurable)1