diff --git a/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js b/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js index 46cdb1981..b54997f87 100644 --- a/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js +++ b/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js @@ -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; diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index 20d7cc739..15594bcef 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -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();