From c852bc92b50b38850ea6d0484e26b9ef797bd515 Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Mon, 23 Sep 2024 17:14:28 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20express=20payment=20error?= =?UTF-8?q?=20in=20block=20cart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/js/checkout-block.js | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-blocks/resources/js/checkout-block.js b/modules/ppcp-blocks/resources/js/checkout-block.js index 4bec20aa6..21d959a4f 100644 --- a/modules/ppcp-blocks/resources/js/checkout-block.js +++ b/modules/ppcp-blocks/resources/js/checkout-block.js @@ -24,9 +24,10 @@ const config = wc.wcSettings.getSetting( 'ppcp-gateway_data' ); window.ppcpFundingSource = config.fundingSource; let registeredContext = false; - let paypalScriptPromise = null; +const PAYPAL_GATEWAY_ID = 'ppcp-gateway'; + const PayPalComponent = ( { onClick, onClose, @@ -62,6 +63,27 @@ const PayPalComponent = ( { ? `${ config.id }-${ fundingSource }` : config.id; + /** + * The block cart displays express checkout buttons. Those buttons are handled by the + * PAYPAL_GATEWAY_ID method on the server ("PayPal Smart Buttons"). + * + * A possible bug in WooCommerce does not use the correct payment method ID for the express + * payment buttons inside the cart, but sends the ID of the _first_ active payment method. + * + * This function uses an internal WooCommerce dispatcher method to set the correct method ID. + */ + const enforcePaymentMethodForCart = () => { + // Do nothing, unless we're handling block cart express payment buttons. + if ( 'cart-block' !== config.scriptData.context ) { + return; + } + + // Set the active payment method to PAYPAL_GATEWAY_ID. + wp.data + .dispatch( 'wc/store/payment' ) + .__internalSetActivePaymentMethod( PAYPAL_GATEWAY_ID, {} ); + }; + useEffect( () => { // fill the form if in continuation (for product or mini-cart buttons) if ( continuationFilled || ! config.scriptData.continuation?.order ) { @@ -230,6 +252,7 @@ const PayPalComponent = ( { location.href = getCheckoutRedirectUrl(); } else { setGotoContinuationOnError( true ); + enforcePaymentMethodForCart(); onSubmit(); } } catch ( err ) { @@ -321,6 +344,7 @@ const PayPalComponent = ( { location.href = getCheckoutRedirectUrl(); } else { setGotoContinuationOnError( true ); + enforcePaymentMethodForCart(); onSubmit(); } } catch ( err ) {