@mnpay/bonum 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/@types/api.d.ts +225 -0
  2. package/@types/constants/apiUrls.d.ts +26 -0
  3. package/@types/constants/index.d.ts +1 -0
  4. package/@types/definitions/index.d.ts +1 -0
  5. package/@types/definitions/store.d.ts +11 -0
  6. package/@types/index.d.ts +6 -0
  7. package/@types/lib/api.d.ts +4 -0
  8. package/@types/lib/index.d.ts +1 -0
  9. package/@types/requests/auth.d.ts +11 -0
  10. package/@types/requests/card.d.ts +49 -0
  11. package/@types/requests/index.d.ts +6 -0
  12. package/@types/requests/invoice.d.ts +33 -0
  13. package/@types/requests/neo.d.ts +10 -0
  14. package/@types/requests/qr.d.ts +28 -0
  15. package/@types/requests/subscription.d.ts +96 -0
  16. package/@types/schemas/auth.d.ts +12 -0
  17. package/@types/schemas/card.d.ts +64 -0
  18. package/@types/schemas/common.d.ts +11 -0
  19. package/@types/schemas/config.d.ts +17 -0
  20. package/@types/schemas/index.d.ts +9 -0
  21. package/@types/schemas/invoice.d.ts +79 -0
  22. package/@types/schemas/neo.d.ts +5 -0
  23. package/@types/schemas/qr.d.ts +46 -0
  24. package/@types/schemas/subscription.d.ts +92 -0
  25. package/@types/schemas/webhook.d.ts +140 -0
  26. package/@types/test/_lib/liveTokenCache.d.ts +19 -0
  27. package/@types/test/_mocks/api.d.ts +241 -0
  28. package/@types/test/api.live.test.d.ts +1 -0
  29. package/@types/test/api.test.d.ts +1 -0
  30. package/@types/test/headers.test.d.ts +1 -0
  31. package/@types/test/setup.d.ts +1 -0
  32. package/@types/test/webhook.test.d.ts +1 -0
  33. package/@types/types/auth.d.ts +4 -0
  34. package/@types/types/card.d.ts +9 -0
  35. package/@types/types/common.d.ts +5 -0
  36. package/@types/types/config.d.ts +3 -0
  37. package/@types/types/index.d.ts +9 -0
  38. package/@types/types/invoice.d.ts +9 -0
  39. package/@types/types/neo.d.ts +3 -0
  40. package/@types/types/qr.d.ts +6 -0
  41. package/@types/types/subscription.d.ts +8 -0
  42. package/@types/types/webhook.d.ts +13 -0
  43. package/@types/utils/headers.d.ts +9 -0
  44. package/@types/utils/index.d.ts +2 -0
  45. package/@types/utils/webhook.d.ts +11 -0
  46. package/@types/vite.config.d.ts +2 -0
  47. package/@types/vitest.config.d.ts +2 -0
  48. package/CHANGELOG.md +57 -0
  49. package/README.md +486 -0
  50. package/dist/index.js +314 -0
  51. package/dist/index.umd.cjs +1 -0
  52. package/package.json +56 -0
