2021-09-15 16:36:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import type {
|
|
|
|
ExpressPaymentMethodConfiguration,
|
|
|
|
Supports,
|
|
|
|
CanMakePaymentCallback,
|
|
|
|
ExpressPaymentMethodConfigInstance,
|
2022-12-23 11:59:02 +00:00
|
|
|
} from '@woocommerce/types';
|
2021-09-15 16:36:02 +00:00
|
|
|
|
2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-09-23 09:09:55 +00:00
|
|
|
import { getCanMakePayment } from './payment-method-config-helper';
|
2021-09-15 16:36:02 +00:00
|
|
|
import { assertConfigHasProperties, assertValidElement } from './assertions';
|
|
|
|
|
|
|
|
export default class ExpressPaymentMethodConfig
|
2022-06-15 09:56:52 +00:00
|
|
|
implements ExpressPaymentMethodConfigInstance
|
|
|
|
{
|
2021-09-15 16:36:02 +00:00
|
|
|
public name: string;
|
|
|
|
public content: ReactNode;
|
|
|
|
public edit: ReactNode;
|
|
|
|
public paymentMethodId?: string;
|
|
|
|
public supports: Supports;
|
2021-09-23 09:09:55 +00:00
|
|
|
public canMakePaymentFromConfig: CanMakePaymentCallback;
|
2020-01-06 22:28:09 +00:00
|
|
|
|
2021-09-15 16:36:02 +00:00
|
|
|
constructor( config: ExpressPaymentMethodConfiguration ) {
|
2020-01-06 22:28:09 +00:00
|
|
|
// validate config
|
|
|
|
ExpressPaymentMethodConfig.assertValidConfig( config );
|
2020-04-09 15:22:34 +00:00
|
|
|
this.name = config.name;
|
2020-03-30 12:07:49 +00:00
|
|
|
this.content = config.content;
|
2020-03-10 16:35:30 +00:00
|
|
|
this.edit = config.edit;
|
2020-04-09 15:22:34 +00:00
|
|
|
this.paymentMethodId = config.paymentMethodId || this.name;
|
2021-01-29 06:28:44 +00:00
|
|
|
this.supports = {
|
2021-02-05 15:31:34 +00:00
|
|
|
features: config?.supports?.features || [ 'products' ],
|
2021-01-29 06:28:44 +00:00
|
|
|
};
|
2021-09-23 09:09:55 +00:00
|
|
|
this.canMakePaymentFromConfig = config.canMakePayment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// canMakePayment is calculated each time based on data that modifies outside of the class (eg: cart data).
|
|
|
|
get canMakePayment(): CanMakePaymentCallback {
|
|
|
|
return getCanMakePayment(
|
|
|
|
this.canMakePaymentFromConfig,
|
|
|
|
this.supports.features,
|
|
|
|
this.name
|
2021-01-29 06:28:44 +00:00
|
|
|
);
|
2020-01-06 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 16:36:02 +00:00
|
|
|
static assertValidConfig = (
|
|
|
|
config: ExpressPaymentMethodConfiguration
|
|
|
|
): void => {
|
2020-04-09 15:22:34 +00:00
|
|
|
assertConfigHasProperties( config, [ 'name', 'content', 'edit' ] );
|
|
|
|
if ( typeof config.name !== 'string' ) {
|
2020-01-06 22:28:09 +00:00
|
|
|
throw new TypeError(
|
2020-04-09 15:22:34 +00:00
|
|
|
'The name property for the express payment method must be a string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
typeof config.paymentMethodId !== 'string' &&
|
|
|
|
typeof config.paymentMethodId !== 'undefined'
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'The paymentMethodId property for the payment method must be a string or undefined (in which case it will be the value of the name property).'
|
2020-01-06 22:28:09 +00:00
|
|
|
);
|
|
|
|
}
|
2021-02-05 15:31:34 +00:00
|
|
|
if (
|
|
|
|
typeof config.supports?.features !== 'undefined' &&
|
|
|
|
! Array.isArray( config.supports?.features )
|
|
|
|
) {
|
2021-01-29 06:28:44 +00:00
|
|
|
throw new Error(
|
2021-02-05 15:31:34 +00:00
|
|
|
'The features property for the payment method must be an array or undefined.'
|
2021-01-29 06:28:44 +00:00
|
|
|
);
|
|
|
|
}
|
2020-03-30 12:07:49 +00:00
|
|
|
assertValidElement( config.content, 'content' );
|
2020-03-10 16:35:30 +00:00
|
|
|
assertValidElement( config.edit, 'edit' );
|
2020-04-09 11:44:29 +00:00
|
|
|
if ( typeof config.canMakePayment !== 'function' ) {
|
2020-01-06 22:28:09 +00:00
|
|
|
throw new TypeError(
|
2020-04-09 11:44:29 +00:00
|
|
|
'The canMakePayment property for the express payment method must be a function.'
|
2020-01-06 22:28:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|