2021-01-20 16:19:01 +01: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://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CheckoutCreditCard {
|
|
|
|
constructor() {
|
|
|
|
this.tokens = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
mountFrames() {
|
|
|
|
console.log('Mount checkout frames..');
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePaymentUsingToken(e) {
|
|
|
|
document.getElementById('checkout--container').classList.add('hidden');
|
|
|
|
document.getElementById('pay-now-with-token--container').classList.remove('hidden');
|
2021-01-26 13:55:32 +01:00
|
|
|
document.getElementById('save-card--container').style.display = 'none';
|
2021-01-20 16:19:01 +01:00
|
|
|
|
|
|
|
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');
|
2021-01-26 13:55:32 +01:00
|
|
|
document.getElementById('save-card--container').style.display = 'grid';
|
2021-01-20 16:19:01 +01: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();
|
|
|
|
});
|
|
|
|
|
2021-01-27 12:57:05 +01:00
|
|
|
Frames.addEventHandler(Frames.Events.CARD_TOKENIZATION_FAILED, function (event) {
|
|
|
|
pay.button.disabled = false;
|
|
|
|
});
|
|
|
|
|
2021-01-20 16:19:01 +01:00
|
|
|
Frames.addEventHandler(Frames.Events.CARD_TOKENIZED, function (event) {
|
2021-01-27 12:57:05 +01:00
|
|
|
payButton.disabled = true;
|
|
|
|
|
2021-01-20 16:19:01 +01:00
|
|
|
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();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new CheckoutCreditCard().handle();
|