@binopartner/pay-sdk 0.1.1 → 0.3.1

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 (154) hide show
  1. package/README.md +145 -2
  2. package/dist/core/api-base.d.ts +49 -0
  3. package/dist/core/api-base.js +196 -0
  4. package/dist/core/appearance.d.ts +17 -0
  5. package/dist/core/appearance.js +122 -0
  6. package/dist/core/auth.d.ts +32 -0
  7. package/dist/core/auth.js +233 -0
  8. package/dist/core/client.d.ts +71 -0
  9. package/dist/core/client.js +307 -0
  10. package/dist/core/clock.d.ts +7 -0
  11. package/dist/core/clock.js +17 -0
  12. package/dist/core/errors.d.ts +29 -0
  13. package/dist/core/errors.js +87 -0
  14. package/dist/core/events.d.ts +50 -0
  15. package/dist/core/events.js +1 -0
  16. package/dist/core/flows/deposit.d.ts +63 -0
  17. package/dist/core/flows/deposit.js +502 -0
  18. package/dist/core/flows/withdraw.d.ts +51 -0
  19. package/dist/core/flows/withdraw.js +413 -0
  20. package/dist/core/i18n/ar.d.ts +2 -0
  21. package/dist/core/i18n/ar.js +314 -0
  22. package/dist/core/i18n/base.d.ts +14 -0
  23. package/dist/core/i18n/base.js +86 -0
  24. package/dist/core/i18n/cs.d.ts +2 -0
  25. package/dist/core/i18n/cs.js +314 -0
  26. package/dist/core/i18n/de.d.ts +2 -0
  27. package/dist/core/i18n/de.js +314 -0
  28. package/dist/core/i18n/en-ui.d.ts +39 -0
  29. package/dist/core/i18n/en-ui.js +39 -0
  30. package/dist/core/i18n/en.d.ts +316 -0
  31. package/dist/core/i18n/en.js +280 -0
  32. package/dist/core/i18n/es.d.ts +2 -0
  33. package/dist/core/i18n/es.js +314 -0
  34. package/dist/core/i18n/fr.d.ts +2 -0
  35. package/dist/core/i18n/fr.js +314 -0
  36. package/dist/core/i18n/hi.d.ts +2 -0
  37. package/dist/core/i18n/hi.js +314 -0
  38. package/dist/core/i18n/index.d.ts +16 -0
  39. package/dist/core/i18n/index.js +39 -0
  40. package/dist/core/i18n/it.d.ts +2 -0
  41. package/dist/core/i18n/it.js +314 -0
  42. package/dist/core/i18n/ru.d.ts +2 -0
  43. package/dist/core/i18n/ru.js +314 -0
  44. package/dist/core/i18n/tr.d.ts +2 -0
  45. package/dist/core/i18n/tr.js +314 -0
  46. package/dist/core/i18n/ua.d.ts +2 -0
  47. package/dist/core/i18n/ua.js +314 -0
  48. package/dist/core/index.d.ts +37 -0
  49. package/dist/core/index.js +19 -0
  50. package/dist/core/intent.d.ts +13 -0
  51. package/dist/core/intent.js +45 -0
  52. package/dist/core/money.d.ts +22 -0
  53. package/dist/core/money.js +178 -0
  54. package/dist/core/poll.d.ts +16 -0
  55. package/dist/core/poll.js +100 -0
  56. package/dist/core/requisites-frame.d.ts +32 -0
  57. package/dist/core/requisites-frame.js +202 -0
  58. package/dist/core/rules/address.d.ts +3 -0
  59. package/dist/core/rules/address.js +29 -0
  60. package/dist/core/rules/limits.d.ts +17 -0
  61. package/dist/core/rules/limits.js +19 -0
  62. package/dist/core/rules/paying.d.ts +10 -0
  63. package/dist/core/rules/paying.js +32 -0
  64. package/dist/core/rules/receipt.d.ts +6 -0
  65. package/dist/core/rules/receipt.js +40 -0
  66. package/dist/core/rules/requisites.d.ts +42 -0
  67. package/dist/core/rules/requisites.js +68 -0
  68. package/dist/core/rules/slippage.d.ts +1 -0
  69. package/dist/core/rules/slippage.js +7 -0
  70. package/dist/core/store.d.ts +15 -0
  71. package/dist/core/store.js +53 -0
  72. package/dist/core/types.d.ts +244 -0
  73. package/dist/core/types.js +1 -0
  74. package/dist/core/wire.d.ts +178 -0
  75. package/dist/core/wire.js +602 -0
  76. package/dist/elements/Root.d.ts +37 -0
  77. package/dist/elements/Root.js +143 -0
  78. package/dist/elements/chains.d.ts +1 -0
  79. package/dist/elements/chains.js +1 -0
  80. package/dist/elements/components/Qr.d.ts +17 -0
  81. package/dist/elements/components/Qr.js +28 -0
  82. package/dist/elements/components/basic.d.ts +42 -0
  83. package/dist/elements/components/basic.js +129 -0
  84. package/dist/elements/components/deposit.d.ts +61 -0
  85. package/dist/elements/components/deposit.js +272 -0
  86. package/dist/elements/components/inputs.d.ts +24 -0
  87. package/dist/elements/components/inputs.js +92 -0
  88. package/dist/elements/components/pickers.d.ts +32 -0
  89. package/dist/elements/components/pickers.js +267 -0
  90. package/dist/elements/components/status.d.ts +25 -0
  91. package/dist/elements/components/status.js +221 -0
  92. package/dist/elements/context.d.ts +47 -0
  93. package/dist/elements/context.js +33 -0
  94. package/dist/elements/hooks/deposit.d.ts +52 -0
  95. package/dist/elements/hooks/deposit.js +215 -0
  96. package/dist/elements/hooks/root.d.ts +70 -0
  97. package/dist/elements/hooks/root.js +125 -0
  98. package/dist/elements/hooks/withdraw.d.ts +30 -0
  99. package/dist/elements/hooks/withdraw.js +145 -0
  100. package/dist/elements/index.d.ts +28 -0
  101. package/dist/elements/index.js +14 -0
  102. package/dist/elements/part.d.ts +2 -0
  103. package/dist/elements/part.js +1 -0
  104. package/dist/elements/presets/DepositForm.d.ts +32 -0
  105. package/dist/elements/presets/DepositForm.js +97 -0
  106. package/dist/elements/presets/WithdrawForm.d.ts +25 -0
  107. package/dist/elements/presets/WithdrawForm.js +44 -0
  108. package/dist/elements/presets/focus.d.ts +2 -0
  109. package/dist/elements/presets/focus.js +26 -0
  110. package/dist/elements.css +2777 -0
  111. package/dist/fonts.css +31 -0
  112. package/dist/frame.d.ts +1 -0
  113. package/dist/frame.js +37 -6
  114. package/dist/types.d.ts +1 -0
  115. package/dist/ui/chains.d.ts +3 -0
  116. package/dist/ui/chains.js +42 -0
  117. package/dist/ui/context.d.ts +51 -0
  118. package/dist/ui/context.js +63 -0
  119. package/dist/ui/crypto.d.ts +67 -0
  120. package/dist/ui/crypto.js +68 -0
  121. package/dist/ui/entry.d.ts +25 -0
  122. package/dist/ui/entry.js +26 -0
  123. package/dist/ui/fiat.d.ts +94 -0
  124. package/dist/ui/fiat.js +149 -0
  125. package/dist/ui/fields.d.ts +76 -0
  126. package/dist/ui/fields.js +71 -0
  127. package/dist/ui/fonts.d.ts +4 -0
  128. package/dist/ui/fonts.js +31 -0
  129. package/dist/ui/icons.d.ts +39 -0
  130. package/dist/ui/icons.js +116 -0
  131. package/dist/ui/index.d.ts +31 -0
  132. package/dist/ui/index.js +17 -0
  133. package/dist/ui/layout.d.ts +36 -0
  134. package/dist/ui/layout.js +44 -0
  135. package/dist/ui/logos.d.ts +14 -0
  136. package/dist/ui/logos.js +61 -0
  137. package/dist/ui/part.d.ts +32 -0
  138. package/dist/ui/part.js +33 -0
  139. package/dist/ui/pickers.d.ts +71 -0
  140. package/dist/ui/pickers.js +167 -0
  141. package/dist/ui/qr-encode.d.ts +30 -0
  142. package/dist/ui/qr-encode.js +340 -0
  143. package/dist/ui/summary.d.ts +31 -0
  144. package/dist/ui/summary.js +36 -0
  145. package/dist/ui/types.d.ts +67 -0
  146. package/dist/ui/types.js +1 -0
  147. package/dist/ui/urls.d.ts +4 -0
  148. package/dist/ui/urls.js +41 -0
  149. package/dist/ui/withdraw.d.ts +38 -0
  150. package/dist/ui/withdraw.js +30 -0
  151. package/dist/ui.css +2699 -0
  152. package/dist/widget-url.d.ts +1 -0
  153. package/dist/widget-url.js +16 -0
  154. package/package.json +46 -5
