- Created a new template for the cart rule form with custom label, switch, and choice widgets. - Implemented the InPost Pay block in the order details template for displaying delivery method, APM, and VAT invoice request. - Added legacy support for the order details template to maintain compatibility with older PrestaShop versions.
1 line
4.9 KiB
JavaScript
1 line
4.9 KiB
JavaScript
!function(){"use strict";var e=()=>{const e={merchantClientId:"",basketBindingApiKey:void 0,unboundWidgetClicked:void 0,handleBasketEvent:()=>!1,apiBaseUrl:void 0,webView:!1,language:"pl"};return{setMerchantClientId:t=>{e.merchantClientId=t},setBasketBindingApiKey:t=>{e.basketBindingApiKey=t},setUnboundWidgetClicked:t=>{e.unboundWidgetClicked=t},setHandleBasketEvent:t=>{e.handleBasketEvent=t},setApiBaseUrl:t=>{e.apiBaseUrl=t},setWebView:t=>{e.webView=t},setLanguage:t=>{e.language=t},build:()=>{const t={};for(const n in e)"undefined"!==e[n]&&(t[n]=e[n]);return t}}};var t={orderConfirmationUrl:"inpost/v2/izi/merchant/order/confirmation-url",bindingKey:"inpost/v2/izi/merchant/basket/binding-key"};var n=e=>{const t=new URL(e),n=(e,n)=>{t.searchParams.set(e,n)};return{getURL:()=>t.toString(),addParam:n,addParams:e=>{Object.keys(e).forEach((t=>{n(t,e[t])}))}}};var r=(e,t,r=null,i={})=>{const{addParam:o,addParams:a,getURL:s}=n(e),d={method:t,headers:{"Content-Type":"application/json",...i}};null!==r&&(d.body=JSON.stringify(r));return{getResponse:()=>{const e=d,t=s();return fetch(t,e)},setParams:e=>{a(e)},setParam:(e,t)=>{o(e,t)}}};const i=window.inpostizi_backend_ajax_url;var o=(e,t,o=null,a={})=>{const{addParam:s,getURL:d}=n(i);return s("path",e),r(d(),t,o,a)};var a=async()=>{const{bindingKey:e}=t,{getResponse:n}=o(e,"GET"),r=await n();if(!r.ok&&404!==r.status)throw new Error("Error while fetching binding key");if(404!==r.status)return r.json()};var s=(e=!1)=>!1===e&&null!==window.inpostizi_binding_api_key?window.inpostizi_binding_api_key:!1!==e||window.inpostizi_fetch_binding_key?new Promise(((e,t)=>{a().then((t=>{e(t)})).catch((()=>{t(void 0)}))})):void 0;const d={productPageForm:"#add-to-cart-or-refresh",inpostIziButton:"inpost-izi-button",inpostIziProductButtonWrapper:".js-inpost-izi-product-btn-wrapper",inpostIziAddToCartAlert:".js-inpost-izi-add-to-cart-alert"};var c=()=>void 0!==window.inpost_izi_selectors_map?{...d,...window.inpost_izi_selectors_map}:d;var p=(e,t="success",n="")=>{const r=document.createElement("div");return r.classList.add("alert",`alert-${t}`,"w-100"),"string"==typeof n?r.classList.add(n):Array.isArray(n)&&n.forEach((e=>{r.classList.add(e)})),r.textContent=e,r};var u=e=>{const{inpostIziProductButtonWrapper:t,inpostIziAddToCartAlert:n}=c(),r=p(e.message,"danger",n.replace(".","")),i=document.querySelector(t);if(i){const e=i.querySelector(n);e&&e.remove(),i.prepend(r)}};var w=async e=>{const{getResponse:t,setParams:n}=r(prestashop.urls.pages.cart,"POST",null,{"X-Requested-With":"XMLHttpRequest","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"});n(e);const i=await t(),o=await i.json();if(o?.hasError&&o?.errors&&Array.isArray(o.errors)){const e=new Error(o.errors.join("\n"));throw setTimeout((()=>{u(e)})),Error("UNDELIVERABLE_PRODUCT")}return prestashop.emit("updateCart",{reason:{idProduct:o.id_product,idProductAttribute:o.id_product_attribute},resp:o}),s()};const l=(e=!1)=>new Promise(((t,n)=>{const r=s(e);r instanceof Promise?r.then((e=>{t(e)})).catch((()=>{n(void 0)})):t(r)}));var h=e=>new Promise(((t,n)=>{if(!e)return void l(!0).then((e=>{t(e)})).catch((e=>{n(e)}));const{productPageForm:r}=c(),i=document.querySelector(r);if(!i)return void n(new Error("Error while adding product to cart: product not found."));const{group:o,...a}=(e=>{const t=new FormData(e),n={},r=[];for(const[e,i]of t.entries())0===e.indexOf("group")?r.push({key:e,value:encodeURIComponent(i)}):n[e]=i;return n.group=r,n})(i),s={add:1,action:"update",ajax:1,...a};o.forEach((e=>{s[e.key]=e.value})),w(s).then((()=>{l(!0).then((e=>{t(e)})).catch((e=>{n(e)}))})).catch((e=>{e instanceof Error&&"UNDELIVERABLE_PRODUCT"===e.message?n(e):n(void 0)}))}));var g=async()=>{const{orderConfirmationUrl:e}=t,{getResponse:n}=o(e,"GET"),r=await n();if(!r.ok)throw new Error("NOK");return r.json()};var f=async e=>{if("basketProductChanged"===e||"basketDeleted"===e)prestashop.emit("updateCart",{reason:{linkAction:"refresh"},resp:{}});else if("orderCreated"===e)try{window.location.href=await g()}catch(e){return!1}return!0};let m;var v=()=>({init:e=>{m=window.InPostPayWidget.init(e)},refresh:()=>{if(void 0===m)throw new Error("Widget is not initialized yet, use init() method first");"function"!=typeof m.rerender?m.refresh():m.rerender()}});var y=()=>({init:()=>{const{init:t,refresh:n}=v(),{setMerchantClientId:r,setBasketBindingApiKey:i,setUnboundWidgetClicked:o,setHandleBasketEvent:a,setLanguage:d,build:c}=e();void 0!==window.prestashop.language.iso_code&&d(window.prestashop.language.iso_code),r(window.inpostizi_merchant_client_id),i(s()),o(h),a(f),t(c()),window.prestashop.on("updatedCart",(()=>n())),window.prestashop.on("updatedProduct",(()=>n()))}});window.prestashop??={},window.prestashop.inpostizi??={},window.prestashop.inpostizi.widget??=v(),"function"!=typeof window.handleInpostIziButtons&&(window.handleInpostIziButtons=window.prestashop.inpostizi.widget.refresh),document.addEventListener("DOMContentLoaded",(()=>{const{init:e}=y();e()}))}(); |