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

161 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-04-30 15:28:48 +03:00
import MiniCartBootstap from './modules/ContextBootstrap/MiniCartBootstap';
import SingleProductBootstap from './modules/ContextBootstrap/SingleProductBootstap';
import CartBootstrap from './modules/ContextBootstrap/CartBootstap';
import CheckoutBootstap from './modules/ContextBootstrap/CheckoutBootstap';
import PayNowBootstrap from "./modules/ContextBootstrap/PayNowBootstrap";
2020-04-30 15:28:48 +03:00
import Renderer from './modules/Renderer/Renderer';
2020-07-22 14:12:49 +03:00
import ErrorHandler from './modules/ErrorHandler';
2020-04-30 15:28:48 +03:00
import CreditCardRenderer from "./modules/Renderer/CreditCardRenderer";
import dataClientIdAttributeHandler from "./modules/DataClientIdAttributeHandler";
2020-08-19 10:27:53 +03:00
import MessageRenderer from "./modules/Renderer/MessageRenderer";
import Spinner from "./modules/Helper/Spinner";
import {
getCurrentPaymentMethod,
ORDER_BUTTON_SELECTOR,
PaymentMethods
} from "./modules/Helper/CheckoutMethodState";
2022-03-17 09:48:00 +02:00
import {hide, setVisible} from "./modules/Helper/Hiding";
const buttonsSpinner = new Spinner('.ppc-button-wrapper');
2020-04-02 08:38:00 +03:00
const bootstrap = () => {
2020-07-28 08:05:18 +03:00
const errorHandler = new ErrorHandler(PayPalCommerceGateway.labels.error.generic);
const spinner = new Spinner();
const creditCardRenderer = new CreditCardRenderer(PayPalCommerceGateway, errorHandler, spinner);
const onSmartButtonClick = data => {
window.ppcpFundingSource = data.fundingSource;
};
2022-03-17 09:48:00 +02:00
const onSmartButtonsInit = () => {
buttonsSpinner.unblock();
};
const renderer = new Renderer(creditCardRenderer, PayPalCommerceGateway, onSmartButtonClick, onSmartButtonsInit);
2020-08-19 10:27:53 +03:00
const messageRenderer = new MessageRenderer(PayPalCommerceGateway.messages);
const context = PayPalCommerceGateway.context;
if (context === 'mini-cart' || context === 'product') {
2021-07-13 09:43:30 +02:00
if (PayPalCommerceGateway.mini_cart_buttons_enabled === '1') {
const miniCartBootstrap = new MiniCartBootstap(
PayPalCommerceGateway,
renderer
);
miniCartBootstrap.init();
}
}
if (context === 'product' && PayPalCommerceGateway.single_product_buttons_enabled === '1') {
const singleProductBootstrap = new SingleProductBootstap(
PayPalCommerceGateway,
renderer,
messageRenderer,
);
singleProductBootstrap.init();
}
if (context === 'cart') {
const cartBootstrap = new CartBootstrap(
PayPalCommerceGateway,
renderer,
);
2020-04-08 12:33:34 +03:00
cartBootstrap.init();
}
2020-04-08 12:33:34 +03:00
if (context === 'checkout') {
const checkoutBootstap = new CheckoutBootstap(
PayPalCommerceGateway,
2020-08-19 10:27:53 +03:00
renderer,
messageRenderer,
spinner
);
checkoutBootstap.init();
}
2020-08-19 10:27:53 +03:00
if (context === 'pay-now' ) {
const payNowBootstrap = new PayNowBootstrap(
PayPalCommerceGateway,
renderer,
2021-01-05 17:12:26 +01:00
messageRenderer,
spinner
);
payNowBootstrap.init();
}
if (context !== 'checkout') {
messageRenderer.render();
}
};
document.addEventListener(
'DOMContentLoaded',
() => {
if (!typeof (PayPalCommerceGateway)) {
console.error('PayPal button could not be configured.');
2020-04-02 08:38:00 +03:00
return;
}
if (
PayPalCommerceGateway.context !== 'checkout'
&& PayPalCommerceGateway.data_client_id.user === 0
&& PayPalCommerceGateway.data_client_id.has_subscriptions
) {
return;
}
// Sometimes PayPal script takes long time to load,
// so we additionally hide the standard order button here to avoid failed orders.
// Normally it is hidden later after the script load.
const hideOrderButtonIfPpcpGateway = () => {
// only in checkout, otherwise it may break things (e.g. payment via product page),
// and also the loading spinner may look weird on other pages
if (PayPalCommerceGateway.context !== 'checkout') {
return;
}
const currentPaymentMethod = getCurrentPaymentMethod();
2022-03-17 09:48:00 +02:00
const isPaypal = currentPaymentMethod === PaymentMethods.PAYPAL;
setVisible(ORDER_BUTTON_SELECTOR, !isPaypal, true);
if (isPaypal) {
// stopped after the first rendering of the buttons, in onInit
buttonsSpinner.block();
} else {
buttonsSpinner.unblock();
2022-03-17 09:48:00 +02:00
}
}
let bootstrapped = false;
hideOrderButtonIfPpcpGateway();
jQuery(document.body).on('updated_checkout payment_method_selected', () => {
if (bootstrapped) {
return;
}
hideOrderButtonIfPpcpGateway();
});
const script = document.createElement('script');
script.addEventListener('load', (event) => {
bootstrapped = true;
bootstrap();
});
script.setAttribute('src', PayPalCommerceGateway.button.url);
2020-04-30 15:28:48 +03:00
Object.entries(PayPalCommerceGateway.script_attributes).forEach(
(keyValue) => {
script.setAttribute(keyValue[0], keyValue[1]);
}
);
if (PayPalCommerceGateway.data_client_id.set_attribute) {
dataClientIdAttributeHandler(script, PayPalCommerceGateway.data_client_id);
return;
}
document.body.append(script);
},
2021-01-05 17:12:26 +01:00
);