From dc90a73f81b3bf0176c6a30c8d59f8f748744bef Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Wed, 26 Feb 2025 18:23:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=94=20Add=20short=20delay=20before=20r?= =?UTF-8?q?edirecting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OnApproveHandler/onApproveForContinue.js | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js b/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js index d492802f1..13b914335 100644 --- a/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js +++ b/modules/ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue.js @@ -1,3 +1,18 @@ +const initiateRedirect = ( successUrl ) => { + /** + * 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. + * + * The redirect will start after a short delay, giving the calling method + * time to process the return value of the `await onApprove()` call. + */ + + setTimeout( () => { + window.location.href = successUrl; + }, 200 ); +}; + const onApprove = ( context, errorHandler ) => { return ( data, actions ) => { const canCreateOrder = @@ -28,24 +43,13 @@ const onApprove = ( context, errorHandler ) => { .then( ( approveData ) => { if ( ! approveData.success ) { errorHandler.genericError(); - return actions.restart().catch( ( err ) => { + return actions.restart().catch( () => { errorHandler.genericError(); } ); } const orderReceivedUrl = approveData.data?.order_received_url; - - /** - * 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; + initiateRedirect( orderReceivedUrl || context.config.redirect ); } ); }; };