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

206 lines
6.2 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 {
useEmitResponse,
useShallowEqual,
useStoreCart,
useStoreNotices,
} from '@woocommerce/base-hooks';
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
import {
CURRENT_USER_IS_ADMIN,
PAYMENT_GATEWAY_SORT_ORDER,
} from '@woocommerce/block-settings';
/**
* Internal dependencies
*/
import { useEditorContext } from '../../editor';
import { useShippingDataContext } from '../shipping';
/**
* 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.
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
* @param {Array} paymentMethodsSortOrder Array of payment method names to
* sort by. This should match keys of
* registeredPaymentMethods.
* @param {string} noticeContext Id of the context to append
* notices to.
*
* @return {boolean} Whether the payment methods have been initialized or not. True when all payment
* methods have been initialized.
*/
const usePaymentMethodRegistration = (
dispatcher,
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
registeredPaymentMethods,
paymentMethodsSortOrder,
noticeContext
) => {
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 );
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
const paymentMethodsOrder = useShallowEqual( paymentMethodsSortOrder );
const {
cartTotals,
cartNeedsShipping,
paymentRequirements,
} = 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,
paymentRequirements,
} );
const { addErrorNotice } = useStoreNotices();
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,
paymentRequirements,
};
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,
paymentRequirements,
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
] );
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 = {};
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 addAvailablePaymentMethod = ( paymentMethod ) => {
availablePaymentMethods = {
...availablePaymentMethods,
[ paymentMethod.name ]: paymentMethod,
};
};
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
for ( let i = 0; i < paymentMethodsOrder.length; i++ ) {
const paymentMethodName = paymentMethodsOrder[ i ];
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 ];
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
if ( ! 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 ) {
if ( CURRENT_USER_IS_ADMIN || isEditor ) {
const errorText = sprintf(
/* translators: %s the id of the payment method being registered (bank transfer, Stripe...) */
__(
`There was an error registering the payment method with id '%s': `,
'woo-gutenberg-products-block'
),
paymentMethod.paymentMethodId
);
addErrorNotice( `${ errorText } ${ e }`, {
context: noticeContext,
id: `wc-${ paymentMethod.paymentMethodId }-registration-error`,
} );
}
}
}
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 );
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
}, [
addErrorNotice,
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
dispatcher,
isEditor,
noticeContext,
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
paymentMethodsOrder,
registeredPaymentMethods,
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
] );
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 or cart totals change.
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
// 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,
cartTotals,
selectedShippingMethods,
paymentRequirements,
] );
return isInitialized;
};
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
/**
* Custom hook for setting up payment methods (standard, non-express).
*
* @param {function(Object):undefined} dispatcher
*
* @return {boolean} True when standard payment methods have been initialized.
*/
export const usePaymentMethods = ( dispatcher ) => {
const standardMethods = getPaymentMethods();
const { noticeContexts } = useEmitResponse();
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
// Ensure all methods are present in order.
// Some payment methods may not be present in PAYMENT_GATEWAY_SORT_ORDER if they
// depend on state, e.g. COD can depend on shipping method.
const displayOrder = new Set( [
...PAYMENT_GATEWAY_SORT_ORDER,
...Object.keys( standardMethods ),
] );
return usePaymentMethodRegistration(
dispatcher,
standardMethods,
Array.from( displayOrder ),
noticeContexts.PAYMENTS
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
);
};
/**
* Custom hook for setting up express payment methods.
*
* @param {function(Object):undefined} dispatcher
*
* @return {boolean} True when express payment methods have been initialized.
*/
export const useExpressPaymentMethods = ( dispatcher ) => {
const expressMethods = getExpressPaymentMethods();
const { noticeContexts } = useEmitResponse();
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
return usePaymentMethodRegistration(
dispatcher,
expressMethods,
Object.keys( expressMethods ),
noticeContexts.EXPRESS_PAYMENTS
Show payment gateways in correct (merchant-configured) order (https://github.com/woocommerce/woocommerce-blocks/pull/2934) * enqueue order of payment gateways as script data * use configured gateway order for payment method tabs * clarify name of payment gateway sort order setting * fix formatting Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * fix bug - express payment methods not working due to ordering logic: - move ordering code to payment methods in usePaymentMethods hook; - this only applies to standard (non express) payment methods - also any code that uses payment methods will get them in correct order * why did this formatting not happen pre-commit? * move method ordering into refreshPaymentMethods to avoid infinite loop: - pass keys of express payment methods so they work correctly (show up) - refreshPaymentMethods dispatches availablePaymentMethods to redux store; I suspect this was causing infinite loop as the callback depends on registeredPaymentMethods - handle case when a payment method is not in PAYMENT_GATEWAY_SORT_ORDER - this happens with COD when dependent on shipping * fix formatting * use shallow equal to prevent refreshCanMakePayments infinite loop * use native Set instead of lodash.union for unique gateway names * code formatting * check to ensure we register paymentGatewaySortOrder asset once only * fix COD tab showing out of order if not available at page load: - return ALL gateways in sort order array - note this includes gateways that are not enabled by merchant * reinstate return bool from usePaymentMethods hook (broken in refactor) * add basic typedefs for payment methods hooks Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-08-05 01:56:33 +00:00
);
};