Search in sources :

Example 1 with MollieToken

use of alfio.model.transaction.token.MollieToken in project alf.io by alfio-event.

the class MollieWebhookPaymentManager method processRemotePayment.

private PaymentWebhookResult processRemotePayment(Transaction transaction, String paymentId, PurchaseContext purchaseContext, Map<ConfigurationKeys, MaybeConfiguration> configuration, HttpResponse<InputStream> response) throws IOException {
    try (var reader = new InputStreamReader(response.body(), UTF_8)) {
        var body = new MolliePaymentDetails(JsonParser.parseReader(reader).getAsJsonObject());
        if (configuration.get(PLATFORM_MODE_ENABLED).getValueAsBooleanOrDefault() && configuration.get(MOLLIE_CONNECT_LIVE_MODE).getValueAsBooleanOrDefault() != body.isLiveMode()) {
            return PaymentWebhookResult.notRelevant("liveMode");
        }
        Validate.isTrue(body.getPaymentId().equals(paymentId));
        Validate.isTrue(transaction.getPaymentId().equals(paymentId));
        var status = body.getStatus();
        var reservationId = body.getReservationId();
        var optionalReservation = ticketReservationRepository.findOptionalStatusAndValidationById(reservationId).filter(reservation -> (reservation.getStatus() == EXTERNAL_PROCESSING_PAYMENT || reservation.getStatus() == WAITING_EXTERNAL_CONFIRMATION));
        if (optionalReservation.isEmpty()) {
            return PaymentWebhookResult.error("reservation not found");
        }
        // see statuses: https://www.mollie.com/en/docs/status
        var transactionMetadata = transaction.getMetadata();
        switch(status) {
            case "paid":
                transactionMetadata.put("paymentMethod", Objects.requireNonNull(body.getPaymentMethod()).name());
                transactionRepository.update(transaction.getId(), paymentId, paymentId, body.getConfirmationTimestamp().orElseThrow(), 0L, 0L, Transaction.Status.COMPLETE, transaction.getMetadata());
                return PaymentWebhookResult.successful(new MollieToken(paymentId, body.getPaymentMethod()));
            case STATUS_FAILED:
            case "expired":
                transactionMetadata.put("paymentMethod", Optional.ofNullable(body.getPaymentMethod()).map(PaymentMethod::name).orElse(null));
                transactionRepository.update(transaction.getId(), paymentId, paymentId, purchaseContext.now(clockProvider), transaction.getPlatformFee(), transaction.getGatewayFee(), transaction.getStatus(), transactionMetadata);
                return status.equals(STATUS_FAILED) ? PaymentWebhookResult.failed(STATUS_FAILED) : PaymentWebhookResult.cancelled();
            case "canceled":
                transactionRepository.update(transaction.getId(), paymentId, paymentId, purchaseContext.now(clockProvider), 0L, 0L, Transaction.Status.CANCELLED, transaction.getMetadata());
                return PaymentWebhookResult.cancelled();
            case "open":
                return PaymentWebhookResult.redirect(body.getCheckoutLink());
            default:
                return PaymentWebhookResult.notRelevant(status);
        }
    }
}
Also used : MollieToken(alfio.model.transaction.token.MollieToken) InputStreamReader(java.io.InputStreamReader)

Aggregations

MollieToken (alfio.model.transaction.token.MollieToken)1 InputStreamReader (java.io.InputStreamReader)1