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 ,
2021-09-24 13:34:07 +00:00
CanMakePaymentExtensionCallback ,
2021-09-15 16:36:02 +00:00
PaymentMethodConfigInstance ,
PaymentMethods ,
ExpressPaymentMethods ,
} from '@woocommerce/type-defs/payments' ;
2022-07-08 05:53:24 +00:00
import { dispatch } from '@wordpress/data' ;
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
2022-10-05 12:01:56 +00:00
import { STORE_KEY as PAYMENT_METHOD_DATA_STORE_KEY } from '../../data/payment/constants' ; // Full path here because otherwise there's a circular dependency.
2021-09-15 16:36:02 +00:00
2022-07-08 05:53:24 +00:00
type LegacyRegisterPaymentMethodFunction = ( config : unknown ) = > unknown ;
type LegacyRegisterExpressPaymentMethodFunction = (
config : unknown
) = > unknown ;
2021-09-15 16:36:02 +00:00
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' ,
2022-06-15 09:56:52 +00:00
link : 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3404' ,
2020-11-18 22:06:33 +00:00
} ) ;
} 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
2022-07-08 05:53:24 +00:00
| LegacyRegisterExpressPaymentMethodFunction
2021-09-15 16:36:02 +00:00
) : 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' ,
2022-06-15 09:56:52 +00:00
link : 'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3404' ,
2020-11-18 22:06:33 +00:00
} ) ;
} 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 ,
2021-09-24 13:34:07 +00:00
callbacks : Record < string , CanMakePaymentExtensionCallback >
2021-09-15 16:36:02 +00:00
) : 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 ] ;
2022-10-05 10:04:16 +00:00
const { __internalRemoveAvailablePaymentMethod } = dispatch (
2022-07-08 05:53:24 +00:00
PAYMENT_METHOD_DATA_STORE_KEY
) ;
2022-10-05 10:04:16 +00:00
__internalRemoveAvailablePaymentMethod ( paymentMethodName ) ;
2020-09-24 14:45:40 +00:00
} ;
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 ] ;
2022-10-05 10:04:16 +00:00
const { __internalRemoveAvailableExpressPaymentMethod } = dispatch (
2022-07-08 05:53:24 +00:00
PAYMENT_METHOD_DATA_STORE_KEY
) ;
2022-10-05 10:04:16 +00:00
__internalRemoveAvailableExpressPaymentMethod ( paymentMethodName ) ;
2020-09-24 14:45:40 +00:00
} ;
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 ;
} ;