2021-01-24 13:59:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import deprecated from '@wordpress/deprecated';
|
|
|
|
|
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
|
|
|
|
2021-01-29 06:28:44 +00:00
|
|
|
import { canMakePaymentWithFeaturesCheck } from './payment-method-config-helper';
|
|
|
|
|
2021-03-15 08:50:49 +00:00
|
|
|
const NullComponent = () => {
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
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;
|
2021-03-15 08:50:49 +00:00
|
|
|
this.savedTokenComponent = config.savedTokenComponent;
|
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-04-09 15:22:34 +00:00
|
|
|
this.paymentMethodId = config.paymentMethodId || this.name;
|
2020-05-12 15:12:28 +00:00
|
|
|
this.supports = {
|
2021-01-24 13:59:13 +00:00
|
|
|
showSavedCards:
|
|
|
|
config?.supports?.showSavedCards ||
|
|
|
|
config?.supports?.savePaymentInfo || // Kept for backward compatibility if methods still pass this when registering.
|
|
|
|
false,
|
|
|
|
showSaveOption: config?.supports?.showSaveOption || false,
|
2021-02-05 15:31:34 +00:00
|
|
|
features: config?.supports?.features || [ 'products' ],
|
2020-05-12 15:12:28 +00:00
|
|
|
};
|
2021-01-29 06:28:44 +00:00
|
|
|
this.canMakePayment = canMakePaymentWithFeaturesCheck(
|
|
|
|
config.canMakePayment,
|
|
|
|
this.supports.features
|
|
|
|
);
|
2020-01-06 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static assertValidConfig = ( config ) => {
|
2021-03-15 08:50:49 +00:00
|
|
|
// set default for optional
|
|
|
|
config.savedTokenComponent = config.savedTokenComponent || (
|
|
|
|
<NullComponent />
|
|
|
|
);
|
2020-01-06 22:28:09 +00:00
|
|
|
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' );
|
2021-03-15 08:50:49 +00:00
|
|
|
assertValidElement( config.savedTokenComponent, 'savedTokenComponent' );
|
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 (
|
2021-01-24 13:59:13 +00:00
|
|
|
typeof config.supports?.showSavedCards !== 'undefined' &&
|
|
|
|
typeof config.supports?.showSavedCards !== 'boolean'
|
|
|
|
) {
|
|
|
|
throw new TypeError(
|
|
|
|
'If the payment method includes the `supports.showSavedCards` property, it must be a boolean'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( typeof config.supports?.savePaymentInfo !== 'undefined' ) {
|
|
|
|
deprecated(
|
|
|
|
'Passing savePaymentInfo when registering a payment method.',
|
|
|
|
{
|
|
|
|
alternative: 'Pass showSavedCards and showSaveOption',
|
|
|
|
plugin: 'woocommerce-gutenberg-products-block',
|
|
|
|
link:
|
|
|
|
'https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3686',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
);
|
|
|
|
}
|
2021-01-24 13:59:13 +00:00
|
|
|
if (
|
|
|
|
typeof config.supports?.showSaveOption !== 'undefined' &&
|
|
|
|
typeof config.supports?.showSaveOption !== 'boolean'
|
2020-05-12 15:12:28 +00:00
|
|
|
) {
|
|
|
|
throw new TypeError(
|
2021-01-24 13:59:13 +00:00
|
|
|
'If the payment method includes the `supports.showSaveOption` property, it must be a boolean'
|
2020-05-12 15:12:28 +00:00
|
|
|
);
|
|
|
|
}
|
2020-01-06 22:28:09 +00:00
|
|
|
};
|
|
|
|
}
|