From 6e6d6b27467d5cda25c90d7d2f0fee1989b1e295 Mon Sep 17 00:00:00 2001 From: Martin Gross Date: Wed, 13 Nov 2024 10:13:50 +0100 Subject: [PATCH] PayPal2: Skip webhook order capture if no OrderPayment exists yet --- src/pretix/plugins/paypal2/views.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pretix/plugins/paypal2/views.py b/src/pretix/plugins/paypal2/views.py index a59a83daaf..6c04b61f8a 100644 --- a/src/pretix/plugins/paypal2/views.py +++ b/src/pretix/plugins/paypal2/views.py @@ -505,9 +505,15 @@ def webhook(request, *args, **kwargs): except Quota.QuotaExceededException: pass elif sale['status'] == 'APPROVED': - request.session['payment_paypal_oid'] = payment.info_data['id'] try: + request.session['payment_paypal_oid'] = payment.info_data['id'] payment.payment_provider.execute_payment(request, payment) + except KeyError: + # We might receive the CHECKOUT.ORDER.APPROVED webhook early if the user approves the payment but + # the order has not been created yet. In these cases, we will skip the immediate capture until + # the Order and OrderPayment has been created, and we can capture the payment in the regular + # execute_payment run. + logger.info('PayPal2 - Did not capture/execute_payment from Webhook: payment was not yet populated.') except PaymentException as e: logger.exception('PayPal2 - Could not capture/execute_payment from Webhook: {}'.format(str(e)))