From 2ce9584a6ff27a768cf37235d8059aa28c709d95 Mon Sep 17 00:00:00 2001 From: Martin Gross Date: Thu, 15 May 2025 16:58:30 +0200 Subject: [PATCH] PPv2: Wrap execute_payment to properly handle PaymentExceptions in atomic transactions --- src/pretix/plugins/paypal2/payment.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pretix/plugins/paypal2/payment.py b/src/pretix/plugins/paypal2/payment.py index f494d9c1b7..677cbe0037 100644 --- a/src/pretix/plugins/paypal2/payment.py +++ b/src/pretix/plugins/paypal2/payment.py @@ -625,8 +625,22 @@ class PaypalMethod(BasePaymentProvider): } return template.render(ctx) - @transaction.atomic + # We are wrapping the actual _execute_payment() here, since PaymentExceptions + # within the atomic transaction would rollback any changes to the payment-object, + # this throwing away any logentries and payment.fail() def execute_payment(self, request: HttpRequest, payment: OrderPayment): + ex = None + with transaction.atomic(): + try: + return self._execute_payment(request, payment) + except PaymentException as e: + ex = e + if ex: + raise ex + + return False + + def _execute_payment(self, request: HttpRequest, payment: OrderPayment): payment = OrderPayment.objects.select_for_update(of=OF_SELF).get(pk=payment.pk) if payment.state == OrderPayment.PAYMENT_STATE_CONFIRMED: logger.warning('payment is already confirmed; possible return-view/webhook race-condition')