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

72 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-08-31 12:48:01 +02:00
import {loadCustomScript} from "@paypal/paypal-js";
import {loadPaypalScript} from "../../../ppcp-button/resources/js/modules/Helper/ScriptLoading";
import ApplepayManager from "./ApplepayManager";
import {setupButtonEvents} from '../../../ppcp-button/resources/js/modules/Helper/ButtonRefreshHelper';
2023-08-31 12:48:01 +02:00
(function ({
buttonConfig,
ppcpConfig,
jQuery
}) {
2023-10-20 18:13:09 +01:00
let manager;
2023-08-31 12:48:01 +02:00
const bootstrap = function () {
2023-10-20 18:13:09 +01:00
manager = new ApplepayManager(buttonConfig, ppcpConfig);
2023-08-31 12:48:01 +02:00
manager.init();
};
setupButtonEvents(function() {
2023-10-20 18:13:09 +01:00
if (manager) {
manager.reinit();
}
});
2023-08-31 12:48:01 +02:00
document.addEventListener(
'DOMContentLoaded',
() => {
if (
(typeof (buttonConfig) === 'undefined') ||
(typeof (ppcpConfig) === 'undefined')
) {
return;
}
2023-09-10 13:21:19 +02:00
const isMiniCart = ppcpConfig.mini_cart_buttons_enabled;
2023-09-11 14:23:05 +02:00
const isButton = jQuery('#' + buttonConfig.button.wrapper).length > 0;
2023-08-31 12:48:01 +02:00
// If button wrapper is not present then there is no need to load the scripts.
2023-09-10 13:21:19 +02:00
// minicart loads later?
if (!isMiniCart && !isButton) {
2023-08-31 12:48:01 +02:00
return;
}
let bootstrapped = false;
let paypalLoaded = false;
let applePayLoaded = false;
const tryToBoot = () => {
2023-08-31 12:48:01 +02:00
if (!bootstrapped && paypalLoaded && applePayLoaded) {
bootstrapped = true;
bootstrap();
}
}
2023-08-31 12:48:01 +02:00
// Load ApplePay SDK
loadCustomScript({ url: buttonConfig.sdk_url }).then(() => {
2023-08-31 12:48:01 +02:00
applePayLoaded = true;
tryToBoot();
});
2023-08-31 12:48:01 +02:00
// Load PayPal
loadPaypalScript(ppcpConfig, () => {
paypalLoaded = true;
tryToBoot();
2023-08-31 12:48:01 +02:00
});
},
);
})({
buttonConfig: window.wc_ppcp_applepay,
ppcpConfig: window.PayPalCommerceGateway,
jQuery: window.jQuery
});