2021-06-16 12:44:40 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useShallowEqual } from '@woocommerce/base-hooks';
|
2021-09-15 16:36:02 +00:00
|
|
|
import type {
|
|
|
|
PaymentMethods,
|
|
|
|
ExpressPaymentMethods,
|
2022-08-23 14:58:56 +00:00
|
|
|
PaymentMethodConfigInstance,
|
|
|
|
ExpressPaymentMethodConfigInstance,
|
2021-09-15 16:36:02 +00:00
|
|
|
} from '@woocommerce/type-defs/payments';
|
2022-08-22 13:56:44 +00:00
|
|
|
import {
|
|
|
|
getPaymentMethods,
|
|
|
|
getExpressPaymentMethods,
|
|
|
|
} from '@woocommerce/blocks-registry';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { PAYMENT_METHOD_DATA_STORE_KEY } from '@woocommerce/block-data';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
2021-09-15 16:36:02 +00:00
|
|
|
interface PaymentMethodState {
|
|
|
|
paymentMethods: PaymentMethods;
|
|
|
|
isInitialized: boolean;
|
|
|
|
}
|
|
|
|
interface ExpressPaymentMethodState {
|
|
|
|
paymentMethods: ExpressPaymentMethods;
|
|
|
|
isInitialized: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const usePaymentMethodState = (
|
|
|
|
express = false
|
|
|
|
): PaymentMethodState | ExpressPaymentMethodState => {
|
2022-08-23 14:58:56 +00:00
|
|
|
const {
|
|
|
|
paymentMethodsInitialized,
|
|
|
|
expressPaymentMethodsInitialized,
|
|
|
|
availablePaymentMethods,
|
|
|
|
availableExpressPaymentMethods,
|
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const store = select( PAYMENT_METHOD_DATA_STORE_KEY );
|
|
|
|
|
|
|
|
return {
|
|
|
|
paymentMethodsInitialized: store.paymentMethodsInitialized(),
|
|
|
|
expressPaymentMethodsInitialized:
|
|
|
|
store.expressPaymentMethodsInitialized(),
|
|
|
|
availableExpressPaymentMethods:
|
|
|
|
store.getAvailableExpressPaymentMethods(),
|
|
|
|
availablePaymentMethods: store.getAvailablePaymentMethods(),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const availablePaymentMethodNames = Object.values(
|
|
|
|
availablePaymentMethods
|
|
|
|
).map( ( { name } ) => name );
|
|
|
|
const availableExpressPaymentMethodNames = Object.values(
|
|
|
|
availableExpressPaymentMethods
|
|
|
|
).map( ( { name } ) => name );
|
|
|
|
|
|
|
|
const registeredPaymentMethods = getPaymentMethods();
|
|
|
|
const registeredExpressPaymentMethods = getExpressPaymentMethods();
|
|
|
|
|
|
|
|
// Remove everything from registeredPaymentMethods that is not in availablePaymentMethodNames.
|
|
|
|
const paymentMethods = Object.keys( registeredPaymentMethods ).reduce(
|
|
|
|
( acc: Record< string, PaymentMethodConfigInstance >, key ) => {
|
|
|
|
if ( availablePaymentMethodNames.includes( key ) ) {
|
|
|
|
acc[ key ] = registeredPaymentMethods[ key ];
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
// Remove everything from registeredExpressPaymentMethods that is not in availableExpressPaymentMethodNames.
|
|
|
|
const expressPaymentMethods = Object.keys(
|
|
|
|
registeredExpressPaymentMethods
|
|
|
|
).reduce(
|
|
|
|
( acc: Record< string, ExpressPaymentMethodConfigInstance >, key ) => {
|
|
|
|
if ( availableExpressPaymentMethodNames.includes( key ) ) {
|
|
|
|
acc[ key ] = registeredExpressPaymentMethods[ key ];
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2022-08-22 13:56:44 +00:00
|
|
|
|
|
|
|
const currentPaymentMethods = useShallowEqual( paymentMethods );
|
2021-06-16 12:44:40 +00:00
|
|
|
const currentExpressPaymentMethods = useShallowEqual(
|
2022-08-22 13:56:44 +00:00
|
|
|
expressPaymentMethods
|
2021-06-16 12:44:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
paymentMethods: express
|
|
|
|
? currentExpressPaymentMethods
|
|
|
|
: currentPaymentMethods,
|
|
|
|
isInitialized: express
|
|
|
|
? expressPaymentMethodsInitialized
|
|
|
|
: paymentMethodsInitialized,
|
|
|
|
};
|
2020-01-06 22:28:09 +00:00
|
|
|
};
|
|
|
|
|
2021-09-15 16:36:02 +00:00
|
|
|
export const usePaymentMethods = ():
|
|
|
|
| PaymentMethodState
|
|
|
|
| ExpressPaymentMethodState => usePaymentMethodState( false );
|
|
|
|
export const useExpressPaymentMethods = (): ExpressPaymentMethodState =>
|
|
|
|
usePaymentMethodState( true );
|