Skip to content

Commit 470d346

Browse files
authored
Fix empty space for express checkout container (#3484)
* return 'true' for 'canMakePayment' only when express checkout is enabled * fix error when 'paymentMethodsConfig' is not defined * add 'adjustButtonHeights' function * add 'checkPaymentMethodIsAvailable' function for blocks
1 parent 3557376 commit 470d346

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

client/blocks/express-checkout/express-checkout-component.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ const getPaymentMethodsOverride = ( enabledPaymentMethod ) => {
2929
};
3030
};
3131

32+
// Visual adjustments to horizontally align the buttons.
33+
const adjustButtonHeights = ( buttonOptions, expressPaymentMethod ) => {
34+
// Apple Pay has a nearly imperceptible height difference. We increase it by 1px here.
35+
if ( buttonOptions.buttonTheme.applePay === 'black' ) {
36+
if ( expressPaymentMethod === 'applePay' ) {
37+
buttonOptions.buttonHeight = buttonOptions.buttonHeight + 0.4;
38+
}
39+
}
40+
41+
// GooglePay with the white theme has a 2px height difference due to its border.
42+
if (
43+
expressPaymentMethod === 'googlePay' &&
44+
buttonOptions.buttonTheme.googlePay === 'white'
45+
) {
46+
buttonOptions.buttonHeight = buttonOptions.buttonHeight - 2;
47+
}
48+
49+
// Clamp the button height to the allowed range 40px to 55px.
50+
buttonOptions.buttonHeight = Math.max(
51+
40,
52+
Math.min( buttonOptions.buttonHeight, 55 )
53+
);
54+
return buttonOptions;
55+
};
56+
3257
const ExpressCheckoutComponent = ( {
3358
api,
3459
billing,
@@ -77,7 +102,7 @@ const ExpressCheckoutComponent = ( {
77102
return (
78103
<ExpressCheckoutElement
79104
options={ {
80-
...buttonOptions,
105+
...adjustButtonHeights( buttonOptions, expressPaymentMethod ),
81106
...getPaymentMethodsOverride( expressPaymentMethod ),
82107
} }
83108
onClick={ onButtonClick }

client/blocks/express-checkout/index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
/* global wc_stripe_express_checkout_params */
2+
13
import { PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT } from './constants';
24
import { ExpressCheckoutContainer } from './express-checkout-container';
35
import ApplePayPreview from './apple-pay-preview';
46
import GooglePayPreview from './google-pay-preview';
57
import { loadStripe } from 'wcstripe/blocks/load-stripe';
68
import { getBlocksConfiguration } from 'wcstripe/blocks/utils';
9+
import { checkPaymentMethodIsAvailable } from 'wcstripe/express-checkout/utils/check-payment-method-availability';
710

811
const stripePromise = loadStripe();
912

@@ -17,7 +20,16 @@ const expressCheckoutElementsGooglePay = ( api ) => ( {
1720
/>
1821
),
1922
edit: <GooglePayPreview />,
20-
canMakePayment: () => true,
23+
canMakePayment: ( { cart } ) => {
24+
// eslint-disable-next-line camelcase
25+
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
26+
return false;
27+
}
28+
29+
return new Promise( ( resolve ) => {
30+
checkPaymentMethodIsAvailable( 'googlePay', api, cart, resolve );
31+
} );
32+
},
2133
paymentMethodId: PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT,
2234
supports: {
2335
features: getBlocksConfiguration()?.supports ?? [],
@@ -34,7 +46,16 @@ const expressCheckoutElementsApplePay = ( api ) => ( {
3446
/>
3547
),
3648
edit: <ApplePayPreview />,
37-
canMakePayment: () => true,
49+
canMakePayment: ( { cart } ) => {
50+
// eslint-disable-next-line camelcase
51+
if ( typeof wc_stripe_express_checkout_params === 'undefined' ) {
52+
return false;
53+
}
54+
55+
return new Promise( ( resolve ) => {
56+
checkPaymentMethodIsAvailable( 'applePay', api, cart, resolve );
57+
} );
58+
},
3859
paymentMethodId: PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT,
3960
supports: {
4061
features: getBlocksConfiguration()?.supports ?? [],

client/blocks/upe/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const api = new WCStripeAPI(
2727
);
2828

2929
const upeMethods = getPaymentMethodsConstants();
30-
Object.entries( getBlocksConfiguration()?.paymentMethodsConfig )
30+
const paymentMethodsConfig =
31+
getBlocksConfiguration()?.paymentMethodsConfig ?? {};
32+
Object.entries( paymentMethodsConfig )
3133
.filter( ( [ upeName ] ) => upeName !== 'link' )
3234
.filter( ( [ upeName ] ) => upeName !== 'giropay' ) // Skip giropay as it was deprecated by Jun, 30th 2024.
3335
.forEach( ( [ upeName, upeConfig ] ) => {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import ReactDOM from 'react-dom';
2+
import { ExpressCheckoutElement, Elements } from '@stripe/react-stripe-js';
3+
import { memoize } from 'lodash';
4+
5+
export const checkPaymentMethodIsAvailable = memoize(
6+
( paymentMethod, api, cart, resolve ) => {
7+
// Create the DIV container on the fly
8+
const containerEl = document.createElement( 'div' );
9+
10+
// Ensure the element is hidden and doesn’t interfere with the page layout.
11+
containerEl.style.display = 'none';
12+
13+
document.querySelector( 'body' ).appendChild( containerEl );
14+
15+
const root = ReactDOM.createRoot( containerEl );
16+
17+
root.render(
18+
<Elements
19+
stripe={ api.loadStripe() }
20+
options={ {
21+
mode: 'payment',
22+
paymentMethodCreation: 'manual',
23+
amount: Number( cart.cartTotals.total_price ),
24+
currency: cart.cartTotals.currency_code.toLowerCase(),
25+
} }
26+
>
27+
<ExpressCheckoutElement
28+
onLoadError={ () => resolve( false ) }
29+
options={ {
30+
paymentMethods: {
31+
amazonPay: 'never',
32+
applePay:
33+
paymentMethod === 'applePay'
34+
? 'always'
35+
: 'never',
36+
googlePay:
37+
paymentMethod === 'googlePay'
38+
? 'always'
39+
: 'never',
40+
link: 'never',
41+
paypal: 'never',
42+
},
43+
} }
44+
onReady={ ( event ) => {
45+
let canMakePayment = false;
46+
if ( event.availablePaymentMethods ) {
47+
canMakePayment =
48+
event.availablePaymentMethods[ paymentMethod ];
49+
}
50+
resolve( canMakePayment );
51+
root.unmount();
52+
containerEl.remove();
53+
} }
54+
/>
55+
</Elements>
56+
);
57+
}
58+
);

0 commit comments

Comments
 (0)