1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/resources/js/clients/payments/square-credit-card.js

203 lines
6.0 KiB
JavaScript
Raw Normal View History

2021-08-18 17:52:32 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
class SquareCreditCard {
constructor() {
this.appId = document.querySelector('meta[name=square-appId]').content;
this.locationId = document.querySelector(
'meta[name=square-locationId]'
).content;
this.isLoaded = false;
}
async init() {
this.payments = Square.payments(this.appId, this.locationId);
this.card = await this.payments.card();
await this.card.attach('#card-container');
this.isLoaded = true;
let iframeContainer = document.querySelector(
'.sq-card-iframe-container'
);
if (iframeContainer) {
iframeContainer.setAttribute('style', '150px !important');
}
let toggleWithToken = document.querySelector(
'.toggle-payment-with-token'
);
if (toggleWithToken) {
document.getElementById('card-container').classList.add('hidden');
}
}
2021-10-03 06:41:11 +02:00
// ,
// function(err,verification) {
// if (err == null) {
// console.log("no error");
// console.log(verification);
// verificationToken = verificationResults.token;
// }
// console.log(err);
// die("verify buyer");
// }
2021-10-03 06:22:23 +02:00
2021-08-18 17:52:32 +02:00
async completePaymentWithoutToken(e) {
document.getElementById('errors').hidden = true;
e.target.parentElement.disabled = true;
let result = await this.card.tokenize();
2021-10-03 05:57:07 +02:00
console.log("square token = " + result.token);
2021-10-03 06:01:49 +02:00
2021-10-03 05:50:01 +02:00
/* SCA */
let verificationToken;
2021-10-03 06:01:49 +02:00
2021-10-03 06:26:58 +02:00
try {
const verificationDetails = {
amount: document.querySelector('meta[name=amount]').content,
2021-10-03 06:43:11 +02:00
billingContact: JSON.parse(document.querySelector('meta[name=square_contact]').content),
2021-10-03 06:26:58 +02:00
currencyCode: document.querySelector('meta[name=currencyCode]').content,
intent: 'CHARGE'
};
console.log(verificationDetails);
const verificationResults = await this.payments.verifyBuyer(
result.token,
2021-10-03 06:41:11 +02:00
verificationDetails
2021-10-03 06:26:58 +02:00
);
2021-10-03 06:01:49 +02:00
2021-10-03 06:26:58 +02:00
verificationToken = verificationResults.token;
}
catch(typeError){
console.log(typeError);
}
2021-10-03 06:01:49 +02:00
2021-10-03 05:50:01 +02:00
console.debug('Verification Token:', verificationToken);
document.querySelector('input[name="verificationToken"]').value =
verificationToken;
2021-08-18 17:52:32 +02:00
if (result.status === 'OK') {
document.getElementById('sourceId').value = result.token;
let tokenBillingCheckbox = document.querySelector(
'input[name="token-billing-checkbox"]:checked'
);
if (tokenBillingCheckbox) {
document.querySelector('input[name="store_card"]').value =
tokenBillingCheckbox.value;
}
return document.getElementById('server_response').submit();
}
document.getElementById('errors').textContent =
result.errors[0].message;
document.getElementById('errors').hidden = false;
e.target.parentElement.disabled = false;
}
async completePaymentUsingToken(e) {
e.target.parentElement.disabled = true;
return document.getElementById('server_response').submit();
}
2021-10-03 05:50:01 +02:00
/* SCA */
2021-10-03 06:13:56 +02:00
async verifyBuyer(token) {
2021-10-03 06:16:28 +02:00
console.log("in verify buyer");
2021-10-03 06:22:23 +02:00
2021-10-03 05:50:01 +02:00
const verificationDetails = {
amount: document.querySelector('meta[name=amount]').content,
2021-10-03 06:13:56 +02:00
billingContact: document.querySelector('meta[name=square_contact]').content,
2021-10-03 05:50:01 +02:00
currencyCode: document.querySelector('meta[name=currencyCode]').content,
intent: 'CHARGE'
};
2021-10-03 06:13:56 +02:00
const verificationResults = await this.payments.verifyBuyer(
2021-10-03 05:50:01 +02:00
token,
verificationDetails
);
2021-10-03 05:57:07 +02:00
console.log(" verification toke = " + verificationResults.token);
2021-10-03 05:50:01 +02:00
return verificationResults.token;
}
2021-08-18 17:52:32 +02:00
async handle() {
2021-08-19 13:34:51 +02:00
await this.init();
2021-08-19 11:05:06 +02:00
2021-08-18 17:52:32 +02:00
document
.getElementById('authorize-card')
?.addEventListener('click', (e) =>
this.completePaymentWithoutToken(e)
);
2021-08-19 11:05:06 +02:00
document.getElementById('pay-now')?.addEventListener('click', (e) => {
2021-08-18 17:52:32 +02:00
let tokenInput = document.querySelector('input[name=token]');
if (tokenInput.value) {
return this.completePaymentUsingToken(e);
}
return this.completePaymentWithoutToken(e);
});
Array.from(
document.getElementsByClassName('toggle-payment-with-token')
).forEach((element) =>
element.addEventListener('click', (element) => {
document
.getElementById('card-container')
.classList.add('hidden');
document.getElementById('save-card--container').style.display =
'none';
document.querySelector('input[name=token]').value =
element.target.dataset.token;
})
);
document
.getElementById('toggle-payment-with-credit-card')
2021-08-19 11:05:06 +02:00
?.addEventListener('click', async (element) => {
2021-08-18 17:52:32 +02:00
document
.getElementById('card-container')
.classList.remove('hidden');
document.getElementById('save-card--container').style.display =
'grid';
document.querySelector('input[name=token]').value = '';
});
let toggleWithToken = document.querySelector(
'.toggle-payment-with-token'
);
if (!toggleWithToken) {
2021-08-19 11:05:06 +02:00
document.getElementById('toggle-payment-with-credit-card')?.click();
2021-08-18 17:52:32 +02:00
}
}
}
new SquareCreditCard().handle();