diff --git a/modules/ppcp-button/resources/js/button.js b/modules/ppcp-button/resources/js/button.js index f766cc6ed..0f5d573ce 100644 --- a/modules/ppcp-button/resources/js/button.js +++ b/modules/ppcp-button/resources/js/button.js @@ -241,7 +241,6 @@ document.addEventListener( if (!typeof (PayPalCommerceGateway)) { console.error('PayPal button could not be configured.'); return; - return; } if ( diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js index c9978306e..41c722465 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js @@ -4,9 +4,8 @@ import {hide, show} from "../Helper/Hiding"; import BootstrapHelper from "../Helper/BootstrapHelper"; import {loadPaypalJsScript} from "../Helper/ScriptLoading"; import {getPlanIdFromVariation} from "../Helper/Subscriptions" -import SimulateCart from "../Helper/SimulateCart"; -import {strRemoveWord, strAddWord, throttle} from "../Helper/Utils"; -import merge from "deepmerge"; +import {throttle} from "../Helper/Utils"; +import CartSimulator from "../Helper/CartSimulator"; class SingleProductBootstap { constructor(gateway, renderer, errorHandler) { @@ -217,71 +216,7 @@ class SingleProductBootstap { } simulateCart() { - if (!this.gateway.simulate_cart.enabled) { - return; - } - - const actionHandler = new SingleProductActionHandler( - null, - null, - this.form(), - this.errorHandler, - ); - - const hasSubscriptions = PayPalCommerceGateway.data_client_id.has_subscriptions - && PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled; - - const products = hasSubscriptions - ? actionHandler.getSubscriptionProducts() - : actionHandler.getProducts(); - - (new SimulateCart( - this.gateway.ajax.simulate_cart.endpoint, - this.gateway.ajax.simulate_cart.nonce, - )).simulate((data) => { - - jQuery(document.body).trigger('ppcp_product_total_updated', [data.total]); - - let newData = {}; - if (typeof data.button.is_disabled === 'boolean') { - newData = merge(newData, {button: {is_disabled: data.button.is_disabled}}); - } - if (typeof data.messages.is_hidden === 'boolean') { - newData = merge(newData, {messages: {is_hidden: data.messages.is_hidden}}); - } - if (newData) { - BootstrapHelper.updateScriptData(this, newData); - } - - if ( this.gateway.single_product_buttons_enabled !== '1' ) { - return; - } - - let enableFunding = this.gateway.url_params['enable-funding']; - let disableFunding = this.gateway.url_params['disable-funding']; - - for (const [fundingSource, funding] of Object.entries(data.funding)) { - if (funding.enabled === true) { - enableFunding = strAddWord(enableFunding, fundingSource); - disableFunding = strRemoveWord(disableFunding, fundingSource); - } else if (funding.enabled === false) { - enableFunding = strRemoveWord(enableFunding, fundingSource); - disableFunding = strAddWord(disableFunding, fundingSource); - } - } - - if ( - (enableFunding !== this.gateway.url_params['enable-funding']) || - (disableFunding !== this.gateway.url_params['disable-funding']) - ) { - this.gateway.url_params['enable-funding'] = enableFunding; - this.gateway.url_params['disable-funding'] = disableFunding; - jQuery(this.gateway.button.wrapper).trigger('ppcp-reload-buttons'); - } - - this.handleButtonStatus(false); - - }, products); + (new CartSimulator(this)).simulate(); } } diff --git a/modules/ppcp-button/resources/js/modules/Helper/CartSimulator.js b/modules/ppcp-button/resources/js/modules/Helper/CartSimulator.js new file mode 100644 index 000000000..24c5f8fa5 --- /dev/null +++ b/modules/ppcp-button/resources/js/modules/Helper/CartSimulator.js @@ -0,0 +1,95 @@ +import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler"; +import SimulateCart from "./SimulateCart"; +import BootstrapHelper from "./BootstrapHelper"; +import {strAddWord, strRemoveWord} from "./Utils"; +import merge from "deepmerge"; + +class CartSimulator { + + constructor(bootstrap) { + this.bootstrap = bootstrap; + } + + /** + * @return + */ + simulate() { + const gatewaySettings = this.bootstrap.gateway; + + if (!gatewaySettings.simulate_cart.enabled) { + return; + } + + const actionHandler = new SingleProductActionHandler( + null, + null, + this.bootstrap.form(), + this.bootstrap.errorHandler, + ); + + const hasSubscriptions = PayPalCommerceGateway.data_client_id.has_subscriptions + && PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled; + + const products = hasSubscriptions + ? actionHandler.getSubscriptionProducts() + : actionHandler.getProducts(); + + (new SimulateCart( + gatewaySettings.ajax.simulate_cart.endpoint, + gatewaySettings.ajax.simulate_cart.nonce, + )).simulate((data) => { + + // Trigger event with simulated total. + jQuery(document.body).trigger('ppcp_product_total_updated', [data.total]); + + // Hide and show fields. + let newData = {}; + if (typeof data.button.is_disabled === 'boolean') { + newData = merge(newData, {button: {is_disabled: data.button.is_disabled}}); + } + if (typeof data.messages.is_hidden === 'boolean') { + newData = merge(newData, {messages: {is_hidden: data.messages.is_hidden}}); + } + if (newData) { + BootstrapHelper.updateScriptData(this.bootstrap, newData); + } + + // Check if single product buttons enabled. + if ( gatewaySettings.single_product_buttons_enabled !== '1' ) { + return; + } + + // Update funding sources. + let enableFunding = gatewaySettings.url_params['enable-funding'] || ''; + let enableFundingBefore = enableFunding; + + let disableFunding = gatewaySettings.url_params['disable-funding'] || ''; + let disableFundingBefore = disableFunding; + + for (const [fundingSource, funding] of Object.entries(data.funding)) { + if (funding.enabled === true) { + enableFunding = strAddWord(enableFunding, fundingSource); + disableFunding = strRemoveWord(disableFunding, fundingSource); + } else if (funding.enabled === false) { + enableFunding = strRemoveWord(enableFunding, fundingSource); + disableFunding = strAddWord(disableFunding, fundingSource); + } + } + + // Detect and update funding changes and reload buttons. + if ( + (enableFunding !== enableFundingBefore) || + (disableFunding !== disableFundingBefore) + ) { + gatewaySettings.url_params['enable-funding'] = enableFunding; + gatewaySettings.url_params['disable-funding'] = disableFunding; + jQuery(gatewaySettings.button.wrapper).trigger('ppcp-reload-buttons'); + } + + this.bootstrap.handleButtonStatus(false); + + }, products); + } +} + +export default CartSimulator;