execute js bootstrap after paypal.com script has been loaded.

This commit is contained in:
David Remer 2020-04-08 13:33:12 +03:00
parent 41f86426d8
commit 6d93f6316a
4 changed files with 74 additions and 79 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -4,13 +4,7 @@ import UpdateCart from './modules/UpdateCart';
import ErrorHandler from './modules/ErrorHandler';
import CartConfig from "./modules/CartConfig";
document.addEventListener(
'DOMContentLoaded',
() => {
if (! typeof(PayPalCommerceGateway)) {
console.error('PayPal button could not be configured.');
return;
}
const bootstrap = ()=> {
const context = PayPalCommerceGateway.context;
const errorHandler = new ErrorHandler();
const defaultConfigurator = new CartConfig(
@ -21,7 +15,6 @@ document.addEventListener(
if (document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper)) {
const renderer = new Renderer(
PayPalCommerceGateway.button.url,
PayPalCommerceGateway.button.mini_cart_wrapper
);
renderer.render(defaultConfigurator.configuration())
@ -31,7 +24,6 @@ document.addEventListener(
return;
}
const renderer = new Renderer(
PayPalCommerceGateway.button.url,
PayPalCommerceGateway.button.mini_cart_wrapper
);
renderer.render(defaultConfigurator.configuration())
@ -41,6 +33,9 @@ document.addEventListener(
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')) {
@ -61,16 +56,34 @@ document.addEventListener(
}
if (context === 'cart') {
configurator = defaultConfigurator;
jQuery( document.body ).on( 'updated_cart_totals updated_checkout', () => {
renderer.render(configurator.configuration())
});
}
if (! configurator) {
console.error('No context for button found.');
return;
}
const renderer = new Renderer(
PayPalCommerceGateway.button.url,
PayPalCommerceGateway.button.wrapper
);
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);
}
);

View file

@ -1,29 +1,11 @@
class Renderer {
constructor(url, wrapper)
constructor(wrapper)
{
this.url = url;
this.wrapper = wrapper;
}
render(buttonConfig)
{
const script = document.createElement('script');
if (typeof paypal !== 'object') {
script.setAttribute('src', this.url);
script.addEventListener('load', (event) => {
this.renderButtons(buttonConfig);
})
document.body.append(script);
return;
}
this.renderButtons(buttonConfig);
}
renderButtons(buttonConfig)
{
paypal.Buttons(