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-02-09 17:49:45 +00:00
|
|
|
|
|
|
|
class AxoManager {
|
|
|
|
|
2024-02-12 18:06:48 +00:00
|
|
|
constructor(jQuery) {
|
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;
|
|
|
|
|
|
|
|
this.elements = {
|
|
|
|
gatewayRadioButton: {
|
|
|
|
selector: '#payment_method_ppcp-axo-gateway',
|
|
|
|
},
|
|
|
|
defaultSubmitButton: {
|
|
|
|
selector: '#place_order',
|
|
|
|
},
|
|
|
|
paymentContainer: {
|
|
|
|
id: 'axo-payment-container',
|
|
|
|
selector: '#axo-payment-container',
|
|
|
|
className: 'axo-payment-container'
|
|
|
|
},
|
|
|
|
watermarkContainer: {
|
|
|
|
id: 'axo-watermark-container',
|
|
|
|
selector: '#axo-watermark-container',
|
|
|
|
className: 'axo-watermark-container'
|
|
|
|
},
|
|
|
|
submitButtonContainer: {
|
|
|
|
selector: '#axo-submit-button-container'
|
|
|
|
},
|
|
|
|
fieldBillingEmail: {
|
|
|
|
selector: '#billing_email_field'
|
|
|
|
},
|
|
|
|
}
|
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-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-02-12 18:06:48 +00:00
|
|
|
this.$(document).on('click', this.elements.submitButtonContainer.selector + ' button', () => {
|
|
|
|
this.onClickSubmitButton();
|
2024-02-09 17:49:45 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-12 18:06:48 +00:00
|
|
|
showAxo() {
|
|
|
|
this.moveEmail();
|
|
|
|
this.init();
|
|
|
|
|
|
|
|
show(this.elements.watermarkContainer.selector);
|
|
|
|
show(this.elements.paymentContainer.selector);
|
|
|
|
show(this.elements.submitButtonContainer.selector);
|
|
|
|
hide(this.elements.defaultSubmitButton.selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
hideAxo() {
|
|
|
|
hide(this.elements.watermarkContainer.selector);
|
|
|
|
hide(this.elements.paymentContainer.selector);
|
|
|
|
hide(this.elements.submitButtonContainer.selector);
|
|
|
|
show(this.elements.defaultSubmitButton.selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
moveEmail() {
|
|
|
|
// Move email row to first place.
|
|
|
|
let emailRow = document.querySelector(this.elements.fieldBillingEmail.selector);
|
|
|
|
emailRow.parentNode.prepend(emailRow);
|
|
|
|
emailRow.querySelector('input').focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
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', `
|
|
|
|
<div class="${this.elements.paymentContainer.className}" id="${this.elements.paymentContainer.id}"></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() {
|
|
|
|
this.emailInput = document.querySelector(this.elements.fieldBillingEmail.selector + ' input');
|
|
|
|
this.emailInput.addEventListener('change', async ()=> {
|
|
|
|
this.onChangeEmail();
|
2024-02-09 17:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.emailInput.value) {
|
2024-02-12 18:06:48 +00:00
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { customerContextId } = await this.fastlane.identity.lookupCustomerByEmail(this.emailInput.value);
|
|
|
|
|
|
|
|
if (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-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-12 18:06:48 +00:00
|
|
|
this.connectCardComponent = await this.fastlane
|
|
|
|
.FastlaneCardComponent(MockData.cardComponent())
|
|
|
|
.render(this.elements.paymentContainer.selector);
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 17:49:45 +00:00
|
|
|
|
2024-02-12 18:06:48 +00:00
|
|
|
onClickSubmitButton() {
|
|
|
|
try {
|
|
|
|
this.connectCardComponent.tokenize(MockData.cardComponentTokenize()).then((response) => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
// fetch('submit.php', {
|
|
|
|
// method: 'POST',
|
|
|
|
// headers: {
|
|
|
|
// 'Content-Type': 'application/json',
|
|
|
|
// },
|
|
|
|
// body: JSON.stringify({
|
|
|
|
// nonce: nonce
|
|
|
|
// }),
|
|
|
|
// })
|
|
|
|
// .then(response => {
|
|
|
|
// if (!response.ok) {
|
|
|
|
// throw new Error('Network response was not ok');
|
|
|
|
// }
|
|
|
|
// return response.json();
|
|
|
|
// })
|
|
|
|
// .then(data => {
|
|
|
|
// log('Submit response', data);
|
|
|
|
// log(JSON.stringify(data));
|
|
|
|
// })
|
|
|
|
// .catch(error => {
|
|
|
|
// console.error('There has been a problem with your fetch operation:', error);
|
|
|
|
// });
|
2024-02-09 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AxoManager;
|