Skip to content

Commit

Permalink
fix: ECE add-to-cart race condition (#10119)
Browse files Browse the repository at this point in the history
  • Loading branch information
frosso authored Jan 9, 2025
1 parent c3a7f1b commit f00666f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
5 changes: 5 additions & 0 deletions changelog/fix-tokenized-ece-applepay-delay
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: fix: ensure the "add to cart" ECE call is completed before other calls are made.


17 changes: 13 additions & 4 deletions client/express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ jQuery( ( $ ) => {
return;
}

let addToCartPromise = Promise.resolve();
const stripe = await api.getStripe();

const elements = stripe.elements( {
Expand Down Expand Up @@ -301,7 +302,14 @@ jQuery( ( $ ) => {
}

// Add products to the cart if everything is right.
wcpayECE.addToCart();
// we are storing the promise to ensure that the "add to cart" call is completed,
// before the `shippingaddresschange` is triggered when the dialog is opened.
// Otherwise, it might happen that the `shippingaddresschange` is triggered before the "add to cart" call is done,
// which can cause errors.
addToCartPromise = wcpayECE.addToCart();
addToCartPromise.finally( () => {
addToCartPromise = Promise.resolve();
} );
}

const clickOptions = {
Expand All @@ -319,9 +327,10 @@ jQuery( ( $ ) => {
event.resolve( clickOptions );
} );

eceButton.on( 'shippingaddresschange', async ( event ) =>
shippingAddressChangeHandler( api, event, elements )
);
eceButton.on( 'shippingaddresschange', async ( event ) => {
await addToCartPromise;
return shippingAddressChangeHandler( api, event, elements );
} );

eceButton.on( 'shippingratechange', async ( event ) =>
shippingRateChangeHandler( api, event, elements )
Expand Down
17 changes: 13 additions & 4 deletions client/tokenized-express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ jQuery( ( $ ) => {
* @param {Object} options ECE options.
*/
startExpressCheckoutElement: async ( options ) => {
let addToCartPromise = Promise.resolve();
const stripe = await api.getStripe();
const elements = stripe.elements( {
mode: 'payment',
Expand Down Expand Up @@ -247,7 +248,14 @@ jQuery( ( $ ) => {
}

// Add products to the cart if everything is right.
getCartApiHandler().addProductToCart();
// we are storing the promise to ensure that the "add to cart" call is completed,
// before the `shippingaddresschange` is triggered when the dialog is opened.
// Otherwise, it might happen that the `shippingaddresschange` is triggered before the "add to cart" call is done,
// which can cause errors.
addToCartPromise = getCartApiHandler().addProductToCart();
addToCartPromise.finally( () => {
addToCartPromise = Promise.resolve();
} );
}

const clickOptions = {
Expand All @@ -270,9 +278,10 @@ jQuery( ( $ ) => {
event.resolve( clickOptions );
} );

eceButton.on( 'shippingaddresschange', async ( event ) =>
shippingAddressChangeHandler( event, elements )
);
eceButton.on( 'shippingaddresschange', async ( event ) => {
await addToCartPromise;
return shippingAddressChangeHandler( event, elements );
} );

eceButton.on( 'shippingratechange', async ( event ) =>
shippingRateChangeHandler( event, elements )
Expand Down

0 comments on commit f00666f

Please sign in to comment.