2020-04-02 08:38:00 +03:00
|
|
|
import Renderer from './modules/Renderer';
|
|
|
|
import SingleProductConfig from './modules/SingleProductConfig';
|
|
|
|
import UpdateCart from './modules/UpdateCart';
|
|
|
|
import ErrorHandler from './modules/ErrorHandler';
|
2020-04-08 15:42:58 +03:00
|
|
|
import CartConfig from './modules/CartConfig';
|
2020-04-02 08:38:00 +03:00
|
|
|
|
2020-04-08 13:33:12 +03:00
|
|
|
const bootstrap = ()=> {
|
|
|
|
const context = PayPalCommerceGateway.context;
|
|
|
|
const errorHandler = new ErrorHandler();
|
|
|
|
const defaultConfigurator = new CartConfig(
|
|
|
|
PayPalCommerceGateway,
|
|
|
|
errorHandler
|
|
|
|
);
|
|
|
|
// Configure mini cart buttons
|
|
|
|
if (document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper)) {
|
|
|
|
|
|
|
|
const renderer = new Renderer(
|
|
|
|
PayPalCommerceGateway.button.mini_cart_wrapper
|
|
|
|
);
|
|
|
|
renderer.render(defaultConfigurator.configuration())
|
|
|
|
}
|
|
|
|
jQuery( document.body ).on( 'wc_fragments_loaded wc_fragments_refreshed', () => {
|
|
|
|
if (! document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper)) {
|
2020-04-02 08:38:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-04-08 13:33:12 +03:00
|
|
|
const renderer = new Renderer(
|
|
|
|
PayPalCommerceGateway.button.mini_cart_wrapper
|
|
|
|
);
|
|
|
|
renderer.render(defaultConfigurator.configuration())
|
|
|
|
} );
|
|
|
|
|
2020-04-08 15:42:58 +03:00
|
|
|
// Configure checkout buttons
|
|
|
|
jQuery( document.body ).on( 'updated_checkout', () => {
|
2020-04-08 17:01:57 +03:00
|
|
|
if (document.querySelector(PayPalCommerceGateway.button.cancel_wrapper)) {
|
2020-04-08 16:54:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:42:58 +03:00
|
|
|
const renderer = new Renderer(
|
2020-04-08 17:50:10 +03:00
|
|
|
PayPalCommerceGateway.button.wrapper
|
2020-04-08 15:42:58 +03:00
|
|
|
);
|
|
|
|
renderer.render(defaultConfigurator.configuration());
|
2020-04-08 15:43:31 +03:00
|
|
|
|
|
|
|
jQuery( document.body ).trigger( 'payment_method_selected' )
|
|
|
|
} );
|
|
|
|
jQuery( document.body ).on( 'payment_method_selected', () => {
|
|
|
|
// TODO: replace this dirty check, possible create a separate context config
|
|
|
|
const currentPaymentMethod = jQuery('input[name="payment_method"]:checked').val();
|
|
|
|
|
|
|
|
if (currentPaymentMethod !== 'ppcp-gateway') {
|
2020-04-08 17:50:10 +03:00
|
|
|
jQuery(PayPalCommerceGateway.button.wrapper).hide();
|
2020-04-08 15:43:31 +03:00
|
|
|
jQuery('#place_order').show();
|
|
|
|
} else {
|
2020-04-08 17:50:10 +03:00
|
|
|
jQuery(PayPalCommerceGateway.button.wrapper).show();
|
2020-04-08 15:43:31 +03:00
|
|
|
jQuery('#place_order').hide();
|
|
|
|
}
|
2020-04-08 15:42:58 +03:00
|
|
|
} );
|
|
|
|
|
2020-04-08 13:33:12 +03:00
|
|
|
// Configure context buttons
|
|
|
|
if (! document.querySelector(PayPalCommerceGateway.button.wrapper)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const renderer = new Renderer(
|
|
|
|
PayPalCommerceGateway.button.wrapper
|
|
|
|
);
|
|
|
|
let configurator = null;
|
|
|
|
if (context === 'product') {
|
|
|
|
if (! document.querySelector('form.cart')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const updateCart = new UpdateCart(
|
|
|
|
PayPalCommerceGateway.ajax.change_cart.endpoint,
|
|
|
|
PayPalCommerceGateway.ajax.change_cart.nonce
|
|
|
|
);
|
|
|
|
configurator = new SingleProductConfig(
|
2020-04-08 12:33:34 +03:00
|
|
|
PayPalCommerceGateway,
|
2020-04-08 13:33:12 +03:00
|
|
|
updateCart,
|
|
|
|
renderer.showButtons.bind(renderer),
|
|
|
|
renderer.hideButtons.bind(renderer),
|
|
|
|
document.querySelector('form.cart'),
|
2020-04-08 12:33:34 +03:00
|
|
|
errorHandler
|
|
|
|
);
|
2020-04-08 13:33:12 +03:00
|
|
|
}
|
|
|
|
if (context === 'cart') {
|
|
|
|
configurator = defaultConfigurator;
|
2020-04-08 12:33:34 +03:00
|
|
|
|
2020-04-08 13:33:12 +03:00
|
|
|
jQuery( document.body ).on( 'updated_cart_totals updated_checkout', () => {
|
|
|
|
renderer.render(configurator.configuration())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (! configurator) {
|
|
|
|
console.error('No context for button found.');
|
|
|
|
return;
|
|
|
|
}
|
2020-04-08 12:33:34 +03:00
|
|
|
|
2020-04-08 13:33:12 +03:00
|
|
|
renderer.render(configurator.configuration());
|
|
|
|
}
|
|
|
|
document.addEventListener(
|
|
|
|
'DOMContentLoaded',
|
|
|
|
() => {
|
|
|
|
|
|
|
|
if (! typeof(PayPalCommerceGateway)) {
|
|
|
|
console.error('PayPal button could not be configured.');
|
2020-04-02 08:38:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-04-08 12:33:34 +03:00
|
|
|
|
2020-04-08 13:33:12 +03:00
|
|
|
const script = document.createElement('script');
|
|
|
|
script.setAttribute('src', PayPalCommerceGateway.button.url);
|
|
|
|
script.addEventListener('load', (event) => {
|
|
|
|
bootstrap();
|
|
|
|
})
|
|
|
|
document.body.append(script);
|
|
|
|
|
|
|
|
|
2020-04-02 08:38:00 +03:00
|
|
|
}
|
|
|
|
);
|