1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/resources/js/clients/invoices/payment.js

128 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-03-23 18:10:42 +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)
2020-03-23 18:10:42 +01:00
*
2022-06-08 06:25:44 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-03-23 18:10:42 +01:00
*/
class Payment {
constructor(displayTerms, displaySignature) {
this.shouldDisplayTerms = displayTerms;
this.shouldDisplaySignature = displaySignature;
this.termsAccepted = false;
2021-11-12 16:43:38 +01:00
this.submitting = false;
2020-03-23 18:10:42 +01:00
}
handleMethodSelect(element) {
2022-07-20 02:08:05 +02:00
2020-05-18 14:06:13 +02:00
document.getElementById("company_gateway_id").value =
element.dataset.companyGatewayId;
document.getElementById("payment_method_id").value =
element.dataset.gatewayTypeId;
2020-03-23 18:10:42 +01:00
if (this.shouldDisplaySignature && !this.shouldDisplayTerms) {
2022-07-31 22:26:04 +02:00
if(this.signaturePad && this.signaturePad.isEmpty())
alert("Please sign");
2020-03-23 18:10:42 +01:00
this.displayTerms();
2020-05-18 14:06:13 +02:00
document
.getElementById("accept-terms-button")
.addEventListener("click", () => {
this.termsAccepted = true;
this.submitForm();
});
2020-03-23 18:10:42 +01:00
}
if (!this.shouldDisplaySignature && this.shouldDisplayTerms) {
this.displaySignature();
2020-05-18 14:06:13 +02:00
document
.getElementById("signature-next-step")
.addEventListener("click", () => {
document.querySelector('input[name="signature"').value = this.signaturePad.toDataURL();
this.submitForm();
});
2020-03-23 18:10:42 +01:00
}
if (this.shouldDisplaySignature && this.shouldDisplayTerms) {
this.displaySignature();
2020-05-18 14:06:13 +02:00
document
.getElementById("signature-next-step")
.addEventListener("click", () => {
this.displayTerms();
document
.getElementById("accept-terms-button")
.addEventListener("click", () => {
document.querySelector('input[name="signature"').value = this.signaturePad.toDataURL();
this.termsAccepted = true;
this.submitForm();
});
2020-03-23 18:10:42 +01:00
});
}
if (!this.shouldDisplaySignature && !this.shouldDisplayTerms) {
this.submitForm();
}
}
submitForm() {
this.submitting = true;
2020-05-18 14:06:13 +02:00
document.getElementById("payment-form").submit();
2020-03-23 18:10:42 +01:00
}
displayTerms() {
2020-05-18 14:06:13 +02:00
let displayTermsModal = document.getElementById("displayTermsModal");
displayTermsModal.removeAttribute("style");
2020-03-23 18:10:42 +01:00
}
displaySignature() {
2020-05-18 14:06:13 +02:00
let displaySignatureModal = document.getElementById(
"displaySignatureModal"
);
displaySignatureModal.removeAttribute("style");
const signaturePad = new SignaturePad(
document.getElementById("signature-pad"),
{
penColor: "rgb(0, 0, 0)"
}
);
2022-07-20 02:08:05 +02:00
signaturePad.onEnd = function(){
document.getElementById("signature-next-step").disabled = false;
};
2020-05-18 14:06:13 +02:00
this.signaturePad = signaturePad;
2020-03-23 18:10:42 +01:00
}
handle() {
document.getElementById("signature-next-step").disabled = true;
2020-05-18 14:06:13 +02:00
document
.querySelectorAll(".dropdown-gateway-button")
.forEach(element => {
2021-11-12 16:43:38 +01:00
element.addEventListener("click", () => {
if (!this.submitting) {
this.handleMethodSelect(element)
}
});
2020-05-18 14:06:13 +02:00
});
2020-03-23 18:10:42 +01:00
}
}
const signature = document.querySelector(
'meta[name="require-invoice-signature"]'
).content;
2020-05-18 14:06:13 +02:00
const terms = document.querySelector('meta[name="show-invoice-terms"]').content;
2020-03-23 18:10:42 +01:00
new Payment(Boolean(+signature), Boolean(+terms)).handle();