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';
|
2020-05-12 16:40:08 +00:00
|
|
|
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
|
2020-04-09 11:44:29 +00:00
|
|
|
import {
|
|
|
|
useEditorContext,
|
|
|
|
useShippingDataContext,
|
|
|
|
} from '@woocommerce/base-context';
|
2020-07-13 22:52:13 +00:00
|
|
|
import { useStoreCart, useShallowEqual } 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-05-12 16:40:08 +00:00
|
|
|
/**
|
|
|
|
* If there was an error registering a payment method, alert the admin.
|
|
|
|
*
|
|
|
|
* @param {Object} error Error object.
|
|
|
|
*/
|
|
|
|
const handleRegistrationError = ( error ) => {
|
|
|
|
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-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
|
|
|
|
) => {
|
|
|
|
const [ isInitialized, setIsInitialized ] = useState( false );
|
2020-05-12 16:40:08 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2020-07-13 22:52:13 +00:00
|
|
|
const { selectedRates, shippingAddress } = useShippingDataContext();
|
|
|
|
const selectedShippingMethods = useShallowEqual( selectedRates );
|
2020-04-09 11:44:29 +00:00
|
|
|
const { cartTotals, cartNeedsShipping } = useStoreCart();
|
|
|
|
const canPayArgument = useRef( {
|
|
|
|
cartTotals,
|
|
|
|
cartNeedsShipping,
|
|
|
|
shippingAddress,
|
2020-07-13 22:52:13 +00:00
|
|
|
selectedShippingMethods,
|
2020-04-09 11:44:29 +00:00
|
|
|
} );
|
2020-04-14 22:44:31 +00:00
|
|
|
|
2020-04-09 11:44:29 +00:00
|
|
|
useEffect( () => {
|
|
|
|
canPayArgument.current = {
|
|
|
|
cartTotals,
|
|
|
|
cartNeedsShipping,
|
|
|
|
shippingAddress,
|
2020-07-13 22:52:13 +00:00
|
|
|
selectedShippingMethods,
|
2020-04-09 11:44:29 +00:00
|
|
|
};
|
2020-07-13 22:52:13 +00:00
|
|
|
}, [
|
|
|
|
cartTotals,
|
|
|
|
cartNeedsShipping,
|
|
|
|
shippingAddress,
|
|
|
|
selectedShippingMethods,
|
|
|
|
] );
|
2020-04-09 11:44:29 +00:00
|
|
|
|
2020-07-13 22:52:13 +00:00
|
|
|
const refreshCanMakePayments = useCallback( async () => {
|
|
|
|
let availablePaymentMethods = {};
|
|
|
|
const addAvailablePaymentMethod = ( paymentMethod ) => {
|
|
|
|
availablePaymentMethods = {
|
|
|
|
...availablePaymentMethods,
|
2020-05-12 16:40:08 +00:00
|
|
|
[ paymentMethod.name ]: paymentMethod,
|
|
|
|
};
|
2020-03-26 11:11:46 +00:00
|
|
|
};
|
2020-04-14 22:44:31 +00:00
|
|
|
for ( const paymentMethodName in registeredPaymentMethods ) {
|
2020-07-13 22:52:13 +00:00
|
|
|
const paymentMethod = registeredPaymentMethods[ paymentMethodName ];
|
2020-05-12 16:40:08 +00:00
|
|
|
|
2020-07-13 22:52:13 +00:00
|
|
|
// In editor, shortcut so all payment methods show as available.
|
2020-03-26 11:11:46 +00:00
|
|
|
if ( isEditor ) {
|
2020-07-13 22:52:13 +00:00
|
|
|
addAvailablePaymentMethod( paymentMethod );
|
2020-05-12 16:40:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-13 22:52:13 +00:00
|
|
|
// In front end, ask payment method if it should be available.
|
2020-05-12 16:40:08 +00:00
|
|
|
try {
|
2020-07-13 22:52:13 +00:00
|
|
|
const canPay = await Promise.resolve(
|
|
|
|
paymentMethod.canMakePayment( canPayArgument.current )
|
2020-05-12 16:40:08 +00:00
|
|
|
);
|
|
|
|
if ( canPay ) {
|
|
|
|
if ( canPay.error ) {
|
|
|
|
throw new Error( canPay.error.message );
|
|
|
|
}
|
2020-07-13 22:52:13 +00:00
|
|
|
addAvailablePaymentMethod( paymentMethod );
|
2020-05-12 16:40:08 +00:00
|
|
|
}
|
|
|
|
} catch ( e ) {
|
2020-07-13 22:52:13 +00:00
|
|
|
// If user is admin, show payment `canMakePayment` errors as a notice.
|
2020-05-12 16:40:08 +00:00
|
|
|
handleRegistrationError( e );
|
2020-03-26 11:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 22:52:13 +00:00
|
|
|
|
|
|
|
// Re-dispatch available payment methods to store.
|
|
|
|
dispatcher( availablePaymentMethods );
|
|
|
|
|
|
|
|
// Note: some payment methods use the `canMakePayment` callback to initialize / setup.
|
|
|
|
// Example: Stripe CC, Stripe Payment Request.
|
|
|
|
// That's why we track "is initialised" state here.
|
2020-05-12 16:40:08 +00:00
|
|
|
setIsInitialized( true );
|
|
|
|
}, [ dispatcher, isEditor, registeredPaymentMethods ] );
|
2020-03-26 11:11:46 +00:00
|
|
|
|
2020-07-13 22:52:13 +00:00
|
|
|
// Determine which payment methods are available initially and whenever
|
|
|
|
// shipping methods change.
|
|
|
|
// Some payment methods (e.g. COD) can be disabled for specific shipping methods.
|
2020-04-14 22:44:31 +00:00
|
|
|
useEffect( () => {
|
2020-07-13 22:52:13 +00:00
|
|
|
refreshCanMakePayments();
|
|
|
|
}, [ refreshCanMakePayments, selectedShippingMethods ] );
|
2020-04-14 22:44:31 +00:00
|
|
|
|
2020-03-26 11:11:46 +00:00
|
|
|
return isInitialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const usePaymentMethods = ( dispatcher ) =>
|
|
|
|
usePaymentMethodRegistration( dispatcher, getPaymentMethods() );
|
|
|
|
export const useExpressPaymentMethods = ( dispatcher ) =>
|
|
|
|
usePaymentMethodRegistration( dispatcher, getExpressPaymentMethods() );
|