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

189 lines
5.8 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');
}
}
async completePaymentWithoutToken(e) {
document.getElementById('errors').hidden = true;
e.target.parentElement.disabled = true;
let result = await this.card.tokenize();
2021-10-03 05:50:01 +02:00
/* SCA */
2021-10-20 16:24:59 +02:00
let verificationToken;
2021-10-03 06:01:49 +02:00
2021-10-20 16:24:59 +02:00
try {
const verificationDetails = {
amount: document.querySelector('meta[name=amount]').content,
billingContact: JSON.parse(
document.querySelector('meta[name=square_contact]').content
),
currencyCode: document.querySelector('meta[name=currencyCode]')
.content,
intent: 'CHARGE',
};
2021-10-03 06:26:58 +02:00
const verificationResults = await this.payments.verifyBuyer(
2021-10-20 16:24:59 +02:00
result.token,
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;
2021-10-20 16:24:59 +02:00
} catch (typeError) {
2021-10-20 16:26:54 +02:00
e.target.parentElement.disabled = true
2021-10-03 06:26:58 +02:00
}
2021-10-20 16:24:59 +02:00
document.querySelector(
'input[name="verificationToken"]'
).value = verificationToken;
2021-10-03 05:50:01 +02:00
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 05:50:01 +02:00
const verificationDetails = {
2021-10-20 16:24:59 +02:00
amount: document.querySelector('meta[name=amount]').content,
billingContact: document.querySelector('meta[name=square_contact]')
.content,
currencyCode: document.querySelector('meta[name=currencyCode]')
.content,
intent: 'CHARGE',
2021-10-03 05:50:01 +02:00
};
2021-10-03 06:13:56 +02:00
const verificationResults = await this.payments.verifyBuyer(
2021-10-20 16:24:59 +02:00
token,
verificationDetails
2021-10-03 05:50:01 +02:00
);
2021-10-03 05:57:07 +02:00
2021-10-03 05:50:01 +02:00
return verificationResults.token;
}
2021-08-18 17:52:32 +02:00
async handle() {
document.getElementById('payment-list').classList.add('hidden');
await this.init().then(() => {
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', async (element) => {
2021-08-18 17:52:32 +02:00
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'
// );
2021-08-18 17:52:32 +02:00
// if (!toggleWithToken) {
document.getElementById('loader').classList.add('hidden');
document.getElementById('payment-list').classList.remove('hidden');
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();