2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-04-29 10:57:58 +00:00
|
|
|
import {
|
|
|
|
assertConfigHasProperties,
|
|
|
|
assertValidElement,
|
|
|
|
assertValidElementOrString,
|
|
|
|
} from './assertions';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
|
|
|
export default class PaymentMethodConfig {
|
|
|
|
constructor( config ) {
|
|
|
|
// validate config
|
|
|
|
PaymentMethodConfig.assertValidConfig( config );
|
2020-04-09 15:22:34 +00:00
|
|
|
this.name = config.name;
|
2020-01-06 22:28:09 +00:00
|
|
|
this.label = config.label;
|
2020-04-30 09:52:36 +00:00
|
|
|
this.placeOrderButtonLabel = config.placeOrderButtonLabel;
|
2020-01-06 22:28:09 +00:00
|
|
|
this.ariaLabel = config.ariaLabel;
|
2020-03-30 12:07:49 +00:00
|
|
|
this.content = config.content;
|
2020-04-29 10:57:58 +00:00
|
|
|
this.icons = config.icons;
|
2020-03-10 16:35:30 +00:00
|
|
|
this.edit = config.edit;
|
2020-01-06 22:28:09 +00:00
|
|
|
this.canMakePayment = config.canMakePayment;
|
2020-04-09 15:22:34 +00:00
|
|
|
this.paymentMethodId = config.paymentMethodId || this.name;
|
2020-05-12 15:12:28 +00:00
|
|
|
this.supports = {
|
|
|
|
savePaymentInfo: config?.supports?.savePaymentInfo || false,
|
|
|
|
};
|
2020-01-06 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static assertValidConfig = ( config ) => {
|
|
|
|
assertConfigHasProperties( config, [
|
2020-04-09 15:22:34 +00:00
|
|
|
'name',
|
2020-01-06 22:28:09 +00:00
|
|
|
'label',
|
|
|
|
'ariaLabel',
|
2020-03-30 12:07:49 +00:00
|
|
|
'content',
|
2020-03-10 16:35:30 +00:00
|
|
|
'edit',
|
2020-01-06 22:28:09 +00:00
|
|
|
'canMakePayment',
|
|
|
|
] );
|
2020-04-09 15:22:34 +00:00
|
|
|
if ( typeof config.name !== 'string' ) {
|
|
|
|
throw new Error(
|
|
|
|
'The name property for the payment method must be a string'
|
|
|
|
);
|
|
|
|
}
|
2020-04-29 10:57:58 +00:00
|
|
|
if (
|
|
|
|
typeof config.icons !== 'undefined' &&
|
|
|
|
! Array.isArray( config.icons ) &&
|
|
|
|
config.icons !== null
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'The icons property for the payment method must be an array or null.'
|
|
|
|
);
|
|
|
|
}
|
2020-04-09 15:22:34 +00:00
|
|
|
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
|
|
|
}
|
2020-04-30 09:52:36 +00:00
|
|
|
if (
|
|
|
|
typeof config.placeOrderButtonLabel !== 'string' &&
|
|
|
|
typeof config.placeOrderButtonLabel !== 'undefined'
|
|
|
|
) {
|
|
|
|
throw new TypeError(
|
|
|
|
'The placeOrderButtonLabel property for the payment method must be a string'
|
|
|
|
);
|
|
|
|
}
|
2020-04-29 10:57:58 +00:00
|
|
|
assertValidElementOrString( config.label, 'label' );
|
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-01-06 22:28:09 +00:00
|
|
|
if ( typeof config.ariaLabel !== 'string' ) {
|
|
|
|
throw new TypeError(
|
2020-04-09 15:22:34 +00:00
|
|
|
'The ariaLabel property for the payment method must be a string'
|
2020-01-06 22:28:09 +00:00
|
|
|
);
|
|
|
|
}
|
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 payment method must be a function.'
|
2020-01-06 22:28:09 +00:00
|
|
|
);
|
|
|
|
}
|
2020-05-12 15:12:28 +00:00
|
|
|
if (
|
|
|
|
config.supports &&
|
|
|
|
typeof config.supports.savePaymentInfo !== 'undefined' &&
|
|
|
|
typeof config.supports.savePaymentInfo !== 'boolean'
|
|
|
|
) {
|
|
|
|
throw new TypeError(
|
|
|
|
'If the payment method includes the `supports.savePaymentInfo` property, it must be a boolean'
|
|
|
|
);
|
|
|
|
}
|
2020-01-06 22:28:09 +00:00
|
|
|
};
|
|
|
|
}
|