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

64 lines
1.9 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";
(function ({
buttonConfig,
ppcpConfig,
jQuery
}) {
const bootstrap = function () {
const manager = new ApplepayManager(buttonConfig, ppcpConfig);
manager.init();
};
document.addEventListener(
'DOMContentLoaded',
() => {
if (
(typeof (buttonConfig) === 'undefined') ||
(typeof (ppcpConfig) === 'undefined')
) {
console.error('PayPal button could not be configured.');
return;
}
2023-09-10 13:21:19 +02:00
const isMiniCart = ppcpConfig.mini_cart_buttons_enabled;
const isButton = jQuery(buttonConfig.button.cart_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
});