2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-06-16 12:44:40 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-01-06 22:28:09 +00:00
|
|
|
import {
|
|
|
|
useExpressPaymentMethods,
|
2020-03-11 10:50:12 +00:00
|
|
|
usePaymentMethodInterface,
|
2021-04-08 12:31:12 +00:00
|
|
|
} from '@woocommerce/base-context/hooks';
|
2020-04-09 15:22:34 +00:00
|
|
|
import {
|
|
|
|
cloneElement,
|
|
|
|
isValidElement,
|
|
|
|
useCallback,
|
|
|
|
useRef,
|
|
|
|
} from '@wordpress/element';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { useEditorContext } from '@woocommerce/base-context';
|
2021-06-16 12:44:40 +00:00
|
|
|
import deprecated from '@wordpress/deprecated';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2020-09-21 13:43:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-09-18 10:27:54 +00:00
|
|
|
import PaymentMethodErrorBoundary from './payment-method-error-boundary';
|
2022-10-06 12:46:46 +00:00
|
|
|
import { STORE_KEY as PAYMENT_STORE_KEY } from '../../../data/payment/constants';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
|
|
|
const ExpressPaymentMethods = () => {
|
2020-04-01 09:27:53 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
const { activePaymentMethod, paymentMethodData } = useSelect(
|
|
|
|
( select ) => {
|
2022-10-06 12:46:46 +00:00
|
|
|
const store = select( PAYMENT_STORE_KEY );
|
2022-07-08 05:53:24 +00:00
|
|
|
return {
|
|
|
|
activePaymentMethod: store.getActivePaymentMethod(),
|
|
|
|
paymentMethodData: store.getPaymentMethodData(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2022-10-05 10:04:16 +00:00
|
|
|
const {
|
|
|
|
__internalSetActivePaymentMethod,
|
2023-02-14 12:08:19 +00:00
|
|
|
__internalSetExpressPaymentStarted,
|
|
|
|
__internalSetPaymentIdle,
|
2022-11-15 12:27:39 +00:00
|
|
|
__internalSetPaymentError,
|
|
|
|
__internalSetPaymentMethodData,
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetExpressPaymentError,
|
2022-10-06 12:46:46 +00:00
|
|
|
} = useDispatch( PAYMENT_STORE_KEY );
|
2020-01-06 22:28:09 +00:00
|
|
|
const { paymentMethods } = useExpressPaymentMethods();
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
const paymentMethodInterface = usePaymentMethodInterface();
|
2020-04-09 15:22:34 +00:00
|
|
|
const previousActivePaymentMethod = useRef( activePaymentMethod );
|
2020-10-08 09:21:47 +00:00
|
|
|
const previousPaymentMethodData = useRef( paymentMethodData );
|
2020-04-09 15:22:34 +00:00
|
|
|
|
2021-06-16 12:44:40 +00:00
|
|
|
/**
|
|
|
|
* onExpressPaymentClick should be triggered when the express payment button is clicked.
|
|
|
|
*
|
2021-12-10 15:26:16 +00:00
|
|
|
* This will store the previous active payment method, set the express method as active, and set the payment status
|
|
|
|
* to started.
|
2021-06-16 12:44:40 +00:00
|
|
|
*/
|
2020-04-09 15:22:34 +00:00
|
|
|
const onExpressPaymentClick = useCallback(
|
|
|
|
( paymentMethodId ) => () => {
|
|
|
|
previousActivePaymentMethod.current = activePaymentMethod;
|
2020-10-08 09:21:47 +00:00
|
|
|
previousPaymentMethodData.current = paymentMethodData;
|
2023-02-14 12:08:19 +00:00
|
|
|
__internalSetExpressPaymentStarted();
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetActivePaymentMethod( paymentMethodId );
|
2020-04-09 15:22:34 +00:00
|
|
|
},
|
2020-10-08 09:21:47 +00:00
|
|
|
[
|
|
|
|
activePaymentMethod,
|
|
|
|
paymentMethodData,
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetActivePaymentMethod,
|
2023-02-14 12:08:19 +00:00
|
|
|
__internalSetExpressPaymentStarted,
|
2020-10-08 09:21:47 +00:00
|
|
|
]
|
2020-04-09 15:22:34 +00:00
|
|
|
);
|
2021-06-16 12:44:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* onExpressPaymentClose should be triggered when the express payment process is cancelled or closed.
|
|
|
|
*
|
|
|
|
* This restores the active method and returns the state to pristine.
|
|
|
|
*/
|
2020-04-09 15:22:34 +00:00
|
|
|
const onExpressPaymentClose = useCallback( () => {
|
2023-02-14 12:08:19 +00:00
|
|
|
__internalSetPaymentIdle();
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetActivePaymentMethod(
|
2021-12-10 15:26:16 +00:00
|
|
|
previousActivePaymentMethod.current,
|
|
|
|
previousPaymentMethodData.current
|
|
|
|
);
|
2023-02-14 12:08:19 +00:00
|
|
|
}, [ __internalSetActivePaymentMethod, __internalSetPaymentIdle ] );
|
2021-06-16 12:44:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* onExpressPaymentError should be triggered when the express payment process errors.
|
|
|
|
*
|
|
|
|
* This shows an error message then restores the active method and returns the state to pristine.
|
|
|
|
*/
|
|
|
|
const onExpressPaymentError = useCallback(
|
|
|
|
( errorMessage ) => {
|
2022-11-15 12:27:39 +00:00
|
|
|
__internalSetPaymentError();
|
|
|
|
__internalSetPaymentMethodData( errorMessage );
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetExpressPaymentError( errorMessage );
|
|
|
|
__internalSetActivePaymentMethod(
|
2021-12-10 15:26:16 +00:00
|
|
|
previousActivePaymentMethod.current,
|
|
|
|
previousPaymentMethodData.current
|
|
|
|
);
|
2021-06-16 12:44:40 +00:00
|
|
|
},
|
2022-10-05 10:04:16 +00:00
|
|
|
[
|
|
|
|
__internalSetActivePaymentMethod,
|
2022-11-15 12:27:39 +00:00
|
|
|
__internalSetPaymentError,
|
|
|
|
__internalSetPaymentMethodData,
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetExpressPaymentError,
|
|
|
|
]
|
2021-06-16 12:44:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calling setExpressPaymentError directly is deprecated.
|
|
|
|
*/
|
|
|
|
const deprecatedSetExpressPaymentError = useCallback(
|
|
|
|
( errorMessage = '' ) => {
|
|
|
|
deprecated(
|
|
|
|
'Express Payment Methods should use the provided onError handler instead.',
|
|
|
|
{
|
|
|
|
alternative: 'onError',
|
|
|
|
plugin: 'woocommerce-gutenberg-products-block',
|
2022-06-15 09:56:52 +00:00
|
|
|
link: 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/4228',
|
2021-06-16 12:44:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
if ( errorMessage ) {
|
|
|
|
onExpressPaymentError( errorMessage );
|
|
|
|
} else {
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetExpressPaymentError( '' );
|
2021-06-16 12:44:40 +00:00
|
|
|
}
|
|
|
|
},
|
2022-10-05 10:04:16 +00:00
|
|
|
[ __internalSetExpressPaymentError, onExpressPaymentError ]
|
2021-06-16 12:44:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Find a way to Memoize Express Payment Method Content
|
|
|
|
*
|
|
|
|
* Payment method content could potentially become a bottleneck if lots of logic is ran in the content component. It
|
|
|
|
* Currently re-renders excessively but is not easy to useMemo because paymentMethodInterface could become stale.
|
|
|
|
* paymentMethodInterface itself also updates on most renders.
|
|
|
|
*/
|
|
|
|
const entries = Object.entries( paymentMethods );
|
2020-01-06 22:28:09 +00:00
|
|
|
const content =
|
2021-06-16 12:44:40 +00:00
|
|
|
entries.length > 0 ? (
|
|
|
|
entries.map( ( [ id, paymentMethod ] ) => {
|
2020-03-10 16:35:30 +00:00
|
|
|
const expressPaymentMethod = isEditor
|
2021-06-16 12:44:40 +00:00
|
|
|
? paymentMethod.edit
|
|
|
|
: paymentMethod.content;
|
2020-03-10 16:35:30 +00:00
|
|
|
return isValidElement( expressPaymentMethod ) ? (
|
2024-08-19 17:54:58 +00:00
|
|
|
<li key={ id } id={ `express-payment-method-${ id }` }>
|
2020-01-06 22:28:09 +00:00
|
|
|
{ cloneElement( expressPaymentMethod, {
|
2020-03-11 10:50:12 +00:00
|
|
|
...paymentMethodInterface,
|
2020-04-09 15:22:34 +00:00
|
|
|
onClick: onExpressPaymentClick( id ),
|
|
|
|
onClose: onExpressPaymentClose,
|
2021-06-16 12:44:40 +00:00
|
|
|
onError: onExpressPaymentError,
|
2022-06-15 09:56:52 +00:00
|
|
|
setExpressPaymentError:
|
|
|
|
deprecatedSetExpressPaymentError,
|
2020-01-06 22:28:09 +00:00
|
|
|
} ) }
|
|
|
|
</li>
|
2020-03-10 16:35:30 +00:00
|
|
|
) : null;
|
2020-01-06 22:28:09 +00:00
|
|
|
} )
|
|
|
|
) : (
|
2021-06-16 12:44:40 +00:00
|
|
|
<li key="noneRegistered">
|
2023-12-12 23:05:20 +00:00
|
|
|
{ __( 'No registered Payment Methods', 'woocommerce' ) }
|
2021-06-16 12:44:40 +00:00
|
|
|
</li>
|
2020-01-06 22:28:09 +00:00
|
|
|
);
|
2021-06-16 12:44:40 +00:00
|
|
|
|
2020-01-06 22:28:09 +00:00
|
|
|
return (
|
2020-09-18 10:27:54 +00:00
|
|
|
<PaymentMethodErrorBoundary isEditor={ isEditor }>
|
|
|
|
<ul className="wc-block-components-express-payment__event-buttons">
|
|
|
|
{ content }
|
|
|
|
</ul>
|
|
|
|
</PaymentMethodErrorBoundary>
|
2020-01-06 22:28:09 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ExpressPaymentMethods;
|