UNPKG

plebpay-utils

Version:
103 lines (95 loc) 3.98 kB
function $parcel$interopDefault(a) { return a && a.__esModule ? a.default : a; } var $e052e26e34323f0e$exports = {}; function $3f5b59ea160f0941$export$2e2bcd8739ae039(obj) { return obj && obj.constructor === Symbol ? "symbol" : typeof obj; } "use strict"; /** * shorthash2 Jecsham (c) 2020 * Based in shorthash (c) 2013 Bibig https://github.com/bibig/node-shorthash (MIT) */ var $e052e26e34323f0e$var$bitwise = function $e052e26e34323f0e$var$bitwise(str) { var hash = 0; if (str.length == 0) return hash; for(var i = 0; i < str.length; i++){ var ch = str.charCodeAt(i); hash = (hash << 5) - hash + ch; hash = hash & hash; } return hash; }; var $e052e26e34323f0e$var$binaryTransfer = function $e052e26e34323f0e$var$binaryTransfer(integer, binary) { var dictionary = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; binary = binary || 62; var stack = []; var num; var result = ''; var sign = integer < 0 ? '-' : ''; integer = Math.abs(integer); while(integer >= binary){ num = integer % binary; integer = Math.floor(integer / binary); stack.push(dictionary[num]); } if (integer > 0) stack.push(dictionary[integer]); for(var i = stack.length - 1; i >= 0; i--)result += stack[i]; return sign + result; }; var $e052e26e34323f0e$var$shortHash = function $e052e26e34323f0e$var$shortHash(text) { var type = typeof text === "undefined" ? "undefined" : $3f5b59ea160f0941$export$2e2bcd8739ae039(text); if (type === "string" || type === "number") { var id = $e052e26e34323f0e$var$binaryTransfer($e052e26e34323f0e$var$bitwise(String(text)), 61); return id.replace('-', 'Z'); } else throw new Error("Unexpected input type"); }; $e052e26e34323f0e$exports = $e052e26e34323f0e$var$shortHash; var $fedbd030e12bccff$var$getPlebPayRefFromQueryParams = function() { var searchParams = new URLSearchParams(window.location.search); return searchParams.get("plebPayRef"); }; var $fedbd030e12bccff$var$createLocalPlebPlayRef = function(invoiceId) { return (/*@__PURE__*/$parcel$interopDefault($e052e26e34323f0e$exports))("".concat(invoiceId).concat(window.navigator.userAgent)); }; var $fedbd030e12bccff$var$removePlebPayRefFromQueryParams = function() { var currentHref = new URL(window.location.href); currentHref.searchParams.delete("plebPayRef"); window.history.replaceState(null, null, currentHref); }; var $fedbd030e12bccff$var$getPlebPayInvoiceId = function(plebPlayUrl) { return plebPlayUrl.replace("https://www.plebpay.com/", ""); }; var $fedbd030e12bccff$var$testLocalStorage = function() { var test = "plebPayTest"; try { localStorage.setItem(test, test); localStorage.removeItem(test); return true; } catch (e) { return false; } }; var $fedbd030e12bccff$var$handleSuccess = function(plebPlayRef) { localStorage.setItem(plebPlayRef, "1"); $fedbd030e12bccff$var$removePlebPayRefFromQueryParams(); }; var $fedbd030e12bccff$var$redirectIfNecessary = function(plebPayUrl) { var localStorageIsAvailable = $fedbd030e12bccff$var$testLocalStorage(); var invoiceId = $fedbd030e12bccff$var$getPlebPayInvoiceId(plebPayUrl); var plebPlayRef = $fedbd030e12bccff$var$createLocalPlebPlayRef(invoiceId); if (localStorageIsAvailable && localStorage.getItem(plebPlayRef, "1")) return; if (document.referrer === "https://www.plebpay.com/" || plebPlayRef === $fedbd030e12bccff$var$getPlebPayRefFromQueryParams()) { if (localStorageIsAvailable) $fedbd030e12bccff$var$handleSuccess(plebPlayRef); return; } window.location = plebPayUrl; }; var $fedbd030e12bccff$var$PlebPay = { redirectIfNecessary: $fedbd030e12bccff$var$redirectIfNecessary }; try { module.exports = $fedbd030e12bccff$var$PlebPay; } catch (e) { window.PlebPay = $fedbd030e12bccff$var$PlebPay; } //# sourceMappingURL=index.js.map