(function(){!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.ES6Promise=t()}(this,function(){"use strict";function e(e){var t=typeof e;return null!==e&&("object"===t||"function"===t)}function t(e){return"function"==typeof e}function n(e){V=e}function o(e){W=e}function i(){return function(){return process.nextTick(p)}}function r(){return"undefined"!=typeof J?function(){J(p)}:c()}function a(){var e=0,t=new $(p),n=document.createTextNode("");return t.observe(n,{characterData:!0}),function(){n.data=e=++e%2}}function s(){var e=new MessageChannel;return e.port1.onmessage=p,function(){return e.port2.postMessage(0)}}function c(){var e=setTimeout;return function(){return e(p,1)}}function p(){for(var e=0;e<B;e+=2){var t=Z[e],n=Z[e+1];t(n),Z[e]=void 0,Z[e+1]=void 0}B=0}function l(){try{var e=require,t=e("vertx");return J=t.runOnLoop||t.runOnContext,r()}catch(e){return c()}}function u(e,t){var n=arguments,o=this,i=new this.constructor(h);void 0===i[te]&&q(i);var r=o._state;return r?!function(){var e=n[r-1];W(function(){return I(r,i,e,o._result)})}():_(o,i,e,t),i}function d(e){var t=this;if(e&&"object"==typeof e&&e.constructor===t)return e;var n=new t(h);return S(n,e),n}function h(){}function f(){return new TypeError("You cannot resolve a promise with itself")}function m(){return new TypeError("A promises callback cannot return that same promise.")}function y(e){try{return e.then}catch(e){return re.error=e,re}}function g(e,t,n,o){try{e.call(t,n,o)}catch(e){return e}}function P(e,t,n){W(function(e){var o=!1,i=g(n,t,function(n){o||(o=!0,t!==n?S(e,n):C(e,n))},function(t){o||(o=!0,T(e,t))},"Settle: "+(e._label||" unknown promise"));!o&&i&&(o=!0,T(e,i))},e)}function v(e,t){t._state===oe?C(e,t._result):t._state===ie?T(e,t._result):_(t,void 0,function(t){return S(e,t)},function(t){return T(e,t)})}function w(e,n,o){n.constructor===e.constructor&&o===u&&n.constructor.resolve===d?v(e,n):o===re?(T(e,re.error),re.error=null):void 0===o?C(e,n):t(o)?P(e,n,o):C(e,n)}function S(t,n){t===n?T(t,f()):e(n)?w(t,n,y(n)):C(t,n)}function b(e){e._onerror&&e._onerror(e._result),M(e)}function C(e,t){e._state===ne&&(e._result=t,e._state=oe,0!==e._subscribers.length&&W(M,e))}function T(e,t){e._state===ne&&(e._state=ie,e._result=t,W(b,e))}function _(e,t,n,o){var i=e._subscribers,r=i.length;e._onerror=null,i[r]=t,i[r+oe]=n,i[r+ie]=o,0===r&&e._state&&W(M,e)}function M(e){var t=e._subscribers,n=e._state;if(0!==t.length){for(var o=void 0,i=void 0,r=e._result,a=0;a<t.length;a+=3)o=t[a],i=t[a+n],o?I(n,o,i,r):i(r);e._subscribers.length=0}}function A(){this.error=null}function k(e,t){try{return e(t)}catch(e){return ae.error=e,ae}}function I(e,n,o,i){var r=t(o),a=void 0,s=void 0,c=void 0,p=void 0;if(r){if(a=k(o,i),a===ae?(p=!0,s=a.error,a.error=null):c=!0,n===a)return void T(n,m())}else a=i,c=!0;n._state!==ne||(r&&c?S(n,a):p?T(n,s):e===oe?C(n,a):e===ie&&T(n,a))}function D(e,t){try{t(function(t){S(e,t)},function(t){T(e,t)})}catch(t){T(e,t)}}function E(){return se++}function q(e){e[te]=se++,e._state=void 0,e._result=void 0,e._subscribers=[]}function x(e,t){this._instanceConstructor=e,this.promise=new e(h),this.promise[te]||q(this.promise),H(t)?(this.length=t.length,this._remaining=t.length,this._result=new Array(this.length),0===this.length?C(this.promise,this._result):(this.length=this.length||0,this._enumerate(t),0===this._remaining&&C(this.promise,this._result))):T(this.promise,N())}function N(){return new Error("Array Methods must be provided an Array")}function U(e){return new x(this,e).promise}function R(e){var t=this;return new t(H(e)?function(n,o){for(var i=e.length,r=0;r<i;r++)t.resolve(e[r]).then(n,o)}:function(e,t){return t(new TypeError("You must pass an array to race."))})}function O(e){var t=this,n=new t(h);return T(n,e),n}function L(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function j(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function F(e){this[te]=E(),this._result=this._state=void 0,this._subscribers=[],h!==e&&("function"!=typeof e&&L(),this instanceof F?D(this,e):j())}function K(){var e=void 0;if("undefined"!=typeof global)e=global;else if("undefined"!=typeof self)e=self;else try{e=Function("return this")()}catch(e){throw new Error("polyfill failed because global object is unavailable in this environment")}var t=e.Promise;if(t){var n=null;try{n=Object.prototype.toString.call(t.resolve())}catch(e){}if("[object Promise]"===n&&!t.cast)return}e.Promise=F}var Y=void 0;Y=Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)};var H=Y,B=0,J=void 0,V=void 0,W=function(e,t){Z[B]=e,Z[B+1]=t,B+=2,2===B&&(V?V(p):ee())},z="undefined"!=typeof window?window:void 0,X=z||{},$=X.MutationObserver||X.WebKitMutationObserver,G="undefined"==typeof self&&"undefined"!=typeof process&&"[object process]"==={}.toString.call(process),Q="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,Z=new Array(1e3),ee=void 0;ee=G?i():$?a():Q?s():void 0===z&&"function"==typeof require?l():c();var te=Math.random().toString(36).substring(16),ne=void 0,oe=1,ie=2,re=new A,ae=new A,se=0;return x.prototype._enumerate=function(e){for(var t=0;this._state===ne&&t<e.length;t++)this._eachEntry(e[t],t)},x.prototype._eachEntry=function(e,t){var n=this._instanceConstructor,o=n.resolve;if(o===d){var i=y(e);if(i===u&&e._state!==ne)this._settledAt(e._state,t,e._result);else if("function"!=typeof i)this._remaining--,this._result[t]=e;else if(n===F){var r=new n(h);w(r,e,i),this._willSettleAt(r,t)}else this._willSettleAt(new n(function(t){return t(e)}),t)}else this._willSettleAt(o(e),t)},x.prototype._settledAt=function(e,t,n){var o=this.promise;o._state===ne&&(this._remaining--,e===ie?T(o,n):this._result[t]=n),0===this._remaining&&C(o,this._result)},x.prototype._willSettleAt=function(e,t){var n=this;_(e,void 0,function(e){return n._settledAt(oe,t,e)},function(e){return n._settledAt(ie,t,e)})},F.all=U,F.race=R,F.resolve=d,F.reject=O,F._setScheduler=n,F._setAsap=o,F._asap=W,F.prototype={constructor:F,then:u,catch:function(e){return this.then(null,e)}},F.polyfill=K,F.Promise=F,F});var e=window.Promise||ES6Promise.Promise;if(!window.location.origin){window.location.origin=window.location.protocol+"//"+window.location.hostname+(window.location.port?":"+window.location.port:"")}var t,n,o,i,r,s,c,p,l,u,d,h,f,m,y={}.hasOwnProperty;var g,P;var v=function(){};var w="https://js.tappaysdk.com";var S=w+"/tpdirect/v2_2_1/view";var b;var C={FUNCTION:"function",API:"api",EXCEPTION:"exception"};var T=function(e,n,i,a){var s=new XMLHttpRequest;var c={name:n,type:i,app_id:t.appID||"",app_key:t.appKey||"",server_type:t._type||"",sdk_version:b||"test",get_prime_type:a===true?"with form":a===false?"without form":"",platform_type:"web",response:e,user_info:{hostname:window.location.hostname,origin:window.location.origin,referrer:document.referrer,href:window.location.href,android_merchant_id:typeof t.paymentRequestApi==="undefined"?"":t.paymentRequestApi.androidPayTapPayMerchantID,android_pub_key:typeof t.paymentRequestApi==="undefined"?"":t.paymentRequestApi.androidPayPublickKey,apple_merchant_id:typeof t.applePay==="undefined"?"":t.applePay.applePayMerchantID}};var c=JSON.stringify(c);s.open("POST",r+o.log,true);s.setRequestHeader("Content-type","application/json");s.send(c)};if(r="https://js.tappaysdk.com",s="https://js.tappaysdk.com",c=!!/tappayapis\.com$/.test("undefined"!=typeof window&&null!==window?window.location.host:void 0),l=document.querySelectorAll('script[src^="'+r+'"]'),l.length<=0){return console.warn("[TapPay] It looks like TPDirect.js is not being loaded from https://js.tappaysdk.com.")}else{b="v"+l[0].src.split("/v")[1]}if("withCredentials"in new XMLHttpRequest){u=true}else if(window.XDomainRequest){u=true}else{u=false;T({msg:"[TapPay] It looks like browser not support cors."},"CORS",C.FUNCTION);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(e){return h=window.location.hostname,f=window.location.origin,m=window.location.port,y=window.location.protocol,e={},e.version=1,e.isDoubleLoaded=false,e.setPublishableKey=function(t,n,o){e.appID=t;e.appKey=n;e._type=o;e.endPoint=o=="production"?"https://prod.tappayapis.com":"https://sandbox.tappayapis.com"},e.setupSDK=function(t,n,i,r){e.appID=t;e.appKey=n;e._type=i;e.endPoint=i=="production"?"https://prod.tappayapis.com":"https://sandbox.tappayapis.com";if(window.location.origin===w){h=r.hostname;f=r.origin;e.endPoint=w;o.getPrime=i=="production"?"/tpdirect/production/getprime":"/tpdirect/sandbox/getprime"}},t=e}(t);this.TPDirect.card=function(e,t){return t={},t.createToken=function(t,n,o,i,r){if(!e.validate.cardNumber(t)){return r({card:{},clientip:"",msg:"Wrong Card Format",status:41})}if(!e.validate.expiry(n,o)){return r({card:{},clientip:"",msg:"Expired Card",status:2013})}if(!e.validate.ccv(i)){return r({card:{},clientip:"",msg:"Wrong Card Format",status:41})}if(!e.validate.sdkError()){return r({card:{},clientip:"",msg:"SDK Loading Error",status:-1})}var a={appid:e.appID,appkey:e.appKey,appname:h,cardnumber:t,cardduedate:"20"+o+n,cardccv:i,url:f,port:m,protocol:y};var s="jsonString="+JSON.stringify(a);getPrimeRequest(s).then(function(e){if(e.status!=0){return r(e)}r({status:e.status,msg:e.msg,card:{prime:e.card.prime,issuer:e.cardinfo.issuer,lastfour:e.cardinfo.lastfour,bincode:e.cardinfo.bincode,funding:e.cardinfo.funding,type:e.cardinfo.type,level:e.cardinfo.level,country:e.cardinfo.country,countrycode:e.cardinfo.countrycode},clientip:e.clientip})})},t.setup=function(t,n){if(typeof t==="string"){t=document.querySelector(t)}if(!t){return console.warn("[TapPay] TPDirect.card.setup(element): Missing element param")}var o={appKey:e.appKey,appID:e.appID,serverType:e._type,hostname:window.location.hostname,origin:window.location.origin,style:n};P=document.createElement("iframe");P.setAttribute("frameborder","0");P.setAttribute("allowtransparency","true");P.setAttribute("scrolling","no");P.setAttribute("style","border: none; margin: 0px; padding: 0px; width: 1px; min-width: 100%; overflow: hidden; display: block; height: 24px;");if(n&&n.lineHeight){P.style.height=n.lineHeight}P.src=S+"?"+encodeURI(JSON.stringify(o));t.appendChild(P);window.addEventListener("message",_,false)},t.onUpdate=function(e){if(typeof e!=="function"){return console.error("[TapPay] TPDirect.card.onUpdate(callback): callback in not a function")}v=e},t.getPrime=function(e){if(typeof e!=="function"){return console.error("[TapPay] TPDirect.card.getPrime(callback): callback is not a function")}if(!P){T({msg:"[TapPay] You need to call TPDirect.card.setup(element) first"},"getPrime",C.FUNCTION,true);return console.error("[TapPay] You need to call TPDirect.card.setup(element) first")}g=e;P.contentWindow.postMessage(JSON.stringify({event:"createToken"}),w)},n=t}(t,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(n,i){i={};i.nowShippingMethod=null;i.sessionCallback={};i.paymentRequest=null;i.merchant={};i.consumer={};i.cart={};i.getSessionEndpoint=r;i.applePayMerchantID="";i.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 false}return true};i.buildSession=function(e,n){if(location.protocol!="https:"){return console.warn("[TapPay] Trying to call an ApplePaySession API from an insecure document.")}i.applePayMerchantID=n||"";i.session=new ApplePaySession(1,e);i.session.onvalidatemerchant=function(e){var n=e.validationURL;var o="appId="+t.appID+"&"+"appKey="+t.appKey+"&"+"appName="+h+"&"+"appleMerchantId="+i.applePayMerchantID+"&"+"merchant_domain="+h+"&"+"validationURL="+n+"&"+"tappay_endpoint="+t.endPoint;i.getApplePaySession(o).then(function(e){i.session.completeMerchantValidation(e)})};return i.session};i.setupPayment=function(e,t,n){if(e==null&&t==null&&n==null){return console.warn("[TapPay] TPDirect.applePay.setupPayment(merchant, consumer, cart), Lost Parameter")}i.merchant=e;i.consumer=t;i.cart=n;i.paymentRequest=i.preparePaymentRequest();if(i.merchant.shippingMethods!=null&&i.merchant.shippingMethods.length>0){i.nowShippingMethod=i.merchant.shippingMethods[0]}return i};i.startPayment=function(){if(location.protocol!="https:"){if(i.sessionCallback.onFailure){i.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.")}i.session=new ApplePaySession(1,i.paymentRequest);i.session.onvalidatemerchant=function(e){if(i.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.")}i.sessionCallback.onValidateMerchant(e)};i.session.onpaymentauthorized=function(e){if(i.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")}i.sessionCallback.onPaymentAuthorized(e)};i.session.onshippingmethodselected=function(e){if(i.merchant.shippingMethods.length>0){i.nowShippingMethod=e.shippingMethod}if(i.merchant.shippingMethods!=null&&i.merchant.shippingMethods.length>0&&i.sessionCallback.onShippingMethodSelected==null){console.log("[TapPay] You Can Implement 'TPDirect.applePay.onShippingMethodSelected' And Listen User Select Shipping Method Behavior");return i.completeShippingMethodSelection(ApplePaySession.STATUS_SUCCESS,i.cart)}i.sessionCallback.onShippingMethodSelected(e)};i.session.onshippingcontactselected=function(e){if(i.sessionCallback.onShippingContactSelected==null){console.log("[TapPay] You Can Implement 'TPDirect.applePay.onShippingContactSelected' And Listen User Select Shipping Contact Behavior");return i.completeShippingContactSelection(ApplePaySession.STATUS_SUCCESS,i.nowShippingMethod,i.cart)}i.sessionCallback.onShippingContactSelected(e)};i.session.onpaymentmethodselected=function(e){if(i.sessionCallback.onPaymentMethodSelected==null){console.log("[TapPay] You Can Implement 'TPDirect.applePay.onPaymentMethodSelected' And Linsten User Select PaymentMethod Behavior");return i.completePaymentMethodSelection(i.cart)}i.sessionCallback.onPaymentMethodSelected(e)};i.session.oncancel=function(e){if(i.sessionCallback.onCancel==null){return console.log("[TapPay] You Can Implement 'TPDirect.applePay.onCancel' And Listen User Cancel Behavior")}i.sessionCallback.onCancel(e)};i.session.begin()};i.getApplePaySession=function(n){return new e(function(e,r){var a=new XMLHttpRequest;a.addEventListener("load",function(t){if(this.status>=200&&this.status<300){var n=JSON.parse(a.response);if(n.status!==0){T(JSON.parse(a.response),i.getSessionEndpoint+o.getApplePaySaeesion,C.API)}e(n)}else{r({status:this.status,statusText:a.statusText})}},false);a.addEventListener("error",function(e){r({status:this.status,statusText:a.statusText})},false);a.addEventListener("abort",function(e){r({status:this.status,statusText:a.statusText})},false);a.open("POST",i.getSessionEndpoint+o.getApplePaySaeesion,true);a.setRequestHeader("x-api-key",t.appKey);a.setRequestHeader("Content-Type","application/x-www-form-urlencoded");a.send(n)})};i.onValidateMerchant=function(e){i.sessionCallback.onValidateMerchant=e;return i};i.onPaymentAuthorized=function(e){i.sessionCallback.onPaymentAuthorized=e;return i};i.onShippingMethodSelected=function(e){i.sessionCallback.onShippingMethodSelected=e;return i};i.onShippingContactSelected=function(e){i.sessionCallback.onShippingContactSelected=e;return i};i.onPaymentMethodSelected=function(e){i.sessionCallback.onPaymentMethodSelected=e;return i};i.onFailure=function(e){i.sessionCallback.onFailure=callback;return i};i.onCancel=function(e){i.sessionCallback.onCancel=e;return i};i.completePayment=function(e){i.session.completePayment(e)};i.completePaymentMethodSelection=function(e){i.cart=e||i.cart;var t=i.preparePaymentItems(i.cart.paymentItems,i.nowShippingMethod);i.session.completePaymentMethodSelection(t.total,t.lineItems)};i.completeShippingContactSelection=function(e,t,n){if(e==ApplePaySession.STATUS_SUCCESS){i.cart=n||i.cart;i.nowShippingMethod=t||i.nowShippingMethod}var o=i.preparePaymentItems(i.cart.paymentItems,i.nowShippingMethod);i.session.completeShippingContactSelection(e,i.nowShippingMethod,o.total,o.lineItems)};i.completeShippingMethodSelection=function(e,t){if(e==ApplePaySession.STATUS_SUCCESS){i.cart=t||i.cart}var n=i.preparePaymentItems(i.cart.paymentItems,i.nowShippingMethod);i.session.completeShippingMethodSelection(e,n.total,n.lineItems)};i.completeMerchantValidation=function(e){i.session.completeMerchantValidation(e)};i.preparePaymentRequest=function(){var e=i.preparePaymentItems(i.cart.paymentItems,i.nowShippingMethod);var t={};t.countryCode=i.merchant.countryCode||"TW";t.currencyCode=i.merchant.currencyCode||"TWD";t.shippingMethods=i.merchant.shippingMethods;t.lineItems=e.lineItems;t.total=e.total;t.supportedNetworks=i.merchant.supportedNetworks;t.merchantCapabilities=i.merchant.merchantCapabilities;t.requiredShippingContactFields=i.consumer.requiredShippingContactFields;t.requiredBillingContactFields=i.consumer.requiredBillingContactFields;if(i.consumer.billingContact!=null){t.billingContact=i.consumer.billingContact}if(i.consumer.shippingContact!=null){t.shippingContact=i.consumer.shippingContact}return t};i.preparePaymentItems=function(e,t){var n=0;var o=[];for(var r=0;r<e.length;r++){var a=e[r];o.push(a);n+=parseInt(a.amount)}if(i.nowShippingMethod!=null&&i.nowShippingMethod.amount!=null){var s={label:"Shipping",amount:i.nowShippingMethod.amount};o.push(s);n+=parseInt(s.amount)}return{lineItems:o,total:{label:i.merchant.merchantName,amount:n+""}}};return i}(t,i);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,t){var n,o,i=e;i+="",t+="";return!!/^\d+$/.test(i)&&(!!/^\d+$/.test(t)&&(1<=i&&i<=12&&(2===t.length&&(t=t<70?"20"+t:"19"+t),4===t.length&&(o=new Date(t,i),n=new Date,o.setMonth(o.getMonth()-1),o.setMonth(o.getMonth()+1,1),o>n))))},luhnCheck:function(e){var t,n,o,i,r,a;for(o=!0,i=0,n=(e+"").split("").reverse(),r=0,a=n.length;r<a;r++)t=n[r],t=parseInt(t,10),(o=!o)&&(t*=2),t>9&&(t-=9),i+=t;return i%10===0},cardTypes:function(e){var t,n={},o,i;for(t=i=50;i<=59;t=++i){n[t]="MasterCard"}for(a=o=40;o<=49;t=++o){n[t]="Visa"}return n[34]=n[37]="American Express",n[60]=n[62]=n[64]=n[65]="Discover",n[35]="JCB",n[30]=n[36]=n[38]=n[39]="Diners Club",n}(),cardType:function(e){e+="";return this.cardTypes[e.slice(0,2)]||"Unknown"},sdkError:function(){return!TPDirect.isDoubleLoaded&&l.length>0&&u}}}();this.api=function(e){return e={},e.getPrime="/tpc/directpay/getprime",e.getApplePaySaeesion="/apple-pay/get-session",e.log="/log",o=e}(o);this.getPrimeRequest=function(n){return new e(function(e,i){var r=new XMLHttpRequest;r.addEventListener("load",function(n){if(this.status>=200&&this.status<300){var i=JSON.parse(r.response);if(i.status!==0){var a=window.location.origin===w?true:false;T(JSON.parse(r.response),t.endPoint+o.getPrime,C.API,a)}e(i)}else{e({card:{},clientip:"",msg:n,status:-3})}},false);r.addEventListener("error",function(t){e({card:{},clientip:"",msg:t,status:-3})},false);r.addEventListener("abort",function(t){e({card:{},clientip:"",msg:t,status:-3})},false);r.open("POST",t.endPoint+o.getPrime,true);r.setRequestHeader("x-api-key",t.appKey);r.setRequestHeader("Content-type","application/x-www-form-urlencoded");r.send(n)})};function _(e){if(e.origin!==w){return}var t=JSON.parse(e.data);if(t.event==="createTokenDone"){var n=t.result;g(n)}else if(t.event==="update"){v(t)}}})();