import Fastlane from "./Connection/Fastlane"; import MockData from "./Helper/MockData"; import {log} from "./Helper/Debug"; import DomElementCollection from "./Components/DomElementCollection"; import ShippingView from "./Views/ShippingView"; import BillingView from "./Views/BillingView"; import CardView from "./Views/CardView"; class AxoManager { constructor(axoConfig, ppcpConfig) { this.axoConfig = axoConfig; this.ppcpConfig = ppcpConfig; this.initialized = false; this.fastlane = new Fastlane(); this.$ = jQuery; this.isConnectProfile = false; this.hideGatewaySelection = false; this.data = { billing: null, shipping: null, card: null, } this.el = new DomElementCollection(); this.styles = { root: { backgroundColorPrimary: '#ffffff' } } this.locale = 'en_us'; this.registerEventHandlers(); this.shippingView = new ShippingView(this.el.shippingAddressContainer.selector); this.billingView = new BillingView(this.el.billingAddressContainer.selector); this.cardView = new CardView(this.el.paymentContainer.selector, this); } registerEventHandlers() { // Listen to Gateway Radio button changes. this.el.gatewayRadioButton.on('change', (ev) => { if (ev.target.checked) { this.showAxo(); } else { this.hideAxo(); } }); this.$(document).on('updated_checkout payment_method_selected', () => { this.triggerGatewayChange(); }); // On checkout form submitted. this.$(document).on('click', this.el.submitButton.selector, () => { this.onClickSubmitButton(); return false; }); // Click change shipping address link. this.$(document).on('click', '*[data-ppcp-axo-change-shipping-address]', async () => { if (this.isConnectProfile) { console.log('profile', this.fastlane.profile); //this.shippingView.deactivate(); const { selectionChanged, selectedAddress } = await this.fastlane.profile.showShippingAddressSelector(); console.log('selectedAddress', selectedAddress); if (selectionChanged) { this.setShipping(selectedAddress); this.shippingView.activate(); } } else { let checkbox = document.querySelector('#ship-to-different-address-checkbox'); if (checkbox && !checkbox.checked) { jQuery(checkbox).trigger('click'); } this.shippingView.deactivate(); } }); this.$(document).on('click', '*[data-ppcp-axo-save-shipping-address]', async () => { this.shippingView.activate(); }); // Click change billing address link. this.$(document).on('click', '*[data-ppcp-axo-change-billing-address]', async () => { if (this.isConnectProfile) { this.$('*[data-ppcp-axo-change-card]').trigger('click'); } else { this.billingView.deactivate(); } }); this.$(document).on('click', '*[data-ppcp-axo-save-billing-address]', async () => { this.billingView.activate(); }); // Click change card link. this.$(document).on('click', '*[data-ppcp-axo-change-card]', async () => { console.log('profile', this.fastlane.profile); const response = await this.fastlane.profile.showCardSelector(); console.log('card response', response); if (response.selectionChanged) { this.setCard(response.selectedCard); this.setBilling({ address: response.selectedCard.paymentSource.card.billingAddress }); } }); // Cancel "continuation" mode. this.$(document).on('click', '*[data-ppcp-axo-show-gateway-selection]', async () => { this.hideGatewaySelection = false; this.$('.wc_payment_methods label').show(); this.cardView.refresh(); }); } showAxo() { this.initPlacements(); this.initFastlane(); this.shippingView.activate(); this.billingView.activate(); this.el.emailWidgetContainer.show(); this.el.watermarkContainer.show(); this.el.paymentContainer.show(); this.el.submitButtonContainer.show(); this.el.defaultSubmitButton.hide(); if (this.useEmailWidget()) { this.el.fieldBillingEmail.hide(); } } hideAxo() { this.shippingView.deactivate(); this.billingView.deactivate(); this.el.emailWidgetContainer.hide(); this.el.watermarkContainer.hide(); this.el.paymentContainer.hide(); this.el.submitButtonContainer.hide(); this.el.defaultSubmitButton.show(); if (this.useEmailWidget()) { this.el.fieldBillingEmail.show(); } } initPlacements() { let emailRow = document.querySelector(this.el.fieldBillingEmail.selector); const bc = this.el.billingAddressContainer; const sc = this.el.shippingAddressContainer; const ec = this.el.emailWidgetContainer; if (!document.querySelector(bc.selector)) { document.querySelector(bc.anchorSelector).insertAdjacentHTML('beforeend', `
`); } if (!document.querySelector(sc.selector)) { document.querySelector(sc.anchorSelector).insertAdjacentHTML('afterbegin', ` `); } if (this.useEmailWidget()) { // Display email widget. if (!document.querySelector(ec.selector)) { emailRow.parentNode.insertAdjacentHTML('afterbegin', `