Streamline button configuraton validation logic

This commit is contained in:
Philipp Stracker 2024-10-08 18:47:32 +02:00
parent 560ec32e26
commit f41fa4f951
No known key found for this signature in database
3 changed files with 113 additions and 59 deletions

View file

@ -215,45 +215,31 @@ class GooglepayButton extends PaymentButton {
/**
* @inheritDoc
*/
validateConfiguration( silent = false ) {
const validEnvs = [ 'PRODUCTION', 'TEST' ];
registerValidationRules( invalidIf, validIf ) {
invalidIf(
() =>
[ 'TEST', 'PRODUCTION' ].includes(
this.buttonConfig.environment
),
`Invalid environment: ${ this.buttonConfig.environment }`
);
const isInvalid = ( ...args ) => {
if ( ! silent ) {
this.error( ...args );
}
return false;
};
validIf( () => this.isPreview );
if ( ! validEnvs.includes( this.buttonConfig.environment ) ) {
return isInvalid(
'Invalid environment:',
this.buttonConfig.environment
);
}
invalidIf(
() => ! this.googlePayConfig,
'No API configuration - missing configure() call?'
);
// Preview buttons only need a valid environment.
if ( this.isPreview ) {
return true;
}
invalidIf(
() => ! this.transactionInfo,
'No transactionInfo - missing configure() call?'
);
if ( ! this.googlePayConfig ) {
return isInvalid(
'No API configuration - missing configure() call?'
);
}
if ( ! this.transactionInfo ) {
return isInvalid(
'No transactionInfo - missing configure() call?'
);
}
if ( ! typeof this.contextHandler?.validateContext() ) {
return isInvalid( 'Invalid context handler.', this.contextHandler );
}
return true;
invalidIf(
() => ! this.contextHandler?.validateContext(),
`Invalid context handler.`
);
}
/**