Skip to content

Commit

Permalink
Refactor loadStripe for Express Checkouts (#9582)
Browse files Browse the repository at this point in the history
  • Loading branch information
asumaran authored Oct 28, 2024
1 parent f9b976e commit c88ae5e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
4 changes: 4 additions & 0 deletions changelog/as-improve-stripe-for-ece
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Refactor loadStripe for Express Checkouts.
8 changes: 4 additions & 4 deletions client/checkout/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ export default class WCPayAPI {
}

/**
* Load Stripe for payment request button.
* Load Stripe for Express Checkout with the merchant’s connected account.
*
* @param {boolean} forceAccountRequest True to instantiate the Stripe object with the merchant's account key.
* @return {Promise} Promise with the Stripe object or an error.
*/
loadStripe( forceAccountRequest = false ) {
loadStripeForExpressCheckout() {
return new Promise( ( resolve ) => {
try {
resolve( this.getStripe( forceAccountRequest ) );
// Force Stripe to be loadded with the connected account.
resolve( this.getStripe( true ) );
} catch ( error ) {
// In order to avoid showing console error publicly to users,
// we resolve instead of rejecting when there is an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ExpressCheckoutContainer = ( props ) => {
const { api, billing, buttonAttributes } = props;

const stripePromise = useMemo( () => {
return api.loadStripe( true );
return api.loadStripeForExpressCheckout();
}, [ api ] );

const options = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const checkPaymentMethodIsAvailable = memoize(

root.render(
<Elements
stripe={ api.loadStripe( true ) }
stripe={ api.loadStripeForExpressCheckout() }
options={ {
mode: 'payment',
paymentMethodCreation: 'manual',
Expand Down
7 changes: 5 additions & 2 deletions client/payment-request/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const paymentRequestPaymentMethod = ( api ) => ( {
),
gatewayId: 'woocommerce_payments',
content: (
<PaymentRequestExpress api={ api } stripe={ api.loadStripe( true ) } />
<PaymentRequestExpress
api={ api }
stripe={ api.loadStripeForExpressCheckout() }
/>
),
edit: <ApplePayPreview />,
canMakePayment: ( cartData ) => {
Expand All @@ -46,7 +49,7 @@ const paymentRequestPaymentMethod = ( api ) => ( {
return false;
}

return api.loadStripe( true ).then( ( stripe ) => {
return api.loadStripeForExpressCheckout().then( ( stripe ) => {
// Create a payment request and check if we can make a payment to determine whether to
// show the Payment Request Button or not. This is necessary because a browser might be
// able to load the Stripe JS object, but not support Payment Requests.
Expand Down
7 changes: 5 additions & 2 deletions client/tokenized-payment-request/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const ApplePayPreview = () => <img src={ applePayImage } alt="" />;
const tokenizedCartPaymentRequestPaymentMethod = ( api ) => ( {
name: PAYMENT_METHOD_NAME_PAYMENT_REQUEST,
content: (
<PaymentRequestExpress api={ api } stripe={ api.loadStripe( true ) } />
<PaymentRequestExpress
api={ api }
stripe={ api.loadStripeForExpressCheckout() }
/>
),
edit: <ApplePayPreview />,
canMakePayment: ( cartData ) => {
Expand All @@ -40,7 +43,7 @@ const tokenizedCartPaymentRequestPaymentMethod = ( api ) => ( {
return false;
}

return api.loadStripe( true ).then( ( stripe ) => {
return api.loadStripeForExpressCheckout().then( ( stripe ) => {
// Create a payment request and check if we can make a payment to determine whether to
// show the Payment Request Button or not. This is necessary because a browser might be
// able to load the Stripe JS object, but not support Payment Requests.
Expand Down

0 comments on commit c88ae5e

Please sign in to comment.