Fix GooglePay mini-cart button loading

Fix GooglePay shipping callback
Fix GooglePay button on cart updates
This commit is contained in:
Pedro Silva 2023-10-17 10:47:26 +01:00
parent 9ab11b40e4
commit 84e4c2c40a
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
4 changed files with 59 additions and 21 deletions

View file

@ -60,6 +60,15 @@ class GooglepayButton {
});
}
reinit() {
if (!this.googlePayConfig) {
return;
}
this.isInitialized = false;
this.init(this.googlePayConfig);
}
validateConfig() {
if ( ['PRODUCTION', 'TEST'].indexOf(this.buttonConfig.environment) === -1) {
console.error('[GooglePayButton] Invalid environment.', this.buttonConfig.environment);
@ -152,19 +161,39 @@ class GooglepayButton {
const { wrapper, ppcpStyle, buttonStyle } = this.contextConfig();
jQuery(wrapper).addClass('ppcp-button-' + ppcpStyle.shape);
jQuery(wrapper).length
const button =
this.paymentsClient.createButton({
onClick: this.onButtonClick.bind(this),
allowedPaymentMethods: [baseCardPaymentMethod],
buttonColor: buttonStyle.color || 'black',
buttonType: buttonStyle.type || 'pay',
buttonLocale: buttonStyle.language || 'en',
buttonSizeMode: 'fill',
});
this.waitForWrapper(wrapper, () => {
jQuery(wrapper).addClass('ppcp-button-' + ppcpStyle.shape);
jQuery(wrapper).append(button);
const button =
this.paymentsClient.createButton({
onClick: this.onButtonClick.bind(this),
allowedPaymentMethods: [baseCardPaymentMethod],
buttonColor: buttonStyle.color || 'black',
buttonType: buttonStyle.type || 'pay',
buttonLocale: buttonStyle.language || 'en',
buttonSizeMode: 'fill',
});
jQuery(wrapper).append(button);
});
}
waitForWrapper(selector, callback, delay = 100, timeout = 2000) {
const startTime = Date.now();
const interval = setInterval(function() {
const el = document.querySelector(selector);
const timeElapsed = Date.now() - startTime;
if (el) {
clearInterval(interval);
callback(el);
} else if (timeElapsed > timeout) {
clearInterval(interval);
console.error('Waiting for wrapper timed out.', selector);
}
}, delay);
}
//------------------------

View file

@ -38,6 +38,12 @@ class GooglepayManager {
})();
}
reinit() {
for (const button of this.buttons) {
button.reinit();
}
}
}
export default GooglepayManager;

View file

@ -8,11 +8,17 @@ import GooglepayManager from "./GooglepayManager";
jQuery
}) {
let manager;
const bootstrap = function () {
const manager = new GooglepayManager(buttonConfig, ppcpConfig);
manager = new GooglepayManager(buttonConfig, ppcpConfig);
manager.init();
};
jQuery(document.body).on('updated_cart_totals updated_checkout', () => {
manager.reinit();
});
document.addEventListener(
'DOMContentLoaded',
() => {
@ -20,12 +26,7 @@ import GooglepayManager from "./GooglepayManager";
(typeof (buttonConfig) === 'undefined') ||
(typeof (ppcpConfig) === 'undefined')
) {
console.error('PayPal button could not be configured.');
return;
}
// If button wrapper is not present then there is no need to load the scripts.
if (!jQuery(buttonConfig.button.wrapper).length) {
// No PayPal buttons present on this page.
return;
}

View file

@ -168,7 +168,7 @@ class UpdatePaymentDataEndpoint {
* @return void
*/
private function update_addresses( array $payment_data ): void {
if ( ( $payment_data['callbackTrigger'] ?? '' ) !== 'SHIPPING_ADDRESS' ) {
if ( ! in_array( $payment_data['callbackTrigger'] ?? '', array( 'SHIPPING_ADDRESS', 'INITIALIZE' ), true ) ) {
return;
}
@ -185,11 +185,13 @@ class UpdatePaymentDataEndpoint {
$customer->set_billing_postcode( $payment_data['shippingAddress']['postalCode'] ?? '' );
$customer->set_billing_country( $payment_data['shippingAddress']['countryCode'] ?? '' );
$customer->set_billing_state( $payment_data['shippingAddress']['locality'] ?? '' );
$customer->set_billing_state( '' );
$customer->set_billing_city( $payment_data['shippingAddress']['locality'] ?? '' );
$customer->set_shipping_postcode( $payment_data['shippingAddress']['postalCode'] ?? '' );
$customer->set_shipping_country( $payment_data['shippingAddress']['countryCode'] ?? '' );
$customer->set_shipping_state( $payment_data['shippingAddress']['locality'] ?? '' );
$customer->set_shipping_state( '' );
$customer->set_shipping_city( $payment_data['shippingAddress']['locality'] ?? '' );
// Save the data.
$customer->save();