woocommerce-paypal-payments/modules/ppcp-button/resources/js/modules/ContextBootstrap/CartBootstap.js

102 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-04-30 15:28:48 +03:00
import CartActionHandler from '../ActionHandler/CartActionHandler';
import BootstrapHelper from "../Helper/BootstrapHelper";
2023-02-15 16:50:34 +02:00
import {setVisible} from "../Helper/Hiding";
class CartBootstrap {
constructor(gateway, renderer, messages, errorHandler) {
this.gateway = gateway;
2020-04-09 12:23:44 +03:00
this.renderer = renderer;
this.messages = messages;
this.errorHandler = errorHandler;
this.lastAmount = this.gateway.messages.amount;
this.renderer.onButtonsInit(this.gateway.button.wrapper, () => {
this.handleButtonStatus();
}, true);
}
init() {
if (!this.shouldRender()) {
return;
}
this.render();
this.handleButtonStatus();
jQuery(document.body).on('updated_cart_totals updated_checkout', () => {
this.render();
this.handleButtonStatus();
2023-02-15 16:50:34 +02:00
fetch(
this.gateway.ajax.cart_script_params.endpoint,
{
method: 'GET',
credentials: 'same-origin',
}
)
.then(result => result.json())
.then(result => {
if (! result.success) {
return;
}
// handle script reload
const newParams = result.data.url_params;
const reloadRequired = JSON.stringify(this.gateway.url_params) !== JSON.stringify(newParams);
2023-02-15 16:50:34 +02:00
if (reloadRequired) {
this.gateway.url_params = newParams;
jQuery(this.gateway.button.wrapper).trigger('ppcp-reload-buttons', this.gateway);
}
// handle button status
if ( result.data.button ) {
this.gateway.button = result.data.button;
}
this.handleButtonStatus();
if (this.lastAmount !== result.data.amount) {
this.lastAmount = result.data.amount;
this.messages.renderWithAmount(this.lastAmount);
}
2023-02-15 16:50:34 +02:00
});
});
}
handleButtonStatus() {
BootstrapHelper.handleButtonStatus(this);
}
shouldRender() {
2023-02-15 16:52:16 +02:00
return document.querySelector(this.gateway.button.wrapper) !== null;
}
shouldEnable() {
return BootstrapHelper.shouldEnable(this);
}
render() {
const actionHandler = new CartActionHandler(
PayPalCommerceGateway,
this.errorHandler,
);
2023-03-30 14:47:28 +02:00
if(
PayPalCommerceGateway.data_client_id.has_subscriptions
&& PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled
) {
this.renderer.render(actionHandler.subscriptionsConfiguration());
return;
}
2020-04-09 12:23:44 +03:00
this.renderer.render(
actionHandler.configuration()
2020-04-09 12:23:44 +03:00
);
this.messages.renderWithAmount(this.lastAmount);
}
}
export default CartBootstrap;