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";
|
2024-06-26 18:16:54 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2024-06-26 18:16:54 +02:00
|
|
|
setupButtonEvents(function() {
|
2023-10-20 18:13:09 +01:00
|
|
|
if (manager) {
|
|
|
|
manager.reinit();
|
|
|
|
}
|
2024-06-26 18:16:54 +02:00
|
|
|
});
|
2023-12-18 17:42:02 +00:00
|
|
|
|
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;
|
|
|
|
|
2023-08-31 16:43:40 +02:00
|
|
|
const tryToBoot = () => {
|
2023-08-31 12:48:01 +02:00
|
|
|
if (!bootstrapped && paypalLoaded && applePayLoaded) {
|
|
|
|
bootstrapped = true;
|
|
|
|
bootstrap();
|
|
|
|
}
|
2023-08-31 16:43:40 +02:00
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
|
|
|
|
// Load ApplePay SDK
|
2023-08-31 16:43:40 +02:00
|
|
|
loadCustomScript({ url: buttonConfig.sdk_url }).then(() => {
|
2023-08-31 12:48:01 +02:00
|
|
|
applePayLoaded = true;
|
2023-08-31 16:43:40 +02:00
|
|
|
tryToBoot();
|
|
|
|
});
|
2023-08-31 12:48:01 +02:00
|
|
|
|
|
|
|
// Load PayPal
|
|
|
|
loadPaypalScript(ppcpConfig, () => {
|
|
|
|
paypalLoaded = true;
|
2023-08-31 16:43:40 +02:00
|
|
|
tryToBoot();
|
2023-08-31 12:48:01 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
})({
|
|
|
|
buttonConfig: window.wc_ppcp_applepay,
|
|
|
|
ppcpConfig: window.PayPalCommerceGateway,
|
|
|
|
jQuery: window.jQuery
|
|
|
|
});
|