From 767bb271754da4e9305f71ba7f9f8bd8759795cc Mon Sep 17 00:00:00 2001 From: Martin Gross Date: Fri, 3 Nov 2023 15:18:53 +0100 Subject: [PATCH] PayPal: Add visibility EventListener for onApprove (Z#23135203) (#3687) --- .../pretixplugins/paypal2/pretix-paypal.js | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/pretix/plugins/paypal2/static/pretixplugins/paypal2/pretix-paypal.js b/src/pretix/plugins/paypal2/static/pretixplugins/paypal2/pretix-paypal.js index e893a3b2cf..75ff8f40f9 100644 --- a/src/pretix/plugins/paypal2/static/pretixplugins/paypal2/pretix-paypal.js +++ b/src/pretix/plugins/paypal2/static/pretixplugins/paypal2/pretix-paypal.js @@ -57,6 +57,7 @@ var pretixpaypal = { wechatpay: gettext('WeChat Pay'), mercadopago: gettext('Mercado Pago') }, + readyToSubmitApproval: false, load: function () { if (pretixpaypal.paypal === null) { @@ -141,6 +142,8 @@ var pretixpaypal = { } } }; + + document.addEventListener("visibilitychange", this.onApproveSubmit); }, ready: function () { @@ -225,12 +228,16 @@ var pretixpaypal = { let method = pretixpaypal.paypage ? "wallet" : pretixpaypal.method.method; let selectorstub = "#payment_paypal_" + method; - var $form = $(selectorstub + "_oid").closest("form"); - // Insert the tokens into the form so it gets submitted to the server + // Insert the tokens into the form, so it gets submitted to the server $(selectorstub + "_oid").val(pretixpaypal.order_id); $(selectorstub + "_payer").val(pretixpaypal.payer_id); - // and submit - $form.get(0).submit(); + + // We are moving the submission to a separate function, which is also an EventListener, since + // SFSafariView refuses to submit a form that is not visible. Unfortunately, that is exactly the case + // when the ticket shop is used on iOS within an SFSafariView and the PayPal payment popup has not + // closed itself quickly enough. + pretixpaypal.readyToSubmitApproval = true; + pretixpaypal.onApproveSubmit(); // billingToken: null // facilitatorAccessToken: "A21AAL_fEu0gDD-sIXyOy65a6MjgSJJrhmxuPcxxUGnL5gW2DzTxiiAksfoC4x8hD-BjeY1LsFVKl7ceuO7UR1a9pQr8Q_AVw" @@ -249,6 +256,16 @@ var pretixpaypal = { } }, + onApproveSubmit: function() { + if (document.visibilityState === "visible" && pretixpaypal.readyToSubmitApproval === true) { + let method = pretixpaypal.paypage ? "wallet" : pretixpaypal.method.method; + let selectorstub = "#payment_paypal_" + method; + var $form = $(selectorstub + "_oid").closest("form"); + + $form.get(0).submit(); + } + }, + renderAPMs: function () { pretixpaypal.restore(); let inputselector = $("input[name=payment][value=paypal_apm]");