2020-11-18 22:06:33 +00:00
/ * *
* External dependencies
* /
import deprecated from '@wordpress/deprecated' ;
2021-09-15 16:36:02 +00:00
import type {
PaymentMethodConfiguration ,
ExpressPaymentMethodConfiguration ,
CanMakePaymentCallback ,
PaymentMethodConfigInstance ,
PaymentMethods ,
ExpressPaymentMethods ,
} from '@woocommerce/type-defs/payments' ;
2020-11-18 22:06:33 +00:00
2020-01-06 22:28:09 +00:00
/ * *
* Internal dependencies
* /
import { default as PaymentMethodConfig } from './payment-method-config' ;
import { default as ExpressPaymentMethodConfig } from './express-payment-method-config' ;
2021-09-08 11:29:29 +00:00
import { canMakePaymentExtensionsCallbacks } from './extensions-config' ;
2021-09-15 16:36:02 +00:00
type LegacyRegisterPaymentMethodFunction = ( config : unknown ) = > unknown ;
type LegacyRegisterExpessPaymentMethodFunction = ( config : unknown ) = > unknown ;
const paymentMethods : PaymentMethods = { } ;
const expressPaymentMethods : ExpressPaymentMethods = { } ;
2020-01-06 22:28:09 +00:00
2020-11-18 22:06:33 +00:00
/ * *
* Register a regular payment method .
* /
2021-09-15 16:36:02 +00:00
export const registerPaymentMethod = (
options : PaymentMethodConfiguration | LegacyRegisterPaymentMethodFunction
) : void = > {
let paymentMethodConfig : PaymentMethodConfigInstance | unknown ;
2020-11-18 22:06:33 +00:00
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 ) ;
}
2020-01-06 22:28:09 +00:00
if ( paymentMethodConfig instanceof PaymentMethodConfig ) {
2020-04-09 15:22:34 +00:00
paymentMethods [ paymentMethodConfig . name ] = paymentMethodConfig ;
2020-01-06 22:28:09 +00:00
}
} ;
2020-11-18 22:06:33 +00:00
/ * *
* Register an express payment method .
* /
2021-09-15 16:36:02 +00:00
export const registerExpressPaymentMethod = (
options :
| ExpressPaymentMethodConfiguration
| LegacyRegisterExpessPaymentMethodFunction
) : void = > {
2020-11-18 22:06:33 +00:00
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 ) ;
}
2020-01-06 22:28:09 +00:00
if ( paymentMethodConfig instanceof ExpressPaymentMethodConfig ) {
2020-04-09 15:22:34 +00:00
expressPaymentMethods [ paymentMethodConfig . name ] = paymentMethodConfig ;
2020-01-06 22:28:09 +00:00
}
} ;
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 = (
2021-09-15 16:36:02 +00:00
namespace : string ,
callbacks : Record < string , CanMakePaymentCallback >
) : void = > {
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. `
) ;
}
}
) ;
}
} ;
2021-09-15 16:36:02 +00:00
export const __experimentalDeRegisterPaymentMethod = (
paymentMethodName : string
) : void = > {
2020-09-24 14:45:40 +00:00
delete paymentMethods [ paymentMethodName ] ;
} ;
export const __experimentalDeRegisterExpressPaymentMethod = (
2021-09-15 16:36:02 +00:00
paymentMethodName : string
) : void = > {
2020-09-24 14:45:40 +00:00
delete expressPaymentMethods [ paymentMethodName ] ;
} ;
2021-09-15 16:36:02 +00:00
export const getPaymentMethods = ( ) : PaymentMethods = > {
2020-01-06 22:28:09 +00:00
return paymentMethods ;
} ;
2021-09-15 16:36:02 +00:00
export const getExpressPaymentMethods = ( ) : ExpressPaymentMethods = > {
2020-01-06 22:28:09 +00:00
return expressPaymentMethods ;
} ;