From d082c59bc4d7bd109dbd0dab6603dfe227ee1399 Mon Sep 17 00:00:00 2001 From: Ty Reynolds Date: Tue, 24 Mar 2026 08:38:32 -0400 Subject: [PATCH] Fixed the Collect JS loading after first instance issue. --- ns_app/public/js/sales_invoice.js | 47 +++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/ns_app/public/js/sales_invoice.js b/ns_app/public/js/sales_invoice.js index 82ad7c2..4562ee2 100644 --- a/ns_app/public/js/sales_invoice.js +++ b/ns_app/public/js/sales_invoice.js @@ -152,14 +152,38 @@ function open_manual_payment_form(frm) { ` } ], + + // Destroy CollectJS instance if close button is clicked primary_action_label: "Close", primary_action() { - dialog.hide(); + dialog.hide(); } }); dialog.show(); + // Destroy CollectJS Instance if closed with the X button + dialog.$wrapper.on("hidden.bs.modal", function () { + + // Remove leftover backdrop (fix dark screen) + document.querySelectorAll(".modal-backdrop").forEach(el => el.remove()); + + // Restore body scroll + click + document.body.classList.remove("modal-open"); + document.body.style.overflow = ""; + + // safely remove dialog + dialog.$wrapper.remove(); + + // WClean CollectJS + if (window.CollectJS) { + try { + delete window.CollectJS; + } catch (e) {} + } + + }); + // Prefill setTimeout(() => { const nameEl = document.getElementById(`cardholder_name_${uid}`); @@ -172,11 +196,19 @@ function open_manual_payment_form(frm) { // Load CollectJS once function loadCollectJS(callback) { - if (window.CollectJS) { - callback(); - return; + + // Remove existing script if it exists + const existingScript = document.querySelector('script[src*="Collect.js"]'); + if (existingScript) { + existingScript.remove(); } + // Reset global + if (window.CollectJS) { + delete window.CollectJS; + } + + // Create fresh script every instance const script = document.createElement("script"); script.src = "https://secure.nmi.com/token/Collect.js"; @@ -185,7 +217,11 @@ function open_manual_payment_form(frm) { "HKx4XR-G549wT-8bZ2YJ-3kbG28" ); - script.onload = callback; + script.onload = () => { + console.log("CollectJS loaded fresh"); + callback(); + }; + document.body.appendChild(script); } @@ -195,7 +231,6 @@ function open_manual_payment_form(frm) { setTimeout(() => { - // DO NOT clear anything — new IDs = fresh mount CollectJS.configure({ variant: "inline",