2020-04-30 15:28:48 +03:00
|
|
|
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
2023-07-04 14:43:55 +01:00
|
|
|
import BootstrapHelper from "../Helper/BootstrapHelper";
|
2023-02-15 16:50:34 +02:00
|
|
|
import {setVisible} from "../Helper/Hiding";
|
2020-04-10 10:34:49 +03:00
|
|
|
|
2020-04-08 18:50:29 +03:00
|
|
|
class CartBootstrap {
|
2023-06-16 11:39:20 +03:00
|
|
|
constructor(gateway, renderer, messages, errorHandler) {
|
2020-04-09 12:56:05 +03:00
|
|
|
this.gateway = gateway;
|
2020-04-09 12:23:44 +03:00
|
|
|
this.renderer = renderer;
|
2023-06-16 11:39:20 +03:00
|
|
|
this.messages = messages;
|
2022-10-27 09:09:45 +03:00
|
|
|
this.errorHandler = errorHandler;
|
2023-06-16 11:39:20 +03:00
|
|
|
this.lastAmount = this.gateway.messages.amount;
|
2023-07-03 17:35:01 +01:00
|
|
|
|
|
|
|
this.renderer.onButtonsInit(this.gateway.button.wrapper, () => {
|
|
|
|
this.handleButtonStatus();
|
|
|
|
}, true);
|
2020-04-08 18:50:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2020-04-09 10:49:37 +03:00
|
|
|
if (!this.shouldRender()) {
|
2020-04-08 18:50:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:45:49 +03:00
|
|
|
this.render();
|
2023-07-03 17:35:01 +01:00
|
|
|
this.handleButtonStatus();
|
2020-04-09 18:45:49 +03:00
|
|
|
|
2020-04-08 18:50:29 +03:00
|
|
|
jQuery(document.body).on('updated_cart_totals updated_checkout', () => {
|
2020-04-09 18:45:49 +03:00
|
|
|
this.render();
|
2023-07-03 17:35:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-20 08:02:15 +01:00
|
|
|
// handle script reload
|
2023-06-16 11:39:20 +03:00
|
|
|
const newParams = result.data.url_params;
|
2023-07-20 08:02:15 +01:00
|
|
|
const reloadRequired = JSON.stringify(this.gateway.url_params) !== JSON.stringify(newParams);
|
2023-02-15 16:50:34 +02:00
|
|
|
|
2023-07-20 08:02:15 +01: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();
|
2023-06-16 11:39:20 +03:00
|
|
|
|
|
|
|
if (this.lastAmount !== result.data.amount) {
|
|
|
|
this.lastAmount = result.data.amount;
|
|
|
|
this.messages.renderWithAmount(this.lastAmount);
|
|
|
|
}
|
2023-02-15 16:50:34 +02:00
|
|
|
});
|
2020-04-08 18:50:29 +03:00
|
|
|
});
|
2020-04-09 18:45:49 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 17:35:01 +01:00
|
|
|
handleButtonStatus() {
|
2023-07-04 14:43:55 +01:00
|
|
|
BootstrapHelper.handleButtonStatus(this);
|
2023-07-03 17:35:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 18:45:49 +03:00
|
|
|
shouldRender() {
|
2023-02-15 16:52:16 +02:00
|
|
|
return document.querySelector(this.gateway.button.wrapper) !== null;
|
2020-04-09 18:45:49 +03:00
|
|
|
}
|
2020-04-08 18:50:29 +03:00
|
|
|
|
2023-07-03 11:40:37 +01:00
|
|
|
shouldEnable() {
|
2023-07-04 14:43:55 +01:00
|
|
|
return BootstrapHelper.shouldEnable(this);
|
2023-07-03 11:40:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 18:45:49 +03:00
|
|
|
render() {
|
2020-04-10 10:34:49 +03:00
|
|
|
const actionHandler = new CartActionHandler(
|
|
|
|
PayPalCommerceGateway,
|
2022-10-27 09:09:45 +03:00
|
|
|
this.errorHandler,
|
2020-04-10 10:34:49 +03:00
|
|
|
);
|
|
|
|
|
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(
|
2022-07-18 16:27:06 +03:00
|
|
|
actionHandler.configuration()
|
2020-04-09 12:23:44 +03:00
|
|
|
);
|
2023-06-16 11:39:20 +03:00
|
|
|
|
|
|
|
this.messages.renderWithAmount(this.lastAmount);
|
2020-04-08 18:50:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 17:27:19 +02:00
|
|
|
export default CartBootstrap;
|