var inpostPayDebug = false; var timeoutId; var bIsOn = true; document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") { iziBasketReload(true); } }); document.addEventListener("DOMContentLoaded", () => { iziBasketReload(); }); function getIziBrowserId() { const value = "; " + document.cookie; const parts = value.split("; BrowserId="); if (parts.length === 2) return parts.pop().split(';').shift(); } function setCookieInpost(cname, cvalue, exdays) { const d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); let expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } function iziBasketReload(bA = false) { if ((typeof getIziBrowserId === "function") && (typeof getIziBrowserId() !== "undefined") && (document.visibilityState === "visible")) { $.ajax({ url: document.location.origin + "/inpost/izi-basket-reload" + "?bA=" + bA, type: 'GET', success: function(response) { if (response) { oJson = response; if (oJson.action === "refresh" && oJson.count !== false) { $('.top-menu #btn-popup-basket-preview .quanity').html(oJson.count); setCookieInpost("inpb", JSON.stringify(oJson), 1); const event = new CustomEvent("inpost-update-count", { detail: oJson.count }); const iziButtonsCollection = Array.from(document.getElementsByTagName("inpost-izi-button")); iziButtonsCollection.forEach(el => el.dispatchEvent(event)); } if (oJson.action === "redirect") { window.location.href = document.location.origin + "/koszyk/"; } if (oJson.action === "reload") { window.location.href = document.location.href; } } } }); } if (!bA) { setTimeout(() => { iziBasketReload(); }, 10000); } } document.addEventListener("DOMContentLoaded", function() { if (iziCanBeBound() === false) { var izibtns = document.getElementsByTagName("inpost-izi-button"); if (izibtns.length > 0) { for (var i = 0; i < izibtns.length; i++) { izibtns[i].setAttribute("hidden", "hidden"); } } } }, false); async function iziGetPayData(prefix, phoneNumber, bindingPlace) { if (inpostPayDebug) { console.log("iziGetPayData", prefix, phoneNumber, bindingPlace); } const browserData = window.iziGetBrowserData({ base64: true }); return new Promise(function(resolve, reject) { $.ajax({ url: document.location.origin + "/inpost/izi-get-pay-data", type: "GET", cache: false, xhrFields: { withCredentials: true }, data: { "prefix": prefix || "", "phoneNumber": phoneNumber || "", "bindingPlace": bindingPlace || "", "browserData": browserData }, success: function(response) { if (response.error) { reject(response); } resolve(response); } }); }); } function handleServerAction(action, resolve, reject) { if (inpostPayDebug) { console.log("handleServerAction", action, resolve, reject); } if (timeoutId) { if (inpostPayDebug) { console.log("clearTimeout: ", timeoutId); } clearTimeout(timeoutId); } switch (action) { case 'error': reject(new Error("Połączenie zostało przerwane, spróbuj ponownie.")); break; case 'retry': timeoutId = setTimeout(() => setupLongPolling(resolve, reject), 5000); break; case 'close': break; default: break; } } async function setupLongPolling(resolve, reject) { if (inpostPayDebug) { console.log("setupLongPolling", resolve, reject); } try { const response = await fetch(document.location.origin + "/inpost/izi-get-is-bound"); const data = await response.json(); if (data?.phone_number) { resolve(data); } else if (data?.action) { handleServerAction(data.action, resolve, reject); } else if (data?.error_code) { reject(new Error(data.error_code)); } else { timeoutId = setTimeout(() => setupLongPolling(resolve, reject), 1000); } } catch (error) { reject(error); } } async function iziGetIsBound() { if (inpostPayDebug) { console.log("iziGetIsBound"); } return new Promise((resolve, reject) => { setupLongPolling(resolve, reject); }); } async function iziGetOrderComplete() { if (inpostPayDebug) { console.log("iziGetOrderComplete"); } console.log("asddsa"); return new Promise(function(resolve, reject) { $.ajax({ url: document.location.origin + "/inpost/izi-get-order-complete", type: 'GET', cache: false, xhrFields: { withCredentials: true }, success: function(response) { resolve(response); } }); }); } async function iziBindingDelete() { if (inpostPayDebug) { console.log("iziBindingDelete"); } return new Promise(function(resolve, reject) { $.ajax({ url: document.location.origin + "/inpost/izi-binding-delete", type: 'GET', cache: false, xhrFields: { withCredentials: true }, success: function() { resolve(); } }); }); } function iziCanBeBound(productId) { if (inpostPayDebug) { console.log("iziCanBeBound", productId); } return true; } async function iziAddToCart(id) { if (inpostPayDebug) { console.log("iziAddToCart", id); } $('#add-to-basket').trigger('submit'); $.ajax({ url: '/inpost/izi-refresh-basket', type: 'GET', dataType: 'json', xhrFields: { withCredentials: true }, }) .done(function() { }); } function iziMobileLink() { if (inpostPayDebug) { console.log("iziMobileLink"); } return new Promise(function(resolve, reject) { $.ajax({ url: document.location.origin + "/inpost/izi-mobile-link", type: 'GET', cache: false, xhrFields: { withCredentials: true }, success: function(response) { resolve(response); } }); }); } jQuery(document).ready(function() { if ($('#submitKoszykForm').length > 0) { $.ajax({ url: '/inpost/izi-refresh-basket', type: 'GET', xhrFields: { withCredentials: true }, dataType: 'json' }) .done(function() { }); } });