2020-03-26 11:11:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
getPaymentMethods,
|
|
|
|
getExpressPaymentMethods,
|
|
|
|
} from '@woocommerce/blocks-registry';
|
|
|
|
import { useState, useEffect, useRef } from '@wordpress/element';
|
2020-04-09 11:44:29 +00:00
|
|
|
import {
|
|
|
|
useEditorContext,
|
|
|
|
useShippingDataContext,
|
|
|
|
} from '@woocommerce/base-context';
|
|
|
|
import { useStoreCart } from '@woocommerce/base-hooks';
|
2020-03-26 11:11:46 +00:00
|
|
|
|
|
|
|
const usePaymentMethodRegistration = (
|
|
|
|
dispatcher,
|
|
|
|
registeredPaymentMethods
|
|
|
|
) => {
|
2020-04-01 09:27:53 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2020-03-26 11:11:46 +00:00
|
|
|
const [ isInitialized, setIsInitialized ] = useState( false );
|
2020-04-09 11:44:29 +00:00
|
|
|
const { shippingAddress } = useShippingDataContext();
|
|
|
|
const { cartTotals, cartNeedsShipping } = useStoreCart();
|
|
|
|
const canPayArgument = useRef( {
|
|
|
|
cartTotals,
|
|
|
|
cartNeedsShipping,
|
|
|
|
shippingAddress,
|
|
|
|
} );
|
2020-03-26 11:11:46 +00:00
|
|
|
const countPaymentMethodsInitializing = useRef(
|
|
|
|
Object.keys( registeredPaymentMethods ).length
|
|
|
|
);
|
|
|
|
|
2020-04-09 11:44:29 +00:00
|
|
|
useEffect( () => {
|
|
|
|
canPayArgument.current = {
|
|
|
|
cartTotals,
|
|
|
|
cartNeedsShipping,
|
|
|
|
shippingAddress,
|
|
|
|
};
|
|
|
|
}, [ cartTotals, cartNeedsShipping, shippingAddress ] );
|
|
|
|
|
2020-03-26 11:11:46 +00:00
|
|
|
useEffect( () => {
|
|
|
|
// if all payment methods are initialized then bail.
|
|
|
|
if ( isInitialized ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const updatePaymentMethods = ( current, canPay = true ) => {
|
|
|
|
if ( canPay ) {
|
|
|
|
dispatcher( current );
|
|
|
|
}
|
|
|
|
// update the initialized count
|
|
|
|
countPaymentMethodsInitializing.current--;
|
|
|
|
// if count remaining less than 1, then set initialized.
|
|
|
|
if ( countPaymentMethodsInitializing.current < 1 ) {
|
|
|
|
setIsInitialized( true );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// loop through payment methods and see what the state is
|
|
|
|
for ( const paymentMethodId in registeredPaymentMethods ) {
|
|
|
|
const current = registeredPaymentMethods[ paymentMethodId ];
|
|
|
|
// if in editor context then we bypass can pay check.
|
|
|
|
if ( isEditor ) {
|
|
|
|
updatePaymentMethods( current );
|
|
|
|
} else {
|
2020-04-09 11:44:29 +00:00
|
|
|
Promise.resolve(
|
|
|
|
current.canMakePayment( canPayArgument.current )
|
|
|
|
)
|
2020-03-26 11:11:46 +00:00
|
|
|
.then( ( canPay ) => {
|
|
|
|
updatePaymentMethods( current, canPay );
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
// @todo, would be a good place to use the checkout error
|
|
|
|
// hooks here? Or maybe throw and catch by error boundary?
|
|
|
|
throw new Error(
|
|
|
|
'Problem with payment method initialization' +
|
|
|
|
( error.message || '' )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [ isInitialized, isEditor ] );
|
|
|
|
|
|
|
|
return isInitialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const usePaymentMethods = ( dispatcher ) =>
|
|
|
|
usePaymentMethodRegistration( dispatcher, getPaymentMethods() );
|
|
|
|
export const useExpressPaymentMethods = ( dispatcher ) =>
|
|
|
|
usePaymentMethodRegistration( dispatcher, getExpressPaymentMethods() );
|