From ac51c2ce4ed52387961394f1f3c4c878d361237e Mon Sep 17 00:00:00 2001 From: Ty Reynolds Date: Mon, 23 Mar 2026 13:24:59 -0400 Subject: [PATCH] Added card holder name and zip to manual payment form. Known bug for loading Collect.Js after 1st instance post cache clear. Working on a fix. --- ns_app/api/payments.py | 54 +++++++++-- ns_app/public/js/sales_invoice.js | 151 +++++++++++++++++++----------- 2 files changed, 144 insertions(+), 61 deletions(-) diff --git a/ns_app/api/payments.py b/ns_app/api/payments.py index 4113c93..7f00b4f 100644 --- a/ns_app/api/payments.py +++ b/ns_app/api/payments.py @@ -118,14 +118,28 @@ def call_payment_api(payload): @frappe.whitelist() -def run_token_payment(invoice, token): +def run_token_payment(invoice, token, cardholder_name=None, billing_zip=None): inv = frappe.get_doc("Sales Invoice", invoice) url = "https://secure.nmi.com/api/transact.php" - # Get and parse customer name. - customer_name = inv.customer_name or inv.customer or "Customer" - parts = customer_name.split(" ", 1) + # Fallback logic + customer_name = ( + cardholder_name + or inv.customer_name + or inv.customer + or "Customer" + ) + + billing_zip = ( + billing_zip + or inv.get("billing_zip") + or inv.get("pincode") + or "" + ) + + # Name Split + parts = customer_name.strip().split(" ", 1) first_name = parts[0] last_name = parts[1] if len(parts) > 1 else "." @@ -139,11 +153,15 @@ def run_token_payment(invoice, token): "first_name": first_name, "last_name": last_name, "email": inv.contact_email or "", + + "zip": billing_zip, } response = requests.post(url, data=data) result = urllib.parse.parse_qs(response.text) + frappe.logger("payments").info(f"NMI RESPONSE: {response.text}") + success = result.get("response", ["0"])[0] transaction_id = result.get("transactionid", [""])[0] @@ -157,18 +175,33 @@ def run_token_payment(invoice, token): return {"success": True} - return {"success": False, "error": result.get("responsetext", ["Error"])[0]} - + return { + "success": False, + "error": result.get("responsetext", ["Error"])[0] + } @frappe.whitelist() -def save_to_autopay(customer, token): +def save_to_autopay(customer, token, cardholder_name=None, billing_zip=None): import requests import urllib.parse inv_customer = frappe.get_doc("Customer", customer) - customer_name = (inv_customer.customer_name or "Customer").strip() + # Fallback logic + customer_name = ( + cardholder_name + or inv_customer.customer_name + or "Customer" + ).strip() + billing_zip = ( + billing_zip + or inv_customer.get("billing_zip") + or inv_customer.get("pincode") + or "" + ) + + # name split if " " in customer_name: first_name, last_name = customer_name.split(" ", 1) else: @@ -182,6 +215,9 @@ def save_to_autopay(customer, token): "first_name": first_name, "last_name": last_name, + + "zip": billing_zip, + "customer_vault": "add_customer" } @@ -194,6 +230,8 @@ def save_to_autopay(customer, token): result = urllib.parse.parse_qs(response.text) + frappe.logger("payments").info(f"NMI VAULT RESPONSE: {response.text}") + success = result.get("response", ["0"])[0] vault_id = result.get("customer_vault_id", [""])[0] message = result.get("responsetext", ["Failed"])[0] diff --git a/ns_app/public/js/sales_invoice.js b/ns_app/public/js/sales_invoice.js index ceb1ad9..82ad7c2 100644 --- a/ns_app/public/js/sales_invoice.js +++ b/ns_app/public/js/sales_invoice.js @@ -118,6 +118,9 @@ function show_payment_failed(frm, message) { function open_manual_payment_form(frm) { + + const uid = Date.now(); // unique per open + const dialog = new frappe.ui.Dialog({ title: "Secure Payment", size: "large", @@ -127,11 +130,22 @@ function open_manual_payment_form(frm) { fieldname: "payment_form", options: `
-
-
-
-
@@ -146,53 +160,83 @@ function open_manual_payment_form(frm) { dialog.show(); - // Load NMI Collect.js - const script = document.createElement("script"); - script.src = "https://secure.nmi.com/token/Collect.js"; + // Prefill + setTimeout(() => { + const nameEl = document.getElementById(`cardholder_name_${uid}`); + const zipEl = document.getElementById(`billing_zip_${uid}`); - script.setAttribute( - "data-tokenization-key", - "HKx4XR-G549wT-8bZ2YJ-3kbG28" -); + if (nameEl) nameEl.value = frm.doc.customer_name || ""; + if (zipEl) zipEl.value = frm.doc.billing_zip || frm.doc.pincode || ""; + }, 50); - script.onload = () => { - console.log("CollectJS loaded"); - CollectJS.configure({ - variant: "inline", - styleSniffer: true, + // Load CollectJS once + function loadCollectJS(callback) { + if (window.CollectJS) { + callback(); + return; + } - fields: { - ccnumber: { - selector: "#cc_number", - placeholder: "Card Number" - }, - ccexp: { - selector: "#cc_exp", - placeholder: "MM / YY" - }, - cvv: { - selector: "#cc_cvv", - placeholder: "CVV" - } - }, - + const script = document.createElement("script"); + script.src = "https://secure.nmi.com/token/Collect.js"; - callback: function (response) { - console.log("Token response:", response); + script.setAttribute( + "data-tokenization-key", + "HKx4XR-G549wT-8bZ2YJ-3kbG28" + ); - if (response.token) { - run_token_payment(frm, response.token, dialog); - } else { - frappe.msgprint("Payment failed to tokenize"); - } - } - }); + script.onload = callback; + document.body.appendChild(script); + } + + + loadCollectJS(() => { + console.log("CollectJS ready"); - console.log("Fields:", document.querySelector("#cc_number")); - setTimeout(() => { - const btn = dialog.$wrapper.find("#pay_btn")[0]; + + // DO NOT clear anything — new IDs = fresh mount + + CollectJS.configure({ + variant: "inline", + styleSniffer: true, + + fields: { + ccnumber: { + selector: `#cc_number_${uid}`, + placeholder: "Card Number" + }, + ccexp: { + selector: `#cc_exp_${uid}`, + placeholder: "MM / YY" + }, + cvv: { + selector: `#cc_cvv_${uid}`, + placeholder: "CVV" + } + }, + + callback: function (response) { + if (response.token) { + + const enteredName = document.getElementById(`cardholder_name_${uid}`)?.value; + const enteredZip = document.getElementById(`billing_zip_${uid}`)?.value; + + const finalName = enteredName || frm.doc.customer_name; + const finalZip = enteredZip || frm.doc.billing_zip || frm.doc.pincode; + + run_token_payment(frm, response.token, dialog, { + cardholder_name: finalName, + billing_zip: finalZip + }); + + } else { + frappe.msgprint("Payment failed to tokenize"); + } + } + }); + + const btn = document.getElementById(`pay_btn_${uid}`); if (!btn) { console.error("Pay button not found"); @@ -200,8 +244,6 @@ function open_manual_payment_form(frm) { } btn.onclick = function () { - console.log("Pay clicked"); - frappe.show_alert({ message: "Processing payment...", indicator: "blue" @@ -209,23 +251,24 @@ function open_manual_payment_form(frm) { CollectJS.startPaymentRequest(); }; - }, 300); - }; + }, 100); - document.body.appendChild(script); + }); } -function run_token_payment(frm, token, dialog) { +function run_token_payment(frm, token, dialog, extra_data = {}) { frappe.call({ method: "ns_app.api.payments.run_token_payment", args: { invoice: frm.doc.name, - token: token + token: token, + cardholder_name: extra_data.cardholder_name, + billing_zip: extra_data.billing_zip }, callback(r) { - if (r.message?.success) { + if (r.message?.success) { frappe.confirm( "Payment successful. Save this card for AutoPay?", @@ -235,7 +278,9 @@ function run_token_payment(frm, token, dialog) { method: "ns_app.api.payments.save_to_autopay", args: { customer: frm.doc.customer, - token: token + token: token, + cardholder_name: extra_data.cardholder_name, + billing_zip: extra_data.billing_zip }, callback(res) { if (res.message?.success) { @@ -266,4 +311,4 @@ function run_token_payment(frm, token, dialog) { } } }); -} +} \ No newline at end of file