2024-03-08 17:41:21 +00:00
|
|
|
import Fastlane from "./Connection/Fastlane";
|
2024-02-12 18:06:48 +00:00
|
|
|
import MockData from "./Helper/MockData";
|
|
|
|
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.isConnectProfile = false;
|
|
|
|
this.hideGatewaySelection = false;
|
|
|
|
|
|
|
|
this.data = {
|
|
|
|
billing: null,
|
|
|
|
shipping: null,
|
|
|
|
card: null,
|
|
|
|
}
|
|
|
|
|
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-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-08 17:41:21 +00:00
|
|
|
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);
|
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) {
|
|
|
|
this.showAxo();
|
2024-02-09 17:49:45 +00:00
|
|
|
} else {
|
2024-02-12 18:06:48 +00:00
|
|
|
this.hideAxo();
|
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-08 17:41:21 +00:00
|
|
|
this.$(document).on('click', this.el.submitButton.selector, () => {
|
2024-02-12 18:06:48 +00:00
|
|
|
this.onClickSubmitButton();
|
2024-02-09 17:49:45 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
// 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);
|
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
//this.shippingView.deactivate();
|
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-08 17:41:21 +00:00
|
|
|
this.shippingView.activate();
|
2024-03-08 14:39:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let checkbox = document.querySelector('#ship-to-different-address-checkbox');
|
|
|
|
if (checkbox && !checkbox.checked) {
|
|
|
|
jQuery(checkbox).trigger('click');
|
|
|
|
}
|
2024-03-08 17:41:21 +00:00
|
|
|
this.shippingView.deactivate();
|
2024-03-08 14:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$(document).on('click', '*[data-ppcp-axo-save-shipping-address]', async () => {
|
2024-03-08 17:41:21 +00:00
|
|
|
this.shippingView.activate();
|
2024-03-08 14:39:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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 {
|
2024-03-08 17:41:21 +00:00
|
|
|
this.billingView.deactivate();
|
2024-03-08 14:39:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$(document).on('click', '*[data-ppcp-axo-save-billing-address]', async () => {
|
2024-03-08 17:41:21 +00:00
|
|
|
this.billingView.activate();
|
2024-03-08 14:39:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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();
|
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-02-12 18:06:48 +00:00
|
|
|
showAxo() {
|
2024-03-08 14:39:50 +00:00
|
|
|
this.initPlacements();
|
|
|
|
this.initFastlane();
|
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
this.shippingView.activate();
|
|
|
|
this.billingView.activate();
|
2024-02-12 18:06:48 +00:00
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
this.el.emailWidgetContainer.show();
|
|
|
|
this.el.watermarkContainer.show();
|
|
|
|
this.el.paymentContainer.show();
|
|
|
|
this.el.submitButtonContainer.show();
|
|
|
|
this.el.defaultSubmitButton.hide();
|
2024-02-15 17:58:56 +00:00
|
|
|
|
|
|
|
if (this.useEmailWidget()) {
|
2024-03-08 17:41:21 +00:00
|
|
|
this.el.fieldBillingEmail.hide();
|
2024-02-15 17:58:56 +00:00
|
|
|
}
|
2024-02-12 18:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hideAxo() {
|
2024-03-08 17:41:21 +00:00
|
|
|
this.shippingView.deactivate();
|
|
|
|
this.billingView.deactivate();
|
2024-03-08 14:39:50 +00:00
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
this.el.emailWidgetContainer.hide();
|
|
|
|
this.el.watermarkContainer.hide();
|
|
|
|
this.el.paymentContainer.hide();
|
|
|
|
this.el.submitButtonContainer.hide();
|
|
|
|
this.el.defaultSubmitButton.show();
|
2024-02-15 17:58:56 +00:00
|
|
|
|
|
|
|
if (this.useEmailWidget()) {
|
2024-03-08 17:41:21 +00:00
|
|
|
this.el.fieldBillingEmail.show();
|
2024-02-15 17:58:56 +00:00
|
|
|
}
|
2024-02-12 18:06:48 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
initPlacements() {
|
2024-03-08 17:41:21 +00:00
|
|
|
let emailRow = document.querySelector(this.el.fieldBillingEmail.selector);
|
2024-02-15 17:58:56 +00:00
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
const bc = this.el.billingAddressContainer;
|
|
|
|
const sc = this.el.shippingAddressContainer;
|
|
|
|
const ec = this.el.emailWidgetContainer;
|
2024-03-08 14:39:50 +00:00
|
|
|
|
|
|
|
if (!document.querySelector(bc.selector)) {
|
|
|
|
document.querySelector(bc.anchorSelector).insertAdjacentHTML('beforeend', `
|
|
|
|
<div id="${bc.id}" class="${bc.className}"></div>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!document.querySelector(sc.selector)) {
|
|
|
|
document.querySelector(sc.anchorSelector).insertAdjacentHTML('afterbegin', `
|
|
|
|
<div id="${sc.id}" class="${sc.className}"></div>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2024-02-15 17:58:56 +00:00
|
|
|
if (this.useEmailWidget()) {
|
|
|
|
|
|
|
|
// Display email widget.
|
2024-03-08 14:39:50 +00:00
|
|
|
if (!document.querySelector(ec.selector)) {
|
2024-02-15 17:58:56 +00:00
|
|
|
emailRow.parentNode.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 {
|
|
|
|
|
|
|
|
// Move email row to first place.
|
|
|
|
emailRow.parentNode.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.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-08 17:41:21 +00:00
|
|
|
<div id="${this.el.paymentContainer.id}" class="${this.el.paymentContainer.className} hidden"></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 () {
|
|
|
|
log('Email changed: ' + this.emailInput.value);
|
2024-02-09 17:49:45 +00:00
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
this.isConnectProfile = false;
|
|
|
|
this.hideGatewaySelection = false;
|
|
|
|
|
2024-02-09 17:49:45 +00:00
|
|
|
if (!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-08 14:39:50 +00:00
|
|
|
const lookupResponse = await this.fastlane.identity.lookupCustomerByEmail(this.emailInput.value);
|
2024-02-09 17:49:45 +00:00
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
this.el.paymentContainer.show();
|
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
|
|
|
|
|
|
|
// TODO : enter hideOtherGateways mode
|
|
|
|
|
|
|
|
const authResponse = await this.fastlane.identity.triggerAuthenticationFlow(lookupResponse.customerContextId);
|
|
|
|
|
|
|
|
log('AuthResponse', authResponse);
|
|
|
|
|
|
|
|
if (authResponse.authenticationState === 'succeeded') {
|
|
|
|
log(JSON.stringify(authResponse));
|
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
// document.querySelector(this.el.paymentContainer.selector).innerHTML =
|
2024-03-08 14:39:50 +00:00
|
|
|
// '<a href="javascript:void(0)" data-ppcp-axo-change-card>Change card</a>';
|
|
|
|
|
|
|
|
// Add addresses
|
|
|
|
this.setShipping(authResponse.profileData.shippingAddress);
|
|
|
|
// TODO : set billing
|
|
|
|
this.setCard(authResponse.profileData.card);
|
|
|
|
|
|
|
|
this.isConnectProfile = true;
|
|
|
|
this.hideGatewaySelection = true;
|
|
|
|
this.$('.wc_payment_methods label').hide();
|
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
this.shippingView.activate();
|
|
|
|
this.billingView.activate();
|
|
|
|
this.cardView.activate();
|
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-02-19 18:17:47 +00:00
|
|
|
this.cardComponent = await this.fastlane
|
2024-02-12 18:06:48 +00:00
|
|
|
.FastlaneCardComponent(MockData.cardComponent())
|
2024-03-08 17:41:21 +00:00
|
|
|
.render(this.el.paymentContainer.selector);
|
2024-02-12 18:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 17:49:45 +00:00
|
|
|
|
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() {
|
|
|
|
try {
|
2024-02-19 18:17:47 +00:00
|
|
|
this.cardComponent.tokenize(MockData.cardComponentTokenize()).then((response) => {
|
2024-02-12 18:06:48 +00:00
|
|
|
this.submit(response.nonce);
|
2024-02-09 17:49:45 +00:00
|
|
|
});
|
2024-02-12 18:06:48 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('Error tokenizing.');
|
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-03-08 17:41:21 +00:00
|
|
|
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;
|