Skip to content

Commit

Permalink
Move Promise instantiation into memoized function to ensure caching c…
Browse files Browse the repository at this point in the history
…onsistency (#9927)

Co-authored-by: Timur Karimov <[email protected]>
  • Loading branch information
timur27 and Timur Karimov authored Dec 17, 2024
1 parent eb54bb9 commit 0c7422e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 65 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-unhandled-promises
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Fix payment method filtering when billing country changes in Blocks checkout.
8 changes: 2 additions & 6 deletions client/express-checkout/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ const expressCheckoutElementApplePay = ( api ) => ( {
return false;
}

return new Promise( ( resolve ) => {
checkPaymentMethodIsAvailable( 'applePay', cart, resolve );
} );
return checkPaymentMethodIsAvailable( 'applePay', cart );
},
} );

Expand Down Expand Up @@ -77,9 +75,7 @@ const expressCheckoutElementGooglePay = ( api ) => {
return false;
}

return new Promise( ( resolve ) => {
checkPaymentMethodIsAvailable( 'googlePay', cart, resolve );
} );
return checkPaymentMethodIsAvailable( 'googlePay', cart );
},
};
};
Expand Down
122 changes: 63 additions & 59 deletions client/express-checkout/utils/checkPaymentMethodIsAvailable.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,75 @@ import WCPayAPI from 'wcpay/checkout/api';
import { getUPEConfig } from 'wcpay/utils/checkout';

export const checkPaymentMethodIsAvailable = memoize(
( paymentMethod, cart, resolve ) => {
// Create the DIV container on the fly
const containerEl = document.createElement( 'div' );
( paymentMethod, cart ) => {
return new Promise( ( resolve ) => {
// Create the DIV container on the fly
const containerEl = document.createElement( 'div' );

// Ensure the element is hidden and doesn’t interfere with the page layout.
containerEl.style.display = 'none';
// Ensure the element is hidden and doesn’t interfere with the page layout.
containerEl.style.display = 'none';

document.querySelector( 'body' ).appendChild( containerEl );
document.querySelector( 'body' ).appendChild( containerEl );

const root = ReactDOM.createRoot( containerEl );
const root = ReactDOM.createRoot( containerEl );

const api = new WCPayAPI(
{
publishableKey: getUPEConfig( 'publishableKey' ),
accountId: getUPEConfig( 'accountId' ),
forceNetworkSavedCards: getUPEConfig(
'forceNetworkSavedCards'
),
locale: getUPEConfig( 'locale' ),
isStripeLinkEnabled: isLinkEnabled(
getUPEConfig( 'paymentMethodsConfig' )
),
},
request
);
const api = new WCPayAPI(
{
publishableKey: getUPEConfig( 'publishableKey' ),
accountId: getUPEConfig( 'accountId' ),
forceNetworkSavedCards: getUPEConfig(
'forceNetworkSavedCards'
),
locale: getUPEConfig( 'locale' ),
isStripeLinkEnabled: isLinkEnabled(
getUPEConfig( 'paymentMethodsConfig' )
),
},
request
);

root.render(
<Elements
stripe={ api.loadStripeForExpressCheckout() }
options={ {
mode: 'payment',
paymentMethodCreation: 'manual',
amount: Number( cart.cartTotals.total_price ),
currency: cart.cartTotals.currency_code.toLowerCase(),
} }
>
<ExpressCheckoutElement
onLoadError={ () => resolve( false ) }
root.render(
<Elements
stripe={ api.loadStripeForExpressCheckout() }
options={ {
paymentMethods: {
amazonPay: 'never',
applePay:
paymentMethod === 'applePay'
? 'always'
: 'never',
googlePay:
paymentMethod === 'googlePay'
? 'always'
: 'never',
link: 'never',
paypal: 'never',
},
mode: 'payment',
paymentMethodCreation: 'manual',
amount: Number( cart.cartTotals.total_price ),
currency: cart.cartTotals.currency_code.toLowerCase(),
} }
onReady={ ( event ) => {
let canMakePayment = false;
if ( event.availablePaymentMethods ) {
canMakePayment =
event.availablePaymentMethods[ paymentMethod ];
}
resolve( canMakePayment );
root.unmount();
containerEl.remove();
} }
/>
</Elements>
);
>
<ExpressCheckoutElement
onLoadError={ () => resolve( false ) }
options={ {
paymentMethods: {
amazonPay: 'never',
applePay:
paymentMethod === 'applePay'
? 'always'
: 'never',
googlePay:
paymentMethod === 'googlePay'
? 'always'
: 'never',
link: 'never',
paypal: 'never',
},
} }
onReady={ ( event ) => {
let canMakePayment = false;
if ( event.availablePaymentMethods ) {
canMakePayment =
event.availablePaymentMethods[
paymentMethod
];
}
resolve( canMakePayment );
root.unmount();
containerEl.remove();
} }
/>
</Elements>
);
} );
}
);

0 comments on commit 0c7422e

Please sign in to comment.