2024-02-09 17:49:45 +00:00
|
|
|
import Fastlane from "./Entity/Fastlane";
|
2024-02-12 18:06:48 +00:00
|
|
|
import MockData from "./Helper/MockData";
|
|
|
|
import {log} from "./Helper/Debug";
|
|
|
|
import {hide, show} from '../../../ppcp-button/resources/js/modules/Helper/Hiding';
|
2024-03-08 14:39:50 +00:00
|
|
|
import FormFieldGroup from "./Helper/FormFieldGroup";
|
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-02-12 18:06:48 +00:00
|
|
|
this.elements = {
|
|
|
|
gatewayRadioButton: {
|
|
|
|
selector: '#payment_method_ppcp-axo-gateway',
|
|
|
|
},
|
|
|
|
defaultSubmitButton: {
|
|
|
|
selector: '#place_order',
|
|
|
|
},
|
|
|
|
paymentContainer: {
|
2024-02-15 17:58:56 +00:00
|
|
|
id: 'ppcp-axo-payment-container',
|
|
|
|
selector: '#ppcp-axo-payment-container',
|
|
|
|
className: 'ppcp-axo-payment-container'
|
2024-02-12 18:06:48 +00:00
|
|
|
},
|
|
|
|
watermarkContainer: {
|
2024-02-15 17:58:56 +00:00
|
|
|
id: 'ppcp-axo-watermark-container',
|
|
|
|
selector: '#ppcp-axo-watermark-container',
|
|
|
|
className: 'ppcp-axo-watermark-container'
|
2024-02-12 18:06:48 +00:00
|
|
|
},
|
2024-02-15 17:58:56 +00:00
|
|
|
emailWidgetContainer: {
|
|
|
|
id: 'ppcp-axo-email-widget',
|
|
|
|
selector: '#ppcp-axo-email-widget',
|
|
|
|
className: 'ppcp-axo-email-widget'
|
2024-02-12 18:06:48 +00:00
|
|
|
},
|
2024-03-08 14:39:50 +00:00
|
|
|
shippingAddressContainer: {
|
|
|
|
id: 'ppcp-axo-shipping-address-container',
|
|
|
|
selector: '#ppcp-axo-shipping-address-container',
|
|
|
|
className: 'ppcp-axo-shipping-address-container',
|
|
|
|
anchorSelector: '.woocommerce-shipping-fields'
|
|
|
|
},
|
|
|
|
billingAddressContainer: {
|
|
|
|
id: 'ppcp-axo-billing-address-container',
|
|
|
|
selector: '#ppcp-axo-billing-address-container',
|
|
|
|
className: 'ppcp-axo-billing-address-container',
|
|
|
|
anchorSelector: '.woocommerce-billing-fields__field-wrapper'
|
|
|
|
},
|
2024-02-12 18:06:48 +00:00
|
|
|
fieldBillingEmail: {
|
|
|
|
selector: '#billing_email_field'
|
|
|
|
},
|
2024-02-15 17:58:56 +00:00
|
|
|
submitButtonContainer: {
|
2024-03-08 14:39:50 +00:00
|
|
|
selector: '#ppcp-axo-submit-button-container',
|
|
|
|
buttonSelector: '#ppcp-axo-submit-button-container button'
|
2024-02-15 17:58:56 +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
|
|
|
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();
|
|
|
|
|
|
|
|
this.shippingFormFields = new FormFieldGroup({
|
|
|
|
baseSelector: '.woocommerce-checkout',
|
|
|
|
contentSelector: this.elements.shippingAddressContainer.selector,
|
|
|
|
template: (data) => {
|
|
|
|
const valueOfSelect = (selectSelector, key) => {
|
|
|
|
const selectElement = document.querySelector(selectSelector);
|
|
|
|
const option = selectElement.querySelector(`option[value="${key}"]`);
|
|
|
|
return option ? option.textContent : key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.isEditing()) {
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
|
|
|
<h3>Shipping details <a href="javascript:void(0)" data-ppcp-axo-save-shipping-address style="margin-left: 20px;">Save</a></h3>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
|
|
|
<h3>Shipping details <a href="javascript:void(0)" data-ppcp-axo-change-shipping-address style="margin-left: 20px;">Edit</a></h3>
|
|
|
|
<div>Please fill in your shipping details.</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
|
|
|
<h3>Shipping details <a href="javascript:void(0)" data-ppcp-axo-change-shipping-address style="margin-left: 20px;">Edit</a></h3>
|
|
|
|
<div>${data.value('company')}</div>
|
|
|
|
<div>${data.value('firstName')} ${data.value('lastName')}</div>
|
|
|
|
<div>${data.value('street1')}</div>
|
|
|
|
<div>${data.value('street2')}</div>
|
|
|
|
<div>${data.value('postCode')} ${data.value('city')}</div>
|
|
|
|
<div>${valueOfSelect('#shipping_state', data.value('stateCode'))}</div>
|
|
|
|
<div>${valueOfSelect('#shipping_country', data.value('countryCode'))}</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
firstName: {
|
|
|
|
'key': 'firstName',
|
|
|
|
'selector': '#shipping_first_name_field',
|
|
|
|
'valuePath': 'shipping.name.firstName',
|
|
|
|
},
|
|
|
|
lastName: {
|
|
|
|
'selector': '#shipping_last_name_field',
|
|
|
|
'valuePath': 'shipping.name.lastName',
|
|
|
|
},
|
|
|
|
street1: {
|
|
|
|
'selector': '#shipping_address_1_field',
|
|
|
|
'valuePath': 'shipping.address.addressLine1',
|
|
|
|
},
|
|
|
|
street2: {
|
|
|
|
'selector': '#shipping_address_2_field',
|
|
|
|
'valuePath': null
|
|
|
|
},
|
|
|
|
postCode: {
|
|
|
|
'selector': '#shipping_postcode_field',
|
|
|
|
'valuePath': 'shipping.address.postalCode',
|
|
|
|
},
|
|
|
|
city: {
|
|
|
|
'selector': '#shipping_city_field',
|
|
|
|
'valuePath': 'shipping.address.adminArea2',
|
|
|
|
},
|
|
|
|
stateCode: {
|
|
|
|
'selector': '#shipping_state_field',
|
|
|
|
'valuePath': 'shipping.address.adminArea1',
|
|
|
|
},
|
|
|
|
countryCode: {
|
|
|
|
'selector': '#shipping_country_field',
|
|
|
|
'valuePath': 'shipping.address.countryCode',
|
|
|
|
},
|
|
|
|
company: {
|
|
|
|
'selector': '#shipping_company_field',
|
|
|
|
'valuePath': null,
|
|
|
|
},
|
|
|
|
shipDifferentAddress: {
|
|
|
|
'selector': '#ship-to-different-address',
|
|
|
|
'valuePath': null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.billingFormFields = new FormFieldGroup({
|
|
|
|
baseSelector: '.woocommerce-checkout',
|
|
|
|
contentSelector: this.elements.billingAddressContainer.selector,
|
|
|
|
template: (data) => {
|
|
|
|
const valueOfSelect = (selectSelector, key) => {
|
|
|
|
const selectElement = document.querySelector(selectSelector);
|
|
|
|
const option = selectElement.querySelector(`option[value="${key}"]`);
|
|
|
|
return option ? option.textContent : key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.isEditing()) {
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
|
|
|
<h4><a href="javascript:void(0)" data-ppcp-axo-save-billing-address>Save</a></h4>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
|
|
|
<div>Please fill in your billing details.</div>
|
|
|
|
<h4><a href="javascript:void(0)" data-ppcp-axo-change-billing-address>Edit</a></h4>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
|
|
|
<h4>Billing address</h4>
|
|
|
|
<div>${data.value('company')}</div>
|
|
|
|
<div>${data.value('firstName')} ${data.value('lastName')}</div>
|
|
|
|
<div>${data.value('street1')}</div>
|
|
|
|
<div>${data.value('street2')}</div>
|
|
|
|
<div>${data.value('postCode')} ${data.value('city')}</div>
|
|
|
|
<div>${valueOfSelect('#billing_state', data.value('stateCode'))}</div>
|
|
|
|
<div>${valueOfSelect('#billing_country', data.value('countryCode'))}</div>
|
|
|
|
<div>${data.value('phone')}</div>
|
|
|
|
<h4><a href="javascript:void(0)" data-ppcp-axo-change-billing-address>Edit</a></h4>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
firstName: {
|
|
|
|
'selector': '#billing_first_name_field',
|
|
|
|
'valuePath': 'billing.name.firstName',
|
|
|
|
},
|
|
|
|
lastName: {
|
|
|
|
'selector': '#billing_last_name_field',
|
|
|
|
'valuePath': 'billing.name.lastName',
|
|
|
|
},
|
|
|
|
street1: {
|
|
|
|
'selector': '#billing_address_1_field',
|
|
|
|
'valuePath': 'billing.address.addressLine1',
|
|
|
|
},
|
|
|
|
street2: {
|
|
|
|
'selector': '#billing_address_2_field',
|
|
|
|
'valuePath': null
|
|
|
|
},
|
|
|
|
postCode: {
|
|
|
|
'selector': '#billing_postcode_field',
|
|
|
|
'valuePath': 'billing.address.postalCode',
|
|
|
|
},
|
|
|
|
city: {
|
|
|
|
'selector': '#billing_city_field',
|
|
|
|
'valuePath': 'billing.address.adminArea2',
|
|
|
|
},
|
|
|
|
stateCode: {
|
|
|
|
'selector': '#billing_state_field',
|
|
|
|
'valuePath': 'billing.address.adminArea1',
|
|
|
|
},
|
|
|
|
countryCode: {
|
|
|
|
'selector': '#billing_country_field',
|
|
|
|
'valuePath': 'billing.address.countryCode',
|
|
|
|
},
|
|
|
|
company: {
|
|
|
|
'selector': '#billing_company_field',
|
|
|
|
'valuePath': null,
|
|
|
|
},
|
|
|
|
phone: {
|
|
|
|
'selector': '#billing_phone_field',
|
|
|
|
'valuePath': null,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.cardFormFields = new FormFieldGroup({
|
|
|
|
baseSelector: '.ppcp-axo-payment-container',
|
|
|
|
contentSelector: this.elements.paymentContainer.selector,
|
|
|
|
template: (data) => {
|
|
|
|
const selectOtherPaymentMethod = () => {
|
|
|
|
if (!this.hideGatewaySelection) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return `<p style="margin-top: 40px; text-align: center;"><a href="javascript:void(0)" data-ppcp-axo-show-gateway-selection>Select other payment method</a></p>`;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px; text-align: center;">
|
|
|
|
<div>Please fill in your card details.</div>
|
|
|
|
<h4><a href="javascript:void(0)" data-ppcp-axo-change-card>Edit</a></h4>
|
|
|
|
${selectOtherPaymentMethod()}
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
|
|
|
<h3>Card Details <a href="javascript:void(0)" data-ppcp-axo-change-card>Edit</a></h3>
|
|
|
|
<div>${data.value('name')}</div>
|
|
|
|
<div>${data.value('brand')}</div>
|
|
|
|
<div>${data.value('lastDigits') ? '************' + data.value('lastDigits'): ''}</div>
|
|
|
|
<div>${data.value('expiry')}</div>
|
|
|
|
${selectOtherPaymentMethod()}
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
brand: {
|
|
|
|
'valuePath': 'card.paymentSource.card.brand',
|
|
|
|
},
|
|
|
|
expiry: {
|
|
|
|
'valuePath': 'card.paymentSource.card.expiry',
|
|
|
|
},
|
|
|
|
lastDigits: {
|
|
|
|
'valuePath': 'card.paymentSource.card.lastDigits',
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
'valuePath': 'card.paymentSource.card.name',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
registerEventHandlers() {
|
|
|
|
|
2024-02-12 18:06:48 +00:00
|
|
|
// Listen to Gateway Radio button changes.
|
|
|
|
this.$(document).on('change', this.elements.gatewayRadioButton.selector, (ev) => {
|
|
|
|
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.
|
|
|
|
this.$(document).on('click', this.elements.submitButtonContainer.buttonSelector, () => {
|
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);
|
|
|
|
|
|
|
|
//this.shippingFormFields.deactivate();
|
|
|
|
|
|
|
|
const { selectionChanged, selectedAddress } = await this.fastlane.profile.showShippingAddressSelector();
|
|
|
|
|
|
|
|
console.log('selectedAddress', selectedAddress);
|
|
|
|
|
|
|
|
if (selectionChanged) {
|
|
|
|
this.setShipping(selectedAddress);
|
|
|
|
this.shippingFormFields.activate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let checkbox = document.querySelector('#ship-to-different-address-checkbox');
|
|
|
|
if (checkbox && !checkbox.checked) {
|
|
|
|
jQuery(checkbox).trigger('click');
|
|
|
|
}
|
|
|
|
this.shippingFormFields.deactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$(document).on('click', '*[data-ppcp-axo-save-shipping-address]', async () => {
|
|
|
|
this.shippingFormFields.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.billingFormFields.deactivate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$(document).on('click', '*[data-ppcp-axo-save-billing-address]', async () => {
|
|
|
|
this.billingFormFields.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.cardFormFields.refresh();
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
this.shippingFormFields.activate();
|
|
|
|
this.billingFormFields.activate();
|
2024-02-12 18:06:48 +00:00
|
|
|
|
2024-02-15 17:58:56 +00:00
|
|
|
show(this.elements.emailWidgetContainer.selector);
|
2024-02-12 18:06:48 +00:00
|
|
|
show(this.elements.watermarkContainer.selector);
|
|
|
|
show(this.elements.paymentContainer.selector);
|
|
|
|
show(this.elements.submitButtonContainer.selector);
|
|
|
|
hide(this.elements.defaultSubmitButton.selector);
|
2024-02-15 17:58:56 +00:00
|
|
|
|
|
|
|
if (this.useEmailWidget()) {
|
|
|
|
hide(this.elements.fieldBillingEmail.selector);
|
|
|
|
}
|
2024-02-12 18:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hideAxo() {
|
2024-03-08 14:39:50 +00:00
|
|
|
this.shippingFormFields.deactivate();
|
|
|
|
this.billingFormFields.deactivate();
|
|
|
|
|
2024-02-15 17:58:56 +00:00
|
|
|
hide(this.elements.emailWidgetContainer.selector);
|
2024-02-12 18:06:48 +00:00
|
|
|
hide(this.elements.watermarkContainer.selector);
|
|
|
|
hide(this.elements.paymentContainer.selector);
|
|
|
|
hide(this.elements.submitButtonContainer.selector);
|
|
|
|
show(this.elements.defaultSubmitButton.selector);
|
2024-02-15 17:58:56 +00:00
|
|
|
|
|
|
|
if (this.useEmailWidget()) {
|
|
|
|
show(this.elements.fieldBillingEmail.selector);
|
|
|
|
}
|
2024-02-12 18:06:48 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
initPlacements() {
|
2024-02-12 18:06:48 +00:00
|
|
|
let emailRow = document.querySelector(this.elements.fieldBillingEmail.selector);
|
2024-02-15 17:58:56 +00:00
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
const bc = this.elements.billingAddressContainer;
|
|
|
|
const sc = this.elements.shippingAddressContainer;
|
|
|
|
const ec = this.elements.emailWidgetContainer;
|
|
|
|
|
|
|
|
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() {
|
|
|
|
this.emailInput = document.querySelector(this.elements.fieldBillingEmail.selector + ' input');
|
|
|
|
this.emailInput.insertAdjacentHTML('afterend', `
|
|
|
|
<div class="${this.elements.watermarkContainer.className}" id="${this.elements.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-02-15 17:58:56 +00:00
|
|
|
<div id="${this.elements.paymentContainer.id}" class="${this.elements.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() {
|
|
|
|
this.$(this.elements.gatewayRadioButton.selector).trigger('change');
|
|
|
|
}
|
|
|
|
|
|
|
|
renderWatermark() {
|
2024-02-09 17:49:45 +00:00
|
|
|
this.fastlane.FastlaneWatermarkComponent({
|
|
|
|
includeAdditionalInfo: true
|
2024-02-12 18:06:48 +00:00
|
|
|
}).render(this.elements.watermarkContainer.selector);
|
|
|
|
}
|
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 {
|
|
|
|
|
|
|
|
this.emailInput = document.querySelector(this.elements.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 () {
|
|
|
|
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 14:39:50 +00:00
|
|
|
show(this.elements.paymentContainer.selector);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
// document.querySelector(this.elements.paymentContainer.selector).innerHTML =
|
|
|
|
// '<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();
|
|
|
|
|
|
|
|
this.shippingFormFields.activate();
|
|
|
|
this.billingFormFields.activate();
|
|
|
|
this.cardFormFields.activate();
|
|
|
|
|
|
|
|
} 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())
|
|
|
|
.render(this.elements.paymentContainer.selector);
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 17:49:45 +00:00
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
setShipping(shipping) {
|
|
|
|
this.data.shipping = shipping;
|
|
|
|
this.shippingFormFields.setData(this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
setBilling(billing) {
|
|
|
|
this.data.billing = billing;
|
|
|
|
this.billingFormFields.setData(this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
setCard(card) {
|
|
|
|
this.data.card = card;
|
|
|
|
this.cardFormFields.setData(this.data);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
document.querySelector(this.elements.defaultSubmitButton.selector).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;
|