woocommerce-paypal-payments/modules/ppcp-axo/resources/js/AxoManager.js

767 lines
24 KiB
JavaScript
Raw Normal View History

2024-03-08 17:41:21 +00:00
import Fastlane from "./Connection/Fastlane";
2024-02-12 18:06:48 +00:00
import {log} from "./Helper/Debug";
2024-03-08 17:41:21 +00:00
import DomElementCollection from "./Components/DomElementCollection";
import ShippingView from "./Views/ShippingView";
import BillingView from "./Views/BillingView";
import CardView from "./Views/CardView";
2024-04-11 17:14:22 +01:00
import PayPalInsights from "./Insights/PayPalInsights";
2024-04-15 17:20:02 +01:00
import {disable,enable} from "../../../ppcp-button/resources/js/modules/Helper/ButtonDisabler";
import {getCurrentPaymentMethod} from "../../../ppcp-button/resources/js/modules/Helper/CheckoutMethodState";
2024-02-09 17:49:45 +00:00
class AxoManager {
constructor(axoConfig, ppcpConfig) {
this.axoConfig = axoConfig;
this.ppcpConfig = ppcpConfig;
2024-02-09 17:49:45 +00:00
this.initialized = false;
this.fastlane = new Fastlane();
2024-02-12 18:06:48 +00:00
this.$ = jQuery;
2024-03-08 14:39:50 +00:00
this.hideGatewaySelection = false;
2024-03-14 10:54:15 +00:00
this.status = {
active: false,
validEmail: false,
hasProfile: false,
useEmailWidget: this.useEmailWidget()
};
2024-03-08 14:39:50 +00:00
this.data = {
2024-04-08 11:31:12 +01:00
email: null,
2024-03-08 14:39:50 +00:00
billing: null,
shipping: null,
card: null,
2024-03-14 10:54:15 +00:00
};
2024-03-08 14:39:50 +00:00
2024-03-08 17:41:21 +00:00
this.el = new DomElementCollection();
2024-02-09 17:49:45 +00:00
2024-02-12 18:06:48 +00:00
this.styles = {
root: {
backgroundColorPrimary: '#ffffff'
}
2024-03-14 10:54:15 +00:00
};
2024-02-09 17:49:45 +00:00
2024-02-12 18:06:48 +00:00
this.locale = 'en_us';
2024-02-09 17:49:45 +00:00
2024-03-08 14:39:50 +00:00
this.registerEventHandlers();
2024-03-12 09:23:42 +00:00
this.shippingView = new ShippingView(this.el.shippingAddressContainer.selector, this.el);
this.billingView = new BillingView(this.el.billingAddressContainer.selector, this.el);
2024-03-14 10:54:15 +00:00
this.cardView = new CardView(this.el.paymentContainer.selector + '-details', this.el, this);
document.axoDebugSetStatus = (key, value) => {
2024-03-14 10:54:15 +00:00
this.setStatus(key, value);
}
2024-04-10 18:27:21 +01:00
document.axoDebugObject = () => {
console.log(this);
2024-04-10 18:27:21 +01:00
return this;
}
2024-04-11 17:14:22 +01:00
if (
this.axoConfig?.insights?.enabled
&& this.axoConfig?.insights?.client_id
&& this.axoConfig?.insights?.session_id
) {
PayPalInsights.config(this.axoConfig?.insights?.client_id, { debug: true });
PayPalInsights.setSessionId(this.axoConfig?.insights?.session_id);
PayPalInsights.trackJsLoad();
if (document.querySelector('.woocommerce-checkout')) {
PayPalInsights.trackBeginCheckout({
amount: this.axoConfig?.insights?.amount,
page_type: 'checkout',
user_data: {
country: 'US',
is_store_member: false,
}
});
}
}
2024-04-17 14:51:30 +01:00
this.triggerGatewayChange();
2024-03-08 14:39:50 +00:00
}
registerEventHandlers() {
2024-04-17 10:14:27 +01:00
this.$(document).on('change', 'input[name=payment_method]', (ev) => {
2024-04-11 17:14:22 +01:00
const map = {
'ppcp-axo-gateway': 'card',
'ppcp-gateway': 'paypal',
}
PayPalInsights.trackSelectPaymentMethod({
payment_method_selected: map[ev.target.value] || 'other',
page_type: 'checkout'
});
});
2024-02-12 18:06:48 +00:00
// Listen to Gateway Radio button changes.
2024-03-08 17:41:21 +00:00
this.el.gatewayRadioButton.on('change', (ev) => {
2024-02-12 18:06:48 +00:00
if (ev.target.checked) {
2024-03-14 10:54:15 +00:00
this.activateAxo();
2024-02-09 17:49:45 +00:00
} else {
2024-03-14 10:54:15 +00:00
this.deactivateAxo();
2024-02-09 17:49:45 +00:00
}
});
2024-02-12 18:06:48 +00:00
this.$(document).on('updated_checkout payment_method_selected', () => {
this.triggerGatewayChange();
2024-02-09 17:49:45 +00:00
});
2024-03-08 14:39:50 +00:00
// On checkout form submitted.
2024-03-12 09:23:42 +00:00
this.el.submitButton.on('click', () => {
2024-02-12 18:06:48 +00:00
this.onClickSubmitButton();
2024-02-09 17:49:45 +00:00
return false;
2024-03-12 09:23:42 +00:00
})
2024-02-09 17:49:45 +00:00
2024-03-08 14:39:50 +00:00
// Click change shipping address link.
2024-03-12 09:23:42 +00:00
this.el.changeShippingAddressLink.on('click', async () => {
2024-03-14 10:54:15 +00:00
if (this.status.hasProfile) {
2024-03-08 14:39:50 +00:00
const { selectionChanged, selectedAddress } = await this.fastlane.profile.showShippingAddressSelector();
if (selectionChanged) {
this.setShipping(selectedAddress);
2024-03-14 10:54:15 +00:00
this.shippingView.refresh();
2024-03-08 14:39:50 +00:00
}
}
});
// Click change billing address link.
2024-03-12 09:23:42 +00:00
this.el.changeBillingAddressLink.on('click', async () => {
2024-03-14 10:54:15 +00:00
if (this.status.hasProfile) {
2024-03-12 09:23:42 +00:00
this.el.changeCardLink.trigger('click');
2024-03-08 14:39:50 +00:00
}
});
// Click change card link.
2024-03-12 09:23:42 +00:00
this.el.changeCardLink.on('click', async () => {
2024-03-08 14:39:50 +00:00
const response = await this.fastlane.profile.showCardSelector();
if (response.selectionChanged) {
this.setCard(response.selectedCard);
this.setBilling({
address: response.selectedCard.paymentSource.card.billingAddress
});
}
});
// Cancel "continuation" mode.
2024-03-12 09:23:42 +00:00
this.el.showGatewaySelectionLink.on('click', async () => {
2024-03-08 14:39:50 +00:00
this.hideGatewaySelection = false;
this.$('.wc_payment_methods label').show();
2024-03-08 17:41:21 +00:00
this.cardView.refresh();
2024-03-08 14:39:50 +00:00
});
// Prevents sending checkout form when pressing Enter key on input field
// and triggers customer lookup
this.$('form.woocommerce-checkout input').on('keydown', async (ev) => {
if(ev.key === 'Enter' && getCurrentPaymentMethod() === 'ppcp-axo-gateway' ) {
ev.preventDefault();
}
});
2024-02-09 17:49:45 +00:00
}
2024-03-14 10:54:15 +00:00
rerender() {
/**
* active | 0 1 1 1
* validEmail | * 0 1 1
* hasProfile | * * 0 1
* --------------------------------
* defaultSubmitButton | 1 0 0 0
* defaultEmailField | 1 0 0 0
* defaultFormFields | 1 0 1 0
* extraFormFields | 0 0 0 1
* axoEmailField | 0 1 0 0
* axoProfileViews | 0 0 0 1
* axoPaymentContainer | 0 0 1 1
* axoSubmitButton | 0 0 1 1
*/
const scenario = this.identifyScenario(
this.status.active,
this.status.validEmail,
this.status.hasProfile
);
log('Scenario', scenario);
// Reset some elements to a default status.
this.el.watermarkContainer.hide();
2024-03-08 14:39:50 +00:00
2024-03-14 10:54:15 +00:00
if (scenario.defaultSubmitButton) {
this.el.defaultSubmitButton.show();
} else {
this.el.defaultSubmitButton.hide();
2024-03-12 09:23:42 +00:00
}
2024-03-14 10:54:15 +00:00
if (scenario.defaultEmailField) {
this.el.fieldBillingEmail.show();
} else {
2024-03-12 09:23:42 +00:00
this.el.fieldBillingEmail.hide();
2024-03-14 10:54:15 +00:00
}
if (scenario.defaultFormFields) {
this.el.customerDetails.show();
} else {
this.el.customerDetails.hide();
}
if (scenario.extraFormFields) {
this.el.customerDetails.show();
// Hiding of unwanted will be handled by the axoProfileViews handler.
}
if (scenario.axoEmailField) {
this.showAxoEmailField();
this.el.watermarkContainer.show();
// Move watermark to after email.
this.$(this.el.fieldBillingEmail.selector).append(
this.$(this.el.watermarkContainer.selector)
);
2024-03-12 09:23:42 +00:00
} else {
this.el.emailWidgetContainer.hide();
2024-03-14 10:54:15 +00:00
if (!scenario.defaultEmailField) {
this.el.fieldBillingEmail.hide();
}
2024-03-12 09:23:42 +00:00
}
2024-03-14 10:54:15 +00:00
if (scenario.axoProfileViews) {
2024-04-30 09:52:20 +02:00
this.el.billingAddressContainer.hide();
2024-03-12 09:23:42 +00:00
this.shippingView.activate();
this.billingView.activate();
2024-03-14 10:54:15 +00:00
this.cardView.activate();
2024-03-12 09:23:42 +00:00
2024-03-14 10:54:15 +00:00
// Move watermark to after shipping.
this.$(this.el.shippingAddressContainer.selector).after(
this.$(this.el.watermarkContainer.selector)
);
this.el.watermarkContainer.show();
} else {
this.shippingView.deactivate();
this.billingView.deactivate();
this.cardView.deactivate();
}
if (scenario.axoPaymentContainer) {
this.el.paymentContainer.show();
} else {
this.el.paymentContainer.hide();
}
if (scenario.axoSubmitButton) {
this.el.submitButtonContainer.show();
} else {
this.el.submitButtonContainer.hide();
2024-03-12 09:23:42 +00:00
}
2024-02-12 18:06:48 +00:00
2024-03-14 10:54:15 +00:00
this.ensureBillingFieldsConsistency();
this.ensureShippingFieldsConsistency();
2024-02-12 18:06:48 +00:00
}
2024-03-14 10:54:15 +00:00
identifyScenario(active, validEmail, hasProfile) {
let response = {
defaultSubmitButton: false,
defaultEmailField: false,
defaultFormFields: false,
extraFormFields: false,
axoEmailField: false,
axoProfileViews: false,
axoPaymentContainer: false,
axoSubmitButton: false,
}
2024-03-12 09:23:42 +00:00
2024-03-14 10:54:15 +00:00
if (active && validEmail && hasProfile) {
response.extraFormFields = true;
response.axoProfileViews = true;
response.axoPaymentContainer = true;
response.axoSubmitButton = true;
return response;
}
if (active && validEmail && !hasProfile) {
response.defaultFormFields = true;
response.axoEmailField = true;
response.axoPaymentContainer = true;
response.axoSubmitButton = true;
return response;
}
if (active && !validEmail) {
response.axoEmailField = true;
return response;
}
if (!active) {
response.defaultSubmitButton = true;
response.defaultEmailField = true;
response.defaultFormFields = true;
return response;
}
throw new Error('Invalid scenario.');
}
2024-03-08 14:39:50 +00:00
2024-03-14 10:54:15 +00:00
ensureBillingFieldsConsistency() {
const $billingFields = this.$('.woocommerce-billing-fields .form-row:visible');
const $billingHeaders = this.$('.woocommerce-billing-fields h3');
if (this.billingView.isActive()) {
if ($billingFields.length) {
$billingHeaders.show();
} else {
$billingHeaders.hide();
}
} else {
$billingHeaders.show();
}
}
2024-03-14 10:54:15 +00:00
ensureShippingFieldsConsistency() {
const $shippingFields = this.$('.woocommerce-shipping-fields .form-row:visible');
const $shippingHeaders = this.$('.woocommerce-shipping-fields h3');
if (this.shippingView.isActive()) {
if ($shippingFields.length) {
$shippingHeaders.show();
} else {
$shippingHeaders.hide();
}
} else {
$shippingHeaders.show();
}
}
showAxoEmailField() {
if (this.status.useEmailWidget) {
this.el.emailWidgetContainer.show();
this.el.fieldBillingEmail.hide();
} else {
this.el.emailWidgetContainer.hide();
this.el.fieldBillingEmail.show();
}
}
setStatus(key, value) {
this.status[key] = value;
log('Status updated', JSON.parse(JSON.stringify(this.status)));
this.rerender();
}
activateAxo() {
this.initPlacements();
this.initFastlane();
this.setStatus('active', true);
const emailInput = document.querySelector(this.el.fieldBillingEmail.selector + ' input');
if (emailInput && this.lastEmailCheckedIdentity !== emailInput.value) {
this.onChangeEmail();
}
}
deactivateAxo() {
this.setStatus('active', false);
2024-02-12 18:06:48 +00:00
}
2024-03-08 14:39:50 +00:00
initPlacements() {
2024-03-14 10:54:15 +00:00
const wrapper = this.el.axoCustomerDetails;
2024-03-14 10:54:15 +00:00
// Customer details container.
if (!document.querySelector(wrapper.selector)) {
document.querySelector(wrapper.anchorSelector).insertAdjacentHTML('afterbegin', `
<div id="${wrapper.id}" class="${wrapper.className}"></div>
`);
}
const wrapperElement = document.querySelector(wrapper.selector);
2024-03-08 14:39:50 +00:00
2024-03-14 10:54:15 +00:00
// Billing view container.
const bc = this.el.billingAddressContainer;
2024-03-08 14:39:50 +00:00
if (!document.querySelector(bc.selector)) {
2024-03-14 10:54:15 +00:00
wrapperElement.insertAdjacentHTML('beforeend', `
2024-03-08 14:39:50 +00:00
<div id="${bc.id}" class="${bc.className}"></div>
`);
}
2024-03-14 10:54:15 +00:00
// Shipping view container.
const sc = this.el.shippingAddressContainer;
2024-03-08 14:39:50 +00:00
if (!document.querySelector(sc.selector)) {
2024-03-14 10:54:15 +00:00
wrapperElement.insertAdjacentHTML('beforeend', `
2024-03-08 14:39:50 +00:00
<div id="${sc.id}" class="${sc.className}"></div>
`);
}
2024-04-10 18:27:21 +01:00
// Watermark container
const wc = this.el.watermarkContainer;
if (!document.querySelector(wc.selector)) {
this.emailInput = document.querySelector(this.el.fieldBillingEmail.selector + ' input');
this.emailInput.insertAdjacentHTML('afterend', `
<div class="${wc.className}" id="${wc.id}"></div>
`);
}
// Payment container
const pc = this.el.paymentContainer;
if (!document.querySelector(pc.selector)) {
const gatewayPaymentContainer = document.querySelector('.payment_method_ppcp-axo-gateway');
gatewayPaymentContainer.insertAdjacentHTML('beforeend', `
<div id="${pc.id}" class="${pc.className} hidden">
<div id="${pc.id}-form" class="${pc.className}-form"></div>
<div id="${pc.id}-details" class="${pc.className}-details"></div>
</div>
`);
}
if (this.useEmailWidget()) {
// Display email widget.
2024-03-14 10:54:15 +00:00
const ec = this.el.emailWidgetContainer;
2024-03-08 14:39:50 +00:00
if (!document.querySelector(ec.selector)) {
2024-03-14 10:54:15 +00:00
wrapperElement.insertAdjacentHTML('afterbegin', `
2024-03-08 14:39:50 +00:00
<div id="${ec.id}" class="${ec.className}">
--- EMAIL WIDGET PLACEHOLDER ---
</div>
`);
}
} else {
2024-03-14 10:54:15 +00:00
// Move email to the AXO container.
let emailRow = document.querySelector(this.el.fieldBillingEmail.selector);
wrapperElement.prepend(emailRow);
emailRow.querySelector('input').focus();
}
2024-02-12 18:06:48 +00:00
}
2024-03-08 14:39:50 +00:00
async initFastlane() {
2024-02-09 17:49:45 +00:00
if (this.initialized) {
return;
}
this.initialized = true;
2024-02-12 18:06:48 +00:00
await this.connect();
this.renderWatermark();
this.watchEmail();
2024-02-09 17:49:45 +00:00
}
2024-02-12 18:06:48 +00:00
async connect() {
if (this.axoConfig.environment.is_sandbox) {
window.localStorage.setItem('axoEnv', 'sandbox');
}
2024-02-09 17:49:45 +00:00
await this.fastlane.connect({
2024-02-12 18:06:48 +00:00
locale: this.locale,
styles: this.styles
2024-02-09 17:49:45 +00:00
});
this.fastlane.setLocale('en_us');
2024-02-12 18:06:48 +00:00
}
2024-02-09 17:49:45 +00:00
2024-02-12 18:06:48 +00:00
triggerGatewayChange() {
2024-03-08 17:41:21 +00:00
this.el.gatewayRadioButton.trigger('change');
2024-02-12 18:06:48 +00:00
}
2024-04-16 16:37:12 +01:00
async renderWatermark() {
(await this.fastlane.FastlaneWatermarkComponent({
2024-02-09 17:49:45 +00:00
includeAdditionalInfo: true
2024-04-16 16:37:12 +01:00
})).render(this.el.watermarkContainer.selector);
2024-02-12 18:06:48 +00:00
}
2024-02-09 17:49:45 +00:00
2024-02-12 18:06:48 +00:00
watchEmail() {
2024-02-09 17:49:45 +00:00
if (this.useEmailWidget()) {
// TODO
} else {
2024-03-08 17:41:21 +00:00
this.emailInput = document.querySelector(this.el.fieldBillingEmail.selector + ' input');
this.emailInput.addEventListener('change', async ()=> {
this.onChangeEmail();
});
if (this.emailInput.value) {
this.onChangeEmail();
}
2024-02-09 17:49:45 +00:00
}
}
2024-02-12 18:06:48 +00:00
async onChangeEmail () {
2024-04-08 11:31:12 +01:00
this.clearData();
2024-03-14 10:54:15 +00:00
if (!this.status.active) {
log('Email checking skipped, AXO not active.');
return;
}
if (!this.emailInput) {
log('Email field not initialized.');
return;
}
log('Email changed: ' + (this.emailInput ? this.emailInput.value : '<empty>'));
this.$(this.el.paymentContainer.selector + '-detail').html('');
this.$(this.el.paymentContainer.selector + '-form').html('');
this.setStatus('validEmail', false);
this.setStatus('hasProfile', false);
2024-02-09 17:49:45 +00:00
2024-03-08 14:39:50 +00:00
this.hideGatewaySelection = false;
2024-03-14 10:54:15 +00:00
this.lastEmailCheckedIdentity = this.emailInput.value;
2024-03-12 09:23:42 +00:00
2024-03-14 10:54:15 +00:00
if (!this.emailInput.value || !this.emailInput.checkValidity()) {
2024-02-12 18:06:48 +00:00
log('The email address is not valid.');
2024-02-09 17:49:45 +00:00
return;
}
2024-03-12 09:23:42 +00:00
this.data.email = this.emailInput.value;
this.billingView.setData(this.data);
2024-02-09 17:49:45 +00:00
2024-04-11 17:14:22 +01:00
if (!this.fastlane.identity) {
log('Not initialized.');
return;
}
PayPalInsights.trackSubmitCheckoutEmail({
page_type: 'checkout'
});
await this.lookupCustomerByEmail();
}
async lookupCustomerByEmail() {
2024-03-12 09:23:42 +00:00
const lookupResponse = await this.fastlane.identity.lookupCustomerByEmail(this.emailInput.value);
2024-03-08 14:39:50 +00:00
if (lookupResponse.customerContextId) {
2024-02-12 18:06:48 +00:00
// Email is associated with a Connect profile or a PayPal member.
// Authenticate the customer to get access to their profile.
log('Email is associated with a Connect profile or a PayPal member');
2024-03-08 14:39:50 +00:00
const authResponse = await this.fastlane.identity.triggerAuthenticationFlow(lookupResponse.customerContextId);
log('AuthResponse', authResponse);
if (authResponse.authenticationState === 'succeeded') {
log(JSON.stringify(authResponse));
// Add addresses
this.setShipping(authResponse.profileData.shippingAddress);
2024-04-08 11:31:12 +01:00
this.setBilling({
address: authResponse.profileData.card.paymentSource.card.billingAddress,
phoneNumber: authResponse.profileData.shippingAddress.phoneNumber.nationalNumber ?? ''
2024-04-08 11:31:12 +01:00
});
2024-03-08 14:39:50 +00:00
this.setCard(authResponse.profileData.card);
2024-03-14 10:54:15 +00:00
this.setStatus('validEmail', true);
this.setStatus('hasProfile', true);
2024-03-08 14:39:50 +00:00
this.hideGatewaySelection = true;
this.$('.wc_payment_methods label').hide();
2024-03-14 10:54:15 +00:00
this.rerender();
2024-03-08 14:39:50 +00:00
} else {
// authentication failed or canceled by the customer
log("Authentication Failed")
}
2024-02-09 17:49:45 +00:00
} else {
// No profile found with this email address.
// This is a guest customer.
2024-02-12 18:06:48 +00:00
log('No profile found with this email address.');
2024-02-09 17:49:45 +00:00
2024-03-14 10:54:15 +00:00
this.setStatus('validEmail', true);
this.setStatus('hasProfile', false);
2024-03-12 09:23:42 +00:00
2024-04-17 16:32:38 +01:00
this.cardComponent = (await this.fastlane.FastlaneCardComponent(
2024-04-17 15:24:07 +01:00
this.cardComponentData()
2024-04-17 16:32:38 +01:00
)).render(this.el.paymentContainer.selector + '-form');
2024-02-12 18:06:48 +00:00
}
}
2024-02-09 17:49:45 +00:00
2024-04-08 11:31:12 +01:00
clearData() {
this.data = {
email: null,
billing: null,
shipping: null,
card: null,
};
}
2024-03-08 14:39:50 +00:00
setShipping(shipping) {
this.data.shipping = shipping;
2024-03-08 17:41:21 +00:00
this.shippingView.setData(this.data);
2024-03-08 14:39:50 +00:00
}
setBilling(billing) {
this.data.billing = billing;
2024-03-08 17:41:21 +00:00
this.billingView.setData(this.data);
2024-03-08 14:39:50 +00:00
}
setCard(card) {
this.data.card = card;
2024-03-08 17:41:21 +00:00
this.cardView.setData(this.data);
2024-03-08 14:39:50 +00:00
}
2024-02-12 18:06:48 +00:00
onClickSubmitButton() {
2024-04-10 18:27:21 +01:00
// TODO: validate data.
2024-04-08 11:31:12 +01:00
if (this.data.card) { // Ryan flow
log('Ryan flow.');
2024-04-10 18:27:21 +01:00
2024-04-17 10:14:27 +01:00
this.$('#ship-to-different-address-checkbox').prop('checked', 'checked');
2024-04-10 18:27:21 +01:00
2024-04-15 17:20:02 +01:00
let data = {};
this.billingView.toSubmitData(data);
this.shippingView.toSubmitData(data);
this.cardView.toSubmitData(data);
this.submit(this.data.card.id, data);
2024-04-08 11:31:12 +01:00
} else { // Gary flow
log('Gary flow.');
try {
2024-04-17 16:32:38 +01:00
this.cardComponent.getPaymentToken(
2024-04-08 11:31:12 +01:00
this.tokenizeData()
).then((response) => {
2024-04-17 16:32:38 +01:00
this.submit(response.id);
2024-04-08 11:31:12 +01:00
});
} catch (e) {
log('Error tokenizing.');
2024-04-17 16:32:38 +01:00
alert('Error tokenizing data.');
2024-04-08 11:31:12 +01:00
}
}
}
cardComponentData() {
let fields = {};
if(this.axoConfig.name_on_card === '1') {
fields.cardholderName = {};
}
2024-04-08 11:31:12 +01:00
return {
fields: fields,
2024-05-01 14:54:59 +02:00
styles: this.remove_keys_with_empty_string(this.axoConfig.style_options)
2024-04-08 11:31:12 +01:00
}
}
tokenizeData() {
return {
name: {
fullName: this.billingView.fullName()
},
billingAddress: {
addressLine1: this.billingView.inputValue('street1'),
addressLine2: this.billingView.inputValue('street2'),
adminArea1: this.billingView.inputValue('city'),
adminArea2: this.billingView.inputValue('stateCode'),
postalCode: this.billingView.inputValue('postCode'),
countryCode: this.billingView.inputValue('countryCode'),
}
2024-02-09 17:49:45 +00:00
}
}
2024-04-15 17:20:02 +01:00
submit(nonce, data) {
2024-02-12 18:06:48 +00:00
// Send the nonce and previously captured device data to server to complete checkout
2024-04-08 11:31:12 +01:00
if (!this.el.axoNonceInput.get()) {
2024-04-15 17:20:02 +01:00
this.$('form.woocommerce-checkout').append(`<input type="hidden" id="${this.el.axoNonceInput.id}" name="axo_nonce" value="" />`);
2024-04-08 11:31:12 +01:00
}
this.el.axoNonceInput.get().value = nonce;
2024-04-11 17:14:22 +01:00
PayPalInsights.trackEndCheckout({
amount: this.axoConfig?.insights?.amount,
page_type: 'checkout',
payment_method_selected: 'card',
user_data: {
country: 'US',
is_store_member: false,
}
});
2024-04-15 17:20:02 +01:00
if (data) {
// Ryan flow.
const form = document.querySelector('form.woocommerce-checkout');
const formData = new FormData(form);
2024-04-17 16:32:38 +01:00
this.showLoading();
2024-04-15 17:20:02 +01:00
// Fill in form data.
Object.keys(data).forEach((key) => {
formData.set(key, data[key]);
});
fetch(wc_checkout_params.checkout_url, { // TODO: maybe create a new endpoint to process_payment.
method: "POST",
body: formData
})
.then(response => response.json())
.then(responseData => {
2024-04-17 10:14:27 +01:00
if (responseData.result === 'failure') {
if (responseData.messages) {
const $notices = this.$('.woocommerce-notices-wrapper').eq(0);
$notices.html(responseData.messages);
this.$('html, body').animate({
scrollTop: $notices.offset().top
}, 500);
}
console.error('Failure:', responseData);
2024-04-17 16:32:38 +01:00
this.hideLoading();
2024-04-17 10:14:27 +01:00
return;
}
2024-04-15 17:20:02 +01:00
if (responseData.redirect) {
window.location.href = responseData.redirect;
}
})
.catch(error => {
console.error('Error:', error);
2024-04-17 16:32:38 +01:00
this.hideLoading();
2024-04-15 17:20:02 +01:00
});
} else {
// Gary flow.
this.el.defaultSubmitButton.click();
}
2024-02-09 17:49:45 +00:00
}
2024-04-17 16:32:38 +01:00
showLoading() {
const submitContainerSelector = '.woocommerce-checkout-payment';
jQuery('form.woocommerce-checkout').append('<div class="blockUI blockOverlay" style="z-index: 1000; border: medium; margin: 0px; padding: 0px; width: 100%; height: 100%; top: 0px; left: 0px; background: rgb(255, 255, 255); opacity: 0.6; cursor: default; position: absolute;"></div>');
disable(submitContainerSelector);
}
hideLoading() {
const submitContainerSelector = '.woocommerce-checkout-payment';
jQuery('form.woocommerce-checkout .blockOverlay').remove();
enable(submitContainerSelector);
}
useEmailWidget() {
return this.axoConfig?.widgets?.email === 'use_widget';
}
2024-05-01 14:54:59 +02:00
remove_keys_with_empty_string = (obj) => {
for(let key of Object.keys(obj)){
if (obj[key] === ''){
delete obj[key];
}
else if (typeof obj[key] === 'object'){
obj[key] = this.remove_keys_with_empty_string(obj[key]);
if (Object.keys(obj[key]).length === 0 ) delete obj[key];
}
}
return Array.isArray(obj) ? obj.filter(val => val) : obj;
}
2024-02-09 17:49:45 +00:00
}
export default AxoManager;