// Wait for document ready to ensure jQuery UI is loaded $(document).ready(function () { // Define authnetAction in global scope window.authnetAction = function (action, onload, callback) { // Check if action already starts with http or / and use it directly, // otherwise prepend the /authnet- part var url; if (action.indexOf('http') === 0 || action.indexOf('/') === 0) { url = action; } else { url = '/authnet-' + action + '.html'; } console.log("Authorize.net payment initialization, URL:", url); // First fetch payment configuration data $.ajax({ url: url, type: 'GET', dataType: 'json', success: function(response) { console.log("Payment configuration received:", response); if (response && response.url) { // Open the payment form window with the URL from response var authnetWindow = window.open(response.url, 'authnet_window', 'width=800,height=600'); // Setup window close monitoring for callback if (callback) { window.authnetCallback = callback; var checkWindowClosed = setInterval(function() { if (authnetWindow.closed) { clearInterval(checkWindowClosed); // Try to get result from closed window if available var result = null; try { if (authnetWindow.document && authnetWindow.document.body && authnetWindow.document.body.dataset && authnetWindow.document.body.dataset.result) { result = JSON.parse(authnetWindow.document.body.dataset.result); } } catch (e) { console.log("Could not retrieve payment result", e); } // Call the callback with result data or null if (callback) { callback(result); } } }, 500); } // Execute onload function if provided if (onload && typeof onload === 'function') { onload(authnetWindow); } } else { console.error("Invalid payment configuration response", response); if (callback) callback({ success: false, message: "Payment initialization failed" }); } }, error: function(xhr, status, error) { console.error("Payment initialization error:", error); console.error("Status:", status); console.error("Response:", xhr.responseText); try { // Try to parse response as JSON var errorData = JSON.parse(xhr.responseText); console.log("Parsed error data:", errorData); } catch (e) { console.log("Could not parse error response as JSON"); } if (callback) callback({ success: false, message: "Payment initialization failed" }); } }); }; });