Skip to content

Commit

Permalink
Register Independent ECE Payment Methods within the Blocks Implementa…
Browse files Browse the repository at this point in the history
…tion (#9047)
  • Loading branch information
rafaelzaleski authored and jessepearson committed Jul 11, 2024
1 parent 0fa52eb commit 503afc3
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 23 deletions.
4 changes: 4 additions & 0 deletions changelog/add-9045-split-ece-pms-registration-for-blocks
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add independent ECE instances in WC Blocks.
8 changes: 6 additions & 2 deletions client/checkout/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import PaymentMethodLabel from './payment-method-label';
import request from '../utils/request';
import enqueueFraudScripts from 'fraud-scripts';
import paymentRequestPaymentMethod from '../../payment-request/blocks';
import expressCheckoutElementPaymentMethod from '../../express-checkout/blocks';
import {
expressCheckoutElementApplePay,
expressCheckoutElementGooglePay,
} from '../../express-checkout/blocks';
import tokenizedCartPaymentRequestPaymentMethod from '../../tokenized-payment-request/blocks';

import {
Expand Down Expand Up @@ -161,7 +164,8 @@ if ( getUPEConfig( 'isTokenizedCartPrbEnabled' ) ) {
tokenizedCartPaymentRequestPaymentMethod( api )
);
} else if ( getUPEConfig( 'isExpressCheckoutElementEnabled' ) ) {
registerExpressPaymentMethod( expressCheckoutElementPaymentMethod( api ) );
registerExpressPaymentMethod( expressCheckoutElementApplePay( api ) );
registerExpressPaymentMethod( expressCheckoutElementGooglePay( api ) );
} else {
registerExpressPaymentMethod( paymentRequestPaymentMethod( api ) );
}
Expand Down
7 changes: 0 additions & 7 deletions client/express-checkout/blocks/apple-pay-preview.js

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@
* External dependencies
*/
import { ExpressCheckoutElement } from '@stripe/react-stripe-js';
/**
* Internal dependencies
*/
import {
shippingAddressChangeHandler,
shippingRateChangeHandler,
} from '../../event-handlers';
import { useExpressCheckout } from '../hooks/use-express-checkout';
import { PAYMENT_METHOD_NAME_EXPRESS_CHECKOUT_ELEMENT } from 'wcpay/checkout/constants';

const getPaymentMethodsOverride = ( enabledPaymentMethod ) => {
const allDisabled = {
amazonPay: 'never',
applePay: 'never',
googlePay: 'never',
link: 'never',
paypal: 'never',
};

const enabledParam = [ 'applePay', 'googlePay' ].includes(
enabledPaymentMethod
)
? 'always'
: 'auto';

return {
paymentMethods: {
...allDisabled,
[ enabledPaymentMethod ]: enabledParam,
},
};
};

/**
* ExpressCheckout express payment method component.
Expand All @@ -22,6 +49,7 @@ const ExpressCheckoutComponent = ( {
setExpressPaymentError,
onClick,
onClose,
expressPaymentMethod = '',
} ) => {
const {
buttonOptions,
Expand All @@ -45,12 +73,33 @@ const ExpressCheckoutComponent = ( {
const onShippingRateChange = ( event ) =>
shippingRateChangeHandler( api, event, elements );

const onElementsReady = ( event ) => {
const paymentMethodContainer = document.getElementById(
`express-payment-method-${ PAYMENT_METHOD_NAME_EXPRESS_CHECKOUT_ELEMENT }_${ expressPaymentMethod }`
);

const availablePaymentMethods = event.availablePaymentMethods || {};

if (
paymentMethodContainer &&
! availablePaymentMethods[ expressPaymentMethod ]
) {
paymentMethodContainer.style.display = 'none';
}

// Any actions that WooPayments needs to perform.
onReady( event );
};

return (
<ExpressCheckoutElement
options={ buttonOptions }
options={ {
...buttonOptions,
...getPaymentMethodsOverride( expressPaymentMethod ),
} }
onClick={ onButtonClick }
onConfirm={ onConfirm }
onReady={ onReady }
onReady={ onElementsReady }
onCancel={ onCancel }
onShippingAddressChange={ onShippingAddressChange }
onShippingRateChange={ onShippingRateChange }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/**
* External dependencies
*/
import { useMemo } from 'react';
import { Elements } from '@stripe/react-stripe-js';
import { getExpressCheckoutButtonAppearance } from 'wcpay/express-checkout/utils';

/**
* Internal dependencies
*/
import ExpressCheckoutComponent from './express-checkout-component';
import { getExpressCheckoutButtonAppearance } from 'wcpay/express-checkout/utils';

const ExpressCheckoutContainer = ( props ) => {
const { stripe, billing } = props;
const { api, billing } = props;

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

const options = {
mode: 'payment',
Expand All @@ -22,7 +27,7 @@ const ExpressCheckoutContainer = ( props ) => {

return (
<div style={ { minHeight: '50px' } }>
<Elements stripe={ stripe } options={ options }>
<Elements stripe={ stripePromise } options={ options }>
<ExpressCheckoutComponent { ...props } />
</Elements>
</div>
Expand Down
Loading

0 comments on commit 503afc3

Please sign in to comment.