frappe.ui.form.on("Sales Invoice", { refresh(frm) { if (frm.doc.docstatus !== 1) return; // submitted only frm.add_custom_button("Run Payment", () => { run_payment_flow(frm); }); } }); function run_payment_flow(frm) { frappe.call({ method: "ns_app.api.payments.check_autopay", args: { customer: frm.doc.customer }, callback(r) { if (!r.message) return; if (r.message.autopay_enabled) { run_autopay(frm, r.message.autopay_id); } else { open_manual_payment_form(frm); } } }); function run_autopay(frm, autopay_id) { frappe.confirm( `Run AutoPay for $${frm.doc.grand_total}?`, () => { 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(r.message || "Payment processed"); frm.reload_doc(); } }); } ); } function open_manual_payment_form(frm) { const dialog = new frappe.ui.Dialog({ title: "Enter Payment", fields: [ { fieldtype: "HTML", fieldname: "payment_form", options: `` } ] }); dialog.show(); } }