🐛 Fix init errors “No API configuration...”

Caused by calling `reinit()` before the invoking `init()` first. New condition in `reinit()` ensures that the button configuration is present.
This commit is contained in:
Philipp Stracker 2024-08-12 18:50:17 +02:00
parent 843403cbdc
commit 966b76102c
No known key found for this signature in database
2 changed files with 63 additions and 47 deletions

View file

@ -136,40 +136,6 @@ class GooglepayButton extends PaymentButton {
this.log( 'Create instance' );
}
/**
* @inheritDoc
*/
get isConfigValid() {
const validEnvs = [ 'PRODUCTION', 'TEST' ];
if ( ! validEnvs.includes( this.buttonConfig.environment ) ) {
this.error( 'Invalid environment.', this.buttonConfig.environment );
return false;
}
// Preview buttons only need a valid environment.
if ( this.isPreview ) {
return true;
}
if ( ! this.googlePayConfig ) {
this.error( 'No API configuration - missing configure() call?' );
return false;
}
if ( ! this.transactionInfo ) {
this.error( 'No transactionInfo - missing configure() call?' );
return false;
}
if ( ! typeof this.contextHandler?.validateContext() ) {
this.error( 'Invalid context handler.', this.contextHandler );
return false;
}
return true;
}
/**
* @inheritDoc
*/
@ -219,6 +185,50 @@ class GooglepayButton extends PaymentButton {
this.refresh();
}
/**
* @inheritDoc
*/
validateConfiguration( silent = false ) {
const validEnvs = [ 'PRODUCTION', 'TEST' ];
const isInvalid = ( ...args ) => {
if ( ! silent ) {
this.error( ...args );
}
return false;
};
if ( ! validEnvs.includes( this.buttonConfig.environment ) ) {
return isInvalid(
'Invalid environment:',
this.buttonConfig.environment
);
}
// Preview buttons only need a valid environment.
if ( this.isPreview ) {
return true;
}
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;
}
/**
* Configures the button instance. Must be called before the initial `init()`.
*
@ -240,7 +250,7 @@ class GooglepayButton extends PaymentButton {
}
// Stop, if configuration is invalid.
if ( ! this.isConfigValid ) {
if ( ! this.validateConfiguration() ) {
return;
}
@ -275,6 +285,11 @@ class GooglepayButton extends PaymentButton {
}
reinit() {
// Missing (invalid) configuration indicates, that the first `init()` call did not happen yet.
if ( ! this.validateConfiguration( true ) ) {
return;
}
super.reinit();
this.init();