From 63b505590326c10174c0e0c54db12e82cb57b33d Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Tue, 20 Aug 2024 14:14:30 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A1=20Document=20payment=20workflow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OnApproveHandler/onApproveForContinue.js | 16 ++++++++++++---- .../resources/js/GooglepayButton.js | 9 +++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js b/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js index c60c163fd..0d699c170 100644 --- a/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js +++ b/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js @@ -18,17 +18,25 @@ const onApprove = ( context, errorHandler ) => { .then( ( res ) => { return res.json(); } ) - .then( ( data ) => { - if ( ! data.success ) { + .then( ( approveData ) => { + if ( ! approveData.success ) { errorHandler.genericError(); return actions.restart().catch( ( err ) => { errorHandler.genericError(); } ); } - const orderReceivedUrl = data.data?.order_received_url; + const orderReceivedUrl = approveData.data?.order_received_url; - location.href = orderReceivedUrl + /** + * Notice how this step initiates a redirect to a new page using a plain + * URL as new location. This process does not send any details about the + * approved order or billed customer. + * Also, due to the redirect starting _instantly_ there should be no other + * logic scheduled after calling `await onApprove()`; + */ + + window.location.href = orderReceivedUrl ? orderReceivedUrl : context.config.redirect; } ); diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index d8a910f34..a68e22f4a 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -587,6 +587,14 @@ class GooglepayButton extends PaymentButton { return 'APPROVED' === confirmOrderResponse?.status; }; + /** + * This approval mainly confirms that the orderID is valid. + * + * It's still needed because this handler redirects to the checkout page if the server-side + * approval was successful. + * + * @param {string} orderID + */ const approveOrderServerSide = async ( orderID ) => { let isApproved = true; @@ -625,6 +633,7 @@ class GooglepayButton extends PaymentButton { return; } + // This must be the last step in the process, as it initiates a redirect. const success = await approveOrderServerSide( id ); if ( success ) {