Skip to content

Commit

Permalink
🐛 Fix express payment error in block cart
Browse files Browse the repository at this point in the history
  • Loading branch information
stracker-phil committed Sep 23, 2024
1 parent 5891d80 commit c852bc9
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion modules/ppcp-blocks/resources/js/checkout-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -230,6 +252,7 @@ const PayPalComponent = ( {
location.href = getCheckoutRedirectUrl();
} else {
setGotoContinuationOnError( true );
enforcePaymentMethodForCart();
onSubmit();
}
} catch ( err ) {
Expand Down Expand Up @@ -321,6 +344,7 @@ const PayPalComponent = ( {
location.href = getCheckoutRedirectUrl();
} else {
setGotoContinuationOnError( true );
enforcePaymentMethodForCart();
onSubmit();
}
} catch ( err ) {
Expand Down

0 comments on commit c852bc9

Please sign in to comment.