// 2023/04/21a - BkBookingAction // #region Booking Actions // Executes a BK Booking Action to run custom business logic on the server. JSON results will be returned and will be passed // to either successCallback function or errorCallback function. In the case of an HTTP error response (400 - 600), a custom // callback can be used to handle this. function ExecuteBookingAction(action, params, successCallback, errorCallback, httpErrorCallback, method) { // Init if (!action) { alert("No Action provided."); return; } if (!params) { params = ""; } if ((!method) || (method != "POST")) { method = "GET"; } //console.log("METHOD: " + method); params = encodeURIComponent(params); // Encode for URL (replacements will be "%3D" for "=" and "%26" for "&") var paramsString = "parameters=" + params; var cacheId = Math.random() + ""; // Used for uniqueness so to circumvent portal caching. Should also add Booking Code to this cacheId to guarantee uniqueness var customActionUrl = window.location.origin + "/bookingaction-execute/?action=" + action; if (method == "GET") { customActionUrl += "&" + paramsString; } customActionUrl += "&cache_id=" + cacheId; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { //console.log("readyState: " + this.readyState + ", status: " + this.status); if (this.readyState == 4) { if (this.status == 200) { try { // Read the JSON results from the Custom Action portal page var result = JSON.parse(this.responseText); //console.log("console log of the result"); //console.log(result); var successMessage = result[0].success_message; // If this is not an empty string, we have a successful result. This will usually be something like "SUCCESS". var errorMessage = result[0].error_message; // If there was an error, the details will be in here. var responseData = result[0].response_data; // The response data in JSON. var responseData2 = result[0].response_data_2; // This is if we need a second result set. if (successMessage != "") { // Success successCallback(result); } else { // Must be an error // Error errorCallback(result); } } catch (err) { // It's possible that the custom action page will not return a proper JSON object - for example if the page times out. In this case, // just send the raw responseText to the error function. // NOTE: The portal seems to time out after around 30 seconds (if a fetch takes too long for example). This timeout occurs even if the fetch succeeds. errorCallback(this.responseText); } } else if ((this.status >= 400) && (this.status <= 600)) { if (httpErrorCallback) { httpErrorCallback(this.status, this.responseText); } } } }; // Send the request if (method == "GET") { //console.log("Custom Action - GET: " + customActionUrl); xhttp.open("GET", customActionUrl, true); xhttp.send(); } else { // POST //console.log("Custom Action - POST: " + customActionUrl); //console.log("POST params: " + paramsString); xhttp.open("POST", customActionUrl, true); xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); // Send the proper header information along with the request xhttp.send(paramsString); } } // #endregion