!function(){var e="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global,t=function(e,t){this.publicKey=e,this.PAY_URL="https://pay.mychoice2pay.com/",this.token=null,this.tokenInfo=null,this.tokenCardGateway=null,this.gatewaysList=[],this.gatewaySelected=null,this.APM_OPEN_TYPE_REDIRECT="redirect",this.APM_OPEN_TYPE_HTML="html",this.APM_OPEN_TYPE_IFRAME="iframe",this.APM_OPEN_TYPE_WINDOW="window",this.options={iframeId:t&&t.iframeId||"zru-iframe",htmlDivId:t&&t.htmlDivId||"zru-html-div",apmOpenType:t&&t.apmOpenType||"redirect",apmSettings:t&&t.apmSettings||{},windowsClosedCallback:t&&t.windowsClosedCallback||void 0,windowsFinishedCallback:t&&t.windowsFinishedCallback||void 0},this.__hideId(this.options.iframeId),this.__hideId(this.options.htmlDivId)};t.prototype.__request=function(e,t,a,s){var o=new XMLHttpRequest;if(s&&(o.onreadystatechange=function(){4==o.readyState&&s(JSON.parse(o.responseText))}),o.open(e,t,!!s),o.setRequestHeader("Authorization","PublicKey "+this.publicKey),o.setRequestHeader("Content-type","application/json"),o.send(a?JSON.stringify(a):null),!s)return JSON.parse(o.responseText)},t.prototype.__defineUrls=function(e){this.API_URL="https://api.zrupay.com/v1/pay/"+this.token+"/",this.API_HTML_URL=this.API_URL+"html/",this.API_CARD_URL=this.API_URL+"card/",this.REDIRECT_URL=this.PAY_URL+this.token+"/redirect/"},t.prototype.__checkIfToken=function(){if(!this.token)throw Error("Must call function setToken before")},t.prototype.__checkIfTokenCardGateway=function(){if(this.__checkIfToken(),!this.tokenCardGateway)throw Error("Must have a card gateway configured")},t.prototype.__selectGateway=function(e){this.__checkIfToken();for(var t=0;t<this.gatewaysList.length;t++)if(this.gatewaysList[t].code===e){this.gatewaySelected=this.gatewaysList[t];return}this.gatewaySelected=null},t.prototype.__receiveMessages=function(e,t){this.__receiveMessagesCallback=e;var a,s=window.addEventListener?"addEventListener":"attachEvent";(0,window[s])("attachEvent"==s?"onmessage":"message",e=>{"MC2P_DONE"==e.data||"ZRU_DONE"==e.data?(this.__hideId(t.id),this.__receiveMessagesCallback({success:!0})):"MC2P_CANCEL"==e.data||"MC2P_EXPIRED"==e.data||"ZRU_CANCEL"==e.data||"ZRU_EXPIRED"==e.data?(this.__hideId(t.id),this.__receiveMessagesCallback({success:!1,error:e.data})):e.data.number&&(this.__hideId(t.id),this.__receiveMessagesCallback({success:!1,error:e.data.number[0],messages:e.data.messages}))},!1)},t.prototype.__hideId=function(e){var t=document.getElementById(e);t&&(t.style.display="none")},t.prototype.__showIframe=function(e,t){if(!e)throw Error("Must have an iframe");e.setAttribute("src",t),e.style.display="block"},t.prototype.__showHtml=function(e,t,a){if(!e)throw Error("Must have an html div");var s=this.__request("GET",this.API_HTML_URL+t+"/",null,null);e.innerHTML=s.html,e.style.display="block",a&&document.querySelector("#"+e.id+" form").submit()},t.prototype.setToken=function(e){if(this.token=e,this.__defineUrls(e),this.tokenInfo=this.__request("GET",this.API_URL,null,null),this.tokenInfo.detail)throw this.token=null,Error(this.tokenInfo.detail);this.gatewaysList=this.tokenInfo.gateways,this.tokenCardGateway=null;for(var t=0;t<this.tokenInfo.gateways.length;t++)if("card"==this.tokenInfo.gateways[t].form){this.tokenCardGateway=this.tokenInfo.gateways[t];break}},t.prototype.getGateways=function(){return this.__checkIfToken(),this.gatewaysList},t.prototype.selectGateway=function(e,t,a){if(this.__selectGateway(e),!this.gatewaySelected)throw Error("Gateway not found");(t||this.options.htmlDivId)&&(document.getElementById(t||this.options.htmlDivId),this.__hideId(t||this.options.htmlDivId));var s=null;if(a||this.options.iframeId){var s=document.getElementById(a||this.options.iframeId);this.__hideId(a||this.options.iFrameId)}var o=this.REDIRECT_URL+this.gatewaySelected.code;switch(this.gatewaySelected.form){case"card":this.__showIframe(s,o);break;case"html":var i=this.options.apmSettings[this.gatewaySelected.code]||this.options.apmOpenType;switch(i){case this.APM_OPEN_TYPE_REDIRECT:document.location=o;break;case this.APM_OPEN_TYPE_HTML:this.__showHtml(null,e,!0);break;case this.APM_OPEN_TYPE_IFRAME:this.__showIframe(s,o);break;case this.APM_OPEN_TYPE_WINDOW:var n=(window.outerWidth-400)/2,l=(window.outerHeight-560)/2,r=window.open(o,e,"width=400,height=560,scrollbars=NO,top="+l+",left="+n),h=this,p=setInterval(function(){r.closed&&(clearInterval(p),h.options.windowsClosedCallback&&h.options.windowsClosedCallback({gateway:e}))},500);window.addEventListener("message",function(t){("MC2P_DONE"==t.data||"MC2P_CANCEL"==t.data||"MC2P_EXPIRED"==t.data||"ZRU_DONE"==t.data||"ZRU_CANCEL"==t.data||"ZRU_EXPIRED"==t.data)&&t.origin==h.PAY_URL&&(clearInterval(p),r.close(),h.options.windowsFinishedCallback&&h.options.windowsFinishedCallback({done:"MC2P_DONE"==t.data||"ZRU_DONE"==t.data,cancel:"MC2P_CANCEL"==t.data||"ZRU_CANCEL"==t.data,expired:"MC2P_EXPIRED"==t.data||"ZRU_EXPIRED"==t.data,gateway:e}))})}}},t.prototype.__requestApplePayInfo=function(){return this.__checkIfTokenCardGateway(),this.__request("POST",this.API_URL+"apple-pay/"+this.tokenCardGateway.code+"/info/",null,null)},t.prototype.__sendAppleData=function(t){this.__checkIfTokenCardGateway();var a=this;this.__request("POST",this.API_URL+"apple-pay/"+this.tokenCardGateway.code+"/",t,function(t){if(t.messages){let s={status:e.ApplePaySession.STATUS_FAILURE};a.__appleSession.completePayment(s),a.__appleCompleteCallback&&a.__appleCompleteCallback({success:!1,error:t.number[0],messages:t.messages})}else if("merchant-validated"==t.result)a.__appleSession.completeMerchantValidation(t.session);else{let o={status:e.ApplePaySession.STATUS_SUCCESS};a.__appleSession.completePayment(o),a.__appleCompleteCallback&&a.__appleCompleteCallback({success:!0})}})},t.prototype.canUseApplePay=function(){if(this.tokenCardGateway&&!this.tokenCardGateway.allow_apple_pay)return!1;try{return e.ApplePaySession&&e.ApplePaySession.canMakePayments()}catch(t){return!1}},t.prototype.startApplePay=function(){return new Promise(t=>{this.__appleCompleteCallback=t;var a=this.__requestApplePayInfo();this.__appleSession=new e.ApplePaySession(1,a.session),this.__appleSession.onvalidatemerchant=e=>{this.__sendAppleData({url:e.validationURL,domain:document.location.hostname})},this.__appleSession.onpaymentmethodselected=e=>{let t=a.order;this.__appleSession.completePaymentMethodSelection(t.newTotal,t.newLineItems)},this.__appleSession.onpaymentauthorized=e=>{this.__sendAppleData(e.payment.token)},this.__appleSession.oncancel=e=>{t({success:!1})},this.__appleSession.begin()})},t.prototype.__requestGooglePayInfo=function(){return this.__checkIfTokenCardGateway(),this.__request("POST",this.API_URL+"google-pay/"+this.tokenCardGateway.code+"/info/",null,null)},t.prototype.__sendGoogleData=function(e,t){this.__checkIfTokenCardGateway();var a=this;(t||this.options.iframeId)&&(document.getElementById(t||this.options.iframeId),this.__hideId(t||this.options.iFrameId)),this.__request("POST",this.API_URL+"google-pay/"+this.tokenCardGateway.code+"/",e,function(e){if(a.__googleCompleteCallback){if(e.messages)a.__googleCompleteCallback({success:!1,error:e.number[0],messages:e.messages});else if("verification-value-needed"==e.result){a.__showIframe(null,a.PAY_URL+a.token+"/cvc/"+a.tokenCardGateway.code+"?r="+e.response_id+"&n="+e.number_masked+"&w="+e.wallet_slug);var t=new Promise(e=>{a.__receiveMessages(e)});a.__googleCompleteCallback({success:!1,completed:t})}else a.__googleCompleteCallback({success:!0})}})},t.prototype.canUseGooglePay=function(){var t,a;return(!this.tokenCardGateway||!!this.tokenCardGateway.allow_google_pay)&&"google"in e&&!!(null===(a=null===(t=null==google?void 0:google.payments)||void 0===t?void 0:t.api)||void 0===a?void 0:a.PaymentsClient)},t.prototype.startGooglePay=function(e){return new Promise(t=>{this.__googleCompleteCallback=t;var a=this.__requestGooglePayInfo(),s=this,o=new google.payments.api.PaymentsClient({environment:this.tokenInfo.google_pay.environment});o.isReadyToPay({allowedPaymentMethods:["CARD","TOKENIZED_CARD"]}).then(function(i){i.result?o.loadPaymentData(a).then(function(t){s.__sendGoogleData(JSON.parse(t.paymentMethodData.tokenizationData.token),e)}).catch(function(e){t({success:!1})}):t({success:!1})})})},"undefined"==typeof exports||exports.nodeType?e.ZRU=t:("undefined"!=typeof module&&!module.nodeType&&module.exports&&(exports=module.exports=t),exports.ZRU=t)}();