2020-03-26 11:11:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-04-15 15:15:56 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2020-03-26 11:11:46 +00:00
|
|
|
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-04-15 15:15:56 +00:00
|
|
|
import { CURRENT_USER_IS_ADMIN } from '@woocommerce/block-settings';
|
2020-03-26 11:11:46 +00:00
|
|
|
|
2020-04-14 22:44:31 +00:00
|
|
|
/**
|
|
|
|
* This hook handles initializing registered payment methods and exposing all
|
|
|
|
* registered payment methods that can be used in the current environment (via
|
|
|
|
* the payment method's `canMakePayment` property).
|
|
|
|
*
|
|
|
|
* @param {function(Object):undefined} dispatcher A dispatcher for setting registered
|
|
|
|
* payment methods to an external
|
|
|
|
* state.
|
|
|
|
* @param {Object} registeredPaymentMethods Registered payment methods to
|
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether the payment methods have been initialized or not. True when all payment
|
|
|
|
* methods have been initialized.
|
|
|
|
*/
|
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-14 22:44:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Object} initializedMethodsDefault Used to hold payment methods that have been
|
|
|
|
* initialized.
|
|
|
|
*/
|
|
|
|
const [
|
|
|
|
initializedPaymentMethods,
|
|
|
|
setInitializedPaymentMethods,
|
|
|
|
] = useState( {} );
|
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-14 22:44:31 +00:00
|
|
|
const setInitializedPaymentMethod = ( paymentMethod ) => {
|
|
|
|
setInitializedPaymentMethods( ( paymentMethods ) => ( {
|
|
|
|
...paymentMethods,
|
|
|
|
[ paymentMethod.name ]: paymentMethod,
|
|
|
|
} ) );
|
|
|
|
};
|
|
|
|
|
2020-04-09 11:44:29 +00:00
|
|
|
useEffect( () => {
|
|
|
|
canPayArgument.current = {
|
|
|
|
cartTotals,
|
|
|
|
cartNeedsShipping,
|
|
|
|
shippingAddress,
|
|
|
|
};
|
|
|
|
}, [ cartTotals, cartNeedsShipping, shippingAddress ] );
|
|
|
|
|
2020-04-14 22:44:31 +00:00
|
|
|
// Handles initialization of all payment methods.
|
|
|
|
// Note: registeredPaymentMethods is not a dependency because this will not
|
|
|
|
// change in the life of the hook, it comes from an externally set value.
|
2020-03-26 11:11:46 +00:00
|
|
|
useEffect( () => {
|
|
|
|
// if all payment methods are initialized then bail.
|
|
|
|
if ( isInitialized ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-15 15:15:56 +00:00
|
|
|
const updatePaymentMethod = ( current, canPay = true ) => {
|
2020-03-26 11:11:46 +00:00
|
|
|
if ( canPay ) {
|
2020-04-14 22:44:31 +00:00
|
|
|
setInitializedPaymentMethod( current );
|
2020-03-26 11:11:46 +00:00
|
|
|
}
|
|
|
|
// 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
|
2020-04-14 22:44:31 +00:00
|
|
|
for ( const paymentMethodName in registeredPaymentMethods ) {
|
|
|
|
const current = registeredPaymentMethods[ paymentMethodName ];
|
2020-03-26 11:11:46 +00:00
|
|
|
// if in editor context then we bypass can pay check.
|
|
|
|
if ( isEditor ) {
|
2020-04-15 15:15:56 +00:00
|
|
|
updatePaymentMethod( current );
|
2020-03-26 11:11:46 +00:00
|
|
|
} 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 ) => {
|
2020-04-15 15:15:56 +00:00
|
|
|
if ( canPay.error ) {
|
|
|
|
throw new Error( canPay.error.message );
|
|
|
|
} else {
|
|
|
|
updatePaymentMethod( current, canPay );
|
|
|
|
}
|
2020-03-26 11:11:46 +00:00
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
2020-04-15 15:15:56 +00:00
|
|
|
updatePaymentMethod( current, false );
|
|
|
|
if ( CURRENT_USER_IS_ADMIN ) {
|
|
|
|
throw new Error(
|
|
|
|
sprintf(
|
|
|
|
__(
|
|
|
|
// translators: %s is the error method returned by the payment method.
|
|
|
|
'Problem with payment method initialization: %s',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
error.message
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2020-03-26 11:11:46 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [ isInitialized, isEditor ] );
|
|
|
|
|
2020-04-14 22:44:31 +00:00
|
|
|
// once all payment methods have been initialized, resort to be in the same
|
|
|
|
// order as registered and then set via the dispatcher.
|
|
|
|
// Note: registeredPaymentMethods is not a dependency because this will not
|
|
|
|
// change in the life of the hook, it comes from an externally set value.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( isInitialized ) {
|
|
|
|
const reSortByRegisteredOrder = () => {
|
|
|
|
const newSet = {};
|
|
|
|
for ( const paymentMethodName in registeredPaymentMethods ) {
|
|
|
|
if ( initializedPaymentMethods[ paymentMethodName ] ) {
|
|
|
|
newSet[ paymentMethodName ] =
|
|
|
|
initializedPaymentMethods[ paymentMethodName ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newSet;
|
|
|
|
};
|
|
|
|
dispatcher( reSortByRegisteredOrder() );
|
|
|
|
}
|
|
|
|
}, [ isInitialized, initializedPaymentMethods ] );
|
|
|
|
|
2020-03-26 11:11:46 +00:00
|
|
|
return isInitialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const usePaymentMethods = ( dispatcher ) =>
|
|
|
|
usePaymentMethodRegistration( dispatcher, getPaymentMethods() );
|
|
|
|
export const useExpressPaymentMethods = ( dispatcher ) =>
|
|
|
|
usePaymentMethodRegistration( dispatcher, getExpressPaymentMethods() );
|