woocommerce/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/registry.ts

129 lines
4.3 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import deprecated from '@wordpress/deprecated';
import type {
PaymentMethodConfiguration,
ExpressPaymentMethodConfiguration,
CanMakePaymentExtensionCallback,
PaymentMethodConfigInstance,
PaymentMethods,
ExpressPaymentMethods,
} from '@woocommerce/type-defs/payments';
/**
* Internal dependencies
*/
import { default as PaymentMethodConfig } from './payment-method-config';
import { default as ExpressPaymentMethodConfig } from './express-payment-method-config';
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
import { canMakePaymentExtensionsCallbacks } from './extensions-config';
type LegacyRegisterPaymentMethodFunction = ( config: unknown ) => unknown;
type LegacyRegisterExpessPaymentMethodFunction = ( config: unknown ) => unknown;
const paymentMethods: PaymentMethods = {};
const expressPaymentMethods: ExpressPaymentMethods = {};
/**
* Register a regular payment method.
*/
export const registerPaymentMethod = (
options: PaymentMethodConfiguration | LegacyRegisterPaymentMethodFunction
): void => {
let paymentMethodConfig: PaymentMethodConfigInstance | unknown;
if ( typeof options === 'function' ) {
// Legacy fallback for previous API, where client passes a function:
// registerPaymentMethod( ( Config ) => new Config( options ) );
paymentMethodConfig = options( PaymentMethodConfig );
deprecated( 'Passing a callback to registerPaymentMethod()', {
alternative: 'a config options object',
plugin: 'woocommerce-gutenberg-products-block',
link: 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3404',
} );
} else {
paymentMethodConfig = new PaymentMethodConfig( options );
}
if ( paymentMethodConfig instanceof PaymentMethodConfig ) {
Refactor logic for handling active payment method with express payment methods via checkout (https://github.com/woocommerce/woocommerce-blocks/pull/2170) * remove logic server side for getting payment method from paymentdata * ensure stripe accounts for payment request type payment methods * make sure legacy payment method handling always runs last * add processedPaymentMethodId to payment method data context state * switch checkout processor to use new processedPaymentMethod id for submission * implement returning paymentMethodId from payment-request-express * include paymentMethodId in stripe cc success return value * include paymentMethodId in cheque success return value * add active payment method setting and handling via checkout express payment methods still need to implement: - onClick when their button is clicked - onClose when the express payment interface is closed (cancelled etc). * don’t expose setActivePaymentMethod on the payment method interface * remove/fix artifacts from earlier iterations of the pull * rename `id` property to `name` property for payment method registration * Revert "include paymentMethodId in cheque success return value" This reverts commit fe4ee8aced6d67bbd9033263ce61844349d18250. * Revert "include paymentMethodId in stripe cc success return value" This reverts commit 359a1f0089866110ec204182f8ffa14ab099c425. * Revert "implement returning paymentMethodId from payment-request-express" This reverts commit 117c68980b0876dee0acc78cec7754ccfe2a9bb1. * Revert "switch checkout processor to use new processedPaymentMethod id for submission" This reverts commit c38a05b63626dfc1336c7bb0e86417b798a803d6. * Revert "add processedPaymentMethodId to payment method data context state" This reverts commit 3d7923e7297f3c76efde536d26eaf68464ba9583. * improve isSuccess response check and variable name * implement paymentMethodId config option * doh php ain’t javascript * add missing dependency from rebase
2020-04-09 15:22:34 +00:00
paymentMethods[ paymentMethodConfig.name ] = paymentMethodConfig;
}
};
/**
* Register an express payment method.
*/
export const registerExpressPaymentMethod = (
options:
| ExpressPaymentMethodConfiguration
| LegacyRegisterExpessPaymentMethodFunction
): void => {
let paymentMethodConfig;
if ( typeof options === 'function' ) {
// Legacy fallback for previous API, where client passes a function:
// registerExpressPaymentMethod( ( Config ) => new Config( options ) );
paymentMethodConfig = options( ExpressPaymentMethodConfig );
deprecated( 'Passing a callback to registerExpressPaymentMethod()', {
alternative: 'a config options object',
plugin: 'woocommerce-gutenberg-products-block',
link: 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3404',
} );
} else {
paymentMethodConfig = new ExpressPaymentMethodConfig( options );
}
if ( paymentMethodConfig instanceof ExpressPaymentMethodConfig ) {
Refactor logic for handling active payment method with express payment methods via checkout (https://github.com/woocommerce/woocommerce-blocks/pull/2170) * remove logic server side for getting payment method from paymentdata * ensure stripe accounts for payment request type payment methods * make sure legacy payment method handling always runs last * add processedPaymentMethodId to payment method data context state * switch checkout processor to use new processedPaymentMethod id for submission * implement returning paymentMethodId from payment-request-express * include paymentMethodId in stripe cc success return value * include paymentMethodId in cheque success return value * add active payment method setting and handling via checkout express payment methods still need to implement: - onClick when their button is clicked - onClose when the express payment interface is closed (cancelled etc). * don’t expose setActivePaymentMethod on the payment method interface * remove/fix artifacts from earlier iterations of the pull * rename `id` property to `name` property for payment method registration * Revert "include paymentMethodId in cheque success return value" This reverts commit fe4ee8aced6d67bbd9033263ce61844349d18250. * Revert "include paymentMethodId in stripe cc success return value" This reverts commit 359a1f0089866110ec204182f8ffa14ab099c425. * Revert "implement returning paymentMethodId from payment-request-express" This reverts commit 117c68980b0876dee0acc78cec7754ccfe2a9bb1. * Revert "switch checkout processor to use new processedPaymentMethod id for submission" This reverts commit c38a05b63626dfc1336c7bb0e86417b798a803d6. * Revert "add processedPaymentMethodId to payment method data context state" This reverts commit 3d7923e7297f3c76efde536d26eaf68464ba9583. * improve isSuccess response check and variable name * implement paymentMethodId config option * doh php ain’t javascript * add missing dependency from rebase
2020-04-09 15:22:34 +00:00
expressPaymentMethods[ paymentMethodConfig.name ] = paymentMethodConfig;
}
};
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
/**
* Allows extension to register callbacks for specific payment methods to determine if they can make payments
*/
export const registerPaymentMethodExtensionCallbacks = (
namespace: string,
callbacks: Record< string, CanMakePaymentExtensionCallback >
): void => {
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
if ( canMakePaymentExtensionsCallbacks[ namespace ] ) {
// eslint-disable-next-line no-console
console.error(
`The namespace provided to registerPaymentMethodExtensionCallbacks must be unique. Callbacks have already been registered for the ${ namespace } namespace.`
);
} else {
// Set namespace up as an empty object.
canMakePaymentExtensionsCallbacks[ namespace ] = {};
Object.entries( callbacks ).forEach(
( [ paymentMethodName, callback ] ) => {
if ( typeof callback === 'function' ) {
canMakePaymentExtensionsCallbacks[ namespace ][
paymentMethodName
] = callback;
} else {
// eslint-disable-next-line no-console
console.error(
`All callbacks provided to registerPaymentMethodExtensionCallbacks must be functions. The callback for the ${ paymentMethodName } payment method in the ${ namespace } namespace was not a function.`
);
}
}
);
}
};
export const __experimentalDeRegisterPaymentMethod = (
paymentMethodName: string
): void => {
delete paymentMethods[ paymentMethodName ];
};
export const __experimentalDeRegisterExpressPaymentMethod = (
paymentMethodName: string
): void => {
delete expressPaymentMethods[ paymentMethodName ];
};
export const getPaymentMethods = (): PaymentMethods => {
return paymentMethods;
};
export const getExpressPaymentMethods = (): ExpressPaymentMethods => {
return expressPaymentMethods;
};