2020-06-09 05:54:09 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AuthorizeAuthorizeCard {
|
2020-06-09 13:53:23 +02:00
|
|
|
|
|
|
|
constructor(publicKey, loginId) {
|
|
|
|
this.publicKey = publicKey;
|
|
|
|
this.loginId = loginId;
|
2020-06-09 05:54:09 +02:00
|
|
|
this.cardHolderName = document.getElementById("cardholder_name");
|
|
|
|
this.cardButton = document.getElementById("card_button");
|
2020-06-16 18:09:43 +02:00
|
|
|
this.form = { valid: false };
|
2020-06-09 05:54:09 +02:00
|
|
|
|
2020-06-16 18:09:43 +02:00
|
|
|
this.translations = {
|
|
|
|
invalidCard: document.querySelector('meta[name="credit-card-invalid"]').content,
|
|
|
|
invalidMonth: document.querySelector('meta[name="month-invalid"]').content,
|
|
|
|
invalidYear: document.querySelector('meta[name="year-invalid"]').content,
|
|
|
|
}
|
2020-06-09 05:54:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleAuthorization() {
|
|
|
|
|
|
|
|
var authData = {};
|
2020-06-09 13:53:23 +02:00
|
|
|
authData.clientKey = this.publicKey;
|
|
|
|
authData.apiLoginID = this.loginId;
|
2020-06-09 05:54:09 +02:00
|
|
|
|
|
|
|
var cardData = {};
|
|
|
|
cardData.cardNumber = document.getElementById("card_number").value;
|
|
|
|
cardData.month = document.getElementById("expiration_month").value;
|
|
|
|
cardData.year = document.getElementById("expiration_year").value;
|
|
|
|
cardData.cardCode = document.getElementById("cvv").value;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-06-09 14:54:22 +02:00
|
|
|
Accept.dispatchData(secureData, this.responseHandler);
|
|
|
|
return false;
|
|
|
|
|
2020-06-09 05:54:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
responseHandler(response) {
|
2020-06-09 14:57:01 +02:00
|
|
|
|
2020-06-09 05:54:09 +02:00
|
|
|
if (response.messages.resultCode === "Error") {
|
|
|
|
var i = 0;
|
|
|
|
while (i < response.messages.message.length) {
|
|
|
|
console.log(
|
|
|
|
response.messages.message[i].code + ": " +
|
|
|
|
response.messages.message[i].text
|
|
|
|
);
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 14:54:22 +02:00
|
|
|
else if(response.messages.resultCode === "Ok"){
|
2020-06-09 14:57:01 +02:00
|
|
|
|
|
|
|
document.getElementById("dataDescriptor").value = response.opaqueData.dataDescriptor;
|
|
|
|
document.getElementById("dataValue").value = response.opaqueData.dataValue;
|
2020-06-09 14:54:22 +02:00
|
|
|
document.getElementById("server_response").submit();
|
2020-06-09 05:54:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 14:54:22 +02:00
|
|
|
return false;
|
2020-06-09 05:54:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 18:09:43 +02:00
|
|
|
handleFormValidation = () => {
|
|
|
|
document.getElementById("card_number").addEventListener('keyup', (e) => {
|
|
|
|
let errors = document.getElementById('card_number_errors');
|
|
|
|
if (valid.number(e.target.value).isValid) {
|
|
|
|
errors.hidden = true;
|
|
|
|
this.form.valid = true;
|
|
|
|
} else {
|
|
|
|
errors.textContent = this.translations.invalidCard;
|
|
|
|
errors.hidden = false;
|
|
|
|
this.form.valid = false;
|
|
|
|
}
|
|
|
|
});
|
2020-06-09 14:54:22 +02:00
|
|
|
|
2020-06-16 18:09:43 +02:00
|
|
|
document.getElementById("expiration_month").addEventListener('keyup', (e) => {
|
|
|
|
let errors = document.getElementById('expiration_month_errors');
|
|
|
|
if (valid.expirationMonth(e.target.value).isValid) {
|
|
|
|
errors.hidden = true;
|
|
|
|
this.form.valid = true;
|
|
|
|
} else {
|
|
|
|
errors.textContent = this.translations.invalidMonth;
|
|
|
|
errors.hidden = false;
|
|
|
|
this.form.valid = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("expiration_year").addEventListener('keyup', (e) => {
|
|
|
|
let errors = document.getElementById('expiration_year_errors');
|
|
|
|
if (valid.expirationYear(e.target.value).isValid) {
|
|
|
|
errors.hidden = true;
|
|
|
|
this.form.valid = true;
|
|
|
|
} else {
|
|
|
|
errors.textContent = this.translations.invalidYear;
|
|
|
|
errors.hidden = false;
|
|
|
|
this.form.valid = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-06-09 05:54:09 +02:00
|
|
|
|
|
|
|
handle() {
|
2020-06-16 18:09:43 +02:00
|
|
|
this.handleFormValidation();
|
|
|
|
|
|
|
|
// At this point as an small API you can request this.form.valid to check if input elements are valid.
|
|
|
|
// Note: this.form.valid will not handle empty fields.
|
2020-06-09 05:54:09 +02:00
|
|
|
|
|
|
|
this.cardButton.addEventListener("click", () => {
|
2020-06-17 15:27:11 +02:00
|
|
|
this.cardButton.disabled = !this.cardButton.disabled;
|
|
|
|
// this.handleAuthorization();
|
2020-06-09 05:54:09 +02:00
|
|
|
});
|
|
|
|
|
2020-06-16 18:09:43 +02:00
|
|
|
|
2020-06-09 05:54:09 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const publicKey = document.querySelector(
|
|
|
|
'meta[name="authorize-public-key"]'
|
|
|
|
).content;
|
|
|
|
|
2020-06-09 13:53:23 +02:00
|
|
|
const loginId = document.querySelector(
|
2020-06-09 14:54:22 +02:00
|
|
|
'meta[name="authorize-login-id"]'
|
2020-06-09 13:53:23 +02:00
|
|
|
).content;
|
|
|
|
|
2020-06-09 05:54:09 +02:00
|
|
|
/** @handle */
|
2020-06-09 13:53:23 +02:00
|
|
|
new AuthorizeAuthorizeCard(publicKey, loginId).handle();
|