woocommerce-paypal-payments/modules.local/ppcp-button/resources/js/button.js

72 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-04-30 15:28:48 +03:00
import MiniCartBootstap from './modules/ContextBootstrap/MiniCartBootstap';
import SingleProductBootstap from './modules/ContextBootstrap/SingleProductBootstap';
import CartBootstrap from './modules/ContextBootstrap/CartBootstap';
import CheckoutBootstap from './modules/ContextBootstrap/CheckoutBootstap';
import Renderer from './modules/Renderer/Renderer';
2020-07-22 14:12:49 +03:00
import ErrorHandler from './modules/ErrorHandler';
2020-04-30 15:28:48 +03:00
import CreditCardRenderer from "./modules/Renderer/CreditCardRenderer";
2020-04-02 08:38:00 +03:00
const bootstrap = () => {
2020-07-28 08:05:18 +03:00
const errorHandler = new ErrorHandler(PayPalCommerceGateway.labels.error.generic);
2020-07-22 14:12:49 +03:00
const creditCardRenderer = new CreditCardRenderer(PayPalCommerceGateway, errorHandler);
2020-04-30 15:28:48 +03:00
const renderer = new Renderer(creditCardRenderer, PayPalCommerceGateway);
const context = PayPalCommerceGateway.context;
if (context === 'mini-cart' || context === 'product') {
const miniCartBootstrap = new MiniCartBootstap(
PayPalCommerceGateway,
renderer
);
miniCartBootstrap.init();
}
if (context === 'product') {
const singleProductBootstrap = new SingleProductBootstap(
PayPalCommerceGateway,
renderer,
);
singleProductBootstrap.init();
}
if (context === 'cart') {
const cartBootstrap = new CartBootstrap(
PayPalCommerceGateway,
renderer,
);
2020-04-08 12:33:34 +03:00
cartBootstrap.init();
}
2020-04-08 12:33:34 +03:00
if (context === 'checkout') {
const checkoutBootstap = new CheckoutBootstap(
PayPalCommerceGateway,
renderer
);
checkoutBootstap.init();
}
};
document.addEventListener(
'DOMContentLoaded',
() => {
if (!typeof (PayPalCommerceGateway)) {
console.error('PayPal button could not be configured.');
2020-04-02 08:38:00 +03:00
return;
}
const script = document.createElement('script');
script.setAttribute('src', PayPalCommerceGateway.button.url);
2020-04-30 15:28:48 +03:00
Object.entries(PayPalCommerceGateway.script_attributes).forEach(
(keyValue) => {
script.setAttribute(keyValue[0], keyValue[1]);
}
);
script.addEventListener('load', (event) => {
bootstrap();
});
document.body.append(script);
},
2020-04-02 08:38:00 +03:00
);