Updated invoice and autopay logic.

This commit is contained in:
Ty Reynolds
2026-02-10 08:42:57 -05:00
parent 75600fdbeb
commit 4357ce7fcc
2 changed files with 36 additions and 36 deletions

View File

@@ -8,8 +8,8 @@ def check_autopay(customer):
cust = frappe.get_doc("Customer", customer) cust = frappe.get_doc("Customer", customer)
return { return {
"autopay_enabled": bool(cust.auto_pay), "autopay_enabled": bool(cust.custom_auto_pay_status),
"autopay_id": cust.auto_pay_id if cust.auto_pay else None "autopay_id": cust.auto_pay_id if cust.custom_auto_pay_status else None
} }
@@ -22,7 +22,7 @@ def run_autopay_payment(invoice):
cust = frappe.get_doc("Customer", inv.customer) cust = frappe.get_doc("Customer", inv.customer)
if not cust.auto_pay or not cust.auto_pay_id: if not cust.custom_auto_pay_status or not cust.auto_pay_id:
frappe.throw("Customer does not have AutoPay enabled") frappe.throw("Customer does not have AutoPay enabled")
payload = { payload = {

View File

@@ -2,10 +2,11 @@ frappe.ui.form.on("Sales Invoice", {
refresh(frm) { refresh(frm) {
frm.clear_custom_buttons(); frm.clear_custom_buttons();
// Only on submitted invoices
if (frm.doc.docstatus !== 1) return; if (frm.doc.docstatus !== 1) return;
if (!frm.doc.customer) return; if (!frm.doc.customer) return;
// If already paid, don't show button // Already paid
if (frm.doc.outstanding_amount <= 0) { if (frm.doc.outstanding_amount <= 0) {
frm.dashboard.add_indicator("Paid", "green"); frm.dashboard.add_indicator("Paid", "green");
return; return;
@@ -27,10 +28,13 @@ function run_payment_flow(frm) {
method: "ns_app.api.payments.check_autopay", method: "ns_app.api.payments.check_autopay",
args: { customer: frm.doc.customer }, args: { customer: frm.doc.customer },
callback(r) { callback(r) {
if (!r.message) return; if (!r.message) {
frm.enable_save();
return;
}
if (r.message.autopay_enabled) { if (r.message.autopay_enabled && r.message.autopay_id) {
run_autopay(frm, r.message.autopay_id); run_autopay(frm);
} else { } else {
open_manual_payment_form(frm); open_manual_payment_form(frm);
} }
@@ -39,8 +43,29 @@ function run_payment_flow(frm) {
} }
function open_manual_payment_form(frm) { function run_autopay(frm) {
frappe.confirm(
`Run AutoPay for $${frm.doc.outstanding_amount}?`,
() => {
frappe.call({
method: "ns_app.api.payments.run_autopay_payment",
args: {
invoice: frm.doc.name
},
callback(r) {
frappe.msgprint("Payment successful");
frm.reload_doc();
}
});
},
() => {
frm.enable_save();
}
);
}
function open_manual_payment_form(frm) {
const dialog = new frappe.ui.Dialog({ const dialog = new frappe.ui.Dialog({
title: "Secure Payment", title: "Secure Payment",
size: "large", size: "large",
@@ -50,7 +75,7 @@ function open_manual_payment_form(frm) {
fieldname: "loader", fieldname: "loader",
options: ` options: `
<div id="payment_loader" style="text-align:center;padding:40px;"> <div id="payment_loader" style="text-align:center;padding:40px;">
<b>Loading secure checkout...</b> <b>Loading secure checkout</b>
</div> </div>
` `
}, },
@@ -91,7 +116,7 @@ function open_manual_payment_form(frm) {
} }
}); });
// Auto refresh every 5 seconds while dialog open // Poll invoice every 5 seconds
const poller = setInterval(() => { const poller = setInterval(() => {
frappe.call({ frappe.call({
method: "frappe.client.get", method: "frappe.client.get",
@@ -100,7 +125,7 @@ function open_manual_payment_form(frm) {
name: frm.doc.name name: frm.doc.name
}, },
callback(r) { callback(r) {
if (r.message.outstanding_amount <= 0) { if (r.message && r.message.outstanding_amount <= 0) {
clearInterval(poller); clearInterval(poller);
dialog.hide(); dialog.hide();
frm.reload_doc(); frm.reload_doc();
@@ -110,28 +135,3 @@ function open_manual_payment_form(frm) {
}); });
}, 5000); }, 5000);
} }
function run_autopay(frm, autopay_id) {
frappe.confirm(
`Run AutoPay for $${frm.doc.grand_total}?`,
() => {
frm.disable_save();
frappe.call({
method: "ns_app.api.payments.run_autopay_payment",
args: {
invoice: frm.doc.name,
autopay_id: autopay_id,
amount: frm.doc.grand_total
},
callback(r) {
frappe.msgprint("Payment successful");
frm.reload_doc();
}
});
}
);
}