UNPKG

@mnpay/qpay

Version:
2 lines (1 loc) 2.88 kB
(function(c,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("axios"),require("zod")):typeof define=="function"&&define.amd?define(["exports","axios","zod"],d):(c=typeof globalThis<"u"?globalThis:c||self,d(c.qpay={},c.axios,c.zod))})(this,function(c,d,y){"use strict";const p="https://api.qpay.mn";var f=(e=>(e.v2="v2",e.v1="v1",e))(f||{}),s=(e=>(e.authenticate="/auth/token",e.refresh="/auth/refresh",e.invoice="/invoice",e.payment="/payment/:payment_id",e.paymentCheck="/payment/check",e.paymentCancel="/payment/cancel/:payment_id",e.paymentRefund="/payment/refund/:payment_id",e.paymentList="/payment/list",e.create="/bill/create",e.createWithTransaction="/bill/create_with_transaction",e.check="/bill/check",e.getInvoice="/invoice/:invoice_id",e))(s||{});const l=e=>e,i=(...e)=>l(...e),v=i((e,{setStore:t})=>async o=>{const r=Buffer.from(`${o.username}:${o.password}`).toString("base64"),a=await e.post(s.authenticate,void 0,{headers:{Authorization:`Basic ${r}`}});return t({accessToken:a.data.access_token,expiresIn:new Date(a.data.expires_in*1e3),refreshToken:a.data.refresh_token}),a}),m=(e,t)=>{const o=/:([_a-zA-Z0-9]+)/g;return e.replace(o,(r,a)=>(typeof(t==null?void 0:t[a])=="string"||typeof(t==null?void 0:t[a])=="number"?t[a]:"").toString())},T=i(e=>t=>e.post(s.invoice,t)),_=i(e=>t=>e.get(m(s.getInvoice,t))),I=i(e=>t=>e.delete(m(s.getInvoice,t))),g=i(e=>t=>e.get(m(s.payment,t))),w=i(e=>t=>e.post(s.paymentCheck,t)),b=i(e=>t=>{const{payment_id:o,...r}=t;return e.delete(m(s.paymentCancel,{payment_id:o}),{data:r})}),C=i(e=>t=>{const{payment_id:o,...r}=t;return e.delete(m(s.paymentRefund,{payment_id:o}),{data:r})}),x=i(e=>t=>e.post(s.paymentList,t)),z=i((e,{store:t,setStore:o})=>async r=>{const a=y.z.string().parse(r.refreshToken??t.refreshToken),n=await e.post(s.refresh,void 0,{headers:{Authorization:`Bearer ${a}`}});return o({accessToken:n.data.access_token,expiresIn:new Date(n.data.expires_in*1e3),refreshToken:n.data.refresh_token}),n}),A=e=>{const t=e.version??f.v2,o=`${e.baseUrl??p}/${t}`,r=d.create({baseURL:o}),a={...e,baseUrl:o},n={store:a,setStore:u=>{Object.assign(a,u)}},k=v(r,n),h=z(r,n),$=T(r,n),B=_(r,n),U=I(r,n),D=g(r,n),L=w(r,n),S=b(r,n),j=C(r,n),G=x(r,n);return r.interceptors.request.use(async u=>(u.url===s.authenticate||u.url===s.refresh||(n.store.accessToken&&n.store.refreshToken&&n.store.expiresIn?n.store.expiresIn.getTime()<Date.now()&&await h({refreshToken:n.store.refreshToken}):await k({username:n.store.username,password:n.store.password}),u.headers.set("Authorization",`Bearer ${n.store.accessToken}`)),u)),{api:r,authenticate:k,refreshToken:h,createInvoice:$,getInvoice:B,cancelInvoice:U,getPayment:D,checkPayment:L,cancelPayment:S,refundPayment:j,getPaymentList:G}};c.QpayApiVersion=f,c.QpayRequestPath=s,c.qpayDefaultBaseUrl=p,c.useQpay=A,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});