mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
🐛 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:
parent
843403cbdc
commit
966b76102c
2 changed files with 63 additions and 47 deletions
|
@ -472,18 +472,6 @@ export default class PaymentButton {
|
|||
return this.methodId === getCurrentPaymentMethod();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the current button instance has valid and complete configuration details.
|
||||
* Used during initialization to decide if the button can be initialized or should be skipped.
|
||||
*
|
||||
* Can be implemented by the derived class.
|
||||
*
|
||||
* @return {boolean} True indicates the config is valid and initialization can continue.
|
||||
*/
|
||||
get isConfigValid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flags a preview button without actual payment logic.
|
||||
*
|
||||
|
@ -640,6 +628,19 @@ export default class PaymentButton {
|
|||
this.#logger.error( ...args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the current button instance has valid and complete configuration details.
|
||||
* Used during initialization to decide if the button can be initialized or should be skipped.
|
||||
*
|
||||
* Can be implemented by the derived class.
|
||||
*
|
||||
* @param {boolean} [silent=false] - Set to true to suppress console errors.
|
||||
* @return {boolean} True indicates the config is valid and initialization can continue.
|
||||
*/
|
||||
validateConfiguration( silent = false ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
applyButtonStyles( buttonConfig, ppcpConfig = null ) {
|
||||
if ( ! ppcpConfig ) {
|
||||
ppcpConfig = this.ppcpConfig;
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue