Updated invoice and autopay logic.
This commit is contained in:
@@ -8,8 +8,8 @@ def check_autopay(customer):
|
||||
cust = frappe.get_doc("Customer", customer)
|
||||
|
||||
return {
|
||||
"autopay_enabled": bool(cust.auto_pay),
|
||||
"autopay_id": cust.auto_pay_id if cust.auto_pay else None
|
||||
"autopay_enabled": bool(cust.custom_auto_pay_status),
|
||||
"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)
|
||||
|
||||
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")
|
||||
|
||||
payload = {
|
||||
|
||||
@@ -2,10 +2,11 @@ frappe.ui.form.on("Sales Invoice", {
|
||||
refresh(frm) {
|
||||
frm.clear_custom_buttons();
|
||||
|
||||
// Only on submitted invoices
|
||||
if (frm.doc.docstatus !== 1) return;
|
||||
if (!frm.doc.customer) return;
|
||||
|
||||
// If already paid, don't show button
|
||||
// Already paid
|
||||
if (frm.doc.outstanding_amount <= 0) {
|
||||
frm.dashboard.add_indicator("Paid", "green");
|
||||
return;
|
||||
@@ -27,10 +28,13 @@ function run_payment_flow(frm) {
|
||||
method: "ns_app.api.payments.check_autopay",
|
||||
args: { customer: frm.doc.customer },
|
||||
callback(r) {
|
||||
if (!r.message) return;
|
||||
if (!r.message) {
|
||||
frm.enable_save();
|
||||
return;
|
||||
}
|
||||
|
||||
if (r.message.autopay_enabled) {
|
||||
run_autopay(frm, r.message.autopay_id);
|
||||
if (r.message.autopay_enabled && r.message.autopay_id) {
|
||||
run_autopay(frm);
|
||||
} else {
|
||||
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({
|
||||
title: "Secure Payment",
|
||||
size: "large",
|
||||
@@ -50,7 +75,7 @@ function open_manual_payment_form(frm) {
|
||||
fieldname: "loader",
|
||||
options: `
|
||||
<div id="payment_loader" style="text-align:center;padding:40px;">
|
||||
<b>Loading secure checkout...</b>
|
||||
<b>Loading secure checkout…</b>
|
||||
</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(() => {
|
||||
frappe.call({
|
||||
method: "frappe.client.get",
|
||||
@@ -100,7 +125,7 @@ function open_manual_payment_form(frm) {
|
||||
name: frm.doc.name
|
||||
},
|
||||
callback(r) {
|
||||
if (r.message.outstanding_amount <= 0) {
|
||||
if (r.message && r.message.outstanding_amount <= 0) {
|
||||
clearInterval(poller);
|
||||
dialog.hide();
|
||||
frm.reload_doc();
|
||||
@@ -110,28 +135,3 @@ function open_manual_payment_form(frm) {
|
||||
});
|
||||
}, 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user