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
/
Copy pathpayment-method-config-helper.ts
106 lines (98 loc) · 2.94 KB
/
payment-method-config-helper.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
/**
* External dependencies
*/
import type {
CanMakePaymentCallback,
CanMakePaymentExtensionCallback,
} from '@woocommerce/type-defs/payments';
/**
* Internal dependencies
*/
import {
NamespacedCanMakePaymentExtensionsCallbacks,
PaymentMethodName,
ExtensionNamespace,
extensionsConfig,
} from './extensions-config';
// Filter out payment methods by supported features and cart requirement.
export const canMakePaymentWithFeaturesCheck =
(
canMakePayment: CanMakePaymentCallback,
features: string[]
): CanMakePaymentCallback =>
( canPayArgument ) => {
const requirements = canPayArgument?.paymentRequirements || [];
const featuresSupportRequirements = requirements.every(
( requirement ) => features.includes( requirement )
);
return featuresSupportRequirements && canMakePayment( canPayArgument );
};
// Filter out payment methods by callbacks registered by extensions.
export const canMakePaymentWithExtensions =
(
canMakePayment: CanMakePaymentCallback,
extensionsCallbacks: NamespacedCanMakePaymentExtensionsCallbacks,
paymentMethodName: PaymentMethodName
): CanMakePaymentCallback =>
( canPayArgument ) => {
// Validate whether the payment method is available based on its own criteria first.
let canPay = canMakePayment( canPayArgument );
if ( canPay ) {
// Gather all callbacks for paymentMethodName.
const namespacedCallbacks: Record<
ExtensionNamespace,
CanMakePaymentExtensionCallback
> = {};
Object.entries( extensionsCallbacks ).forEach(
( [ namespace, callbacks ] ) => {
if (
! ( paymentMethodName in callbacks ) ||
typeof callbacks[ paymentMethodName ] !== 'function'
) {
return;
}
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;
};
export const getCanMakePayment = (
canMakePayment: CanMakePaymentCallback,
features: string[],
paymentMethodName: string
): CanMakePaymentCallback => {
const canPay = canMakePaymentWithFeaturesCheck( canMakePayment, features );
// Loop through all callbacks to check if there are any registered for this payment method.
return (
Object.values( extensionsConfig.canMakePayment ) as Record<
PaymentMethodName,
CanMakePaymentCallback
>[]
).some( ( callbacks ) => paymentMethodName in callbacks )
? canMakePaymentWithExtensions(
canPay,
extensionsConfig.canMakePayment,
paymentMethodName
)
: canPay;
};