1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/resources/js/clients/payments/authorize-credit-card-payment.js

146 lines
5.0 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://opensource.org/licenses/AAL
*/
class AuthorizeAuthorizeCard {
constructor(publicKey, loginId) {
this.publicKey = publicKey;
this.loginId = loginId;
this.cardHolderName = document.getElementById("cardholder_name");
}
handleAuthorization = () => {
2020-06-17 02:55:56 +02:00
var myCard = $('#my-card');
2020-10-09 12:43:26 +02:00
var authData = {};
authData.clientKey = this.publicKey;
authData.apiLoginID = this.loginId;
2020-10-09 12:43:26 +02:00
var cardData = {};
2020-11-20 23:22:57 +01:00
cardData.cardNumber = myCard.CardJs('cardNumber').replace(/[^\d]/g, '');
cardData.month = myCard.CardJs('expiryMonth').replace(/[^\d]/g, '');
cardData.year = myCard.CardJs('expiryYear').replace(/[^\d]/g, '');
cardData.cardCode = document.getElementById("cvv").value.replace(/[^\d]/g, '');
2020-10-09 12:43:26 +02:00
var secureData = {};
secureData.authData = authData;
secureData.cardData = cardData;
// If using banking information instead of card information,
// send the bankData object instead of the cardData object.
//
// secureData.bankData = bankData;
2021-01-25 16:12:21 +01:00
let payNowButton = document.getElementById('pay-now');
if (payNowButton) {
document.getElementById('pay-now').disabled = true;
document.querySelector('#pay-now > svg').classList.remove('hidden');
document.querySelector('#pay-now > span').classList.add('hidden');
}
2020-10-09 12:43:26 +02:00
Accept.dispatchData(secureData, this.responseHandler);
2020-06-17 03:26:58 +02:00
return false;
}
handlePayNowAction(token_hashed_id) {
2021-01-25 16:12:21 +01:00
document.getElementById('pay-now').disabled = true;
document.querySelector('#pay-now > svg').classList.remove('hidden');
document.querySelector('#pay-now > span').classList.add('hidden');
document.getElementById("token").value = token_hashed_id;
document.getElementById("server_response").submit();
}
2020-10-09 12:43:26 +02:00
responseHandler = (response) => {
if (response.messages.resultCode === "Error") {
var i = 0;
2020-06-17 02:55:56 +02:00
var $errors = $('#errors'); // get the reference of the div
2020-10-09 12:43:26 +02:00
$errors.show().html("<p>" + response.messages.message[i].code + ": " + response.messages.message[i].text + "</p>");
2021-01-26 14:31:52 +01:00
document.getElementById('pay-now').disabled = false;
document.querySelector('#pay-now > svg').classList.add('hidden');
document.querySelector('#pay-now > span').classList.remove('hidden');
2020-10-09 12:43:26 +02:00
} else if (response.messages.resultCode === "Ok") {
document.getElementById("dataDescriptor").value = response.opaqueData.dataDescriptor;
document.getElementById("dataValue").value = response.opaqueData.dataValue;
2021-01-25 16:12:21 +01:00
let storeCard = document.querySelector('input[name=token-billing-checkbox]:checked');
2021-01-13 15:36:28 +01:00
2021-01-25 16:12:21 +01:00
if (storeCard) {
document.getElementById("store_card").value = storeCard.value;
2021-01-13 15:36:28 +01:00
}
document.getElementById("server_response").submit();
2020-10-09 12:43:26 +02:00
}
2020-10-09 12:43:26 +02:00
return false;
}
handle = () => {
2021-01-25 16:12:21 +01:00
Array
.from(document.getElementsByClassName('toggle-payment-with-token'))
.forEach((element) => element.addEventListener('click', (e) => {
document
.getElementById('save-card--container').style.display = 'none';
document
.getElementById('authorize--credit-card-container').style.display = 'none';
document
.getElementById('token').value = e.target.dataset.token;
}));
let payWithCreditCardToggle = document.getElementById('toggle-payment-with-credit-card');
if (payWithCreditCardToggle) {
payWithCreditCardToggle
.addEventListener('click', () => {
document
.getElementById('save-card--container').style.display = 'grid';
document
.getElementById('authorize--credit-card-container').style.display = 'flex';
document
.getElementById('token').value = null;
});
}
let payNowButton = document.getElementById('pay-now');
2021-01-25 16:12:21 +01:00
if (payNowButton) {
payNowButton
.addEventListener('click', (e) => {
let token = document.getElementById('token');
2021-01-25 16:12:21 +01:00
token.value
? this.handlePayNowAction(token.value)
: this.handleAuthorization();
});
}
return this;
}
}
const publicKey = document.querySelector(
'meta[name="authorize-public-key"]'
).content;
const loginId = document.querySelector(
'meta[name="authorize-login-id"]'
).content;
/** @handle */
2020-10-09 12:43:26 +02:00
new AuthorizeAuthorizeCard(publicKey, loginId).handle();