package/dist/index.js ADDED
@@ -0,0 +1,314 @@
1
+ import { z as e } from "zod";
2
+ import { createHmac as t, timingSafeEqual as n } from "node:crypto";
3
+ import r from "axios";
4
+ //#region constants/apiUrls.ts
5
+ var i = "https://testapi.bonum.mn", a = "https://apis.bonum.mn", o = /* @__PURE__ */ function(e) {
6
+ return e.authenticate = "/bonum-gateway/ecommerce/auth/create", e.refresh = "/bonum-gateway/ecommerce/auth/refresh", e.paymentProviders = "/bonum-gateway/ecommerce/invoices/payment-providers", e.createInvoice = "/bonum-gateway/ecommerce/invoices", e.getInvoiceStatus = "/bonum-gateway/ecommerce/invoices/:invoiceId", e.setInvoicePaid = "/bonum-gateway/ecommerce/invoices/paid", e.createQrCode = "/mpay-service/merchant/transaction/qr/create", e.invoiceByQrCode = "/mpay-service/merchant/transaction/qr", e.payByCardTokenQr = "/mpay-service/merchant/transaction/qr/pay", e.createCardToken = "/mpay-service/merchant/cards/tokenize/request", e.purchaseWithCardToken = "/mpay-service/merchant/transaction/purchase", e.rollbackPurchase = "/mpay-service/merchant/transaction/rollback/:id", e.listPaymentPlans = "/mpay-service/merchant/values/payment-plans", e.subscribe = "/mpay-service/merchant/subscriptions/subscribe", e.getSubscriptions = "/mpay-service/merchant/subscriptions", e.unsubscribe = "/mpay-service/merchant/subscriptions/:subscriptionId", e.deleteSubscription = "/mpay-service/merchant/subscriptions/:subscriptionId/delete", e.changeTokenNewCard = "/mpay-service/merchant/subscriptions/:subscriptionId/change/create-new-token", e.changeTokenExisting = "/mpay-service/merchant/subscriptions/:subscriptionId/change", e.executePayment = "/mpay-service/merchant/subscriptions/:subscriptionId/execute", e.getNeoUserData = "/users/apps/share/requests", e.sendNeoMessage = "/messages/send", e;
7
+ }({}), s = e.object({
8
+ baseUrl: e.string().optional(),
9
+ appSecret: e.string(),
10
+ terminalId: e.string(),
11
+ checksumKey: e.string().optional(),
12
+ language: e.enum(["mn", "en"]).optional(),
13
+ initialTokens: e.object({
14
+ accessToken: e.string(),
15
+ refreshToken: e.string(),
16
+ expiresIn: e.number(),
17
+ refreshExpiresIn: e.number()
18
+ }).optional()
19
+ }), c = e.object({
20
+ tokenType: e.preprocess((e) => typeof e == "string" ? e.trim() : e, e.literal("Bearer")),
21
+ accessToken: e.string(),
22
+ expiresIn: e.number(),
23
+ refreshToken: e.string(),
24
+ refreshExpiresIn: e.number(),
25
+ unit: e.literal("SECONDS")
26
+ }), l = e.object({ accessToken: e.string() }), u = e.enum([
27
+ "QPAY",
28
+ "E_COMMERCE",
29
+ "WE_CHAT",
30
+ "SONO_SHOP"
31
+ ]), d = e.object({
32
+ provider: u,
33
+ enabled: e.boolean()
34
+ }), f = e.object({
35
+ image: e.string().optional(),
36
+ title: e.string(),
37
+ remark: e.string().optional(),
38
+ amount: e.number(),
39
+ count: e.number()
40
+ }), p = e.enum([
41
+ "NUMBER",
42
+ "PHONE",
43
+ "EMAIL",
44
+ "TEXT",
45
+ "ALL"
46
+ ]), m = e.object({
47
+ placeholder: e.string(),
48
+ type: p,
49
+ required: e.boolean()
50
+ }), h = e.object({
51
+ amount: e.number(),
52
+ callback: e.string(),
53
+ transactionId: e.string(),
54
+ expiresIn: e.number(),
55
+ providers: e.array(u).optional(),
56
+ items: e.array(f).optional(),
57
+ extras: e.array(m).optional(),
58
+ language: e.enum(["mn", "en"]).optional()
59
+ }), g = e.object({
60
+ invoiceId: e.string(),
61
+ followUpLink: e.string()
62
+ }), _ = e.object({
63
+ amount: e.number(),
64
+ transactionId: e.string(),
65
+ expiresIn: e.number()
66
+ }), v = e.object({
67
+ name: e.string(),
68
+ description: e.string(),
69
+ logo: e.string(),
70
+ link: e.string(),
71
+ appStoreId: e.string(),
72
+ androidPackageName: e.string()
73
+ }), y = e.object({
74
+ invoiceId: e.string(),
75
+ qrCode: e.string(),
76
+ qrImage: e.string(),
77
+ links: e.array(v)
78
+ }), b = e.object({ data: y }), x = e.object({
79
+ qrCode: e.string(),
80
+ transactionId: e.string()
81
+ }), S = e.object({ amount: e.number() }), C = e.object({
82
+ planId: e.number(),
83
+ cycleValue: e.string(),
84
+ cycles: e.number().nullable().optional(),
85
+ payNow: e.boolean().optional(),
86
+ custEmail: e.string().optional()
87
+ }), w = e.object({
88
+ callback: e.string(),
89
+ transactionId: e.string(),
90
+ payment: S.optional(),
91
+ subscription: C.optional(),
92
+ items: e.array(f).optional()
93
+ }), T = e.object({
94
+ followUpLink: e.string(),
95
+ id: e.string()
96
+ }), E = e.object({
97
+ id: e.number(),
98
+ code: e.string(),
99
+ name: e.string(),
100
+ icon: e.string(),
101
+ iBanCode: e.string(),
102
+ transferCode: e.string()
103
+ }), D = e.object({
104
+ amount: e.number(),
105
+ currency: e.string(),
106
+ transactionId: e.string()
107
+ }), O = e.object({
108
+ id: e.number(),
109
+ completedAt: e.string().optional(),
110
+ status: e.enum([
111
+ "SUCCESS",
112
+ "FAILED",
113
+ "QUEUED"
114
+ ]),
115
+ description: e.string().optional(),
116
+ cardStatus: e.enum(["ACTIVE", "INACTIVE"]).nullable().optional(),
117
+ respCode: e.string().nullable().optional()
118
+ }), k = e.enum([
119
+ "WEEKLY",
120
+ "MONTHLY",
121
+ "YEARLY"
122
+ ]), A = e.object({
123
+ planId: e.number(),
124
+ name: e.string(),
125
+ remark: e.string(),
126
+ createdAt: e.string(),
127
+ recurringType: k,
128
+ amount: e.number(),
129
+ status: e.enum(["ACTIVE", "INACTIVE"]),
130
+ cardCount: e.number(),
131
+ retryCount: e.number()
132
+ }), j = e.object({
133
+ planId: e.number(),
134
+ cycleValue: e.union([e.number(), e.string()]),
135
+ cycles: e.number().nullable().optional(),
136
+ payNow: e.boolean().optional(),
137
+ custEmail: e.string().optional()
138
+ }), M = e.object({
139
+ subscriptionId: e.number(),
140
+ subscribedAt: e.string(),
141
+ cardMask: e.string(),
142
+ plan: A,
143
+ nextBillAt: e.string(),
144
+ lastBilledAt: e.string(),
145
+ status: e.enum([
146
+ "ACTIVE",
147
+ "INACTIVE",
148
+ "CANCELLED"
149
+ ])
150
+ }), N = e.object({
151
+ callback: e.string(),
152
+ transactionId: e.string(),
153
+ items: e.array(f).optional()
154
+ }), P = e.object({ data: e.array(A) }), ee = e.object({
155
+ phoneNumber: e.string(),
156
+ message: e.string()
157
+ }), te = e.object({
158
+ traceId: e.string(),
159
+ errorCode: e.string().nullable().optional(),
160
+ error: e.string().nullable().optional(),
161
+ message: e.string().nullable().optional(),
162
+ data: e.any().nullable().optional(),
163
+ detail: e.string().nullable().optional(),
164
+ duration: e.number(),
165
+ status: e.number()
166
+ }), F = e.enum([
167
+ "PAYMENT",
168
+ "CARD-TOKEN",
169
+ "SUBSCRIPTION-PAYMENT",
170
+ "UNSUBSCRIBED"
171
+ ]), I = e.enum(["SUCCESS", "FAILED"]), L = e.object({
172
+ type: F,
173
+ status: I,
174
+ message: e.string().optional(),
175
+ body: e.unknown()
176
+ }), R = e.object({
177
+ amount: e.number(),
178
+ currency: e.string(),
179
+ transactionId: e.string(),
180
+ terminalId: e.string(),
181
+ completedAt: e.string().optional(),
182
+ invoiceId: e.string().optional(),
183
+ paymentVendor: e.string().optional(),
184
+ initType: e.string().optional(),
185
+ status: e.string().optional(),
186
+ "extras-inputs": e.array(e.unknown()).optional(),
187
+ extras: e.array(e.unknown()).optional(),
188
+ invoiceStatus: e.string().optional()
189
+ }), z = e.object({
190
+ subscriptionId: e.number(),
191
+ invoiceId: e.number(),
192
+ planId: e.number(),
193
+ transactionId: e.string(),
194
+ completedAt: e.string(),
195
+ amount: e.number(),
196
+ currency: e.string()
197
+ }), B = e.object({
198
+ id: e.number(),
199
+ code: e.string(),
200
+ name: e.string(),
201
+ icon: e.string()
202
+ }), V = e.object({
203
+ token: e.string(),
204
+ mask: e.string(),
205
+ expiry: e.string(),
206
+ bank: B,
207
+ transactionId: e.string(),
208
+ completedAt: e.string()
209
+ }), ne = e.object({
210
+ type: e.literal("PAYMENT"),
211
+ status: I,
212
+ message: e.string().optional(),
213
+ body: R
214
+ }), H = e.object({
215
+ type: e.literal("CARD-TOKEN"),
216
+ status: I,
217
+ message: e.string().optional(),
218
+ body: V
219
+ }), U = e.object({
220
+ type: e.literal("SUBSCRIPTION-PAYMENT"),
221
+ status: I,
222
+ message: e.string().optional(),
223
+ body: z
224
+ }), W = e.discriminatedUnion("type", [
225
+ ne,
226
+ H,
227
+ U
228
+ ]), G = (e, r, i) => {
229
+ let a = t("sha256", r).update(e).digest(), o = Buffer.from(i, "hex");
230
+ return o.length === a.length && n(a, o);
231
+ }, K = (e, t) => {
232
+ let n = t ?? e.language;
233
+ return n ? { "Accept-Language": n } : {};
234
+ }, q = (e) => ({ "X-CARD-TOKEN": e }), J = (e) => e, Y = (e) => e, X = (e, ...t) => {
235
+ let n = t[0];
236
+ return n ? Object.entries(n).reduce((e, [t, n]) => e.replaceAll(`:${t}`, n), e) : e;
237
+ }, Z = (e) => J(e), Q = (e) => Y(e), re = Q((e, { setStore: t, store: n }) => async () => {
238
+ let r = await e.get(o.authenticate, { headers: {
239
+ Authorization: `AppSecret ${n.appSecret}`,
240
+ "X-TERMINAL-ID": n.terminalId
241
+ } });
242
+ return t({
243
+ accessToken: r.data.accessToken,
244
+ expiresIn: new Date(Date.now() + r.data.expiresIn * 1e3),
245
+ refreshToken: r.data.refreshToken,
246
+ refreshExpiresIn: new Date(Date.now() + r.data.refreshExpiresIn * 1e3)
247
+ }), r;
248
+ }), ie = Q((e, { setStore: t, store: n }) => async () => {
249
+ let r = await e.get(o.refresh, { headers: { Authorization: `Bearer ${n.refreshToken}` } });
250
+ return t({ accessToken: r.data.accessToken }), r;
251
+ }), ae = Q((e) => async () => await e.get(o.paymentProviders)), oe = Z((e, { store: t }) => async (n) => {
252
+ let { language: r, ...i } = n;
253
+ return await e.post(o.createInvoice, i, { headers: K(t, r) });
254
+ }), se = Z((e) => async ({ invoiceId: t }) => await e.get(X(o.getInvoiceStatus, { invoiceId: t }))), ce = Z((e) => async ({ invoiceId: t }) => await e.get(o.setInvoicePaid, { params: { invoiceId: t } })), le = Z((e) => async (t) => await e.post(o.createQrCode, t)), ue = Z((e) => async (t) => await e.post(o.invoiceByQrCode, t)), de = Z((e, { store: t }) => async ({ cardToken: n, ...r }) => await e.put(o.payByCardTokenQr, r, { headers: {
255
+ ...q(n),
256
+ ...K(t)
257
+ } })), fe = Z((e, { store: t }) => async (n) => await e.post(o.createCardToken, n, { headers: K(t) })), pe = Z((e, { store: t }) => async ({ cardToken: n, ...r }) => await e.post(o.purchaseWithCardToken, r, { headers: {
258
+ ...q(n),
259
+ ...K(t)
260
+ } })), me = Z((e) => async ({ id: t, cardToken: n }) => await e.get(X(o.rollbackPurchase, { id: t }), { headers: q(n) })), he = Q((e) => async () => await e.get(o.listPaymentPlans)), $ = Z((e) => async ({ cardToken: t, ...n }) => await e.post(o.subscribe, n, { headers: q(t) })), ge = Z((e) => async ({ cardToken: t }) => await e.get(o.getSubscriptions, { headers: q(t) })), _e = Z((e) => async ({ subscriptionId: t, planId: n }) => await e.delete(X(o.unsubscribe, { subscriptionId: t }), { data: n == null ? {} : { planId: n } })), ve = Z((e) => async ({ subscriptionId: t, planId: n }) => await e.delete(X(o.deleteSubscription, { subscriptionId: t }), { data: n == null ? {} : { planId: n } })), ye = Z((e) => async ({ subscriptionId: t, ...n }) => await e.put(X(o.changeTokenNewCard, { subscriptionId: t }), n)), be = Z((e) => async ({ subscriptionId: t, newCardToken: n }) => await e.put(X(o.changeTokenExisting, { subscriptionId: t }), void 0, { headers: { "X-CARD-TOKEN": n } })), xe = Z((e) => async ({ subscriptionId: t }) => await e.put(X(o.executePayment, { subscriptionId: t }))), Se = Z((e) => async ({ requestId: t, mChatBaseUrl: n }) => await e.get(`${n}${o.getNeoUserData}?requestId=${t}`)), Ce = Z((e) => async ({ neoBaseUrl: t, ...n }) => await e.post(`${t}${o.sendNeoMessage}`, n)), we = (e) => {
261
+ let t = e.baseUrl ?? (e.isTestEnv ? "https://testapi.bonum.mn" : "https://apis.bonum.mn"), n = r.create({ baseURL: t }), i = { ...e }, a = {
262
+ store: i,
263
+ setStore: (e) => {
264
+ Object.assign(i, e);
265
+ }
266
+ };
267
+ e.initialTokens && (i.accessToken = e.initialTokens.accessToken, i.refreshToken = e.initialTokens.refreshToken, i.expiresIn = new Date(Date.now() + e.initialTokens.expiresIn * 1e3), i.refreshExpiresIn = new Date(Date.now() + e.initialTokens.refreshExpiresIn * 1e3));
268
+ let s = re(n, a), c = ie(n, a), l = null, u = () => (l ||= s().finally(() => {
269
+ l = null;
270
+ }), l), d = ae(n, a), f = oe(n, a), p = se(n, a), m = ce(n, a), h = le(n, a), g = ue(n, a), _ = de(n, a), v = fe(n, a), y = pe(n, a), b = me(n, a), x = he(n, a), S = $(n, a), C = ge(n, a), w = _e(n, a), T = ve(n, a), E = ye(n, a), D = be(n, a), O = xe(n, a), k = Se(n, a), A = Ce(n, a);
271
+ return n.interceptors.request.use(async (e) => {
272
+ if (e.url === o.authenticate || e.url === o.refresh) return e;
273
+ let t = Date.now();
274
+ if (a.store.accessToken && a.store.expiresIn) {
275
+ if (a.store.expiresIn.getTime() - 3e4 < t) try {
276
+ a.store.refreshToken && a.store.refreshExpiresIn && a.store.refreshExpiresIn.getTime() > t ? await c() : await s();
277
+ } catch {
278
+ await u();
279
+ }
280
+ } else await u();
281
+ return e.headers.set("Authorization", `Bearer ${a.store.accessToken}`), e;
282
+ }), {
283
+ api: n,
284
+ getToken: s,
285
+ refreshToken: c,
286
+ getPaymentProviders: d,
287
+ createInvoice: f,
288
+ getInvoiceStatus: p,
289
+ setInvoicePaid: m,
290
+ createQrCode: h,
291
+ invoiceByQrCode: g,
292
+ payByCardTokenQr: _,
293
+ createCardToken: v,
294
+ purchaseWithCardToken: y,
295
+ rollbackPurchase: b,
296
+ listPaymentPlans: x,
297
+ subscribe: S,
298
+ getSubscriptions: C,
299
+ unsubscribe: w,
300
+ deleteSubscription: T,
301
+ changeSubscriptionTokenNewCard: E,
302
+ changeSubscriptionTokenExisting: D,
303
+ executeSubscriptionPayment: O,
304
+ getNeoUserData: k,
305
+ sendNeoMessage: A,
306
+ verifyWebhookChecksum: (t, n) => {
307
+ let r = e.checksumKey?.trim();
308
+ if (!r) throw Error("Bonum checksumKey is not configured");
309
+ return G(t, r, n);
310
+ }
311
+ };
312
+ };
313
+ //#endregion
314
+ export { E as BankInfoSchema, te as BonumApiResponseSchema, s as BonumConfigSchema, o as BonumRequestPath, S as CardTokenPaymentSchema, C as CardTokenSubscriptionSchema, V as CardTokenWebhookBodySchema, N as ChangeSubscriptionTokenRequestSchema, w as CreateCardTokenRequestSchema, T as CreateCardTokenResponseSchema, h as CreateInvoiceRequestSchema, g as CreateInvoiceResponseSchema, _ as CreateQrCodeRequestSchema, y as CreateQrCodeResponseDataSchema, b as CreateQrCodeResponseSchema, p as ExtraInputTypeSchema, c as GetTokenResponseSchema, m as InvoiceExtraSchema, f as InvoiceItemSchema, P as ListPaymentPlansResponseSchema, x as PayByCardTokenQrRequestSchema, A as PaymentPlanSchema, d as PaymentProviderItemSchema, u as PaymentProviderSchema, R as PaymentWebhookBodySchema, D as PurchaseRequestSchema, O as PurchaseTransactionDataSchema, v as QrDeepLinkSchema, k as RecurringTypeSchema, l as RefreshTokenResponseSchema, ee as SendChatMessageRequestSchema, j as SubscribeRequestSchema, M as SubscriptionDataSchema, z as SubscriptionPaymentWebhookBodySchema, B as WebhookBankInfoSchema, L as WebhookMessageSchema, I as WebhookStatusSchema, F as WebhookTypeSchema, a as apiBaseUrl, W as bonumWebhookSchema, q as cardTokenHeader, K as langHeader, i as testApiBaseUrl, we as useBonum, G as validateWebhookChecksum };
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`zod`),require(`node:crypto`),require(`axios`)):typeof define==`function`&&define.amd?define([`exports`,`zod`,`node:crypto`,`axios`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.bonum={},e.zod,e.node_crypto,e.axios))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=Object.create,a=Object.defineProperty,o=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,c=Object.getPrototypeOf,l=Object.prototype.hasOwnProperty,u=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=s(t),c=0,u=i.length,d;c<u;c++)d=i[c],!l.call(e,d)&&d!==n&&a(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=o(t,d))||r.enumerable});return e};r=((e,t,n)=>(n=e==null?{}:i(c(e)),u(t||!e||!e.__esModule?a(n,`default`,{value:e,enumerable:!0}):n,e)))(r);var d=`https://testapi.bonum.mn`,f=`https://apis.bonum.mn`,p=function(e){return e.authenticate=`/bonum-gateway/ecommerce/auth/create`,e.refresh=`/bonum-gateway/ecommerce/auth/refresh`,e.paymentProviders=`/bonum-gateway/ecommerce/invoices/payment-providers`,e.createInvoice=`/bonum-gateway/ecommerce/invoices`,e.getInvoiceStatus=`/bonum-gateway/ecommerce/invoices/:invoiceId`,e.setInvoicePaid=`/bonum-gateway/ecommerce/invoices/paid`,e.createQrCode=`/mpay-service/merchant/transaction/qr/create`,e.invoiceByQrCode=`/mpay-service/merchant/transaction/qr`,e.payByCardTokenQr=`/mpay-service/merchant/transaction/qr/pay`,e.createCardToken=`/mpay-service/merchant/cards/tokenize/request`,e.purchaseWithCardToken=`/mpay-service/merchant/transaction/purchase`,e.rollbackPurchase=`/mpay-service/merchant/transaction/rollback/:id`,e.listPaymentPlans=`/mpay-service/merchant/values/payment-plans`,e.subscribe=`/mpay-service/merchant/subscriptions/subscribe`,e.getSubscriptions=`/mpay-service/merchant/subscriptions`,e.unsubscribe=`/mpay-service/merchant/subscriptions/:subscriptionId`,e.deleteSubscription=`/mpay-service/merchant/subscriptions/:subscriptionId/delete`,e.changeTokenNewCard=`/mpay-service/merchant/subscriptions/:subscriptionId/change/create-new-token`,e.changeTokenExisting=`/mpay-service/merchant/subscriptions/:subscriptionId/change`,e.executePayment=`/mpay-service/merchant/subscriptions/:subscriptionId/execute`,e.getNeoUserData=`/users/apps/share/requests`,e.sendNeoMessage=`/messages/send`,e}({}),m=t.z.object({baseUrl:t.z.string().optional(),appSecret:t.z.string(),terminalId:t.z.string(),checksumKey:t.z.string().optional(),language:t.z.enum([`mn`,`en`]).optional(),initialTokens:t.z.object({accessToken:t.z.string(),refreshToken:t.z.string(),expiresIn:t.z.number(),refreshExpiresIn:t.z.number()}).optional()}),h=t.z.object({tokenType:t.z.preprocess(e=>typeof e==`string`?e.trim():e,t.z.literal(`Bearer`)),accessToken:t.z.string(),expiresIn:t.z.number(),refreshToken:t.z.string(),refreshExpiresIn:t.z.number(),unit:t.z.literal(`SECONDS`)}),g=t.z.object({accessToken:t.z.string()}),_=t.z.enum([`QPAY`,`E_COMMERCE`,`WE_CHAT`,`SONO_SHOP`]),v=t.z.object({provider:_,enabled:t.z.boolean()}),y=t.z.object({image:t.z.string().optional(),title:t.z.string(),remark:t.z.string().optional(),amount:t.z.number(),count:t.z.number()}),b=t.z.enum([`NUMBER`,`PHONE`,`EMAIL`,`TEXT`,`ALL`]),x=t.z.object({placeholder:t.z.string(),type:b,required:t.z.boolean()}),S=t.z.object({amount:t.z.number(),callback:t.z.string(),transactionId:t.z.string(),expiresIn:t.z.number(),providers:t.z.array(_).optional(),items:t.z.array(y).optional(),extras:t.z.array(x).optional(),language:t.z.enum([`mn`,`en`]).optional()}),C=t.z.object({invoiceId:t.z.string(),followUpLink:t.z.string()}),w=t.z.object({amount:t.z.number(),transactionId:t.z.string(),expiresIn:t.z.number()}),T=t.z.object({name:t.z.string(),description:t.z.string(),logo:t.z.string(),link:t.z.string(),appStoreId:t.z.string(),androidPackageName:t.z.string()}),E=t.z.object({invoiceId:t.z.string(),qrCode:t.z.string(),qrImage:t.z.string(),links:t.z.array(T)}),D=t.z.object({data:E}),O=t.z.object({qrCode:t.z.string(),transactionId:t.z.string()}),k=t.z.object({amount:t.z.number()}),A=t.z.object({planId:t.z.number(),cycleValue:t.z.string(),cycles:t.z.number().nullable().optional(),payNow:t.z.boolean().optional(),custEmail:t.z.string().optional()}),j=t.z.object({callback:t.z.string(),transactionId:t.z.string(),payment:k.optional(),subscription:A.optional(),items:t.z.array(y).optional()}),M=t.z.object({followUpLink:t.z.string(),id:t.z.string()}),ee=t.z.object({id:t.z.number(),code:t.z.string(),name:t.z.string(),icon:t.z.string(),iBanCode:t.z.string(),transferCode:t.z.string()}),te=t.z.object({amount:t.z.number(),currency:t.z.string(),transactionId:t.z.string()}),N=t.z.object({id:t.z.number(),completedAt:t.z.string().optional(),status:t.z.enum([`SUCCESS`,`FAILED`,`QUEUED`]),description:t.z.string().optional(),cardStatus:t.z.enum([`ACTIVE`,`INACTIVE`]).nullable().optional(),respCode:t.z.string().nullable().optional()}),P=t.z.enum([`WEEKLY`,`MONTHLY`,`YEARLY`]),F=t.z.object({planId:t.z.number(),name:t.z.string(),remark:t.z.string(),createdAt:t.z.string(),recurringType:P,amount:t.z.number(),status:t.z.enum([`ACTIVE`,`INACTIVE`]),cardCount:t.z.number(),retryCount:t.z.number()}),I=t.z.object({planId:t.z.number(),cycleValue:t.z.union([t.z.number(),t.z.string()]),cycles:t.z.number().nullable().optional(),payNow:t.z.boolean().optional(),custEmail:t.z.string().optional()}),ne=t.z.object({subscriptionId:t.z.number(),subscribedAt:t.z.string(),cardMask:t.z.string(),plan:F,nextBillAt:t.z.string(),lastBilledAt:t.z.string(),status:t.z.enum([`ACTIVE`,`INACTIVE`,`CANCELLED`])}),re=t.z.object({callback:t.z.string(),transactionId:t.z.string(),items:t.z.array(y).optional()}),ie=t.z.object({data:t.z.array(F)}),ae=t.z.object({phoneNumber:t.z.string(),message:t.z.string()}),oe=t.z.object({traceId:t.z.string(),errorCode:t.z.string().nullable().optional(),error:t.z.string().nullable().optional(),message:t.z.string().nullable().optional(),data:t.z.any().nullable().optional(),detail:t.z.string().nullable().optional(),duration:t.z.number(),status:t.z.number()}),L=t.z.enum([`PAYMENT`,`CARD-TOKEN`,`SUBSCRIPTION-PAYMENT`,`UNSUBSCRIBED`]),R=t.z.enum([`SUCCESS`,`FAILED`]),se=t.z.object({type:L,status:R,message:t.z.string().optional(),body:t.z.unknown()}),z=t.z.object({amount:t.z.number(),currency:t.z.string(),transactionId:t.z.string(),terminalId:t.z.string(),completedAt:t.z.string().optional(),invoiceId:t.z.string().optional(),paymentVendor:t.z.string().optional(),initType:t.z.string().optional(),status:t.z.string().optional(),"extras-inputs":t.z.array(t.z.unknown()).optional(),extras:t.z.array(t.z.unknown()).optional(),invoiceStatus:t.z.string().optional()}),B=t.z.object({subscriptionId:t.z.number(),invoiceId:t.z.number(),planId:t.z.number(),transactionId:t.z.string(),completedAt:t.z.string(),amount:t.z.number(),currency:t.z.string()}),V=t.z.object({id:t.z.number(),code:t.z.string(),name:t.z.string(),icon:t.z.string()}),H=t.z.object({token:t.z.string(),mask:t.z.string(),expiry:t.z.string(),bank:V,transactionId:t.z.string(),completedAt:t.z.string()}),U=t.z.object({type:t.z.literal(`PAYMENT`),status:R,message:t.z.string().optional(),body:z}),W=t.z.object({type:t.z.literal(`CARD-TOKEN`),status:R,message:t.z.string().optional(),body:H}),G=t.z.object({type:t.z.literal(`SUBSCRIPTION-PAYMENT`),status:R,message:t.z.string().optional(),body:B}),K=t.z.discriminatedUnion(`type`,[U,W,G]),q=(e,t,r)=>{let i=(0,n.createHmac)(`sha256`,t).update(e).digest(),a=Buffer.from(r,`hex`);return a.length===i.length&&(0,n.timingSafeEqual)(i,a)},J=(e,t)=>{let n=t??e.language;return n?{"Accept-Language":n}:{}},Y=e=>({"X-CARD-TOKEN":e}),ce=e=>e,le=e=>e,X=(e,...t)=>{let n=t[0];return n?Object.entries(n).reduce((e,[t,n])=>e.replaceAll(`:${t}`,n),e):e},Z=e=>ce(e),Q=e=>le(e),ue=Q((e,{setStore:t,store:n})=>async()=>{let r=await e.get(p.authenticate,{headers:{Authorization:`AppSecret ${n.appSecret}`,"X-TERMINAL-ID":n.terminalId}});return t({accessToken:r.data.accessToken,expiresIn:new Date(Date.now()+r.data.expiresIn*1e3),refreshToken:r.data.refreshToken,refreshExpiresIn:new Date(Date.now()+r.data.refreshExpiresIn*1e3)}),r}),de=Q((e,{setStore:t,store:n})=>async()=>{let r=await e.get(p.refresh,{headers:{Authorization:`Bearer ${n.refreshToken}`}});return t({accessToken:r.data.accessToken}),r}),$=Q(e=>async()=>await e.get(p.paymentProviders)),fe=Z((e,{store:t})=>async n=>{let{language:r,...i}=n;return await e.post(p.createInvoice,i,{headers:J(t,r)})}),pe=Z(e=>async({invoiceId:t})=>await e.get(X(p.getInvoiceStatus,{invoiceId:t}))),me=Z(e=>async({invoiceId:t})=>await e.get(p.setInvoicePaid,{params:{invoiceId:t}})),he=Z(e=>async t=>await e.post(p.createQrCode,t)),ge=Z(e=>async t=>await e.post(p.invoiceByQrCode,t)),_e=Z((e,{store:t})=>async({cardToken:n,...r})=>await e.put(p.payByCardTokenQr,r,{headers:{...Y(n),...J(t)}})),ve=Z((e,{store:t})=>async n=>await e.post(p.createCardToken,n,{headers:J(t)})),ye=Z((e,{store:t})=>async({cardToken:n,...r})=>await e.post(p.purchaseWithCardToken,r,{headers:{...Y(n),...J(t)}})),be=Z(e=>async({id:t,cardToken:n})=>await e.get(X(p.rollbackPurchase,{id:t}),{headers:Y(n)})),xe=Q(e=>async()=>await e.get(p.listPaymentPlans)),Se=Z(e=>async({cardToken:t,...n})=>await e.post(p.subscribe,n,{headers:Y(t)})),Ce=Z(e=>async({cardToken:t})=>await e.get(p.getSubscriptions,{headers:Y(t)})),we=Z(e=>async({subscriptionId:t,planId:n})=>await e.delete(X(p.unsubscribe,{subscriptionId:t}),{data:n==null?{}:{planId:n}})),Te=Z(e=>async({subscriptionId:t,planId:n})=>await e.delete(X(p.deleteSubscription,{subscriptionId:t}),{data:n==null?{}:{planId:n}})),Ee=Z(e=>async({subscriptionId:t,...n})=>await e.put(X(p.changeTokenNewCard,{subscriptionId:t}),n)),De=Z(e=>async({subscriptionId:t,newCardToken:n})=>await e.put(X(p.changeTokenExisting,{subscriptionId:t}),void 0,{headers:{"X-CARD-TOKEN":n}})),Oe=Z(e=>async({subscriptionId:t})=>await e.put(X(p.executePayment,{subscriptionId:t}))),ke=Z(e=>async({requestId:t,mChatBaseUrl:n})=>await e.get(`${n}${p.getNeoUserData}?requestId=${t}`)),Ae=Z(e=>async({neoBaseUrl:t,...n})=>await e.post(`${t}${p.sendNeoMessage}`,n));e.BankInfoSchema=ee,e.BonumApiResponseSchema=oe,e.BonumConfigSchema=m,e.BonumRequestPath=p,e.CardTokenPaymentSchema=k,e.CardTokenSubscriptionSchema=A,e.CardTokenWebhookBodySchema=H,e.ChangeSubscriptionTokenRequestSchema=re,e.CreateCardTokenRequestSchema=j,e.CreateCardTokenResponseSchema=M,e.CreateInvoiceRequestSchema=S,e.CreateInvoiceResponseSchema=C,e.CreateQrCodeRequestSchema=w,e.CreateQrCodeResponseDataSchema=E,e.CreateQrCodeResponseSchema=D,e.ExtraInputTypeSchema=b,e.GetTokenResponseSchema=h,e.InvoiceExtraSchema=x,e.InvoiceItemSchema=y,e.ListPaymentPlansResponseSchema=ie,e.PayByCardTokenQrRequestSchema=O,e.PaymentPlanSchema=F,e.PaymentProviderItemSchema=v,e.PaymentProviderSchema=_,e.PaymentWebhookBodySchema=z,e.PurchaseRequestSchema=te,e.PurchaseTransactionDataSchema=N,e.QrDeepLinkSchema=T,e.RecurringTypeSchema=P,e.RefreshTokenResponseSchema=g,e.SendChatMessageRequestSchema=ae,e.SubscribeRequestSchema=I,e.SubscriptionDataSchema=ne,e.SubscriptionPaymentWebhookBodySchema=B,e.WebhookBankInfoSchema=V,e.WebhookMessageSchema=se,e.WebhookStatusSchema=R,e.WebhookTypeSchema=L,e.apiBaseUrl=f,e.bonumWebhookSchema=K,e.cardTokenHeader=Y,e.langHeader=J,e.testApiBaseUrl=d,e.useBonum=e=>{let t=e.baseUrl??(e.isTestEnv?`https://testapi.bonum.mn`:`https://apis.bonum.mn`),n=r.default.create({baseURL:t}),i={...e},a={store:i,setStore:e=>{Object.assign(i,e)}};e.initialTokens&&(i.accessToken=e.initialTokens.accessToken,i.refreshToken=e.initialTokens.refreshToken,i.expiresIn=new Date(Date.now()+e.initialTokens.expiresIn*1e3),i.refreshExpiresIn=new Date(Date.now()+e.initialTokens.refreshExpiresIn*1e3));let o=ue(n,a),s=de(n,a),c=null,l=()=>(c||=o().finally(()=>{c=null}),c),u=$(n,a),d=fe(n,a),f=pe(n,a),m=me(n,a),h=he(n,a),g=ge(n,a),_=_e(n,a),v=ve(n,a),y=ye(n,a),b=be(n,a),x=xe(n,a),S=Se(n,a),C=Ce(n,a),w=we(n,a),T=Te(n,a),E=Ee(n,a),D=De(n,a),O=Oe(n,a),k=ke(n,a),A=Ae(n,a);return n.interceptors.request.use(async e=>{if(e.url===p.authenticate||e.url===p.refresh)return e;let t=Date.now();if(a.store.accessToken&&a.store.expiresIn){if(a.store.expiresIn.getTime()-3e4<t)try{a.store.refreshToken&&a.store.refreshExpiresIn&&a.store.refreshExpiresIn.getTime()>t?await s():await o()}catch{await l()}}else await l();return e.headers.set(`Authorization`,`Bearer ${a.store.accessToken}`),e}),{api:n,getToken:o,refreshToken:s,getPaymentProviders:u,createInvoice:d,getInvoiceStatus:f,setInvoicePaid:m,createQrCode:h,invoiceByQrCode:g,payByCardTokenQr:_,createCardToken:v,purchaseWithCardToken:y,rollbackPurchase:b,listPaymentPlans:x,subscribe:S,getSubscriptions:C,unsubscribe:w,deleteSubscription:T,changeSubscriptionTokenNewCard:E,changeSubscriptionTokenExisting:D,executeSubscriptionPayment:O,getNeoUserData:k,sendNeoMessage:A,verifyWebhookChecksum:(t,n)=>{let r=e.checksumKey?.trim();if(!r)throw Error(`Bonum checksumKey is not configured`);return q(t,r,n)}}},e.validateWebhookChecksum=q});
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@mnpay/bonum",
3
+ "version": "1.5.0",
4
+ "description": "Client library for the Bonum payment gateway — invoices, QR payments, card tokenization and subscriptions.",
5
+ "license": "MIT",
6
+ "homepage": "https://github.com/mnpay/mn-payment-platforms/tree/main/packages/bonum#readme",
7
+ "type": "module",
8
+ "scripts": {
9
+ "lint": "yarn g:lint",
10
+ "test": "yarn g:test",
11
+ "coverage": "yarn g:vitest run --coverage",
12
+ "build": "yarn g:build",
13
+ "release": "yarn g:publish && yarn g:publish-public"
14
+ },
15
+ "main": "./dist/index.umd.cjs",
16
+ "module": "./dist/index.js",
17
+ "types": "@types/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./@types/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ },
24
+ "require": {
25
+ "types": "./@types/index.d.ts",
26
+ "default": "./dist/index.umd.cjs"
27
+ }
28
+ }
29
+ },
30
+ "author": {
31
+ "name": "Sura Kh",
32
+ "email": "sura0111@gmail.com"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/mnpay/mn-payment-platforms.git",
37
+ "directory": "packages/bonum"
38
+ },
39
+ "keywords": [
40
+ "bonum",
41
+ "mongolian payment platform",
42
+ "payment",
43
+ "mongolia",
44
+ "@mnpay/bonum"
45
+ ],
46
+ "files": [
47
+ "dist",
48
+ "@types",
49
+ "README.md",
50
+ "CHANGELOG.md"
51
+ ],
52
+ "dependencies": {
53
+ "axios": "^1.13.6",
54
+ "zod": "^4.3.6"
55
+ }
56
+ }