2020-03-23 18:10:42 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-13 09:22:36 +01:00
|
|
|
* @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 {
|
2024-01-31 18:47:43 +01:00
|
|
|
constructor(displayTerms, displaySignature, displayRff) {
|
2020-03-23 18:10:42 +01:00
|
|
|
this.shouldDisplayTerms = displayTerms;
|
|
|
|
this.shouldDisplaySignature = displaySignature;
|
2024-01-31 18:47:43 +01:00
|
|
|
this.shouldDisplayRff = displayRff;
|
|
|
|
|
2021-11-12 16:43:38 +01:00
|
|
|
this.submitting = false;
|
2024-01-31 18:47:43 +01:00
|
|
|
this.steps = new Map()
|
|
|
|
|
|
|
|
if (this.shouldDisplayRff) {
|
|
|
|
this.steps.set("rff", {
|
|
|
|
element: document.getElementById('displayRequiredFieldsModal'),
|
|
|
|
callback: () => {
|
|
|
|
const fields = {
|
|
|
|
firstName: document.querySelector('input[name="rff_first_name"]'),
|
|
|
|
lastName: document.querySelector('input[name="rff_last_name"]'),
|
|
|
|
email: document.querySelector('input[name="rff_email"]'),
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fields.firstName) {
|
|
|
|
document.querySelector('input[name="contact_first_name"]').value = fields.firstName.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fields.lastName) {
|
|
|
|
document.querySelector('input[name="contact_last_name"]').value = fields.lastName.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fields.email) {
|
|
|
|
document.querySelector('input[name="contact_email"]').value = fields.email.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.shouldDisplaySignature) {
|
|
|
|
this.steps.set("signature", {
|
|
|
|
element: document.getElementById('displaySignatureModal'),
|
|
|
|
boot: () => this.signaturePad = new SignaturePad(
|
|
|
|
document.getElementById("signature-pad"),
|
|
|
|
{
|
|
|
|
penColor: "rgb(0, 0, 0)"
|
|
|
|
}
|
|
|
|
),
|
|
|
|
callback: () => document.querySelector('input[name="signature"').value = this.signaturePad.toDataURL(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.shouldDisplayTerms) {
|
|
|
|
this.steps.set("terms", {
|
|
|
|
element: document.getElementById('displayTermsModal'),
|
|
|
|
});
|
|
|
|
}
|
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;
|
2024-01-31 18:47:43 +01:00
|
|
|
|
|
|
|
if (this.steps.size === 0) {
|
|
|
|
return this.submitForm();
|
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2024-01-31 18:47:43 +01:00
|
|
|
const next = this.steps.values().next().value;
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2024-01-31 18:47:43 +01:00
|
|
|
next.element.removeAttribute("style");
|
|
|
|
|
|
|
|
if (next.boot) {
|
|
|
|
next.boot();
|
2020-03-23 18:10:42 +01:00
|
|
|
}
|
|
|
|
|
2024-01-31 18:47:43 +01:00
|
|
|
next.element.querySelector('#next-step').addEventListener('click', () => {
|
|
|
|
next.element.setAttribute("style", "display: none;");
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2024-01-31 18:47:43 +01:00
|
|
|
this.steps = new Map(Array.from(this.steps.entries()).slice(1));
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2024-01-31 18:47:43 +01:00
|
|
|
if (next.callback) {
|
|
|
|
next.callback();
|
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2024-01-31 18:47:43 +01:00
|
|
|
this.handleMethodSelect(element);
|
|
|
|
});
|
2020-03-23 18:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
submitForm() {
|
2021-12-15 10:25:18 +01:00
|
|
|
this.submitting = true;
|
|
|
|
|
2020-05-18 14:06:13 +02:00
|
|
|
document.getElementById("payment-form").submit();
|
2020-03-23 18:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handle() {
|
2022-07-20 02:41:36 +02:00
|
|
|
|
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;
|
2024-01-31 18:47:43 +01:00
|
|
|
const rff = document.querySelector('meta[name="show-required-fields-form"]').content;
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2024-01-31 18:47:43 +01:00
|
|
|
new Payment(Boolean(+terms), Boolean(+signature), Boolean(+rff)).handle();
|