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)))