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-02-09 17:49:45 +00:00
|
|
|
|
|
|
|
class AxoManager {
|
|
|
|
|
2024-02-15 17:58:56 +00:00
|
|
|
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);
|
|
|
|
|
2024-04-10 15:51:19 +01:00
|
|
|
document.axoDebugSetStatus = (key, value) => {
|
2024-03-14 10:54:15 +00:00
|
|
|
this.setStatus(key, value);
|
|
|
|
}
|
2024-04-10 15:51:19 +01:00
|
|
|
|
|
|
|
document.axoDebugObject = (key, value) => {
|
|
|
|
console.log(this);
|
|
|
|
}
|
2024-03-08 14:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
registerEventHandlers() {
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
console.log('selectedAddress', selectedAddress);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
console.log('card response', response);
|
|
|
|
|
|
|
|
if (response.selectionChanged) {
|
2024-04-08 11:31:12 +01:00
|
|
|
|
|
|
|
console.log('response.selectedCard.paymentToken', response.selectedCard.paymentToken);
|
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
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-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-02-15 17:58:56 +00:00
|
|
|
|
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-02-15 17:58:56 +00:00
|
|
|
|
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-02-15 17:58:56 +00:00
|
|
|
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}">
|
2024-02-15 17:58:56 +00:00
|
|
|
--- 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);
|
2024-02-15 17:58:56 +00:00
|
|
|
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.insertDomElements();
|
|
|
|
this.renderWatermark();
|
|
|
|
this.watchEmail();
|
2024-02-09 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 18:06:48 +00:00
|
|
|
async connect() {
|
|
|
|
window.localStorage.setItem('axoEnv', 'sandbox'); // TODO: check 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
|
|
|
insertDomElements() {
|
2024-03-08 17:41:21 +00:00
|
|
|
this.emailInput = document.querySelector(this.el.fieldBillingEmail.selector + ' input');
|
2024-02-12 18:06:48 +00:00
|
|
|
this.emailInput.insertAdjacentHTML('afterend', `
|
2024-03-08 17:41:21 +00:00
|
|
|
<div class="${this.el.watermarkContainer.className}" id="${this.el.watermarkContainer.id}"></div>
|
2024-02-09 17:49:45 +00:00
|
|
|
`);
|
|
|
|
|
2024-02-12 18:06:48 +00:00
|
|
|
const gatewayPaymentContainer = document.querySelector('.payment_method_ppcp-axo-gateway');
|
|
|
|
gatewayPaymentContainer.insertAdjacentHTML('beforeend', `
|
2024-03-14 10:54:15 +00:00
|
|
|
<div id="${this.el.paymentContainer.id}" class="${this.el.paymentContainer.className} hidden">
|
|
|
|
<div id="${this.el.paymentContainer.id}-form" class="${this.el.paymentContainer.className}-form"></div>
|
|
|
|
<div id="${this.el.paymentContainer.id}-details" class="${this.el.paymentContainer.className}-details"></div>
|
|
|
|
</div>
|
2024-02-09 17:49:45 +00:00
|
|
|
`);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
renderWatermark() {
|
2024-02-09 17:49:45 +00:00
|
|
|
this.fastlane.FastlaneWatermarkComponent({
|
|
|
|
includeAdditionalInfo: true
|
2024-03-08 17:41:21 +00: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
|
|
|
|
2024-02-15 17:58:56 +00:00
|
|
|
if (this.useEmailWidget()) {
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
this.emailInput = document.querySelector(this.el.fieldBillingEmail.selector + ' input');
|
2024-02-15 17:58:56 +00:00
|
|
|
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-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
|
|
|
|
});
|
2024-03-08 14:39:50 +00:00
|
|
|
this.setCard(authResponse.profileData.card);
|
|
|
|
|
2024-04-08 11:31:12 +01:00
|
|
|
console.log('authResponse', authResponse);
|
|
|
|
|
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-08 11:31:12 +01:00
|
|
|
console.log('this.cardComponentData()', this.cardComponentData());
|
|
|
|
|
2024-02-19 18:17:47 +00:00
|
|
|
this.cardComponent = await this.fastlane
|
2024-04-08 11:31:12 +01:00
|
|
|
.FastlaneCardComponent(
|
|
|
|
this.cardComponentData()
|
|
|
|
)
|
2024-03-14 10:54:15 +00: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-08 11:31:12 +01:00
|
|
|
if (this.data.card) { // Ryan flow
|
|
|
|
log('Ryan flow.');
|
2024-04-10 15:51:19 +01:00
|
|
|
console.log('this.data.card', this.data.card);
|
|
|
|
this.submit(this.data.card.id);
|
2024-04-08 11:31:12 +01:00
|
|
|
|
|
|
|
} else { // Gary flow
|
|
|
|
log('Gary flow.');
|
|
|
|
console.log('this.tokenizeData()', this.tokenizeData());
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.cardComponent.tokenize(
|
|
|
|
this.tokenizeData()
|
|
|
|
).then((response) => {
|
|
|
|
this.submit(response.nonce);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log('Error tokenizing.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cardComponentData() {
|
|
|
|
return {
|
|
|
|
fields: {
|
|
|
|
phoneNumber: {
|
|
|
|
prefill: this.billingView.inputValue('phone')
|
|
|
|
},
|
|
|
|
cardholderName: {} // optionally pass this to show the card holder name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-12 18:06:48 +00:00
|
|
|
submit(nonce) {
|
|
|
|
// Send the nonce and previously captured device data to server to complete checkout
|
|
|
|
log('nonce: ' + nonce);
|
|
|
|
alert('nonce: ' + nonce);
|
|
|
|
|
2024-02-19 18:17:47 +00:00
|
|
|
// Submit form.
|
2024-04-08 11:31:12 +01:00
|
|
|
if (!this.el.axoNonceInput.get()) {
|
|
|
|
this.$('.woocommerce-checkout').append(`<input type="hidden" id="${this.el.axoNonceInput.id}" name="axo_nonce" value="" />`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.el.axoNonceInput.get().value = nonce;
|
|
|
|
|
|
|
|
this.el.defaultSubmitButton.click();
|
2024-02-09 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 17:58:56 +00:00
|
|
|
useEmailWidget() {
|
|
|
|
return this.axoConfig?.widgets?.email === 'use_widget';
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default AxoManager;
|