mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
113 lines
No EOL
3.7 KiB
JavaScript
113 lines
No EOL
3.7 KiB
JavaScript
import Renderer from './modules/Renderer';
|
|
import SingleProductConfig from './modules/SingleProductConfig';
|
|
import UpdateCart from './modules/UpdateCart';
|
|
import ErrorHandler from './modules/ErrorHandler';
|
|
import CartConfig from './modules/CartConfig';
|
|
|
|
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)) {
|
|
return;
|
|
}
|
|
const renderer = new Renderer(
|
|
PayPalCommerceGateway.button.mini_cart_wrapper
|
|
);
|
|
renderer.render(defaultConfigurator.configuration())
|
|
} );
|
|
|
|
// 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(
|
|
PayPalCommerceGateway,
|
|
updateCart,
|
|
renderer.showButtons.bind(renderer),
|
|
renderer.hideButtons.bind(renderer),
|
|
document.querySelector('form.cart'),
|
|
errorHandler
|
|
);
|
|
}
|
|
if (context === 'cart') {
|
|
configurator = defaultConfigurator;
|
|
|
|
jQuery( document.body ).on( 'updated_cart_totals updated_checkout', () => {
|
|
renderer.render(configurator.configuration())
|
|
});
|
|
}
|
|
if (context === 'checkout' ) {
|
|
configurator = defaultConfigurator;
|
|
|
|
if (!document.querySelector(PayPalCommerceGateway.button.cancel_wrapper)) {
|
|
|
|
const toggleButtons = () => {
|
|
const currentPaymentMethod = jQuery('input[name="payment_method"]:checked').val();
|
|
|
|
if (currentPaymentMethod !== 'ppcp-gateway') {
|
|
renderer.hideButtons();
|
|
jQuery('#place_order').show();
|
|
} else {
|
|
renderer.showButtons();
|
|
jQuery('#place_order').hide();
|
|
}
|
|
}
|
|
jQuery(document.body).on('updated_checkout', () => {
|
|
renderer.render(defaultConfigurator.configuration());
|
|
jQuery(document.body).trigger('payment_method_selected')
|
|
});
|
|
jQuery(document.body).on('payment_method_selected', toggleButtons );
|
|
toggleButtons();
|
|
}
|
|
}
|
|
if (! configurator) {
|
|
console.error('No context for button found.');
|
|
return;
|
|
}
|
|
|
|
renderer.render(configurator.configuration());
|
|
}
|
|
document.addEventListener(
|
|
'DOMContentLoaded',
|
|
() => {
|
|
|
|
if (! typeof(PayPalCommerceGateway)) {
|
|
console.error('PayPal button could not be configured.');
|
|
return;
|
|
}
|
|
|
|
const script = document.createElement('script');
|
|
script.setAttribute('src', PayPalCommerceGateway.button.url);
|
|
script.addEventListener('load', (event) => {
|
|
bootstrap();
|
|
})
|
|
document.body.append(script);
|
|
|
|
|
|
}
|
|
); |