2021-09-15 16:36:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-09-24 13:34:07 +00:00
|
|
|
import type {
|
|
|
|
CanMakePaymentCallback,
|
|
|
|
CanMakePaymentExtensionCallback,
|
|
|
|
} from '@woocommerce/type-defs/payments';
|
2021-09-15 16:36:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-09-23 09:09:55 +00:00
|
|
|
import {
|
2021-09-15 16:36:02 +00:00
|
|
|
NamespacedCanMakePaymentExtensionsCallbacks,
|
|
|
|
PaymentMethodName,
|
|
|
|
ExtensionNamespace,
|
2021-09-23 09:09:55 +00:00
|
|
|
extensionsConfig,
|
2021-09-15 16:36:02 +00:00
|
|
|
} from './extensions-config';
|
|
|
|
|
2021-01-29 06:28:44 +00:00
|
|
|
// Filter out payment methods by supported features and cart requirement.
|
2022-06-15 09:56:52 +00:00
|
|
|
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 );
|
|
|
|
};
|
2021-09-08 11:29:29 +00:00
|
|
|
|
|
|
|
// Filter out payment methods by callbacks registered by extensions.
|
2022-06-15 09:56:52 +00:00
|
|
|
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 );
|
2021-09-08 11:29:29 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
if ( canPay ) {
|
|
|
|
// Gather all callbacks for paymentMethodName.
|
|
|
|
const namespacedCallbacks: Record<
|
|
|
|
ExtensionNamespace,
|
|
|
|
CanMakePaymentExtensionCallback
|
|
|
|
> = {};
|
2021-09-15 16:36:02 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
Object.entries( extensionsCallbacks ).forEach(
|
|
|
|
( [ namespace, callbacks ] ) => {
|
2022-10-12 14:29:04 +00:00
|
|
|
if (
|
|
|
|
! ( paymentMethodName in callbacks ) ||
|
|
|
|
typeof callbacks[ paymentMethodName ] !== 'function'
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-15 09:56:52 +00:00
|
|
|
namespacedCallbacks[ namespace ] =
|
|
|
|
callbacks[ paymentMethodName ];
|
|
|
|
}
|
|
|
|
);
|
2021-09-15 16:36:02 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-09-08 11:29:29 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
return canPay;
|
|
|
|
};
|
2021-09-23 09:09:55 +00:00
|
|
|
|
|
|
|
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.
|
2022-06-15 09:56:52 +00:00
|
|
|
return (
|
|
|
|
Object.values( extensionsConfig.canMakePayment ) as Record<
|
|
|
|
PaymentMethodName,
|
|
|
|
CanMakePaymentCallback
|
|
|
|
>[]
|
|
|
|
).some( ( callbacks ) => paymentMethodName in callbacks )
|
2021-09-23 09:09:55 +00:00
|
|
|
? canMakePaymentWithExtensions(
|
|
|
|
canPay,
|
|
|
|
extensionsConfig.canMakePayment,
|
|
|
|
paymentMethodName
|
|
|
|
)
|
|
|
|
: canPay;
|
|
|
|
};
|