(()=>{/*!
 * Copyright 2024-2025 Simply.IN Sp. z o.o.
 *
 * NOTICE OF LICENSE
 * Licensed under the EUPL-1.2 or later.
 * You may not use this work except in compliance with the Licence.
 * Copy of the Licence is available at: https://joinup.ec.europa.eu/software/page/eupl
 * It is bundled with this package in the file LICENSE.txt
 *
 * @author    Simply.IN Sp. z o.o.
 * @copyright 2024-2025 Simply.IN Sp. z o.o.
 * @license   https://joinup.ec.europa.eu/software/page/eupl
 */var e=window.jQuery,o=window.simplyin;e(function(){window.SimplyInPayment={errorMsgBlik:null,successMsgBlik:null,cc:null,successMsg:null,errorMsg:null,ott_corect:!1,blik_corect:!1,inputs:[],newImg:!1,card_interval:null,duplicated_card:null,init:function(){this.inlinePaymentOptionLogo(),this.setupCardForm(),this.setupBlikInputs(),this.setupPaymentOptionChange(),this.setupBlikEvents(),this.setupOrderStatusCheck(),this.bindPaymentMethodChecker(),this.autoSelectPayment()},inlinePaymentOptionLogo:function(){e('input[type="radio"][data-module-name^="simplyin"]').each(function(){let t=e(this).closest(".payment-option");if(!t.length)return;let i=t.find("label").first().css({display:"flex",alignItems:"center",gap:"8px"}),n=t.find('img[src*="/simplyin/views/img/"]').first();if(!n.length||(n.css({maxHeight:"20px",width:"auto"}),i.find("img").length))return;let a=n.closest("label");i.prepend(n),a.length&&!a.is(i)&&a.children().length===0&&a.text().trim()===""&&a.remove()})},bindGeneralConfirmationEvent:function(){let t=this,i=document.querySelector("#payment-confirmation button");if(!i)return;let n=a=>{let s=e(".simplyin-payment-option:checked"),r=e("#simplyin-payment-error");if(!s.length)return a.preventDefault(),a.stopPropagation(),a.stopImmediatePropagation(),r&&(r.html(t.l("Please select a payment method")),r.fadeIn(),e(".simplyin-payment-option-item").addClass("select-method")),!1;r&&r.fadeOut()};i._simplyinGeneralHandler&&i.removeEventListener("click",i._simplyinGeneralHandler,{capture:!0}),i.addEventListener("click",n,{capture:!0}),i._simplyinGeneralHandler=n},unbindGeneralConfirmationEvent:function(){let t=document.querySelector("#payment-confirmation button");t&&t._simplyinGeneralHandler&&(t.removeEventListener("click",t._simplyinGeneralHandler,{capture:!0}),delete t._simplyinGeneralHandler)},bindPaymentMethodChecker:function(){let t=this;e(document).on("change","input[name=payment-option]",function(){if(e(this).data("module-name")&&e(this).data("module-name").startsWith("simplyin")){if(e(".simplyin-payment-option:checked").length==0)return t.bindGeneralConfirmationEvent(),!0}else return t.unbindGeneralConfirmationEvent(),!0})},l:function(t){return typeof o.labels<"u"&&o.labels[t]?o.labels[t]:t},setupCardForm:function(){let t=this;t.duplicated_card&&clearTimeout(t.duplicated_card),t.duplicated_card=setTimeout(function(){if(e('#simplyin-add-card-payment-method input[name="simplyin-payment-option"]').length&&e("#card-number").find("div").length==0){let i=e('#simplyin-add-card-payment-method input[name="simplyin-payment-option"]').data("iframe-url"),a=e('#simplyin-add-card-payment-method input[name="simplyin-payment-option"]').data("integrator-id"),s=i,r=document.createElement("script");r.src=s,r.onload=()=>t.initCardForm(a),document.head.appendChild(r),t.watchCardFocus()}},150)},watchCardFocus:function(){var t=this;this._cardFocusObserver&&this._cardFocusObserver.disconnect();var i=document.querySelectorAll("#card-holder, #card-number, #expiry-date, #verification-code");if(i.length){this._cardFocusObserver=new MutationObserver(function(){t.hideCardErrors()});for(var n=0;n<i.length;n++)this._cardFocusObserver.observe(i[n],{attributes:!0,attributeFilter:["class"],subtree:!0})}},hideCardErrors:function(){e(".ccIframe-component.has-focus").length&&e(this.errorMsg).hide()},initCardForm:function(t){try{this.cc=CCIframe({integratorId:t});let i={fonts:[{family:"Inter",src:"url(https://fonts.gstatic.com/s/inter/v18/UcCo3FwrK3iLTcviYwYZ8UA3.woff2) format('woff2')",style:"normal",weight:"100 900"}],styles:{base:{color:"rgba(0, 0, 0, 0.8)",fontFamily:"Inter",fontSize:"14px"}}};this.cc.createComponent("cardHolder","#card-holder",i),this.cc.createComponent("cardNumber","#card-number",i),this.cc.createComponent("cardExp","#expiry-date",i),this.cc.createComponent("cardCvv","#verification-code",i),this.successMsg="#card-success-message-1",this.errorMsg="#form-error-1"}catch{}},setupBlikInputs:function(){this.successMsgBlik="#blik-success-message-1",this.errorMsgBlik="#blik-form-error-1",this.inputs=".simplyin-blik-digit-input";let t=this;e(document).on("click",this.inputs,function(){e('input[name="simplyin-payment-option"][data-payment-type="blik"]').is(":checked")||e('input[name="simplyin-payment-option"][data-payment-type="blik"]').prop("checked",!0).trigger("change")}),e(document).on("input",this.inputs,function(){let i=e(this),n=e(t.inputs),a=n.index(this),s=i.val().replace(/\D/g,"");i.val(s.substring(0,1)),s&&a<n.length-1&&e(n[a+1]).focus()}),e(document).on("keydown",this.inputs,function(i){if(i.key==="Backspace"){let n=e(this),a=e(t.inputs),s=a.index(this);!n.val()&&s>0&&e(a[s-1]).focus()}}),e(document).on("paste",this.inputs,function(i){i.preventDefault();let n=e(t.inputs),a=e(this),s=n.index(this),r=(i.originalEvent.clipboardData||window.clipboardData).getData("text").replace(/\D/g,"");if(!r)return;[...r].slice(0,n.length).forEach((c,d)=>{e(n[d]).val(c)});let l=Math.min(r.length,n.length)-1;e(n[l]).focus(),e(n[n.length-1]).trigger("change")})},getBlikCode:function(){return e(this.inputs).map(function(){return e(this).val()}).get().join("")},checkBlikCode:function(){let t=this;return this.getBlikCode().length===6},setPaymentsTerm:function(t){let i=this,n=t.data("regulationsurl"),a=t.data("gdprurl"),s=t.data("payment-provider-name"),r=i.l("By paying, I accept the terms and [conditions] and [privacy policy] of %s");r=r.replace(/\[(.*?)\]/,`<a class="simplyin-payment-provider-url" href="${n}" target="_blank">$1</a>`),r=r.replace(/\[(.*?)\]/,`<a class="simplyin-payment-provider-url" href="${a}" target="_blank">$1</a>`),r=r.replace("%s",s),e(".simplyin-payment-provider-footer").html(`<p>${r}</p>`)},setupPaymentOptionChange:function(){let t=this;e(document).on("change","#simplyin-save-card",function(){e(this).is(":checked")?e(".simplyin-card-name").fadeIn():e(".simplyin-card-name").fadeOut()}),e(document).on("change",'input[name="simplyin-payment-option"]',i=>{let n=e(i.currentTarget),a=n.data("payment-type"),s={action:"selectPaymentMethod",ajax:1,ajax_token:o.ajax_token,cart_token:o.cart_token,payment_id:n.val(),payment_type:a,payment_provider:n.data("payment-provider-name"),provider_image:n.data("provider-image"),logo:n.data("logo")};t.setPaymentsTerm(n),a==="card"?(s.card_name=n.data("card-name"),s.last_digits=n.data("last-digits"),s.card_provider=n.data("card-provider"),this.unbindCardConfirmationEvents(),this.unbindBlikConfirmationEvents()):a==="blik"?(this.unbindCardConfirmationEvents(),this.bindBlikConfirmationEvents()):a==="add-card"&&(this.bindCardConfirmationEvents(),this.unbindBlikConfirmationEvents()),e.ajax({url:o.controllers.payment_ajax,method:"POST",data:s,dataType:"json"}),t.unbindGeneralConfirmationEvent(),e("#simplyin-payment-error").hide(),e(".simplyin-payment-option-item").removeClass("select-method")})},setupBlikEvents:function(){let t=this;e(document).on("focus",".simplyin-blik-digit-input",()=>{e(".simplyin-blik-digit-input").removeClass("valid error"),e(".simplyin-blik-error").hide()}),e(document).on("change",'input[name="simplyin-payment-option"]',function(){e(this).data("payment-type")!=="blik"?(e(".simplyin-blik-container").hide(),e(".simplyin-blik-digit-input").removeClass("valid error").val(""),e("#blik-form-error-1").hide()):e(".simplyin-blik-container").show()}),e(document).on("keyup",".simplyin-blik-digit-input",()=>{let i=this.getBlikCode();if(t.checkBlikCode(),i.length===6){let n={action:"enterBlikCode",ajax:1,ajax_token:o.ajax_token,cart_token:o.cart_token,blik_code:i};e(".simplyin-blik-digit-input").addClass("valid"),e(".simplyin-blik-error").hide(),e.ajax({url:o.controllers.payment_ajax,method:"POST",data:n,dataType:"json"}).done(a=>{a.status==="success"&&e(".simplyin-blik-error").hide()})}}),e(document).on("focusout",".simplyin-blik-inputs-grid",i=>{e(i.relatedTarget).closest(".simplyin-blik-inputs-grid").length||(this.checkBlikCode()===!1?(e(".simplyin-blik-digit-input").removeClass("valid").addClass("error"),e(".simplyin-blik-error").show()):(e(".simplyin-blik-digit-input").removeClass("error").addClass("valid"),e(".simplyin-blik-error").hide()))})},bindCardConfirmationEvents:function(){let t=this,i=document.querySelector("#payment-confirmation button");if(!i)return;let n=a=>{if(this.ott_corect)return!0;a.preventDefault(),a.stopPropagation(),a.stopImmediatePropagation(),e(this.errorMsg).hide(),e(this.successMsg).hide(),this.cc.createToken().then(s=>{if(s&&s.oneTimeToken){e(this.successMsg).show();let r={action:"selectPaymentMethod",ajax:1,ajax_token:o.ajax_token,cart_token:o.cart_token,payment_id:e('input[name="simplyin-payment-option"]:checked').val(),payment_type:"add-card",payment_provider:e('input[name="simplyin-payment-option"]:checked').data("payment-provider-name"),provider_image:e('input[name="simplyin-payment-option"]:checked').data("provider-image"),logo:e('input[name="simplyin-payment-option"]:checked').data("logo")};e("#simplyin-save-card").is(":checked")?(r.card_name=e("#card-name").val(),r.card_save="1"):(r.card_name="",r.card_save="0"),r.last_digits=s.last4digits,r.card_provider=s.organization,r.ott=s.oneTimeToken,this.unbindCardConfirmationEvents(),e.ajax({url:o.controllers.payment_ajax,method:"POST",data:r,dataType:"json"}).done(()=>{this.ott_corect=!0,setTimeout(()=>{e("#payment-confirmation button").click()},300)})}else e(this.errorMsg).text(t.l("Invalid tokenization response")),e(this.errorMsg).show()}).catch(s=>{e(this.errorMsg).text(t.l("Problem processing card")),e(this.errorMsg).show()})};i.addEventListener("click",n,{capture:!0}),i._simplyinHandler=n},unbindCardConfirmationEvents:function(){let t=document.querySelector("#payment-confirmation button");t&&t._simplyinHandler&&(t.removeEventListener("click",t._simplyinHandler,{capture:!0}),delete t._simplyinHandler)},bindBlikConfirmationEvents:function(){let t=this,i=document.querySelector("#payment-confirmation button");if(!i)return;let n=a=>{if(this.blik_corect)return!0;if(a.preventDefault(),a.stopPropagation(),a.stopImmediatePropagation(),e(this.errorMsgBlik).hide(),e(this.successMsgBlik).hide(),!this.checkBlikCode()){e(this.errorMsgBlik).text(t.l("Invalid BLIK code")),e(this.errorMsgBlik).show();return}let s=this.getBlikCode(),r={action:"selectPaymentMethod",ajax:1,ajax_token:o.ajax_token,cart_token:o.cart_token,payment_id:e('input[name="simplyin-payment-option"]:checked').val(),payment_type:"blik",blik_code:s,payment_provider:e('input[name="simplyin-payment-option"]:checked').data("payment-provider-name"),provider_image:e('input[name="simplyin-payment-option"]:checked').data("provider-image"),logo:e('input[name="simplyin-payment-option"]:checked').data("logo")};this.unbindBlikConfirmationEvents(),e.ajax({url:o.controllers.payment_ajax,method:"POST",data:r,dataType:"json"}).done(l=>{l.success===!0?(this.blik_corect=!0,setTimeout(()=>{e("#payment-confirmation button").click()},300)):(e(this.errorMsgBlik).text(t.l("BLIK code rejected")),e(this.errorMsgBlik).show())}).fail(()=>{e(this.errorMsgBlik).text(t.l("Problem with BLIK processing")),e(this.errorMsgBlik).show()})};i.addEventListener("click",n,{capture:!0}),i._simplyinBlikHandler=n},unbindBlikConfirmationEvents:function(){let t=document.querySelector("#payment-confirmation button");t&&t._simplyinBlikHandler&&(t.removeEventListener("click",t._simplyinBlikHandler,{capture:!0}),delete t._simplyinBlikHandler)},setupOrderStatusCheck:function(){if(typeof o.order_sended<"u"&&!o.order_status_paid){if(o.payment_status=="PAID"){e("#status-success").show().css("display","flex"),e("#status-pending").hide();return}e("#status-pending").show().css("display","flex");var t=0,i=100;let n=setInterval(()=>{if(t++,t>i){clearInterval(n),e("#status-error").show().css("display","flex"),e("#status-pending").hide();return}let a={id_cart:o.id_cart,ajax:1,ajax_token:o.ajax_token,cart_token:o.cart_token,action:"checkOrderStatus",id_module:o.id_module,payment_check_token:o.payment_check_token};e.ajax({url:o.controllers.payment_ajax,method:"POST",data:a,dataType:"json"}).done(s=>{s.status==="PAID"?(e("#status-success").show().css("display","flex"),e("#status-pending").hide(),clearInterval(n)):(s.status=="REJECTED"||s.status=="FAILED"||s.status=="FAILD")&&(e("#status-error").show().css("display","flex"),e("#status-pending").hide(),clearInterval(n))})},3e3)}},autoSelectPayment:function(){let t=this;e('input[type="radio"][data-module-name^="simplyin"]').length&&e('input[type="radio"][data-module-name^="simplyin"]').prop("checked",!0).trigger("change")}},(o.checkout=="classic"||o.checkout=="hummingbird")&&window.SimplyInPayment.init()});})();
