woocommerce-paypal-payments/modules/ppcp-googlepay/resources/js/boot.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-08-22 11:34:52 +01:00
import {loadCustomScript} from "@paypal/paypal-js";
2023-08-22 08:44:32 +01:00
import {loadPaypalScript} from "../../../ppcp-button/resources/js/modules/Helper/ScriptLoading";
2023-08-22 11:34:52 +01:00
import GooglepayManager from "./GooglepayManager";
2023-08-22 08:44:32 +01:00
(function ({
buttonConfig,
ppcpConfig,
jQuery
}) {
let manager;
2023-08-22 08:44:32 +01:00
const bootstrap = function () {
manager = new GooglepayManager(buttonConfig, ppcpConfig);
2023-08-22 11:34:52 +01:00
manager.init();
2023-08-22 08:44:32 +01:00
};
jQuery(document.body).on('updated_cart_totals updated_checkout', () => {
if (manager) {
manager.reinit();
}
});
2023-08-22 08:44:32 +01:00
document.addEventListener(
'DOMContentLoaded',
() => {
if (
(typeof (buttonConfig) === 'undefined') ||
(typeof (ppcpConfig) === 'undefined')
) {
// No PayPal buttons present on this page.
2023-08-24 18:02:59 +01:00
return;
}
2023-08-22 08:44:32 +01:00
let bootstrapped = false;
2023-08-22 11:34:52 +01:00
let paypalLoaded = false;
let googlePayLoaded = false;
const tryToBoot = () => {
if (!bootstrapped && paypalLoaded && googlePayLoaded) {
bootstrapped = true;
bootstrap();
}
}
// Load GooglePay SDK
loadCustomScript({ url: buttonConfig.sdk_url }).then(() => {
googlePayLoaded = true;
tryToBoot();
});
2023-08-22 08:44:32 +01:00
2023-08-22 11:34:52 +01:00
// Load PayPal
2023-08-22 08:44:32 +01:00
loadPaypalScript(ppcpConfig, () => {
2023-08-22 11:34:52 +01:00
paypalLoaded = true;
tryToBoot();
2023-08-22 08:44:32 +01:00
});
},
);
})({
buttonConfig: window.wc_ppcp_googlepay,
ppcpConfig: window.PayPalCommerceGateway,
jQuery: window.jQuery
});