(()=>{/*!
 * 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.setupCardForm(),this.setupBlikInputs(),this.setupPaymentOptionChange(),this.setupBlikEvents(),this.setupOrderStatusCheck(),this.bindPaymentMethodChecker(),this.autoSelectPayment()},bindGeneralConfirmationEvent:function(){let t=this,n=document.querySelector("#payment-confirmation button");if(!n)return;let i=r=>{let s=e(".simplyin-payment-option:checked"),a=e("#simplyin-payment-error");if(!s.length)return r.preventDefault(),r.stopPropagation(),r.stopImmediatePropagation(),a&&(a.html(t.l("Please select a payment method")),a.fadeIn(),e(".simplyin-payment-option-item").addClass("select-method")),!1;a&&a.fadeOut()};n._simplyinGeneralHandler&&n.removeEventListener("click",n._simplyinGeneralHandler,{capture:!0}),n.addEventListener("click",i,{capture:!0}),n._simplyinGeneralHandler=i},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 n=e('#simplyin-add-card-payment-method input[name="simplyin-payment-option"]').data("iframe-url"),r=e('#simplyin-add-card-payment-method input[name="simplyin-payment-option"]').data("integrator-id"),s=n,a=document.createElement("script");a.src=s,a.onload=()=>t.initCardForm(r),document.head.appendChild(a),t.watchCardFocus()}},150)},watchCardFocus:function(){var t=this;this._cardFocusObserver&&this._cardFocusObserver.disconnect();var n=document.querySelectorAll("#card-holder, #card-number, #expiry-date, #verification-code");if(n.length){this._cardFocusObserver=new MutationObserver(function(){t.hideCardErrors()});for(var i=0;i<n.length;i++)this._cardFocusObserver.observe(n[i],{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 n={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",n),this.cc.createComponent("cardNumber","#card-number",n),this.cc.createComponent("cardExp","#expiry-date",n),this.cc.createComponent("cardCvv","#verification-code",n),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 n=e(this),i=e(t.inputs),r=i.index(this),s=n.val().replace(/\D/g,"");n.val(s.substring(0,1)),s&&r<i.length-1&&e(i[r+1]).focus()}),e(document).on("keydown",this.inputs,function(n){if(n.key==="Backspace"){let i=e(this),r=e(t.inputs),s=r.index(this);!i.val()&&s>0&&e(r[s-1]).focus()}}),e(document).on("paste",this.inputs,function(n){n.preventDefault();let i=e(t.inputs),r=e(this),s=i.index(this),a=(n.originalEvent.clipboardData||window.clipboardData).getData("text").replace(/\D/g,"");if(!a)return;[...a].slice(0,i.length).forEach((c,d)=>{e(i[d]).val(c)});let l=Math.min(a.length,i.length)-1;e(i[l]).focus(),e(i[i.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 n=this,i=t.data("regulationsurl"),r=t.data("gdprurl"),s=t.data("payment-provider-name"),a=n.l("By paying, I accept the terms and [conditions] and [privacy policy] of %s");a=a.replace(/\[(.*?)\]/,`<a class="simplyin-payment-provider-url" href="${i}" target="_blank">$1</a>`),a=a.replace(/\[(.*?)\]/,`<a class="simplyin-payment-provider-url" href="${r}" target="_blank">$1</a>`),a=a.replace("%s",s),e(".simplyin-payment-provider-footer").html(`<p>${a}</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"]',n=>{let i=e(n.currentTarget),r=i.data("payment-type"),s={action:"selectPaymentMethod",ajax:1,ajax_token:o.ajax_token,cart_token:o.cart_token,payment_id:i.val(),payment_type:r,payment_provider:i.data("payment-provider-name"),provider_image:i.data("provider-image"),logo:i.data("logo")};t.setPaymentsTerm(i),r==="card"?(s.card_name=i.data("card-name"),s.last_digits=i.data("last-digits"),s.card_provider=i.data("card-provider"),this.unbindCardConfirmationEvents(),this.unbindBlikConfirmationEvents()):r==="blik"?(this.unbindCardConfirmationEvents(),this.bindBlikConfirmationEvents()):r==="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 n=this.getBlikCode();if(t.checkBlikCode(),n.length===6){let i={action:"enterBlikCode",ajax:1,ajax_token:o.ajax_token,cart_token:o.cart_token,blik_code:n};e(".simplyin-blik-digit-input").addClass("valid"),e(".simplyin-blik-error").hide(),e.ajax({url:o.controllers.payment_ajax,method:"POST",data:i,dataType:"json"}).done(r=>{r.status==="success"&&e(".simplyin-blik-error").hide()})}}),e(document).on("focusout",".simplyin-blik-inputs-grid",n=>{e(n.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,n=document.querySelector("#payment-confirmation button");if(!n)return;let i=r=>{if(this.ott_corect)return!0;r.preventDefault(),r.stopPropagation(),r.stopImmediatePropagation(),e(this.errorMsg).hide(),e(this.successMsg).hide(),this.cc.createToken().then(s=>{if(s&&s.oneTimeToken){e(this.successMsg).show();let a={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")?(a.card_name=e("#card-name").val(),a.card_save="1"):(a.card_name="",a.card_save="0"),a.last_digits=s.last4digits,a.card_provider=s.organization,a.ott=s.oneTimeToken,this.unbindCardConfirmationEvents(),e.ajax({url:o.controllers.payment_ajax,method:"POST",data:a,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()})};n.addEventListener("click",i,{capture:!0}),n._simplyinHandler=i},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,n=document.querySelector("#payment-confirmation button");if(!n)return;let i=r=>{if(this.blik_corect)return!0;if(r.preventDefault(),r.stopPropagation(),r.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(),a={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:a,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()})};n.addEventListener("click",i,{capture:!0}),n._simplyinBlikHandler=i},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,n=100;let i=setInterval(()=>{if(t++,t>n){clearInterval(i),e("#status-error").show().css("display","flex"),e("#status-pending").hide();return}let r={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:r,dataType:"json"}).done(s=>{s.status==="PAID"?(e("#status-success").show().css("display","flex"),e("#status-pending").hide(),clearInterval(i)):(s.status=="REJECTED"||s.status=="FAILED"||s.status=="FAILD")&&(e("#status-error").show().css("display","flex"),e("#status-pending").hide(),clearInterval(i))})},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()});})();
