@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.
- package/README.md +145 -2
- package/dist/core/api-base.d.ts +49 -0
- package/dist/core/api-base.js +196 -0
- package/dist/core/appearance.d.ts +17 -0
- package/dist/core/appearance.js +122 -0
- package/dist/core/auth.d.ts +32 -0
- package/dist/core/auth.js +233 -0
- package/dist/core/client.d.ts +71 -0
- package/dist/core/client.js +307 -0
- package/dist/core/clock.d.ts +7 -0
- package/dist/core/clock.js +17 -0
- package/dist/core/errors.d.ts +29 -0
- package/dist/core/errors.js +87 -0
- package/dist/core/events.d.ts +50 -0
- package/dist/core/events.js +1 -0
- package/dist/core/flows/deposit.d.ts +63 -0
- package/dist/core/flows/deposit.js +502 -0
- package/dist/core/flows/withdraw.d.ts +51 -0
- package/dist/core/flows/withdraw.js +413 -0
- package/dist/core/i18n/ar.d.ts +2 -0
- package/dist/core/i18n/ar.js +314 -0
- package/dist/core/i18n/base.d.ts +14 -0
- package/dist/core/i18n/base.js +86 -0
- package/dist/core/i18n/cs.d.ts +2 -0
- package/dist/core/i18n/cs.js +314 -0
- package/dist/core/i18n/de.d.ts +2 -0
- package/dist/core/i18n/de.js +314 -0
- package/dist/core/i18n/en-ui.d.ts +39 -0
- package/dist/core/i18n/en-ui.js +39 -0
- package/dist/core/i18n/en.d.ts +316 -0
- package/dist/core/i18n/en.js +280 -0
- package/dist/core/i18n/es.d.ts +2 -0
- package/dist/core/i18n/es.js +314 -0
- package/dist/core/i18n/fr.d.ts +2 -0
- package/dist/core/i18n/fr.js +314 -0
- package/dist/core/i18n/hi.d.ts +2 -0
- package/dist/core/i18n/hi.js +314 -0
- package/dist/core/i18n/index.d.ts +16 -0
- package/dist/core/i18n/index.js +39 -0
- package/dist/core/i18n/it.d.ts +2 -0
- package/dist/core/i18n/it.js +314 -0
- package/dist/core/i18n/ru.d.ts +2 -0
- package/dist/core/i18n/ru.js +314 -0
- package/dist/core/i18n/tr.d.ts +2 -0
- package/dist/core/i18n/tr.js +314 -0
- package/dist/core/i18n/ua.d.ts +2 -0
- package/dist/core/i18n/ua.js +314 -0
- package/dist/core/index.d.ts +37 -0
- package/dist/core/index.js +19 -0
- package/dist/core/intent.d.ts +13 -0
- package/dist/core/intent.js +45 -0
- package/dist/core/money.d.ts +22 -0
- package/dist/core/money.js +178 -0
- package/dist/core/poll.d.ts +16 -0
- package/dist/core/poll.js +100 -0
- package/dist/core/requisites-frame.d.ts +32 -0
- package/dist/core/requisites-frame.js +202 -0
- package/dist/core/rules/address.d.ts +3 -0
- package/dist/core/rules/address.js +29 -0
- package/dist/core/rules/limits.d.ts +17 -0
- package/dist/core/rules/limits.js +19 -0
- package/dist/core/rules/paying.d.ts +10 -0
- package/dist/core/rules/paying.js +32 -0
- package/dist/core/rules/receipt.d.ts +6 -0
- package/dist/core/rules/receipt.js +40 -0
- package/dist/core/rules/requisites.d.ts +42 -0
- package/dist/core/rules/requisites.js +68 -0
- package/dist/core/rules/slippage.d.ts +1 -0
- package/dist/core/rules/slippage.js +7 -0
- package/dist/core/store.d.ts +15 -0
- package/dist/core/store.js +53 -0
- package/dist/core/types.d.ts +244 -0
- package/dist/core/types.js +1 -0
- package/dist/core/wire.d.ts +178 -0
- package/dist/core/wire.js +602 -0
- package/dist/elements/Root.d.ts +37 -0
- package/dist/elements/Root.js +143 -0
- package/dist/elements/chains.d.ts +1 -0
- package/dist/elements/chains.js +1 -0
- package/dist/elements/components/Qr.d.ts +17 -0
- package/dist/elements/components/Qr.js +28 -0
- package/dist/elements/components/basic.d.ts +42 -0
- package/dist/elements/components/basic.js +129 -0
- package/dist/elements/components/deposit.d.ts +61 -0
- package/dist/elements/components/deposit.js +272 -0
- package/dist/elements/components/inputs.d.ts +24 -0
- package/dist/elements/components/inputs.js +92 -0
- package/dist/elements/components/pickers.d.ts +32 -0
- package/dist/elements/components/pickers.js +267 -0
- package/dist/elements/components/status.d.ts +25 -0
- package/dist/elements/components/status.js +221 -0
- package/dist/elements/context.d.ts +47 -0
- package/dist/elements/context.js +33 -0
- package/dist/elements/hooks/deposit.d.ts +52 -0
- package/dist/elements/hooks/deposit.js +215 -0
- package/dist/elements/hooks/root.d.ts +70 -0
- package/dist/elements/hooks/root.js +125 -0
- package/dist/elements/hooks/withdraw.d.ts +30 -0
- package/dist/elements/hooks/withdraw.js +145 -0
- package/dist/elements/index.d.ts +28 -0
- package/dist/elements/index.js +14 -0
- package/dist/elements/part.d.ts +2 -0
- package/dist/elements/part.js +1 -0
- package/dist/elements/presets/DepositForm.d.ts +32 -0
- package/dist/elements/presets/DepositForm.js +97 -0
- package/dist/elements/presets/WithdrawForm.d.ts +25 -0
- package/dist/elements/presets/WithdrawForm.js +44 -0
- package/dist/elements/presets/focus.d.ts +2 -0
- package/dist/elements/presets/focus.js +26 -0
- package/dist/elements.css +2777 -0
- package/dist/fonts.css +31 -0
- package/dist/frame.d.ts +1 -0
- package/dist/frame.js +37 -6
- package/dist/types.d.ts +1 -0
- package/dist/ui/chains.d.ts +3 -0
- package/dist/ui/chains.js +42 -0
- package/dist/ui/context.d.ts +51 -0
- package/dist/ui/context.js +63 -0
- package/dist/ui/crypto.d.ts +67 -0
- package/dist/ui/crypto.js +68 -0
- package/dist/ui/entry.d.ts +25 -0
- package/dist/ui/entry.js +26 -0
- package/dist/ui/fiat.d.ts +94 -0
- package/dist/ui/fiat.js +149 -0
- package/dist/ui/fields.d.ts +76 -0
- package/dist/ui/fields.js +71 -0
- package/dist/ui/fonts.d.ts +4 -0
- package/dist/ui/fonts.js +31 -0
- package/dist/ui/icons.d.ts +39 -0
- package/dist/ui/icons.js +116 -0
- package/dist/ui/index.d.ts +31 -0
- package/dist/ui/index.js +17 -0
- package/dist/ui/layout.d.ts +36 -0
- package/dist/ui/layout.js +44 -0
- package/dist/ui/logos.d.ts +14 -0
- package/dist/ui/logos.js +61 -0
- package/dist/ui/part.d.ts +32 -0
- package/dist/ui/part.js +33 -0
- package/dist/ui/pickers.d.ts +71 -0
- package/dist/ui/pickers.js +167 -0
- package/dist/ui/qr-encode.d.ts +30 -0
- package/dist/ui/qr-encode.js +340 -0
- package/dist/ui/summary.d.ts +31 -0
- package/dist/ui/summary.js +36 -0
- package/dist/ui/types.d.ts +67 -0
- package/dist/ui/types.js +1 -0
- package/dist/ui/urls.d.ts +4 -0
- package/dist/ui/urls.js +41 -0
- package/dist/ui/withdraw.d.ts +38 -0
- package/dist/ui/withdraw.js +30 -0
- package/dist/ui.css +2699 -0
- package/dist/widget-url.d.ts +1 -0
- package/dist/widget-url.js +16 -0
- package/package.json +46 -5
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import { defaultTimers } from "../api-base.js";
|
|
2
|
+
import { createIntentKeys } from "../intent.js";
|
|
3
|
+
import { toCreateDepositBody, toCryptoDepositBody } from "../wire.js";
|
|
4
|
+
import { PayError, isAbortError, toPayError } from "../errors.js";
|
|
5
|
+
import { compareDecimal, isPositiveDecimal, sanitizeAmountInput } from "../money.js";
|
|
6
|
+
import { createPoller } from "../poll.js";
|
|
7
|
+
import { getPayView, isTerminalDepositStatus, shownPayStatus, } from "../rules/requisites.js";
|
|
8
|
+
import { createStore } from "../store.js";
|
|
9
|
+
const INITIAL = {
|
|
10
|
+
step: "loading",
|
|
11
|
+
context: null,
|
|
12
|
+
methods: null,
|
|
13
|
+
method: null,
|
|
14
|
+
amount: "",
|
|
15
|
+
customerPhone: "",
|
|
16
|
+
amountError: null,
|
|
17
|
+
quote: null,
|
|
18
|
+
quoteLoading: false,
|
|
19
|
+
quoteError: null,
|
|
20
|
+
deposit: null,
|
|
21
|
+
view: null,
|
|
22
|
+
shownStatus: null,
|
|
23
|
+
knownRequisites: null,
|
|
24
|
+
onrampUrl: null,
|
|
25
|
+
onrampLoading: false,
|
|
26
|
+
onrampError: null,
|
|
27
|
+
credited: [],
|
|
28
|
+
receiptPending: false,
|
|
29
|
+
receiptError: null,
|
|
30
|
+
outcome: null,
|
|
31
|
+
submitError: null,
|
|
32
|
+
error: null,
|
|
33
|
+
};
|
|
34
|
+
const routeKey = (chainId, currency) => `${chainId}/${currency}`;
|
|
35
|
+
const stepForView = (view) => view.kind === "redirect" ? "redirect" : "requisites";
|
|
36
|
+
export const fiatAmountHint = (method, amount) => {
|
|
37
|
+
if (!amount)
|
|
38
|
+
return null;
|
|
39
|
+
if (!isPositiveDecimal(amount))
|
|
40
|
+
return new PayError("VALIDATION_FAILED", "Invalid amount", { details: { field: "amount" } });
|
|
41
|
+
if (method.minAmount && compareDecimal(amount, method.minAmount) === -1) {
|
|
42
|
+
return new PayError("AMOUNT_TOO_SMALL", "Amount below minimum", {
|
|
43
|
+
details: { min: method.minAmount, currency: method.currency },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (method.maxAmount && compareDecimal(amount, method.maxAmount) === 1) {
|
|
47
|
+
return new PayError("AMOUNT_TOO_LARGE", "Amount above maximum", {
|
|
48
|
+
details: { max: method.maxAmount, currency: method.currency },
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
};
|
|
53
|
+
export const createDepositFlow = (client, options = {}) => {
|
|
54
|
+
const timers = options.timers ?? defaultTimers;
|
|
55
|
+
const store = createStore(INITIAL);
|
|
56
|
+
const cryptoCache = new Map();
|
|
57
|
+
const cryptoInflight = new Map();
|
|
58
|
+
const seenPayments = new Set();
|
|
59
|
+
let loadController = null;
|
|
60
|
+
let quoteController = null;
|
|
61
|
+
let quoteTimer = null;
|
|
62
|
+
let depositPoller = null;
|
|
63
|
+
let activityPoller = null;
|
|
64
|
+
let onrampPromise = null;
|
|
65
|
+
const intents = createIntentKeys();
|
|
66
|
+
const cryptoIntents = createIntentKeys();
|
|
67
|
+
let since = 0;
|
|
68
|
+
let selection = 0;
|
|
69
|
+
let destroyed = false;
|
|
70
|
+
let started = false;
|
|
71
|
+
const get = store.getSnapshot;
|
|
72
|
+
const set = store.set;
|
|
73
|
+
const reportError = (error, scope) => {
|
|
74
|
+
client.emit({ type: "error", code: error.code, scope });
|
|
75
|
+
};
|
|
76
|
+
const clearQuote = () => {
|
|
77
|
+
if (quoteTimer !== null)
|
|
78
|
+
timers.clearTimeout(quoteTimer);
|
|
79
|
+
quoteTimer = null;
|
|
80
|
+
quoteController?.abort();
|
|
81
|
+
quoteController = null;
|
|
82
|
+
};
|
|
83
|
+
const stopDepositPoll = () => {
|
|
84
|
+
depositPoller?.stop();
|
|
85
|
+
depositPoller = null;
|
|
86
|
+
};
|
|
87
|
+
const releaseOnramp = () => {
|
|
88
|
+
if (!onrampPromise && get().onrampUrl === null && get().onrampError === null)
|
|
89
|
+
return;
|
|
90
|
+
onrampPromise = null;
|
|
91
|
+
set({ onrampUrl: null, onrampLoading: false, onrampError: null });
|
|
92
|
+
};
|
|
93
|
+
const isCryptoReusable = (deposit) => deposit.status === "active" &&
|
|
94
|
+
(deposit.expiresAt === null || deposit.expiresAt > client.clock.now());
|
|
95
|
+
const finish = (outcome) => {
|
|
96
|
+
stopDepositPoll();
|
|
97
|
+
set({ step: "done", outcome });
|
|
98
|
+
};
|
|
99
|
+
const applyFiat = (deposit) => {
|
|
100
|
+
const previous = get();
|
|
101
|
+
const known = previous.knownRequisites;
|
|
102
|
+
const view = getPayView(deposit, known, client.clock.now());
|
|
103
|
+
const shownStatus = shownPayStatus(deposit, view);
|
|
104
|
+
const knownRequisites = view.kind === "requisites" ? view.requisites : known;
|
|
105
|
+
if (previous.deposit?.id === deposit.id &&
|
|
106
|
+
previous.deposit.kind === "fiat" &&
|
|
107
|
+
previous.deposit.status !== deposit.status) {
|
|
108
|
+
client.emit({ type: "deposit.status", depositId: deposit.id, status: deposit.status });
|
|
109
|
+
}
|
|
110
|
+
set({ deposit, view, shownStatus, knownRequisites });
|
|
111
|
+
if (isTerminalDepositStatus(deposit.status)) {
|
|
112
|
+
finish(deposit.status);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const pollDeposit = (id) => {
|
|
116
|
+
stopDepositPoll();
|
|
117
|
+
depositPoller = createPoller(async (signal) => {
|
|
118
|
+
const deposit = await client.getDeposit(id, signal);
|
|
119
|
+
if (destroyed || get().deposit?.id !== id)
|
|
120
|
+
return "stop";
|
|
121
|
+
if (deposit.kind === "fiat") {
|
|
122
|
+
applyFiat(deposit);
|
|
123
|
+
return isTerminalDepositStatus(deposit.status) ? "stop" : "continue";
|
|
124
|
+
}
|
|
125
|
+
const previous = get().deposit;
|
|
126
|
+
if (previous && previous.kind === "crypto" && previous.status !== deposit.status) {
|
|
127
|
+
client.emit({ type: "deposit.status", depositId: deposit.id, status: deposit.status });
|
|
128
|
+
}
|
|
129
|
+
const key = routeKey(deposit.chainId, deposit.currency);
|
|
130
|
+
if (deposit.status === "active")
|
|
131
|
+
cryptoCache.set(key, deposit);
|
|
132
|
+
else
|
|
133
|
+
cryptoCache.delete(key);
|
|
134
|
+
set({ deposit });
|
|
135
|
+
return deposit.status === "active" ? "continue" : "stop";
|
|
136
|
+
}, {
|
|
137
|
+
intervalMs: options.depositPollMs ?? 5000,
|
|
138
|
+
maxIntervalMs: options.maxPollMs ?? 30000,
|
|
139
|
+
immediate: false,
|
|
140
|
+
timers,
|
|
141
|
+
});
|
|
142
|
+
depositPoller.start();
|
|
143
|
+
};
|
|
144
|
+
const startActivity = () => {
|
|
145
|
+
if (activityPoller)
|
|
146
|
+
return;
|
|
147
|
+
activityPoller = createPoller(async (signal) => {
|
|
148
|
+
const activity = await client.getActivity(since, signal);
|
|
149
|
+
const fresh = activity.deposits.filter((entry) => !seenPayments.has(entry.paymentId) && entry.creditedAt >= since);
|
|
150
|
+
if (fresh.length === 0)
|
|
151
|
+
return "continue";
|
|
152
|
+
fresh.forEach((entry) => {
|
|
153
|
+
seenPayments.add(entry.paymentId);
|
|
154
|
+
client.emit({
|
|
155
|
+
type: "deposit.credited",
|
|
156
|
+
paymentId: entry.paymentId,
|
|
157
|
+
amountUsd: entry.amountUsd,
|
|
158
|
+
depositSource: entry.source,
|
|
159
|
+
creditedAt: entry.creditedAt,
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
set((state) => ({ credited: [...state.credited, ...fresh] }));
|
|
163
|
+
const step = get().step;
|
|
164
|
+
const expected = step === "crypto-address" ? "crypto" : step === "onramp" ? "onramp" : null;
|
|
165
|
+
if (expected && fresh.some((entry) => entry.source === expected))
|
|
166
|
+
finish("credited");
|
|
167
|
+
void actions.refreshContext();
|
|
168
|
+
return "continue";
|
|
169
|
+
}, {
|
|
170
|
+
intervalMs: options.activityPollMs ?? 10000,
|
|
171
|
+
maxIntervalMs: options.maxPollMs ?? 30000,
|
|
172
|
+
immediate: false,
|
|
173
|
+
timers,
|
|
174
|
+
});
|
|
175
|
+
activityPoller.start();
|
|
176
|
+
};
|
|
177
|
+
const load = () => {
|
|
178
|
+
loadController?.abort();
|
|
179
|
+
const controller = new AbortController();
|
|
180
|
+
loadController = controller;
|
|
181
|
+
set({ step: "loading", error: null });
|
|
182
|
+
Promise.all([client.getContext(controller.signal), client.listMethods(controller.signal)])
|
|
183
|
+
.then(([context, methods]) => {
|
|
184
|
+
if (destroyed || controller.signal.aborted)
|
|
185
|
+
return;
|
|
186
|
+
since = context.serverTime;
|
|
187
|
+
set({ context, methods, step: "pick-method" });
|
|
188
|
+
startActivity();
|
|
189
|
+
})
|
|
190
|
+
.catch((error) => {
|
|
191
|
+
if (destroyed || controller.signal.aborted || isAbortError(error))
|
|
192
|
+
return;
|
|
193
|
+
const payError = toPayError(error);
|
|
194
|
+
set({ step: "error", error: payError });
|
|
195
|
+
reportError(payError, "load");
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
const scheduleQuote = () => {
|
|
199
|
+
clearQuote();
|
|
200
|
+
const { method, amount, amountError } = get();
|
|
201
|
+
if (!method || method.kind !== "fiat" || !amount || amountError) {
|
|
202
|
+
set({ quote: null, quoteLoading: false, quoteError: null });
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
set({ quoteLoading: true, quoteError: null });
|
|
206
|
+
quoteTimer = timers.setTimeout(() => {
|
|
207
|
+
quoteTimer = null;
|
|
208
|
+
const controller = new AbortController();
|
|
209
|
+
quoteController = controller;
|
|
210
|
+
client
|
|
211
|
+
.quoteDeposit({ methodId: method.id, amount }, controller.signal)
|
|
212
|
+
.then((quote) => {
|
|
213
|
+
if (controller.signal.aborted)
|
|
214
|
+
return;
|
|
215
|
+
set({ quote, quoteLoading: false });
|
|
216
|
+
})
|
|
217
|
+
.catch((error) => {
|
|
218
|
+
if (controller.signal.aborted || isAbortError(error))
|
|
219
|
+
return;
|
|
220
|
+
set({ quote: null, quoteLoading: false, quoteError: toPayError(error) });
|
|
221
|
+
});
|
|
222
|
+
}, options.quoteDebounceMs ?? 400);
|
|
223
|
+
};
|
|
224
|
+
const openCrypto = async (method, token) => {
|
|
225
|
+
const key = routeKey(method.chainId, method.currency);
|
|
226
|
+
const cached = cryptoCache.get(key);
|
|
227
|
+
if (cached && isCryptoReusable(cached)) {
|
|
228
|
+
set({ step: "crypto-address", deposit: cached, submitError: null });
|
|
229
|
+
pollDeposit(cached.id);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
cryptoCache.delete(key);
|
|
233
|
+
set({ step: "creating", deposit: null, submitError: null });
|
|
234
|
+
let pending = cryptoInflight.get(key);
|
|
235
|
+
let created = false;
|
|
236
|
+
if (!pending) {
|
|
237
|
+
created = true;
|
|
238
|
+
const input = { chainId: method.chainId, currency: method.currency };
|
|
239
|
+
pending = client
|
|
240
|
+
.createCryptoDeposit(input, { idempotencyKey: cryptoIntents.keyFor(toCryptoDepositBody(input)) })
|
|
241
|
+
.then((deposit) => {
|
|
242
|
+
cryptoIntents.settle({ ok: true });
|
|
243
|
+
return deposit;
|
|
244
|
+
}, (error) => {
|
|
245
|
+
cryptoIntents.settle({ ok: false, error });
|
|
246
|
+
throw error;
|
|
247
|
+
})
|
|
248
|
+
.finally(() => cryptoInflight.delete(key));
|
|
249
|
+
cryptoInflight.set(key, pending);
|
|
250
|
+
}
|
|
251
|
+
try {
|
|
252
|
+
const deposit = await pending;
|
|
253
|
+
if (deposit.status === "active")
|
|
254
|
+
cryptoCache.set(key, deposit);
|
|
255
|
+
if (created) {
|
|
256
|
+
client.emit({
|
|
257
|
+
type: "deposit.created",
|
|
258
|
+
depositId: deposit.id,
|
|
259
|
+
kind: "crypto",
|
|
260
|
+
methodId: method.id,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
if (destroyed || token !== selection)
|
|
264
|
+
return;
|
|
265
|
+
set({ step: "crypto-address", deposit });
|
|
266
|
+
pollDeposit(deposit.id);
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
if (destroyed || token !== selection)
|
|
270
|
+
return;
|
|
271
|
+
const payError = toPayError(error);
|
|
272
|
+
set({ step: "pick-method", submitError: payError });
|
|
273
|
+
reportError(payError, "deposit.create");
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const actions = {
|
|
277
|
+
start: () => {
|
|
278
|
+
if (started || destroyed)
|
|
279
|
+
return;
|
|
280
|
+
started = true;
|
|
281
|
+
load();
|
|
282
|
+
},
|
|
283
|
+
selectMethod: (methodId) => {
|
|
284
|
+
const methods = get().methods;
|
|
285
|
+
const method = methods?.methods.find((entry) => entry.id === methodId) ?? null;
|
|
286
|
+
if (!method || destroyed)
|
|
287
|
+
return;
|
|
288
|
+
selection += 1;
|
|
289
|
+
clearQuote();
|
|
290
|
+
stopDepositPoll();
|
|
291
|
+
if (method.kind !== "onramp")
|
|
292
|
+
releaseOnramp();
|
|
293
|
+
client.emit({ type: "method.selected", methodId: method.id });
|
|
294
|
+
set({
|
|
295
|
+
method,
|
|
296
|
+
amount: "",
|
|
297
|
+
amountError: null,
|
|
298
|
+
quote: null,
|
|
299
|
+
quoteLoading: false,
|
|
300
|
+
quoteError: null,
|
|
301
|
+
deposit: null,
|
|
302
|
+
view: null,
|
|
303
|
+
shownStatus: null,
|
|
304
|
+
knownRequisites: null,
|
|
305
|
+
submitError: null,
|
|
306
|
+
outcome: null,
|
|
307
|
+
});
|
|
308
|
+
if (method.kind === "crypto") {
|
|
309
|
+
if (!method.available) {
|
|
310
|
+
set({ submitError: new PayError("METHOD_UNAVAILABLE") });
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
void openCrypto(method, selection);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (method.kind === "onramp") {
|
|
317
|
+
set({ step: "onramp" });
|
|
318
|
+
void actions.loadOnramp();
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
set({ step: "enter-amount" });
|
|
322
|
+
},
|
|
323
|
+
setAmount: (raw) => {
|
|
324
|
+
const { method, amount: previous } = get();
|
|
325
|
+
const rupees = method?.kind === "fiat" && method.rail === "in";
|
|
326
|
+
const amount = sanitizeAmountInput(rupees ? raw.replace(/,/g, "") : raw, {
|
|
327
|
+
decimals: rupees ? 0 : 2,
|
|
328
|
+
});
|
|
329
|
+
const amountError = method?.kind === "fiat" ? fiatAmountHint(method, amount) : null;
|
|
330
|
+
set({ amount, amountError, submitError: null });
|
|
331
|
+
scheduleQuote();
|
|
332
|
+
},
|
|
333
|
+
setCustomerPhone: (value) => {
|
|
334
|
+
const customerPhone = value.replace(/[^\d+\s()-]/g, "").slice(0, 32);
|
|
335
|
+
set({ customerPhone });
|
|
336
|
+
},
|
|
337
|
+
submit: async () => {
|
|
338
|
+
const state = get();
|
|
339
|
+
const method = state.method;
|
|
340
|
+
if (!method || method.kind !== "fiat" || state.step === "creating")
|
|
341
|
+
return;
|
|
342
|
+
const hint = fiatAmountHint(method, state.amount);
|
|
343
|
+
if (!state.amount || hint) {
|
|
344
|
+
set({ amountError: hint ?? new PayError("VALIDATION_FAILED", "Amount required", { details: { field: "amount" } }) });
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const input = {
|
|
348
|
+
methodId: method.id,
|
|
349
|
+
amount: state.amount,
|
|
350
|
+
...(state.customerPhone.trim()
|
|
351
|
+
? { customerPhone: state.customerPhone.replace(/[\s()-]/g, "") }
|
|
352
|
+
: {}),
|
|
353
|
+
...(options.returnUrl ? { returnUrl: options.returnUrl } : {}),
|
|
354
|
+
};
|
|
355
|
+
const idempotencyKey = intents.keyFor(toCreateDepositBody(input));
|
|
356
|
+
const token = selection;
|
|
357
|
+
clearQuote();
|
|
358
|
+
set({ step: "creating", submitError: null, quoteLoading: false });
|
|
359
|
+
try {
|
|
360
|
+
let deposit;
|
|
361
|
+
try {
|
|
362
|
+
deposit = await client.createDeposit(input, { idempotencyKey });
|
|
363
|
+
intents.settle({ ok: true });
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
intents.settle({ ok: false, error });
|
|
367
|
+
throw error;
|
|
368
|
+
}
|
|
369
|
+
client.emit({
|
|
370
|
+
type: "deposit.created",
|
|
371
|
+
depositId: deposit.id,
|
|
372
|
+
kind: "fiat",
|
|
373
|
+
methodId: method.id,
|
|
374
|
+
});
|
|
375
|
+
if (destroyed || token !== selection)
|
|
376
|
+
return;
|
|
377
|
+
set({ knownRequisites: null, deposit: null });
|
|
378
|
+
applyFiat(deposit);
|
|
379
|
+
if (get().step === "done")
|
|
380
|
+
return;
|
|
381
|
+
const view = get().view;
|
|
382
|
+
set({ step: view ? stepForView(view) : "requisites" });
|
|
383
|
+
pollDeposit(deposit.id);
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
if (destroyed || token !== selection)
|
|
387
|
+
return;
|
|
388
|
+
const payError = toPayError(error);
|
|
389
|
+
set({ step: "enter-amount", submitError: payError });
|
|
390
|
+
reportError(payError, "deposit.create");
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
attachReceipt: async (file) => {
|
|
394
|
+
const deposit = get().deposit;
|
|
395
|
+
if (!deposit || deposit.kind !== "fiat" || get().receiptPending)
|
|
396
|
+
return;
|
|
397
|
+
set({ receiptPending: true, receiptError: null });
|
|
398
|
+
try {
|
|
399
|
+
const updated = await client.attachReceipt(deposit.id, file);
|
|
400
|
+
if (destroyed || get().deposit?.id !== deposit.id)
|
|
401
|
+
return;
|
|
402
|
+
set({ receiptPending: false });
|
|
403
|
+
applyFiat(updated);
|
|
404
|
+
}
|
|
405
|
+
catch (error) {
|
|
406
|
+
if (destroyed)
|
|
407
|
+
return;
|
|
408
|
+
const payError = toPayError(error);
|
|
409
|
+
set({ receiptPending: false, receiptError: payError });
|
|
410
|
+
reportError(payError, "deposit.receipt");
|
|
411
|
+
}
|
|
412
|
+
},
|
|
413
|
+
loadOnramp: () => {
|
|
414
|
+
if (onrampPromise)
|
|
415
|
+
return onrampPromise;
|
|
416
|
+
set({ onrampLoading: true, onrampError: null });
|
|
417
|
+
const request = client
|
|
418
|
+
.getOnrampSession(options.onrampTheme ? { theme: options.onrampTheme } : {})
|
|
419
|
+
.then(({ url }) => {
|
|
420
|
+
if (onrampPromise !== request)
|
|
421
|
+
return null;
|
|
422
|
+
set({ onrampUrl: url, onrampLoading: false });
|
|
423
|
+
return url;
|
|
424
|
+
})
|
|
425
|
+
.catch((error) => {
|
|
426
|
+
if (onrampPromise !== request)
|
|
427
|
+
return null;
|
|
428
|
+
const payError = toPayError(error);
|
|
429
|
+
set({ onrampLoading: false, onrampError: payError });
|
|
430
|
+
reportError(payError, "onramp");
|
|
431
|
+
return null;
|
|
432
|
+
});
|
|
433
|
+
onrampPromise = request;
|
|
434
|
+
return request;
|
|
435
|
+
},
|
|
436
|
+
retryOnramp: () => {
|
|
437
|
+
if (get().onrampLoading)
|
|
438
|
+
return onrampPromise ?? Promise.resolve(null);
|
|
439
|
+
onrampPromise = null;
|
|
440
|
+
set({ onrampUrl: null });
|
|
441
|
+
return actions.loadOnramp();
|
|
442
|
+
},
|
|
443
|
+
refreshContext: async () => {
|
|
444
|
+
try {
|
|
445
|
+
const context = await client.getContext();
|
|
446
|
+
if (!destroyed)
|
|
447
|
+
set({ context });
|
|
448
|
+
}
|
|
449
|
+
catch {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
},
|
|
453
|
+
back: () => {
|
|
454
|
+
selection += 1;
|
|
455
|
+
clearQuote();
|
|
456
|
+
stopDepositPoll();
|
|
457
|
+
releaseOnramp();
|
|
458
|
+
set({
|
|
459
|
+
step: get().methods ? "pick-method" : "loading",
|
|
460
|
+
method: null,
|
|
461
|
+
deposit: null,
|
|
462
|
+
view: null,
|
|
463
|
+
shownStatus: null,
|
|
464
|
+
knownRequisites: null,
|
|
465
|
+
submitError: null,
|
|
466
|
+
outcome: null,
|
|
467
|
+
amount: "",
|
|
468
|
+
amountError: null,
|
|
469
|
+
quote: null,
|
|
470
|
+
quoteError: null,
|
|
471
|
+
quoteLoading: false,
|
|
472
|
+
});
|
|
473
|
+
},
|
|
474
|
+
retry: () => {
|
|
475
|
+
if (get().step === "error" || !get().methods) {
|
|
476
|
+
load();
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const method = get().method;
|
|
480
|
+
if (method?.kind === "crypto")
|
|
481
|
+
actions.selectMethod(method.id);
|
|
482
|
+
else if (method?.kind === "fiat" && get().step === "enter-amount")
|
|
483
|
+
void actions.submit();
|
|
484
|
+
else if (method?.kind === "onramp")
|
|
485
|
+
void actions.retryOnramp();
|
|
486
|
+
},
|
|
487
|
+
reset: () => {
|
|
488
|
+
actions.back();
|
|
489
|
+
},
|
|
490
|
+
destroy: () => {
|
|
491
|
+
if (destroyed)
|
|
492
|
+
return;
|
|
493
|
+
destroyed = true;
|
|
494
|
+
clearQuote();
|
|
495
|
+
stopDepositPoll();
|
|
496
|
+
activityPoller?.stop();
|
|
497
|
+
activityPoller = null;
|
|
498
|
+
loadController?.abort();
|
|
499
|
+
},
|
|
500
|
+
};
|
|
501
|
+
return { getSnapshot: store.getSnapshot, subscribe: store.subscribe, actions };
|
|
502
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Timers } from "../api-base.js";
|
|
2
|
+
import type { PayClient } from "../client.js";
|
|
3
|
+
import { PayError } from "../errors.js";
|
|
4
|
+
import type { CryptoMethod, PayContext, PayMethods, SlippageWarning, Withdrawal, WithdrawalQuote } from "../types.js";
|
|
5
|
+
import type { FlowStore } from "./deposit.js";
|
|
6
|
+
export type WithdrawStep = "loading" | "form" | "quoting" | "slippage-confirm" | "two-factor" | "submitting" | "status" | "error";
|
|
7
|
+
export interface WithdrawFlowState {
|
|
8
|
+
step: WithdrawStep;
|
|
9
|
+
context: PayContext | null;
|
|
10
|
+
methods: PayMethods | null;
|
|
11
|
+
method: CryptoMethod | null;
|
|
12
|
+
address: string;
|
|
13
|
+
amount: string;
|
|
14
|
+
addressError: PayError | null;
|
|
15
|
+
amountError: PayError | null;
|
|
16
|
+
quote: WithdrawalQuote | null;
|
|
17
|
+
quoteError: PayError | null;
|
|
18
|
+
slippageWarning: SlippageWarning | null;
|
|
19
|
+
acceptedSlippagePct: number | null;
|
|
20
|
+
twoFactorRequired: boolean;
|
|
21
|
+
twoFactorCode: string;
|
|
22
|
+
withdrawal: Withdrawal | null;
|
|
23
|
+
submitError: PayError | null;
|
|
24
|
+
error: PayError | null;
|
|
25
|
+
}
|
|
26
|
+
export interface WithdrawFlowActions {
|
|
27
|
+
start: () => void;
|
|
28
|
+
selectMethod: (methodId: string) => void;
|
|
29
|
+
setAddress: (value: string) => void;
|
|
30
|
+
setAmount: (raw: string) => void;
|
|
31
|
+
setMax: () => void;
|
|
32
|
+
acceptSlippage: () => void;
|
|
33
|
+
setTwoFactorCode: (value: string) => void;
|
|
34
|
+
submit: () => Promise<void>;
|
|
35
|
+
requote: () => void;
|
|
36
|
+
refreshContext: () => Promise<void>;
|
|
37
|
+
retry: () => void;
|
|
38
|
+
reset: () => void;
|
|
39
|
+
destroy: () => void;
|
|
40
|
+
}
|
|
41
|
+
export type WithdrawFlow = FlowStore<WithdrawFlowState, WithdrawFlowActions>;
|
|
42
|
+
export interface WithdrawFlowOptions {
|
|
43
|
+
quoteDebounceMs?: number;
|
|
44
|
+
pollMs?: number;
|
|
45
|
+
maxPollMs?: number;
|
|
46
|
+
timers?: Timers;
|
|
47
|
+
}
|
|
48
|
+
export declare const isTerminalWithdrawal: (withdrawal: Withdrawal) => boolean;
|
|
49
|
+
export declare const withdrawableOf: (context: PayContext | null) => string | null;
|
|
50
|
+
export declare const withdrawAmountHint: (amount: string, method: CryptoMethod | null, methods: PayMethods | null, context: PayContext | null) => PayError | null;
|
|
51
|
+
export declare const createWithdrawFlow: (client: PayClient, options?: WithdrawFlowOptions) => WithdrawFlow;
|