mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-03 08:37:53 +08:00
🐛 Prevent duplicate payment button instances
This commit is contained in:
parent
f52b01b275
commit
8afa7e34dc
1 changed files with 33 additions and 4 deletions
|
@ -5,6 +5,19 @@ import UpdatePaymentData from './Helper/UpdatePaymentData';
|
|||
import { apmButtonsInit } from '../../../ppcp-button/resources/js/modules/Helper/ApmButtons';
|
||||
|
||||
class GooglepayButton {
|
||||
/**
|
||||
* Reference to the payment button created by this instance.
|
||||
*
|
||||
* @type {HTMLElement}
|
||||
*/
|
||||
#button;
|
||||
|
||||
/**
|
||||
* Client reference, provided by the Google Pay JS SDK.
|
||||
* @see https://developers.google.com/pay/api/web/reference/client
|
||||
*/
|
||||
paymentsClient = null;
|
||||
|
||||
constructor(
|
||||
context,
|
||||
externalHandler,
|
||||
|
@ -22,8 +35,6 @@ class GooglepayButton {
|
|||
this.ppcpConfig = ppcpConfig;
|
||||
this.contextHandler = contextHandler;
|
||||
|
||||
this.paymentsClient = null;
|
||||
|
||||
this.log = function () {
|
||||
if ( this.buttonConfig.is_debug ) {
|
||||
//console.log('[GooglePayButton]', ...arguments);
|
||||
|
@ -235,13 +246,19 @@ class GooglepayButton {
|
|||
const { wrapper, ppcpStyle, buttonStyle } = this.contextConfig();
|
||||
|
||||
this.waitForWrapper( wrapper, () => {
|
||||
// Prevent duplicate payment buttons.
|
||||
this.removeButton();
|
||||
|
||||
jQuery( wrapper ).addClass( 'ppcp-button-' + ppcpStyle.shape );
|
||||
|
||||
if ( ppcpStyle.height ) {
|
||||
jQuery( wrapper ).css( 'height', `${ ppcpStyle.height }px` );
|
||||
}
|
||||
|
||||
const button = this.paymentsClient.createButton( {
|
||||
/**
|
||||
* @see https://developers.google.com/pay/api/web/reference/client#createButton
|
||||
*/
|
||||
this.#button = this.paymentsClient.createButton( {
|
||||
onClick: this.onButtonClick.bind( this ),
|
||||
allowedPaymentMethods: [ baseCardPaymentMethod ],
|
||||
buttonColor: buttonStyle.color || 'black',
|
||||
|
@ -250,10 +267,22 @@ class GooglepayButton {
|
|||
buttonSizeMode: 'fill',
|
||||
} );
|
||||
|
||||
jQuery( wrapper ).append( button );
|
||||
jQuery( wrapper ).append( this.#button );
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the payment button that was injected via addButton()
|
||||
*/
|
||||
removeButton() {
|
||||
if ( ! this.#button ) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.#button.remove();
|
||||
this.#button = null;
|
||||
}
|
||||
|
||||
addButtonCheckout( baseCardPaymentMethod, wrapper, buttonStyle ) {
|
||||
const button = this.paymentsClient.createButton( {
|
||||
onClick: this.onButtonClick.bind( this ),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue