From 77290077743bea050727e562e5350735d68ea99c Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Thu, 8 Aug 2024 14:05:57 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Unify=20constructor=20argu?= =?UTF-8?q?ments=20of=20PaymentButton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Factory method and constuctor have same arguments in the same order --- .../js/modules/Renderer/PaymentButton.js | 27 ++++++++++++++++--- .../resources/js/GooglepayButton.js | 10 +++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js b/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js index 258807b6c..9248f3be9 100644 --- a/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js +++ b/modules/ppcp-button/resources/js/modules/Renderer/PaymentButton.js @@ -139,6 +139,11 @@ export default class PaymentButton { */ #ppcpConfig; + /** + * A variation of a context bootstrap handler. + */ + #externalHandler; + /** * A variation of a context handler object, like CheckoutHandler. * This handler provides a standardized interface for certain standardized checks and actions. @@ -190,9 +195,9 @@ export default class PaymentButton { if ( ! buttonInstances.has( instanceKey ) ) { const button = new this( context, + externalHandler, buttonConfig, ppcpConfig, - externalHandler, contextHandler ); @@ -238,11 +243,18 @@ export default class PaymentButton { * * @private * @param {string} context - Button context name. + * @param {Object} externalHandler - Handler object. * @param {Object} buttonConfig - Payment button specific configuration. * @param {Object} ppcpConfig - Plugin wide configuration object. * @param {Object} contextHandler - Handler object. */ - constructor( context, buttonConfig, ppcpConfig, contextHandler ) { + constructor( + context, + externalHandler = null, + buttonConfig = {}, + ppcpConfig = {}, + contextHandler = null + ) { if ( this.methodId === PaymentButton.methodId ) { throw new Error( 'Cannot initialize the PaymentButton base class' ); } @@ -257,6 +269,7 @@ export default class PaymentButton { this.#context = context; this.#buttonConfig = buttonConfig; this.#ppcpConfig = ppcpConfig; + this.#externalHandler = externalHandler; this.#contextHandler = contextHandler; this.#wrappers = this.constructor.getWrappers( @@ -339,6 +352,13 @@ export default class PaymentButton { return this.#ppcpConfig; } + /** + * @return {Object} The bootstrap handler instance, or an empty object. + */ + get externalHandler() { + return this.#externalHandler || {}; + } + /** * Access the button's context handler. * When no context handler was provided (like for a preview button), an empty object is returned. @@ -708,13 +728,14 @@ export default class PaymentButton { return; } - this.log( 'addButton', button ); const wrapper = this.wrapperElement; if ( this.#button ) { this.removeButton(); } + this.log( 'addButton', button ); + this.#button = button; wrapper.appendChild( this.#button ); } diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index d4569ee43..3f56cd9dc 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -93,12 +93,18 @@ class GooglepayButton extends PaymentButton { constructor( context, + externalHandler, buttonConfig, ppcpConfig, - externalHandler, contextHandler ) { - super( context, buttonConfig, ppcpConfig, contextHandler ); + super( + context, + externalHandler, + buttonConfig, + ppcpConfig, + contextHandler + ); this.onPaymentAuthorized = this.onPaymentAuthorized.bind( this ); this.onPaymentDataChanged = this.onPaymentDataChanged.bind( this );