(function(){var e,n,t,o,i,r,s,c,l,p,u,d,h,m,y={}.hasOwnProperty;if(i="https://js.tappaysdk.com",r="https://js.tappaysdk.com",s=!!/tappayapis\.com$/.test("undefined"!=typeof window&&null!==window?window.location.host:void 0),l=document.querySelectorAll('script[src^="'+i+'"]'),l.length<=0){return console.warn("[TapPay] It looks like TPDirect.js is not being loaded from https://js.tappaysdk.com.")}if("withCredentials"in new XMLHttpRequest){p=true}else if(window.XDomainRequest){p=true}else{p=false;return console.warn("[TapPay] It looks like browser not support cors.")}if(this.TPDirect){this.TPDirect.isDoubleLoaded=true;return console.warn("[TapPay] It looks like TPDirect.js was loaded more than one time.")}this.TPDirect=function(n){return d=window.location.hostname,h=window.location.origin,m=window.location.port,y=window.location.protocol,n={},n.version=1,n.isDoubleLoaded=false,n.setPublishableKey=function(e,t,a){n.appID=e;n.appKey=t;n.endPoint=a=="production"?"https://prod.tappayapis.com":"https://sandbox.tappayapis.com"},n.setupSDK=function(e,t,a){n.appID=e;n.appKey=t;n.endPoint=a=="production"?"https://prod.tappayapis.com":"https://sandbox.tappayapis.com"},e=n}(e);this.TPDirect.card=function(e,t){return t={},t.createToken=function(n,t,a,o,i){if(!e.validate.cardNumber(n)){return i({card:{},clientip:"",msg:"Wrong Card Format",status:41})}if(!e.validate.expiry(t,a)){return i({card:{},clientip:"",msg:"Expired Card",status:2013})}if(!e.validate.ccv(o)){return i({card:{},clientip:"",msg:"Wrong Card Format",status:41})}var r={appid:e.appID,appkey:e.appKey,appname:d,cardnumber:n,cardduedate:"20"+a+t,cardccv:o,url:h,port:m,protocol:y};u=i;var s="jsonString="+JSON.stringify(r);getPrimeRequest.send(s)},n=t}(e,n);this.TPDirect.consumer=function(){this.requiredShippingContactFields={};this.requiredBillingContactFields={};this.shippingContact=null;this.billingContact=null};this.TPDirect.merchant=function(){this.merchantCapabilities=["supports3DS"];this.acquirerMerchantID="";this.applePayMerchantIdentifier="";this.countryCode="TW";this.currencyCode="TWD";this.supportedNetworks=["amex","discover","masterCard","visa"];this.shippingMethods=null;this.merchantName=""};this.TPDirect.cart=function(){this.shippingType="shipping";this.paymentItems=[]};this.TPDirect.applePay=function(e,n){n={};n.nowShippingMethod=null;n.sessionCallback={};n.paymentRequest=null;n.merchant={};n.consumer={};n.cart={};n.checkAvailability=function(){if(!window.ApplePaySession){console.warn("[TapPay] Your Browser Did Not Support Apple Pay");return false}if(!ApplePaySession.canMakePayments){console.warn("[TapPay] User Can Not Use Apple Pay");return false}if(location.protocol!="https:"){console.warn("[TapPay] Trying to call an ApplePaySession API from an insecure document.")}return true};n.buildSession=function(e){if(location.protocol!="https:"){return console.warn("[TapPay] Trying to call an ApplePaySession API from an insecure document.")}return new ApplePaySession(1,e)};n.setupPayment=function(e,t,a){if(e==null&&t==null&&a==null){return console.warn("[TapPay] TPDirect.applePay.setupPayment(merchant, consumer, cart), Lost Parameter")}n.merchant=e;n.consumer=t;n.cart=a;n.paymentRequest=n.preparePaymentRequest();if(n.merchant.shippingMethods!=null&&n.merchant.shippingMethods.length>0){n.nowShippingMethod=n.merchant.shippingMethods[0]}return n};n.startPayment=function(){if(location.protocol!="https:"){if(n.sessionCallback.onFailure){n.sessionCallback.onFailure(-1,"Trying to call an ApplePaySession API from an insecure document")}return console.warn("[TapPay] Trying to call an ApplePaySession API from an insecure document.")}n.session=new ApplePaySession(1,n.paymentRequest);n.session.onvalidatemerchant=function(e){if(n.sessionCallback.onValidateMerchant==null){return console.warn("[TapPay] You Need To Implement 'TPDirect.applePay.onValidateMerchant' For Validate Merchant And Use Url To Send Apple Pay Session By Your Server.")}n.sessionCallback.onValidateMerchant(e)};n.session.onpaymentauthorized=function(e){if(n.sessionCallback.onPaymentAuthorized==null){return console.warn("[TapPay] You Need To Implement 'TPDirect.applePay.onPaymentAuthorized' Get Payment Data, Shipping Contact And Send Payment Data To TapPay Server Finished Payment")}n.sessionCallback.onPaymentAuthorized(e)};n.session.onshippingmethodselected=function(e){if(n.merchant.shippingMethods.length>0){n.nowShippingMethod=e.shippingMethod}if(n.merchant.shippingMethods!=null&&n.merchant.shippingMethods.length>0&&n.sessionCallback.onShippingMethodSelected==null){console.log("[TapPay] You Can Implement 'TPDirect.applePay.onShippingMethodSelected' And Listen User Select Shipping Method Behavior");return n.completeShippingMethodSelection(ApplePaySession.STATUS_SUCCESS,n.cart)}n.sessionCallback.onShippingMethodSelected(e)};n.session.onpaymentmethodselected=function(e){if(n.sessionCallback.onPaymentMethodSelected==null){console.log("[TapPay] You Can Implement 'TPDirect.applePay.onPaymentMethodSelected' And Linsten User Select PaymentMethod Behavior");return n.completePaymentMethodSelection(n.cart)}n.sessionCallback.onPaymentMethodSelected(e)};n.session.oncancel=function(e){if(n.sessionCallback.onCancel==null){return console.log("[TapPay] You Can Implement 'TPDirect.applePay.onCancel' And Listen User Cancel Behavior")}n.sessionCallback.onCancel(e)};n.session.begin()},n.onValidateMerchant=function(e){n.sessionCallback.onValidateMerchant=e;return n};n.onPaymentAuthorized=function(e){n.sessionCallback.onPaymentAuthorized=e;return n};n.onShippingMethodSelected=function(e){n.sessionCallback.onShippingMethodSelected=e;return n};n.onPaymentMethodSelected=function(e){n.sessionCallback.onPaymentMethodSelected=e;return n};n.onFailure=function(e){n.sessionCallback.onFailure=callback;return n};n.onCancel=function(e){n.sessionCallback.onCancel=e;return n};n.completePayment=function(e){n.session.completePayment(e)};n.completePaymentMethodSelection=function(e){n.cart=e||n.cart;var t=n.preparePaymentItems(n.cart.paymentItems,n.nowShippingMethod);n.session.completePaymentMethodSelection(t.total,t.lineItems)};n.completeShippingMethodSelection=function(e,t){if(e==ApplePaySession.STATUS_SUCCESS){n.cart=t||n.cart}var a=n.preparePaymentItems(n.cart.paymentItems,n.nowShippingMethod);n.session.completeShippingMethodSelection(e,a.total,a.lineItems)};n.completeMerchantValidation=function(e){n.session.completeMerchantValidation(e)};n.preparePaymentRequest=function(){var e=n.preparePaymentItems(n.cart.paymentItems,n.nowShippingMethod);var t={};t.countryCode=n.merchant.countryCode||"TW";t.currencyCode=n.merchant.currencyCode||"TWD";t.shippingMethods=n.merchant.shippingMethods;t.lineItems=e.lineItems;t.total=e.total;t.supportedNetworks=n.merchant.supportedNetworks;t.merchantCapabilities=n.merchant.merchantCapabilities;t.requiredShippingContactFields=n.consumer.requiredShippingContactFields;t.requiredBillingContactFields=n.consumer.requiredBillingContactFields;if(n.consumer.billingContact!=null){t.billingContact=n.consumer.billingContact}if(n.consumer.shippingContact!=null){t.shippingContact=n.consumer.shippingContact}return t};n.preparePaymentItems=function(e,t){var a=0;var o=[];for(var i=0;i<e.length;i++){var r=e[i];o.push(r);a+=parseInt(r.amount)}if(n.nowShippingMethod!=null&&n.nowShippingMethod.amount!=null){var s={label:"Shipping",amount:n.nowShippingMethod.amount};o.push(s);a+=parseInt(s.amount)}return{lineItems:o,total:{label:n.merchant.merchantName,amount:a+""}}};return n}(e,o);this.TPDirect.validate=function(){return{cardNumber:function(e){return e=(e+"").replace(/\s+|-/g,""),e.length>=10&&e.length<=16&&this.luhnCheck(e)},ccv:function(e){return/^\d+$/.test(e)&&e.length>=3&&e.length<=4},expiry:function(e,n){var t,a,o=e;o+="",n+="";return!!/^\d+$/.test(o)&&(!!/^\d+$/.test(n)&&(1<=o&&o<=12&&(2===n.length&&(n=n<70?"20"+n:"19"+n),4===n.length&&(a=new Date(n,o),t=new Date,a.setMonth(a.getMonth()-1),a.setMonth(a.getMonth()+1,1),a>t))))},luhnCheck:function(e){var n,t,a,o,i,r;for(a=!0,o=0,t=(e+"").split("").reverse(),i=0,r=t.length;i<r;i++)n=t[i],n=parseInt(n,10),(a=!a)&&(n*=2),n>9&&(n-=9),o+=n;return o%10===0},cardTypes:function(e){var n,t={},o,i;for(n=i=50;i<=59;n=++i){t[n]="MasterCard"}for(a=o=40;o<=49;n=++o){t[n]="Visa"}return t[34]=t[37]="American Express",t[60]=t[62]=t[64]=t[65]="Discover",t[35]="JCB",t[30]=t[36]=t[38]=t[39]="Diners Club",t}(),cardType:function(e){e+="";return this.cardTypes[e.slice(0,2)]||"Unknown"},sdkError:function(){return!TPDirect.isDoubleLoaded&&l.length>0&&p}}}();this.api=function(e){return e={},e.prime="/tpc/directpay/getprime",t=e}(t);this.getPrimeRequest=function(n){var t,a;return a=n,t=new XMLHttpRequest,t.addEventListener("load",function(e){var n=t.responseText;u(JSON.parse(n))},false),t.addEventListener("error",function(e){u({card:{},clientip:"",msg:e,status:-3})},false),t.addEventListener("abort",function(e){u({card:{},clientip:"",msg:e,status:-3})},false),{send:function(n){if(!TPDirect.validate.sdkError()){return u({card:{},clientip:"",msg:"SDK Loading Error",status:-1})}t.open("POST",e.endPoint+a,true);t.setRequestHeader("x-api-key",e.appKey);t.setRequestHeader("Content-type","application/x-www-form-urlencoded");t.send(n)}}}(this.api.prime)})();