Search in sources :

Example 1 with PaymentIntent

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

the class StripeWebhookPaymentManager method forceTransactionCheck.

@Override
public PaymentWebhookResult forceTransactionCheck(TicketReservation reservation, Transaction transaction, PaymentContext paymentContext) {
    Validate.isTrue(transaction.getPaymentProxy() == PaymentProxy.STRIPE, "invalid transaction");
    try {
        PurchaseContext purchaseContext = paymentContext.getPurchaseContext();
        var options = baseStripeManager.options(purchaseContext, builder -> builder.setIdempotencyKey(reservation.getId())).orElseThrow();
        var intent = PaymentIntent.retrieve(transaction.getPaymentId(), options);
        switch(intent.getStatus()) {
            case "processing":
            case "requires_action":
            case "requires_confirmation":
                return PaymentWebhookResult.pending();
            case "succeeded":
                return processSuccessfulPaymentIntent(transaction, intent, reservation, purchaseContext);
            case REQUIRES_PAYMENT_METHOD:
                // payment is failed.
                return processFailedPaymentIntent(transaction, reservation, purchaseContext);
            default:
                return null;
        }
    } catch (Exception ex) {
        log.error("Error trying to check PaymentIntent status", ex);
        return PaymentWebhookResult.error("failed");
    }
}
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) PurchaseContext(alfio.model.PurchaseContext) StripeException(com.stripe.exception.StripeException)

Example 2 with PaymentIntent

use of com.stripe.model.PaymentIntent 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

STRIPE_MANAGER_TYPE_KEY (alfio.manager.payment.BaseStripeManager.STRIPE_MANAGER_TYPE_KEY)2 PaymentResult (alfio.manager.support.PaymentResult)2 PaymentWebhookResult (alfio.manager.support.PaymentWebhookResult)2 ConfigurationLevel (alfio.manager.system.ConfigurationLevel)2 ConfigurationManager (alfio.manager.system.ConfigurationManager)2 Audit (alfio.model.Audit)2 PaymentInformation (alfio.model.PaymentInformation)2 PurchaseContext (alfio.model.PurchaseContext)2 TicketReservation (alfio.model.TicketReservation)2 EXTERNAL_PROCESSING_PAYMENT (alfio.model.TicketReservation.TicketReservationStatus.EXTERNAL_PROCESSING_PAYMENT)2 WAITING_EXTERNAL_CONFIRMATION (alfio.model.TicketReservation.TicketReservationStatus.WAITING_EXTERNAL_CONFIRMATION)2 ConfigurationKeys (alfio.model.system.ConfigurationKeys)2 alfio.model.transaction (alfio.model.transaction)2 alfio.model.transaction.capabilities (alfio.model.transaction.capabilities)2 StripeSCACreditCardToken (alfio.model.transaction.token.StripeSCACreditCardToken)2 StripeChargeTransactionWebhookPayload (alfio.model.transaction.webhook.StripeChargeTransactionWebhookPayload)2 StripePaymentIntentWebhookPayload (alfio.model.transaction.webhook.StripePaymentIntentWebhookPayload)2 alfio.repository (alfio.repository)2 ConfigurationRepository (alfio.repository.system.ConfigurationRepository)2 ClockProvider (alfio.util.ClockProvider)2