UNPKG

@mnpay/hipay

Version:
2 lines (1 loc) 5.08 kB
(function(r,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("axios")):typeof define=="function"&&define.amd?define(["exports","axios"],d):(r=typeof globalThis<"u"?globalThis:r||self,d(r.hipay={},r.axios))})(this,function(r,d){"use strict";var x=Object.defineProperty;var F=(r,d,y)=>d in r?x(r,d,{enumerable:!0,configurable:!0,writable:!0,value:y}):r[d]=y;var A=(r,d,y)=>F(r,typeof d!="symbol"?d+"":d,y);const y="v2";var a=(e=>(e.accessToken="/:version/auth/token",e.cardGet="/:version/card/get/:cardId",e.cardAdd="/:version/card/init",e.cardRemove="/:version/card/remove/:cardId",e.payment="/:version/payment",e.checkout="/checkout",e.getCheckout="/checkout/get/:checkoutId",e))(a||{}),o=(e=>(e.accessToken="accessToken",e.cardGet="cardGet",e.cardAdd="cardAdd",e.cardRemove="cardRemove",e.payment="payment",e.checkout="checkout",e.getCheckout="getCheckout",e))(o||{}),u=(e=>(e.accessToken="[Hipay] токен авах үед алдаа гарлаа",e.cardGet="[Hipay] кард лавлах үед алдаа гарлаа",e.cardAdd="[Hipay] кард нэмэх үед алдаа гарлаа",e.cardRemove="[Hipay] кард хасах үед алдаа гарлаа",e.payment="[Hipay] төлбөрийн гүйлгээ хийх үед алдаа гарлаа",e.checkout="[Hipay] төлбөрийн нэхэмжлэл үүсгэх үед алдаа гарлаа",e.getCheckout="[Hipay] төлбөрийн нэхэмжлэл лавлах үед алдаа гарлаа",e))(u||{}),w=(e=>(e.mnt="MNT",e.usd="USD",e))(w||{});const v={[o.accessToken]:{method:"post",url:a.accessToken,dataTransferKey:"data",errorTitle:u.accessToken},[o.cardGet]:{method:"get",url:a.cardGet,dataTransferKey:"pathParams",errorTitle:u.cardGet},[o.cardAdd]:{method:"post",url:a.cardAdd,dataTransferKey:"data",errorTitle:u.cardAdd},[o.cardRemove]:{method:"post",url:a.cardRemove,dataTransferKey:"pathParams",errorTitle:u.cardRemove},[o.payment]:{method:"post",url:a.payment,dataTransferKey:"data",errorTitle:u.payment},[o.checkout]:{method:"post",url:a.checkout,dataTransferKey:"data",errorTitle:u.checkout},[o.getCheckout]:{method:"get",url:a.getCheckout,dataTransferKey:"pathParams",errorTitle:u.getCheckout}};class g extends Error{constructor(s,c={code:0,description:"unexpected error",details:[]},i){const h=v[s],_=`${i??h.errorTitle}: ${JSON.stringify(c)}`;super(_);A(this,"requestName");A(this,"responseData");A(this,"title");this.requestName=s,this.title=h.errorTitle,this.responseData=c}}const $=(e,n)=>{if(n.code===1||n.code==="1")return n;throw n.code==="0"||n.code===0?new g(e,n):new g(e)},C=(e,n)=>{const s=/:([a-zA-Z0-9]+)/g;return e.replace(s,(c,i)=>(typeof(n==null?void 0:n[i])=="string"||typeof(n==null?void 0:n[i])=="number"?n[i]:"").toString())},k=(e,n,s,c)=>{const i=v[e],h=v[e].method.toUpperCase(),_=s.config.version;return async p=>{const T=(c==null?void 0:c(p))??p,R=C(i.url,{...T,version:_});return $(e,(await(async()=>{try{return await n({method:h,url:R,...i.dataTransferKey==="data"?{data:T}:{},..."access_token"in T?{headers:{Authorization:`Bearer ${T.access_token}`}}:{}})}catch{throw new g(e)}})()).data)}},b=e=>{const{baseURL:n}=e,s=d.create({baseURL:n}),c={config:e},i=k(o.accessToken,s,c,t=>({...t,client_id:(t==null?void 0:t.client_id)??e.client_id,client_secret:(t==null?void 0:t.client_secret)??e.client_secret,code:(t==null?void 0:t.code)??e.code,grant_type:(t==null?void 0:t.grant_type)??"authorization_code",redirect_uri:(t==null?void 0:t.redirect_uri)??""})),h=async t=>await i(t??e),_=k(o.cardGet,s,c),p=k(o.getCheckout,s,c),T=k(o.cardRemove,s,c),R=k(o.cardAdd,s,c,t=>({...t,entityId:t.entityId??e.client_id,redirect_uri:t.redirect_uri??"",return_uri:t.return_uri??""})),I=k(o.checkout,s,c,t=>({...t,entityId:t.entityId??e.client_id,currency:t.currency??w.mnt})),z=k(o.payment,s,c,t=>{var l;return{...t,access_token:t.access_token??((l=c.token)==null?void 0:l.access_token)??"",entityId:t.entityId??e.client_id,ipaddress:t.ipaddress??""}}),G=async t=>{var K,P,O,S,U;const l=new Date,f=(K=c.token)!=null&&K.expires?new Date((P=c.token)==null?void 0:P.expires):void 0,m=l.valueOf()+5*60*1e3<((f==null?void 0:f.valueOf())??0);if((O=c.token)!=null&&O.access_token&&m)return{access_token:(S=c.token)==null?void 0:S.access_token,expires:(U=c.token)==null?void 0:U.access_token};const{access_token:D,expires:j}=await i(t??e);c.token={access_token:D,expires:j*1e3}};return s.interceptors.request.use(async t=>{var m;const l=C(a.accessToken,e);return!((m=t.url)!=null&&m.startsWith(l))&&(await G(),c.token&&(t.headers.Authorization=`Bearer ${c.token.access_token}`)),t},async t=>await Promise.reject(t)),{getAccessToken:h,loadAccessToken:G,getCard:_,addCard:R,removeCard:T,checkout:I,getCheckout:p,payment:z,getCardAddFormUrl:()=>`${e.baseURL}/${e.version}/card/form`,resetToken:()=>{c.token=void 0}}};r.HipayCurrency=w,r.HipayError=g,r.HipayErrorTitle=u,r.HipayRequestName=o,r.HipayUrl=a,r.createHipay=b,r.createHipayRequestHandler=k,r.defaultVersion=y,r.getHipayUrlPath=C,r.requestConfig=v,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});