1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/resources/js/clients/payments/checkout-credit-card.js

115 lines
3.5 KiB
JavaScript
Raw Normal View History

/**
* 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
*/
import { wait } from '../wait';
class CheckoutCreditCard {
constructor() {
this.tokens = [];
}
handlePaymentUsingToken(e) {
document.getElementById('checkout--container').classList.add('hidden');
document
.getElementById('pay-now-with-token--container')
.classList.remove('hidden');
document.getElementById('save-card--container').style.display = 'none';
document.querySelector('input[name=token]').value =
e.target.dataset.token;
}
handlePaymentUsingCreditCard(e) {
document
.getElementById('checkout--container')
.classList.remove('hidden');
document
.getElementById('pay-now-with-token--container')
.classList.add('hidden');
document.getElementById('save-card--container').style.display = 'grid';
document.querySelector('input[name=token]').value = '';
2021-05-21 13:18:03 +02:00
const payButton = document.getElementById('pay-button');
const publicKey =
document.querySelector('meta[name="public-key"]').content ?? '';
const form = document.getElementById('payment-form');
Frames.init(publicKey);
Frames.addEventHandler(
Frames.Events.CARD_VALIDATION_CHANGED,
function (event) {
payButton.disabled = !Frames.isCardValid();
}
);
Frames.addEventHandler(
Frames.Events.CARD_TOKENIZATION_FAILED,
function (event) {
payButton.disabled = false;
}
);
Frames.addEventHandler(Frames.Events.CARD_TOKENIZED, function (event) {
payButton.disabled = true;
document.querySelector('input[name="gateway_response"]').value =
JSON.stringify(event);
document.querySelector('input[name="store_card"]').value =
document.querySelector(
'input[name=token-billing-checkbox]:checked'
).value;
document.getElementById('server-response').submit();
});
form.addEventListener('submit', function (event) {
event.preventDefault();
2023-12-07 03:59:36 +01:00
payButton.disabled = true;
Frames.submitCard();
});
}
completePaymentUsingToken(e) {
let btn = document.getElementById('pay-now-with-token');
btn.disabled = true;
btn.querySelector('svg').classList.remove('hidden');
btn.querySelector('span').classList.add('hidden');
document.getElementById('server-response').submit();
}
handle() {
this.handlePaymentUsingCreditCard();
Array.from(
document.getElementsByClassName('toggle-payment-with-token')
).forEach((element) =>
element.addEventListener('click', this.handlePaymentUsingToken)
);
document
.getElementById('toggle-payment-with-credit-card')
.addEventListener('click', this.handlePaymentUsingCreditCard);
document
.getElementById('pay-now-with-token')
.addEventListener('click', this.completePaymentUsingToken);
}
}
wait('#checkout-credit-card-payment').then(() =>
new CheckoutCreditCard().handle()
);