woocommerce/plugins/woocommerce-blocks/assets/js/base/context/cart-checkout/payment-methods/use-payment-method-registra...

138 lines
4.4 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import {
getPaymentMethods,
getExpressPaymentMethods,
} from '@woocommerce/blocks-registry';
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
import {
useEditorContext,
useShippingDataContext,
} from '@woocommerce/base-context';
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
import { useStoreCart, useShallowEqual } from '@woocommerce/base-hooks';
import { CURRENT_USER_IS_ADMIN } from '@woocommerce/block-settings';
/**
* 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
)
);
}
};
/**
* 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.
*/
const usePaymentMethodRegistration = (
dispatcher,
registeredPaymentMethods
) => {
const [ isInitialized, setIsInitialized ] = useState( false );
const { isEditor } = useEditorContext();
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
const { selectedRates, shippingAddress } = useShippingDataContext();
const selectedShippingMethods = useShallowEqual( selectedRates );
const { cartTotals, cartNeedsShipping } = useStoreCart();
const canPayArgument = useRef( {
cartTotals,
cartNeedsShipping,
shippingAddress,
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
selectedShippingMethods,
} );
useEffect( () => {
canPayArgument.current = {
cartTotals,
cartNeedsShipping,
shippingAddress,
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
selectedShippingMethods,
};
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
}, [
cartTotals,
cartNeedsShipping,
shippingAddress,
selectedShippingMethods,
] );
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
const refreshCanMakePayments = useCallback( async () => {
let availablePaymentMethods = {};
const addAvailablePaymentMethod = ( paymentMethod ) => {
availablePaymentMethods = {
...availablePaymentMethods,
[ paymentMethod.name ]: paymentMethod,
};
};
for ( const paymentMethodName in registeredPaymentMethods ) {
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
const paymentMethod = registeredPaymentMethods[ paymentMethodName ];
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
// In editor, shortcut so all payment methods show as available.
if ( isEditor ) {
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
addAvailablePaymentMethod( paymentMethod );
continue;
}
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
// In front end, ask payment method if it should be available.
try {
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
const canPay = await Promise.resolve(
paymentMethod.canMakePayment( canPayArgument.current )
);
if ( canPay ) {
if ( canPay.error ) {
throw new Error( canPay.error.message );
}
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
addAvailablePaymentMethod( paymentMethod );
}
} catch ( e ) {
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
// If user is admin, show payment `canMakePayment` errors as a notice.
handleRegistrationError( e );
}
}
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
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.
setIsInitialized( true );
}, [ dispatcher, isEditor, registeredPaymentMethods ] );
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
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.
useEffect( () => {
allow payment methods to disable based on shipping (API change) (https://github.com/woocommerce/woocommerce-blocks/pull/2840) * allow payment methods to disable based on shipping or other factors: - renamed 'initialized' array 'available' to match primary purpose of `canMakePayment` api - whether payment method should be available - trigger refresh of available payment methods when shopper chooses different shipping method - rename resolveCanMakePayments => refreshCanMakePayments - tweaked some variable names and scope for clarity - added comments to clarify things Note this should not affect behaviour yet - no existing payment methods use this new feature. COD payment method will need this - woocommerce/woocommerce-blocks#2831 * optimise refreshCanMakePayments: - useShallowEqual to avoid unnecessary call when shipping methods have not actually changed (but object value has) * replace ("set") payment methods in store, was appending: - payment methods may come and go depending on cart/checkout state - the previous SET action appended provided payment methods to the collection - this prevents dynamic payment methods e.g. COD from being able to hide i.e. disable * cache test payment request to avoid unnecessary stripe API calls: - in the canMakePayment callback there's a test payment to determine if chrome pay/apple pay is set up and available - canMakePayment is now called multiple times as checkout state changes - now the results of the test payment are stored in variable, and returned on subsequent calls * set init flag to avoid additional attempts to init stripe API: + tweak naming of init flag
2020-07-13 22:52:13 +00:00
refreshCanMakePayments();
}, [ refreshCanMakePayments, selectedShippingMethods ] );
return isInitialized;
};
export const usePaymentMethods = ( dispatcher ) =>
usePaymentMethodRegistration( dispatcher, getPaymentMethods() );
export const useExpressPaymentMethods = ( dispatcher ) =>
usePaymentMethodRegistration( dispatcher, getExpressPaymentMethods() );