@@ -0,0 +1,602 @@
1
+ import { PayError, codeForStatus, isServerErrorCode } from "./errors.js";
2
+ export const PATHS = {
3
+ redeem: "/broker/widget-sessions/redeem",
4
+ ping: "/broker/pay/ping",
5
+ context: "/broker/pay/context",
6
+ methods: "/broker/pay/methods",
7
+ depositQuote: "/broker/pay/deposits/quote",
8
+ cryptoDeposit: "/broker/pay/deposits/crypto",
9
+ deposits: "/broker/pay/deposits",
10
+ deposit: (id) => `/broker/pay/deposits/${encodeURIComponent(id)}`,
11
+ receipt: (id) => `/broker/pay/deposits/${encodeURIComponent(id)}/receipt`,
12
+ frameTicket: (id) => `/broker/pay/deposits/${encodeURIComponent(id)}/frame-ticket`,
13
+ onramp: "/broker/pay/onramp/session",
14
+ withdrawalQuote: "/broker/pay/withdrawals/quote",
15
+ withdrawals: "/broker/pay/withdrawals",
16
+ withdrawal: (id) => `/broker/pay/withdrawals/${encodeURIComponent(id)}`,
17
+ activity: "/broker/pay/activity",
18
+ };
19
+ export const IDEMPOTENCY_HEADER = "Idempotency-Key";
20
+ export const REQUISITES_FRAME_PATH = "/embed/pay/requisites";
21
+ const FRAME_PATH = /^\/[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*$/;
22
+ export const safeFramePath = (value) => typeof value === "string" && FRAME_PATH.test(value) ? value : REQUISITES_FRAME_PATH;
23
+ export const FRAME_TICKET_PREFIX = "wft_";
24
+ const invalid = (what) => new PayError("INVALID_RESPONSE", `Unexpected response: ${what}`);
25
+ const asObject = (value, what) => {
26
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
27
+ throw invalid(what);
28
+ }
29
+ return value;
30
+ };
31
+ const isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
32
+ const str = (data, key) => {
33
+ const value = data[key];
34
+ if (typeof value === "string" && value.length > 0)
35
+ return value;
36
+ if (typeof value === "number" && Number.isFinite(value))
37
+ return String(value);
38
+ throw invalid(key);
39
+ };
40
+ const optStr = (data, key) => {
41
+ const value = data[key];
42
+ if (typeof value === "string" && value.length > 0)
43
+ return value;
44
+ if (typeof value === "number" && Number.isFinite(value))
45
+ return String(value);
46
+ return null;
47
+ };
48
+ const num = (data, key) => {
49
+ const value = data[key];
50
+ if (typeof value === "number" && Number.isFinite(value))
51
+ return value;
52
+ throw invalid(key);
53
+ };
54
+ const optNum = (data, key) => {
55
+ const value = data[key];
56
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
57
+ };
58
+ const strList = (value) => Array.isArray(value)
59
+ ? value.filter((item) => typeof item === "string")
60
+ : [];
61
+ const oneOf = (value, allowed, fallback) => typeof value === "string" && allowed.includes(value)
62
+ ? value
63
+ : fallback;
64
+ const strictOneOf = (value, allowed, what) => {
65
+ if (typeof value === "string" && allowed.includes(value)) {
66
+ return value;
67
+ }
68
+ throw invalid(what);
69
+ };
70
+ const MODES = ["deposit", "withdraw"];
71
+ const REGIONS = ["ru", "global", "unknown"];
72
+ const RAILS = ["ru", "kz", "ua", "in"];
73
+ const THEMES = ["light", "dark"];
74
+ const CRYPTO_STATUSES = ["active", "expired", "consumed", "closed"];
75
+ const FIAT_STATUSES = [
76
+ "pending",
77
+ "completed",
78
+ "failed",
79
+ "expired",
80
+ "disputed",
81
+ ];
82
+ const WITHDRAWAL_STATUSES = [
83
+ "pending",
84
+ "processing",
85
+ "completed",
86
+ "failed",
87
+ "cancelled",
88
+ "refund_pending",
89
+ "refunded",
90
+ ];
91
+ const DISPLAY_STATUSES = ["pending", "success", "failed"];
92
+ const DEPOSIT_SOURCES = ["crypto", "card", "onramp"];
93
+ export const readSessionExtras = (data) => {
94
+ const apiBases = strList(data.api_bases);
95
+ if (apiBases.length === 0 && typeof data.api_base === "string") {
96
+ apiBases.push(data.api_base);
97
+ }
98
+ const widgetOrigins = strList(data.widget_origins);
99
+ if (widgetOrigins.length === 0 && typeof data.widget_origin === "string") {
100
+ widgetOrigins.push(data.widget_origin);
101
+ }
102
+ let clientId = typeof data.client_id === "string" ? data.client_id : null;
103
+ const widgetUrl = typeof data.widget_url === "string" ? data.widget_url : null;
104
+ if (!clientId && widgetUrl) {
105
+ try {
106
+ clientId = new URL(widgetUrl).searchParams.get("client_id");
107
+ }
108
+ catch {
109
+ clientId = null;
110
+ }
111
+ }
112
+ return {
113
+ apiBases,
114
+ widgetOrigins,
115
+ widgetUrl,
116
+ region: typeof data.geo === "string"
117
+ ? oneOf(data.geo, REGIONS, "unknown")
118
+ : typeof data.region === "string"
119
+ ? oneOf(data.region, REGIONS, "unknown")
120
+ : null,
121
+ payApi: typeof data.pay_api === "boolean" ? data.pay_api : null,
122
+ clientId: clientId || null,
123
+ };
124
+ };
125
+ export const toRedeemBody = (input) => ({
126
+ session: input.session,
127
+ origin: input.origin,
128
+ client_id: input.clientId,
129
+ mode: input.mode,
130
+ });
131
+ export const readRedeem = (value) => {
132
+ const data = asObject(value, "redeem");
133
+ const accessToken = data.access_token;
134
+ if (typeof accessToken !== "string" || accessToken.length === 0) {
135
+ throw invalid("access_token");
136
+ }
137
+ return {
138
+ accessToken,
139
+ expiresIn: optNum(data, "expires_in"),
140
+ expiresAt: optNum(data, "expires_at"),
141
+ payApi: typeof data.pay_api === "boolean" ? data.pay_api : null,
142
+ };
143
+ };
144
+ export const readPing = (value) => {
145
+ const data = asObject(value, "ping");
146
+ if (data.ok !== true)
147
+ throw invalid("ok");
148
+ return { serverTime: optNum(data, "server_time") };
149
+ };
150
+ export const readError = (status, value) => {
151
+ const envelope = isObject(value) && isObject(value.error) ? value.error : null;
152
+ const details = envelope && isObject(envelope.details) ? envelope.details : {};
153
+ const rawCode = details.code;
154
+ const code = isServerErrorCode(rawCode) ? rawCode : codeForStatus(status);
155
+ const message = envelope && typeof envelope.message === "string"
156
+ ? envelope.message.slice(0, 300)
157
+ : `HTTP ${status}`;
158
+ const retry = details.retry_after_seconds;
159
+ return new PayError(code, message, {
160
+ status,
161
+ details: {
162
+ ...(typeof details.field === "string" ? { field: details.field } : {}),
163
+ ...(optStr(details, "min") !== null ? { min: optStr(details, "min") } : {}),
164
+ ...(optStr(details, "max") !== null ? { max: optStr(details, "max") } : {}),
165
+ ...(typeof details.currency === "string" ? { currency: details.currency } : {}),
166
+ ...(typeof retry === "number" && Number.isFinite(retry)
167
+ ? { retryAfterSeconds: retry }
168
+ : {}),
169
+ ...(typeof details.withdrawal_id === "string"
170
+ ? { withdrawalId: details.withdrawal_id }
171
+ : {}),
172
+ },
173
+ });
174
+ };
175
+ const readBalance = (value) => {
176
+ const data = asObject(value, "balance");
177
+ return {
178
+ available: optStr(data, "available"),
179
+ held: optStr(data, "held"),
180
+ total: optStr(data, "total"),
181
+ withdrawable: optStr(data, "withdrawable"),
182
+ currency: optStr(data, "currency") ?? "USD",
183
+ };
184
+ };
185
+ export const readContext = (value) => {
186
+ const data = asObject(value, "context");
187
+ const withdrawal = isObject(data.withdrawal) ? data.withdrawal : null;
188
+ const branding = isObject(data.branding) ? data.branding : {};
189
+ return {
190
+ mode: strictOneOf(data.mode, MODES, "mode"),
191
+ serverTime: num(data, "server_time"),
192
+ tokenExpiresAt: optNum(data, "token_expires_at"),
193
+ balance: readBalance(data.balance),
194
+ withdrawal: withdrawal
195
+ ? {
196
+ twoFactorRequired: withdrawal.two_factor_required === true,
197
+ inFlightId: optStr(withdrawal, "in_flight_id"),
198
+ }
199
+ : null,
200
+ requisitesDelivery: data.requisites_delivery === "raw" ? "raw" : "frame",
201
+ branding: {
202
+ theme: typeof branding.theme === "string" &&
203
+ THEMES.includes(branding.theme)
204
+ ? branding.theme
205
+ : null,
206
+ accentColor: optStr(branding, "accent_color"),
207
+ backgroundColor: optStr(branding, "background_color"),
208
+ logoUrl: optStr(branding, "logo_url"),
209
+ },
210
+ };
211
+ };
212
+ const readWithdrawalLimits = (value) => {
213
+ if (!isObject(value))
214
+ return null;
215
+ return {
216
+ minUsd: optStr(value, "min_usd") ?? "0",
217
+ minNative: optStr(value, "min_native"),
218
+ maxNative: optStr(value, "max_native"),
219
+ feeUsd: optStr(value, "fee_usd") ?? "0",
220
+ etaSeconds: optNum(value, "eta_seconds"),
221
+ maxSlippagePct: optStr(value, "max_slippage_pct"),
222
+ };
223
+ };
224
+ const readMethod = (value) => {
225
+ if (!isObject(value))
226
+ return null;
227
+ const id = optStr(value, "id");
228
+ if (!id)
229
+ return null;
230
+ switch (value.kind) {
231
+ case "crypto": {
232
+ const method = {
233
+ id,
234
+ kind: "crypto",
235
+ chainId: str(value, "chain_id"),
236
+ currency: str(value, "currency"),
237
+ minUsd: optStr(value, "min_usd"),
238
+ maxUsd: optStr(value, "max_usd"),
239
+ minNative: optStr(value, "min_native"),
240
+ maxNative: optStr(value, "max_native"),
241
+ feeUsd: optStr(value, "fee_usd"),
242
+ etaSeconds: optNum(value, "eta_seconds"),
243
+ available: value.available !== false,
244
+ withdrawal: readWithdrawalLimits(value.withdrawal),
245
+ };
246
+ return method;
247
+ }
248
+ case "fiat": {
249
+ if (typeof value.rail !== "string" ||
250
+ !RAILS.includes(value.rail)) {
251
+ return null;
252
+ }
253
+ const delivery = value.delivery === "redirect" || value.delivery === "qr"
254
+ ? value.delivery
255
+ : "requisites";
256
+ const method = {
257
+ id,
258
+ kind: "fiat",
259
+ rail: value.rail,
260
+ methodId: optStr(value, "method_id") ?? id,
261
+ label: optStr(value, "label"),
262
+ subtitle: optStr(value, "subtitle"),
263
+ currency: str(value, "currency"),
264
+ minAmount: optStr(value, "min_amount"),
265
+ maxAmount: optStr(value, "max_amount"),
266
+ displayedCommissionPct: optStr(value, "displayed_commission_pct"),
267
+ requiresReceipt: value.requires_receipt === true,
268
+ isDefault: value.is_default === true,
269
+ delivery,
270
+ };
271
+ return method;
272
+ }
273
+ case "onramp":
274
+ return { id, kind: "onramp", delivery: "embed" };
275
+ default:
276
+ return null;
277
+ }
278
+ };
279
+ export const readMethods = (value) => {
280
+ const data = asObject(value, "methods");
281
+ const ordering = isObject(data.ordering) ? data.ordering : {};
282
+ const methods = [];
283
+ if (Array.isArray(data.methods)) {
284
+ for (const entry of data.methods) {
285
+ try {
286
+ const method = readMethod(entry);
287
+ if (method)
288
+ methods.push(method);
289
+ }
290
+ catch {
291
+ continue;
292
+ }
293
+ }
294
+ }
295
+ return {
296
+ mode: strictOneOf(data.mode, MODES, "mode"),
297
+ suggestedCountry: optStr(data, "suggested_country"),
298
+ methods,
299
+ ordering: {
300
+ currencies: strList(ordering.currencies),
301
+ chains: strList(ordering.chains),
302
+ },
303
+ unavailableRails: strList(data.unavailable_rails),
304
+ maxWithdrawUsd: optStr(data, "max_withdraw_usd"),
305
+ };
306
+ };
307
+ export const toDepositQuoteQuery = (input) => ({
308
+ method_id: input.methodId,
309
+ amount: input.amount,
310
+ });
311
+ export const readDepositQuote = (value) => {
312
+ const data = asObject(value, "quote");
313
+ return {
314
+ methodId: str(data, "method_id"),
315
+ amount: str(data, "amount"),
316
+ currency: str(data, "currency"),
317
+ estimatedUsd: optStr(data, "estimated_usd"),
318
+ displayedCommissionPct: optStr(data, "displayed_commission_pct"),
319
+ minAmount: optStr(data, "min_amount"),
320
+ maxAmount: optStr(data, "max_amount"),
321
+ };
322
+ };
323
+ export const toCryptoDepositBody = (input) => ({
324
+ chain_id: input.chainId,
325
+ currency: input.currency,
326
+ });
327
+ const readRequisites = (value) => {
328
+ if (!isObject(value))
329
+ return null;
330
+ const raw = value.value;
331
+ if (typeof raw !== "string" || raw.trim().length === 0)
332
+ return null;
333
+ return {
334
+ kind: optStr(value, "kind") ?? "account",
335
+ value: raw,
336
+ amount: optStr(value, "amount") ?? "",
337
+ recipientName: optStr(value, "recipient_name"),
338
+ bank: optStr(value, "bank"),
339
+ country: optStr(value, "country"),
340
+ };
341
+ };
342
+ const httpsUrl = (value) => {
343
+ if (typeof value !== "string")
344
+ return null;
345
+ try {
346
+ const url = new URL(value);
347
+ return url.protocol === "https:" && !url.username && !url.password ? url.toString() : null;
348
+ }
349
+ catch {
350
+ return null;
351
+ }
352
+ };
353
+ const readNextAction = (data) => {
354
+ if (!("next_action" in data) || data.next_action === undefined)
355
+ return undefined;
356
+ const value = data.next_action;
357
+ if (!isObject(value))
358
+ return null;
359
+ switch (value.type) {
360
+ case "show_requisites": {
361
+ const frame = isObject(value.frame) ? value.frame : null;
362
+ return {
363
+ type: "show_requisites",
364
+ requisites: readRequisites(value.requisites),
365
+ frame: frame
366
+ ? {
367
+ origins: strList(frame.origins),
368
+ path: safeFramePath(frame.path),
369
+ }
370
+ : null,
371
+ };
372
+ }
373
+ case "redirect": {
374
+ const url = httpsUrl(value.url);
375
+ return url ? { type: "redirect", url } : null;
376
+ }
377
+ case "qr": {
378
+ const payload = optStr(value, "payload");
379
+ const imageUrl = httpsUrl(value.image_url);
380
+ return payload || imageUrl ? { type: "qr", payload, imageUrl } : null;
381
+ }
382
+ default:
383
+ return null;
384
+ }
385
+ };
386
+ const readCryptoDeposit = (data) => ({
387
+ id: str(data, "id"),
388
+ kind: "crypto",
389
+ chainId: str(data, "chain_id"),
390
+ currency: str(data, "currency"),
391
+ address: str(data, "address"),
392
+ status: oneOf(data.status, CRYPTO_STATUSES, "closed"),
393
+ expiresAt: optNum(data, "expires_at"),
394
+ createdAt: optNum(data, "created_at") ?? 0,
395
+ });
396
+ const readFiatDeposit = (data) => {
397
+ const receipt = isObject(data.receipt) ? data.receipt : {};
398
+ const rail = strictOneOf(data.rail, RAILS, "rail");
399
+ return {
400
+ id: str(data, "id"),
401
+ kind: "fiat",
402
+ rail,
403
+ methodId: str(data, "method_id"),
404
+ status: oneOf(data.status, FIAT_STATUSES, "pending"),
405
+ amount: optStr(data, "amount"),
406
+ currency: str(data, "currency"),
407
+ estimatedUsd: optStr(data, "estimated_usd"),
408
+ creditedUsd: optStr(data, "credited_usd"),
409
+ expiresAt: optNum(data, "expires_at"),
410
+ serverTime: optNum(data, "server_time"),
411
+ nextAction: readNextAction(data),
412
+ receipt: {
413
+ required: receipt.required === true,
414
+ attachedAt: optNum(receipt, "attached_at"),
415
+ },
416
+ createdAt: optNum(data, "created_at"),
417
+ updatedAt: optNum(data, "updated_at") ?? optNum(data, "created_at"),
418
+ };
419
+ };
420
+ export const readCryptoDepositResponse = (value) => {
421
+ const data = asObject(value, "deposit");
422
+ if (data.kind !== undefined && data.kind !== "crypto")
423
+ throw invalid("kind");
424
+ return readCryptoDeposit(data);
425
+ };
426
+ export const readFiatDepositResponse = (value) => {
427
+ const data = asObject(value, "deposit");
428
+ if (data.kind !== "fiat")
429
+ throw invalid("kind");
430
+ return readFiatDeposit(data);
431
+ };
432
+ export const readDeposit = (value) => {
433
+ const data = asObject(value, "deposit");
434
+ if (data.kind === "crypto")
435
+ return readCryptoDeposit(data);
436
+ if (data.kind === "fiat")
437
+ return readFiatDeposit(data);
438
+ throw invalid("kind");
439
+ };
440
+ export const toCreateDepositBody = (input) => ({
441
+ method_id: input.methodId,
442
+ amount: input.amount,
443
+ ...(input.customerPhone ? { customer_phone: input.customerPhone } : {}),
444
+ ...(input.returnUrl ? { return_url: input.returnUrl } : {}),
445
+ });
446
+ export const toReceiptBody = (dataUrl) => ({
447
+ file_base64: dataUrl,
448
+ });
449
+ export const readFrameTicket = (value) => {
450
+ const data = asObject(value, "frame ticket");
451
+ const ticket = str(data, "ticket");
452
+ if (!ticket.startsWith(FRAME_TICKET_PREFIX))
453
+ throw invalid("ticket");
454
+ return {
455
+ ticket,
456
+ expiresIn: optNum(data, "expires_in") ?? 60,
457
+ expiresAt: optNum(data, "expires_at"),
458
+ frameOrigins: strList(data.frame_origins),
459
+ framePath: typeof data.frame_path === "string" ? safeFramePath(data.frame_path) : null,
460
+ };
461
+ };
462
+ export const toOnrampQuery = (input) => (input.theme ? { theme: input.theme } : {});
463
+ export const readOnramp = (value) => {
464
+ const data = asObject(value, "onramp");
465
+ return { url: str(data, "url") };
466
+ };
467
+ export const toQuoteWithdrawalBody = (input) => ({
468
+ method_id: input.methodId,
469
+ address: input.address,
470
+ amount: input.amount,
471
+ ...(input.acceptSlippagePct !== undefined
472
+ ? { accept_slippage_pct: input.acceptSlippagePct }
473
+ : {}),
474
+ });
475
+ export const toCreateWithdrawalBody = (input) => ({
476
+ ...toQuoteWithdrawalBody(input),
477
+ ...(input.quote
478
+ ? {
479
+ quote: {
480
+ fee_usd: input.quote.feeUsd,
481
+ receive_amount: input.quote.receiveAmount,
482
+ receive_amount_usd: input.quote.receiveAmountUsd,
483
+ eta_seconds: input.quote.etaSeconds,
484
+ slippage_pct: input.quote.slippagePct,
485
+ },
486
+ }
487
+ : {}),
488
+ ...(input.twoFactorCode ? { two_factor_code: input.twoFactorCode } : {}),
489
+ });
490
+ export const readWithdrawalQuote = (value) => {
491
+ const data = asObject(value, "withdrawal quote");
492
+ if (isObject(data.quote)) {
493
+ const quote = data.quote;
494
+ return {
495
+ quote: {
496
+ feeUsd: optStr(quote, "fee_usd"),
497
+ receiveAmount: optStr(quote, "receive_amount"),
498
+ receiveAmountUsd: optStr(quote, "receive_amount_usd"),
499
+ etaSeconds: optNum(quote, "eta_seconds"),
500
+ slippagePct: optStr(quote, "slippage_pct"),
501
+ },
502
+ slippageWarning: null,
503
+ };
504
+ }
505
+ const warning = asObject(data.slippage_warning, "slippage_warning");
506
+ return {
507
+ quote: null,
508
+ slippageWarning: {
509
+ slippagePct: optStr(warning, "slippage_pct"),
510
+ maxAllowedPct: optStr(warning, "max_allowed_pct"),
511
+ receiveAmount: optStr(warning, "receive_amount"),
512
+ receiveAmountUsd: optStr(warning, "receive_amount_usd"),
513
+ },
514
+ };
515
+ };
516
+ const readWithdrawalStatus = (value) => oneOf(value, WITHDRAWAL_STATUSES, "pending");
517
+ export const readWithdrawal = (value) => {
518
+ const data = asObject(value, "withdrawal");
519
+ return {
520
+ id: str(data, "id"),
521
+ paymentId: optNum(data, "payment_id"),
522
+ status: readWithdrawalStatus(data.status),
523
+ displayStatus: oneOf(data.display_status, DISPLAY_STATUSES, "pending"),
524
+ amountUsd: optStr(data, "amount_usd"),
525
+ feeUsd: optStr(data, "fee_usd"),
526
+ receiveAmount: optStr(data, "receive_amount"),
527
+ receiveAmountUsd: optStr(data, "receive_amount_usd"),
528
+ chainId: optStr(data, "chain_id"),
529
+ currency: optStr(data, "currency"),
530
+ address: optStr(data, "address"),
531
+ destinationTxHash: optStr(data, "destination_tx_hash"),
532
+ etaSeconds: optNum(data, "eta_seconds"),
533
+ createdAt: optNum(data, "created_at") ?? 0,
534
+ updatedAt: optNum(data, "updated_at") ?? optNum(data, "created_at") ?? 0,
535
+ };
536
+ };
537
+ export const toActivityQuery = (since) => ({
538
+ since: String(Math.max(0, Math.floor(since))),
539
+ });
540
+ export const readActivity = (value) => {
541
+ const data = asObject(value, "activity");
542
+ const deposits = [];
543
+ const withdrawals = [];
544
+ if (Array.isArray(data.deposits)) {
545
+ for (const entry of data.deposits) {
546
+ if (!isObject(entry))
547
+ continue;
548
+ const paymentId = optNum(entry, "payment_id");
549
+ const creditedAt = optNum(entry, "credited_at");
550
+ if (paymentId === null || creditedAt === null)
551
+ continue;
552
+ deposits.push({
553
+ paymentId,
554
+ amountUsd: optStr(entry, "amount_usd"),
555
+ source: oneOf(entry.source, DEPOSIT_SOURCES, "other"),
556
+ creditedAt,
557
+ });
558
+ }
559
+ }
560
+ if (Array.isArray(data.withdrawals)) {
561
+ for (const entry of data.withdrawals) {
562
+ if (!isObject(entry))
563
+ continue;
564
+ const paymentId = optNum(entry, "payment_id");
565
+ const id = optStr(entry, "id");
566
+ if (paymentId === null || !id)
567
+ continue;
568
+ withdrawals.push({
569
+ paymentId,
570
+ id,
571
+ status: readWithdrawalStatus(entry.status),
572
+ updatedAt: optNum(entry, "updated_at") ?? 0,
573
+ });
574
+ }
575
+ }
576
+ return { serverTime: num(data, "server_time"), deposits, withdrawals };
577
+ };
578
+ export const REQUISITES_FRAME_MESSAGES = {
579
+ ready: "binodex-embed:ready",
580
+ resize: "binodex-embed:resize",
581
+ receiptAttached: "binodex-embed:receipt-attached",
582
+ error: "binodex-embed:error",
583
+ init: "binodex-embed:requisites-init",
584
+ };
585
+ export const requisitesInitMessage = (input) => ({
586
+ type: REQUISITES_FRAME_MESSAGES.init,
587
+ ticket: input.ticket,
588
+ deposit_id: input.depositId,
589
+ channel: input.channel,
590
+ });
591
+ export const requisitesFrameUrl = (input) => {
592
+ const url = new URL(safeFramePath(input.path), input.origin);
593
+ url.searchParams.set("deposit", input.depositId);
594
+ if (input.lang)
595
+ url.searchParams.set("lang", input.lang);
596
+ if (input.theme)
597
+ url.searchParams.set("theme", input.theme);
598
+ if (input.skin && input.theme !== "light")
599
+ url.searchParams.set("skin", input.skin);
600
+ url.hash = new URLSearchParams({ channel: input.channel }).toString();
601
+ return url.toString();
602
+ };
@@ -0,0 +1,37 @@
1
+ import { type HTMLAttributes, type ReactNode } from "react";
2
+ import { type Appearance } from "../core/appearance.js";
3
+ import type { PayEvent } from "../core/events.js";
4
+ import { type MessageKey } from "../core/i18n/index.js";
5
+ import type { CreateSession, PayMode } from "../core/types.js";
6
+ export type PayFallback = "iframe" | "error";
7
+ interface RootBaseProps extends Omit<HTMLAttributes<HTMLDivElement>, "children" | "dir" | "lang"> {
8
+ mode: PayMode;
9
+ clientId?: string;
10
+ allowedApiOrigins?: readonly string[];
11
+ allowedWidgetOrigins?: readonly string[];
12
+ apiBase?: string;
13
+ locale?: string;
14
+ messages?: Partial<Record<MessageKey, string>>;
15
+ appearance?: Appearance;
16
+ open?: boolean;
17
+ defaultOpen?: boolean;
18
+ onOpenChange?: (open: boolean) => void;
19
+ onEvent?: (event: PayEvent) => void;
20
+ fallback?: PayFallback;
21
+ platformUrl?: string;
22
+ returnUrl?: string;
23
+ unstyled?: boolean;
24
+ assetBaseUrl?: string;
25
+ fontBaseUrl?: string | false;
26
+ fetch?: typeof fetch;
27
+ children?: ReactNode;
28
+ }
29
+ export type RootProps = RootBaseProps & ({
30
+ sessionEndpoint: string;
31
+ createSession?: undefined;
32
+ } | {
33
+ createSession: CreateSession;
34
+ sessionEndpoint?: undefined;
35
+ });
36
+ export declare const Root: import("react").ForwardRefExoticComponent<RootProps & import("react").RefAttributes<HTMLDivElement>>;
37
+ export {};