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

49 lines
1.6 KiB
JavaScript
Raw Normal View History

// Filter out payment methods by supported features and cart requirement.
export const canMakePaymentWithFeaturesCheck = ( canMakePayment, features ) => (
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,
extensionsCallbacks,
paymentMethodName
) => ( canPayArgument ) => {
// Validate whether the payment method is available based on its own criteria first.
let canPay = canMakePayment( canPayArgument );
if ( canPay ) {
const namespacedCallbacks = {};
Object.entries( extensionsCallbacks ).forEach(
( [ namespace, callbacks ] ) => {
if ( typeof callbacks[ paymentMethodName ] === 'function' ) {
namespacedCallbacks[ namespace ] =
callbacks[ paymentMethodName ];
}
}
);
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
// this ensures that the error is ignored when computing the whole result.
return true;
}
} );
}
return canPay;
};