This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
registry.ts
128 lines (117 loc) · 4.32 KB
/
registry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* 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';
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 ) {
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 ) {
expressPaymentMethods[ paymentMethodConfig.name ] = paymentMethodConfig;
}
};
/**
* 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 => {
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;
};