| 1 |
/* |
| 2 |
|
| 3 |
This file is part of the plugin Pay with Vipps and MobilePay for WooCommerce |
| 4 |
Copyright (c) 2019 WP-Hosting AS |
| 5 |
|
| 6 |
MIT License |
| 7 |
|
| 8 |
Copyright (c) 2019 WP-Hosting AS |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
of this software and associated documentation files (the "Software"), to deal |
| 12 |
in the Software without restriction, including without limitation the rights |
| 13 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
copies of the Software, and to permit persons to whom the Software is |
| 15 |
furnished to do so, subject to the following conditions: |
| 16 |
|
| 17 |
The above copyright notice and this permission notice shall be included in all |
| 18 |
copies or substantial portions of the Software. |
| 19 |
|
| 20 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
SOFTWARE. |
| 27 |
*/ |
| 28 |
|
| 29 |
// IOK 2018-05-04 Call ajax methods determining if and order is complete or not from the "confirm" waiting screen |
| 30 |
jQuery(document).ready(function () { |
| 31 |
var start = new Date(); |
| 32 |
var fkey = jQuery("#fkey").val(); |
| 33 |
var fkey404 = false; |
| 34 |
var iterate = 0; |
| 35 |
var statusok = 0; |
| 36 |
var done=0; |
| 37 |
var halfminute = 1 * 30 * 1000; |
| 38 |
var data = jQuery("#vippsdata").serialize(); |
| 39 |
|
| 40 |
function checkStatusReady() { |
| 41 |
var now = new Date(); |
| 42 |
iterate++; |
| 43 |
// If we have a fkey that hasn't responed 404 yet and a half a minute hasn't passed, call that directly and often IOK 2018-05-04 |
| 44 |
if (!statusok && fkey && !fkey404 && (now.getTime() - start.getTime()) < halfminute) { |
| 45 |
// Use post to hide data slightly and to avoid caches |
| 46 |
var url = fkey + "?v="+iterate; |
| 47 |
jQuery.ajax(url, |
| 48 |
{"cache":false, |
| 49 |
"headers": { |
| 50 |
'Cache-Control': 'max-age=0, must-revalidate, no-cache, no-store' |
| 51 |
}, |
| 52 |
"dataType":"json", |
| 53 |
"error": function (xhr, statustext, error) { |
| 54 |
console.log("Error checking status: " + statustext + " : " + error); |
| 55 |
if (error == 'timeout') { |
| 56 |
return setTimeout(checkStatusReady,500); |
| 57 |
} |
| 58 |
// Could be other errors but we"ll treat them the same - assume that we can't find the signal and call the ajax method |
| 59 |
fkey404 = true; |
| 60 |
statusok= true; |
| 61 |
setTimeout(checkStatus,500); |
| 62 |
}, |
| 63 |
"method": "GET", // We must use GET because nginx will return 405 for POST to static files |
| 64 |
"success": function (result,statustext, xhr) { |
| 65 |
statusok=result*1; |
| 66 |
if (!statusok) { |
| 67 |
// No result yet, check often as this is cheap |
| 68 |
setTimeout(checkStatusReady,500); |
| 69 |
} else { |
| 70 |
// We have a result, so check what it is |
| 71 |
setTimeout(checkStatus,500); |
| 72 |
} |
| 73 |
}, |
| 74 |
"timeout": 3000 |
| 75 |
}); |
| 76 |
} else { |
| 77 |
// This happens when we've waited for a minute or moe, if we don't have a key and so forth. |
| 78 |
statusok=1; |
| 79 |
setTimeout(checkStatus,500); |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
// Actually check order status by calling admin-ajax |
| 84 |
function checkStatus() { |
| 85 |
jQuery.ajax(VippsConfig['vippsajaxurl'], { |
| 86 |
"method": "POST", |
| 87 |
"data":data, |
| 88 |
"cache":false, |
| 89 |
"dataType": "json", |
| 90 |
"error": function (xhr, statustext, error) { |
| 91 |
console.log("Error checking order status " + statustext + " " + error); |
| 92 |
done=1; |
| 93 |
var errorhandler = function (statustext, error) { |
| 94 |
jQuery("#waiting").hide(); |
| 95 |
jQuery("#success").hide(); |
| 96 |
jQuery("#failure").hide(); |
| 97 |
jQuery("#error").show(); |
| 98 |
}; |
| 99 |
|
| 100 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') { |
| 101 |
errorhandler = wp.hooks.applyFilters('vippsStatusCheckErrorHandler', errorhandler); |
| 102 |
} |
| 103 |
return errorhandler(statustext, error); |
| 104 |
}, |
| 105 |
|
| 106 |
"success": function (result, statustext, xhr) { |
| 107 |
if (result["status"] == "waiting") { |
| 108 |
console.log("Waiting for Vipps callback.."); |
| 109 |
// Do some update here. |
| 110 |
done=0; |
| 111 |
setTimeout(checkStatus,3000); |
| 112 |
} else if (result["status"] == "ok") { |
| 113 |
console.log("Success result"); |
| 114 |
done=1; |
| 115 |
setTimeout(function () { |
| 116 |
var next = jQuery("#continueToThankYou").attr("href"); |
| 117 |
console.log("Redirecting to " +next ); |
| 118 |
window.location.href = next; |
| 119 |
}, 500); |
| 120 |
jQuery("#waiting").hide(); |
| 121 |
jQuery("#success").show(); |
| 122 |
jQuery("#failure").hide(); |
| 123 |
jQuery("#error").hide(); |
| 124 |
} else if (result["status"]=="failed") { |
| 125 |
console.log("Failure result"); |
| 126 |
done=1; |
| 127 |
|
| 128 |
const orderStatus = result['order_status'] ?? ''; |
| 129 |
|
| 130 |
// If provided, continue to custom 'order failed' page IOK 2018-12-04 |
| 131 |
var next = jQuery("#continueToOrderFailed").attr("href"); |
| 132 |
if (next != '') { |
| 133 |
setTimeout(function () { |
| 134 |
console.log("Redirecting to " +next ); |
| 135 |
window.location.href = next; |
| 136 |
}, 500); |
| 137 |
} |
| 138 |
|
| 139 |
// Fallback redirect on failed status, unless already redirected above. |
| 140 |
// added gw return url as a fallback here so we can separate redirect on status |
| 141 |
// 'failed' vs 'cancelled', but still prioritize #continueToOrderFailed because it comes from a filter. LP 2026-03-27 |
| 142 |
if ('failed' === orderStatus) { |
| 143 |
const fallbackRedirect = jQuery("#continueToOrderFailedFallback").attr("href"); |
| 144 |
if (fallbackRedirect != '') { |
| 145 |
setTimeout(function () { |
| 146 |
console.log("Redirecting to", fallbackRedirect); |
| 147 |
window.location.href = fallbackRedirect; |
| 148 |
}, 500); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
jQuery("#waiting").hide(); |
| 153 |
jQuery("#success").hide(); |
| 154 |
jQuery("#failure").show(); |
| 155 |
jQuery("#error").hide(); |
| 156 |
} else { |
| 157 |
console.log("Error result %j",result); |
| 158 |
done=1; |
| 159 |
var errorhandler = function (statustext, error) { |
| 160 |
jQuery("#waiting").hide(); |
| 161 |
jQuery("#success").hide(); |
| 162 |
jQuery("#failure").hide(); |
| 163 |
jQuery("#error").show(); |
| 164 |
}; |
| 165 |
|
| 166 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') { |
| 167 |
errorhandler = wp.hooks.applyFilters('vippsStatusCheckErrorHandler', errorhandler); |
| 168 |
} |
| 169 |
return errorhandler("Unknown result from WooCommerce", result); |
| 170 |
} |
| 171 |
}, |
| 172 |
"timeout": 0 |
| 173 |
}); |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
checkStatusReady(); |
| 179 |
|
| 180 |
}); |
| 181 |
|