Autopay api logic prototype.
This commit is contained in:
68
ns_app/public/js/sales_invoice.js
Normal file
68
ns_app/public/js/sales_invoice.js
Normal file
@@ -0,0 +1,68 @@
|
||||
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: `<iframe
|
||||
src="https://payments.yourprovider.com/pay?invoice=${frm.doc.name}&amount=${frm.doc.grand_total}"
|
||||
style="width:100%;height:500px;border:none;"
|
||||
></iframe>`
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user