woocommerce/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/payment-method-config-helpe...

91 lines
2.7 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import type {
CanMakePaymentCallback,
CanMakePaymentExtensionCallback,
} from '@woocommerce/type-defs/payments';
/**
* Internal dependencies
*/
import {
NamespacedCanMakePaymentExtensionsCallbacks,
PaymentMethodName,
ExtensionNamespace,
extensionsConfig,
} from './extensions-config';
// Filter out payment methods by supported features and cart requirement.
export const canMakePaymentWithFeaturesCheck = (
canMakePayment: CanMakePaymentCallback,
features: string[]
): CanMakePaymentCallback => ( canPayArgument ) => {
const requirements = canPayArgument?.paymentRequirements || [];
const featuresSupportRequirements = requirements.every( ( requirement ) =>
features.includes( requirement )
);
return featuresSupportRequirements && canMakePayment( canPayArgument );
};
Add extensibility point for extensions to filter payment methods (https://github.com/woocommerce/woocommerce-blocks/pull/4668) * Add extensionsConfig when registering a payment method The extension config has its own canMakePayment where extensions can add callback using a payment method's name. * Make canMakePayment a getter on PaymentMethodConfig Because extensions can register canMakePayment callbacks for a payment method before it is registered we need to transform canMakePayment into a getter so that it's always recalculating it's value based on the registered callbacks/ * Rename extension related config and method * Format comments * Add an extension namespace to registerPaymentMethodExtensionCallback utility This commit changes the API for how extensions will register their own callbacks to canMakePayment, so that they can add their namespace and also callbacks for multiple payment methods. * Format comments * Update assets/js/blocks-registry/payment-methods/payment-method-config.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix eslint warning * Handle errors at registerPaymentMethodExtensionCallbacks level * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix formatting issues Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2021-09-08 11:29:29 +00:00
// Filter out payment methods by callbacks registered by extensions.
export const canMakePaymentWithExtensions = (
canMakePayment: CanMakePaymentCallback,
extensionsCallbacks: NamespacedCanMakePaymentExtensionsCallbacks,
paymentMethodName: PaymentMethodName
): CanMakePaymentCallback => ( canPayArgument ) => {
Add extensibility point for extensions to filter payment methods (https://github.com/woocommerce/woocommerce-blocks/pull/4668) * Add extensionsConfig when registering a payment method The extension config has its own canMakePayment where extensions can add callback using a payment method's name. * Make canMakePayment a getter on PaymentMethodConfig Because extensions can register canMakePayment callbacks for a payment method before it is registered we need to transform canMakePayment into a getter so that it's always recalculating it's value based on the registered callbacks/ * Rename extension related config and method * Format comments * Add an extension namespace to registerPaymentMethodExtensionCallback utility This commit changes the API for how extensions will register their own callbacks to canMakePayment, so that they can add their namespace and also callbacks for multiple payment methods. * Format comments * Update assets/js/blocks-registry/payment-methods/payment-method-config.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix eslint warning * Handle errors at registerPaymentMethodExtensionCallbacks level * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix formatting issues Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2021-09-08 11:29:29 +00:00
// Validate whether the payment method is available based on its own criteria first.
let canPay = canMakePayment( canPayArgument );
if ( canPay ) {
// Gather all callbacks for paymentMethodName.
const namespacedCallbacks: Record<
ExtensionNamespace,
CanMakePaymentExtensionCallback
> = {};
Add extensibility point for extensions to filter payment methods (https://github.com/woocommerce/woocommerce-blocks/pull/4668) * Add extensionsConfig when registering a payment method The extension config has its own canMakePayment where extensions can add callback using a payment method's name. * Make canMakePayment a getter on PaymentMethodConfig Because extensions can register canMakePayment callbacks for a payment method before it is registered we need to transform canMakePayment into a getter so that it's always recalculating it's value based on the registered callbacks/ * Rename extension related config and method * Format comments * Add an extension namespace to registerPaymentMethodExtensionCallback utility This commit changes the API for how extensions will register their own callbacks to canMakePayment, so that they can add their namespace and also callbacks for multiple payment methods. * Format comments * Update assets/js/blocks-registry/payment-methods/payment-method-config.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix eslint warning * Handle errors at registerPaymentMethodExtensionCallbacks level * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix formatting issues Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2021-09-08 11:29:29 +00:00
Object.entries( extensionsCallbacks ).forEach(
( [ namespace, callbacks ] ) => {
namespacedCallbacks[ namespace ] =
callbacks[ paymentMethodName ];
Add extensibility point for extensions to filter payment methods (https://github.com/woocommerce/woocommerce-blocks/pull/4668) * Add extensionsConfig when registering a payment method The extension config has its own canMakePayment where extensions can add callback using a payment method's name. * Make canMakePayment a getter on PaymentMethodConfig Because extensions can register canMakePayment callbacks for a payment method before it is registered we need to transform canMakePayment into a getter so that it's always recalculating it's value based on the registered callbacks/ * Rename extension related config and method * Format comments * Add an extension namespace to registerPaymentMethodExtensionCallback utility This commit changes the API for how extensions will register their own callbacks to canMakePayment, so that they can add their namespace and also callbacks for multiple payment methods. * Format comments * Update assets/js/blocks-registry/payment-methods/payment-method-config.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix eslint warning * Handle errors at registerPaymentMethodExtensionCallbacks level * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix formatting issues Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2021-09-08 11:29:29 +00:00
}
);
Add extensibility point for extensions to filter payment methods (https://github.com/woocommerce/woocommerce-blocks/pull/4668) * Add extensionsConfig when registering a payment method The extension config has its own canMakePayment where extensions can add callback using a payment method's name. * Make canMakePayment a getter on PaymentMethodConfig Because extensions can register canMakePayment callbacks for a payment method before it is registered we need to transform canMakePayment into a getter so that it's always recalculating it's value based on the registered callbacks/ * Rename extension related config and method * Format comments * Add an extension namespace to registerPaymentMethodExtensionCallback utility This commit changes the API for how extensions will register their own callbacks to canMakePayment, so that they can add their namespace and also callbacks for multiple payment methods. * Format comments * Update assets/js/blocks-registry/payment-methods/payment-method-config.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix eslint warning * Handle errors at registerPaymentMethodExtensionCallbacks level * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix formatting issues Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2021-09-08 11:29:29 +00:00
canPay = Object.keys( namespacedCallbacks ).every( ( namespace ) => {
try {
return namespacedCallbacks[ namespace ]( canPayArgument );
} catch ( err ) {
// eslint-disable-next-line no-console
console.error(
`Error when executing callback for ${ paymentMethodName } in ${ namespace }`,
err
);
// .every() expects a return value at the end of every arrow function and
Add extensibility point for extensions to filter payment methods (https://github.com/woocommerce/woocommerce-blocks/pull/4668) * Add extensionsConfig when registering a payment method The extension config has its own canMakePayment where extensions can add callback using a payment method's name. * Make canMakePayment a getter on PaymentMethodConfig Because extensions can register canMakePayment callbacks for a payment method before it is registered we need to transform canMakePayment into a getter so that it's always recalculating it's value based on the registered callbacks/ * Rename extension related config and method * Format comments * Add an extension namespace to registerPaymentMethodExtensionCallback utility This commit changes the API for how extensions will register their own callbacks to canMakePayment, so that they can add their namespace and also callbacks for multiple payment methods. * Format comments * Update assets/js/blocks-registry/payment-methods/payment-method-config.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/payment-method-config-helper.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix eslint warning * Handle errors at registerPaymentMethodExtensionCallbacks level * Update assets/js/blocks-registry/payment-methods/registry.js Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Fix formatting issues Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2021-09-08 11:29:29 +00:00
// this ensures that the error is ignored when computing the whole result.
return true;
}
} );
}
return canPay;
};
export const getCanMakePayment = (
canMakePayment: CanMakePaymentCallback,
features: string[],
paymentMethodName: string
): CanMakePaymentCallback => {
const canPay = canMakePaymentWithFeaturesCheck( canMakePayment, features );
// Loop through all callbacks to check if there are any registered for this payment method.
return ( Object.values( extensionsConfig.canMakePayment ) as Record<
PaymentMethodName,
CanMakePaymentCallback
>[] ).some( ( callbacks ) => paymentMethodName in callbacks )
? canMakePaymentWithExtensions(
canPay,
extensionsConfig.canMakePayment,
paymentMethodName
)
: canPay;
};