@binopartner/pay-sdk 0.1.1 → 0.3.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.
- 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 +33 -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,16 @@
|
|
|
1
|
+
import { type Timers } from "./api-base.js";
|
|
2
|
+
export type PollOutcome = "continue" | "stop";
|
|
3
|
+
export interface PollerOptions {
|
|
4
|
+
intervalMs: number;
|
|
5
|
+
maxIntervalMs?: number;
|
|
6
|
+
pauseWhenHidden?: boolean;
|
|
7
|
+
immediate?: boolean;
|
|
8
|
+
timers?: Timers;
|
|
9
|
+
}
|
|
10
|
+
export interface Poller {
|
|
11
|
+
start: () => void;
|
|
12
|
+
stop: () => void;
|
|
13
|
+
now: () => void;
|
|
14
|
+
readonly running: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare const createPoller: (task: (signal: AbortSignal) => Promise<PollOutcome>, options: PollerOptions) => Poller;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { defaultTimers } from "./api-base.js";
|
|
2
|
+
const isHidden = () => typeof document !== "undefined" && document.visibilityState === "hidden";
|
|
3
|
+
export const createPoller = (task, options) => {
|
|
4
|
+
const timers = options.timers ?? defaultTimers;
|
|
5
|
+
const maxInterval = options.maxIntervalMs ?? options.intervalMs;
|
|
6
|
+
const pauseWhenHidden = options.pauseWhenHidden ?? true;
|
|
7
|
+
let delay = options.intervalMs;
|
|
8
|
+
let handle = null;
|
|
9
|
+
let controller = null;
|
|
10
|
+
let running = false;
|
|
11
|
+
let inFlight = false;
|
|
12
|
+
let pausedTick = false;
|
|
13
|
+
const clear = () => {
|
|
14
|
+
if (handle !== null)
|
|
15
|
+
timers.clearTimeout(handle);
|
|
16
|
+
handle = null;
|
|
17
|
+
};
|
|
18
|
+
const schedule = (ms) => {
|
|
19
|
+
clear();
|
|
20
|
+
if (!running)
|
|
21
|
+
return;
|
|
22
|
+
handle = timers.setTimeout(() => {
|
|
23
|
+
handle = null;
|
|
24
|
+
void tick();
|
|
25
|
+
}, ms);
|
|
26
|
+
};
|
|
27
|
+
const tick = async () => {
|
|
28
|
+
if (!running || inFlight)
|
|
29
|
+
return;
|
|
30
|
+
if (pauseWhenHidden && isHidden()) {
|
|
31
|
+
pausedTick = true;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
inFlight = true;
|
|
35
|
+
controller = new AbortController();
|
|
36
|
+
let outcome = "continue";
|
|
37
|
+
try {
|
|
38
|
+
outcome = await task(controller.signal);
|
|
39
|
+
delay = options.intervalMs;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
delay = Math.min(maxInterval, delay * 2);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
inFlight = false;
|
|
46
|
+
controller = null;
|
|
47
|
+
}
|
|
48
|
+
if (!running)
|
|
49
|
+
return;
|
|
50
|
+
if (outcome === "stop") {
|
|
51
|
+
stop();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
schedule(delay);
|
|
55
|
+
};
|
|
56
|
+
const onVisibility = () => {
|
|
57
|
+
if (!running || isHidden() || !pausedTick)
|
|
58
|
+
return;
|
|
59
|
+
pausedTick = false;
|
|
60
|
+
void tick();
|
|
61
|
+
};
|
|
62
|
+
const start = () => {
|
|
63
|
+
if (running)
|
|
64
|
+
return;
|
|
65
|
+
running = true;
|
|
66
|
+
delay = options.intervalMs;
|
|
67
|
+
if (pauseWhenHidden && typeof document !== "undefined") {
|
|
68
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
69
|
+
}
|
|
70
|
+
if (options.immediate === false)
|
|
71
|
+
schedule(delay);
|
|
72
|
+
else
|
|
73
|
+
void tick();
|
|
74
|
+
};
|
|
75
|
+
const stop = () => {
|
|
76
|
+
if (!running)
|
|
77
|
+
return;
|
|
78
|
+
running = false;
|
|
79
|
+
clear();
|
|
80
|
+
controller?.abort();
|
|
81
|
+
controller = null;
|
|
82
|
+
pausedTick = false;
|
|
83
|
+
if (typeof document !== "undefined") {
|
|
84
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
start,
|
|
89
|
+
stop,
|
|
90
|
+
now: () => {
|
|
91
|
+
if (!running || inFlight)
|
|
92
|
+
return;
|
|
93
|
+
clear();
|
|
94
|
+
void tick();
|
|
95
|
+
},
|
|
96
|
+
get running() {
|
|
97
|
+
return running;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Timers } from "./api-base.js";
|
|
2
|
+
import { PayError } from "./errors.js";
|
|
3
|
+
import type { AppearanceSkin } from "./appearance.js";
|
|
4
|
+
import type { FrameTicket, PayTheme } from "./types.js";
|
|
5
|
+
export declare const FRAME_READY_TIMEOUT_MS = 6000;
|
|
6
|
+
export declare const FRAME_MAX_REFRESHES = 3;
|
|
7
|
+
export declare const FRAME_REFRESH_BACKOFF_MS = 1000;
|
|
8
|
+
export type RequisitesFrameStatus = "loading" | "ready" | "unavailable";
|
|
9
|
+
export interface RequisitesFrameOptions {
|
|
10
|
+
depositId: string;
|
|
11
|
+
origins: readonly string[];
|
|
12
|
+
path?: string;
|
|
13
|
+
getTicket: (signal: AbortSignal) => Promise<FrameTicket>;
|
|
14
|
+
lang?: string;
|
|
15
|
+
theme?: PayTheme;
|
|
16
|
+
skin?: AppearanceSkin;
|
|
17
|
+
title?: string;
|
|
18
|
+
readyTimeoutMs?: number;
|
|
19
|
+
timers?: Timers;
|
|
20
|
+
onStatus: (status: RequisitesFrameStatus, error: PayError | null) => void;
|
|
21
|
+
onResize?: (height: number) => void;
|
|
22
|
+
onReceiptAttached?: () => void;
|
|
23
|
+
onFrameError?: (reason: string) => void;
|
|
24
|
+
isActive?: () => boolean;
|
|
25
|
+
maxRefreshes?: number;
|
|
26
|
+
refreshBackoffMs?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface RequisitesFrameHandle {
|
|
29
|
+
retry: () => void;
|
|
30
|
+
destroy: () => void;
|
|
31
|
+
}
|
|
32
|
+
export declare const runRequisitesFrame: (container: HTMLElement, options: RequisitesFrameOptions) => RequisitesFrameHandle;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { createChannel } from "../channel.js";
|
|
2
|
+
import { defaultTimers } from "./api-base.js";
|
|
3
|
+
import { PayError, isAbortError, toPayError } from "./errors.js";
|
|
4
|
+
import { REQUISITES_FRAME_MESSAGES, requisitesFrameUrl, requisitesInitMessage, } from "./wire.js";
|
|
5
|
+
export const FRAME_READY_TIMEOUT_MS = 6000;
|
|
6
|
+
export const FRAME_MAX_REFRESHES = 3;
|
|
7
|
+
export const FRAME_REFRESH_BACKOFF_MS = 1000;
|
|
8
|
+
const MIN_HEIGHT = 120;
|
|
9
|
+
const MAX_HEIGHT = 900;
|
|
10
|
+
const REASON = /^[a-z0-9_]{1,64}$/;
|
|
11
|
+
const isPlainObject = (value) => {
|
|
12
|
+
if (Object.prototype.toString.call(value) !== "[object Object]")
|
|
13
|
+
return false;
|
|
14
|
+
const prototype = Object.getPrototypeOf(value);
|
|
15
|
+
return prototype === null || prototype === Object.prototype;
|
|
16
|
+
};
|
|
17
|
+
export const runRequisitesFrame = (container, options) => {
|
|
18
|
+
const timers = options.timers ?? defaultTimers;
|
|
19
|
+
const timeoutMs = options.readyTimeoutMs ?? FRAME_READY_TIMEOUT_MS;
|
|
20
|
+
let frame = null;
|
|
21
|
+
let origin = "";
|
|
22
|
+
let channel = "";
|
|
23
|
+
let timer = null;
|
|
24
|
+
let ticketController = null;
|
|
25
|
+
let destroyed = false;
|
|
26
|
+
let attempt = 0;
|
|
27
|
+
let initialized = false;
|
|
28
|
+
let refreshes = 0;
|
|
29
|
+
let refreshTimer = null;
|
|
30
|
+
const clearTimer = () => {
|
|
31
|
+
if (timer !== null)
|
|
32
|
+
timers.clearTimeout(timer);
|
|
33
|
+
timer = null;
|
|
34
|
+
};
|
|
35
|
+
const clearRefresh = () => {
|
|
36
|
+
if (refreshTimer !== null)
|
|
37
|
+
timers.clearTimeout(refreshTimer);
|
|
38
|
+
refreshTimer = null;
|
|
39
|
+
};
|
|
40
|
+
const teardownFrame = () => {
|
|
41
|
+
clearTimer();
|
|
42
|
+
clearRefresh();
|
|
43
|
+
ticketController?.abort();
|
|
44
|
+
ticketController = null;
|
|
45
|
+
window.removeEventListener("message", onMessage);
|
|
46
|
+
frame?.remove();
|
|
47
|
+
frame = null;
|
|
48
|
+
initialized = false;
|
|
49
|
+
};
|
|
50
|
+
const unavailable = (error) => {
|
|
51
|
+
teardownFrame();
|
|
52
|
+
options.onStatus("unavailable", error ?? new PayError("REQUISITES_FRAME_UNAVAILABLE", "The requisites frame did not load"));
|
|
53
|
+
};
|
|
54
|
+
const sendInit = async () => {
|
|
55
|
+
if (initialized || !frame)
|
|
56
|
+
return;
|
|
57
|
+
initialized = true;
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
ticketController = controller;
|
|
60
|
+
try {
|
|
61
|
+
const ticket = await options.getTicket(controller.signal);
|
|
62
|
+
const target = frame?.contentWindow;
|
|
63
|
+
if (destroyed || controller.signal.aborted || !target)
|
|
64
|
+
return;
|
|
65
|
+
if (ticket.frameOrigins.length > 0 && !ticket.frameOrigins.includes(origin)) {
|
|
66
|
+
unavailable();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
target.postMessage(requisitesInitMessage({ ticket: ticket.ticket, depositId: options.depositId, channel }), origin);
|
|
70
|
+
options.onStatus("ready", null);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (destroyed || controller.signal.aborted || isAbortError(error))
|
|
74
|
+
return;
|
|
75
|
+
unavailable(toPayError(error));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
function onMessage(event) {
|
|
79
|
+
if (destroyed || !frame)
|
|
80
|
+
return;
|
|
81
|
+
if (event.source !== frame.contentWindow || event.origin !== origin)
|
|
82
|
+
return;
|
|
83
|
+
const data = event.data;
|
|
84
|
+
if (!isPlainObject(data) || data.channel !== channel)
|
|
85
|
+
return;
|
|
86
|
+
switch (data.type) {
|
|
87
|
+
case REQUISITES_FRAME_MESSAGES.ready:
|
|
88
|
+
clearTimer();
|
|
89
|
+
void sendInit();
|
|
90
|
+
return;
|
|
91
|
+
case REQUISITES_FRAME_MESSAGES.resize: {
|
|
92
|
+
const height = data.height;
|
|
93
|
+
if (typeof height !== "number" || !Number.isFinite(height) || height <= 0)
|
|
94
|
+
return;
|
|
95
|
+
const clamped = Math.min(MAX_HEIGHT, Math.max(MIN_HEIGHT, Math.ceil(height)));
|
|
96
|
+
frame.style.height = `${clamped}px`;
|
|
97
|
+
if (initialized)
|
|
98
|
+
refreshes = 0;
|
|
99
|
+
options.onResize?.(clamped);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
case REQUISITES_FRAME_MESSAGES.receiptAttached:
|
|
103
|
+
options.onReceiptAttached?.();
|
|
104
|
+
return;
|
|
105
|
+
case REQUISITES_FRAME_MESSAGES.error: {
|
|
106
|
+
const reason = typeof data.message === "string" && REASON.test(data.message) ? data.message : "unknown";
|
|
107
|
+
options.onFrameError?.(reason);
|
|
108
|
+
handleFrameError(reason);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
default:
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const tryOrigin = (index) => {
|
|
116
|
+
teardownFrame();
|
|
117
|
+
if (destroyed)
|
|
118
|
+
return;
|
|
119
|
+
const next = options.origins[index];
|
|
120
|
+
if (!next) {
|
|
121
|
+
unavailable();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
attempt = index;
|
|
125
|
+
origin = next;
|
|
126
|
+
channel = createChannel();
|
|
127
|
+
const element = document.createElement("iframe");
|
|
128
|
+
element.setAttribute("title", options.title ?? "Payment details");
|
|
129
|
+
element.setAttribute("allow", `clipboard-write ${origin}`);
|
|
130
|
+
element.setAttribute("referrerpolicy", "origin");
|
|
131
|
+
element.style.display = "block";
|
|
132
|
+
element.style.width = "100%";
|
|
133
|
+
element.style.height = `${MIN_HEIGHT * 2}px`;
|
|
134
|
+
element.style.border = "0";
|
|
135
|
+
element.style.background = "transparent";
|
|
136
|
+
// A color-scheme mismatch between the iframe element and the framed page makes browsers paint an opaque canvas.
|
|
137
|
+
if (options.theme)
|
|
138
|
+
element.style.colorScheme = options.theme;
|
|
139
|
+
element.setAttribute("src", requisitesFrameUrl({
|
|
140
|
+
origin,
|
|
141
|
+
path: options.path,
|
|
142
|
+
depositId: options.depositId,
|
|
143
|
+
channel,
|
|
144
|
+
lang: options.lang,
|
|
145
|
+
theme: options.theme,
|
|
146
|
+
skin: options.skin,
|
|
147
|
+
}));
|
|
148
|
+
frame = element;
|
|
149
|
+
window.addEventListener("message", onMessage);
|
|
150
|
+
timer = timers.setTimeout(() => {
|
|
151
|
+
timer = null;
|
|
152
|
+
tryOrigin(attempt + 1);
|
|
153
|
+
}, timeoutMs);
|
|
154
|
+
options.onStatus("loading", null);
|
|
155
|
+
container.appendChild(element);
|
|
156
|
+
};
|
|
157
|
+
function handleFrameError(reason) {
|
|
158
|
+
if (reason === "invalid_params") {
|
|
159
|
+
tryOrigin(attempt + 1);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (reason !== "ticket_expired") {
|
|
163
|
+
unavailable(reason === "rate_limited"
|
|
164
|
+
? new PayError("RATE_LIMITED", "The requisites frame is rate limited")
|
|
165
|
+
: new PayError("REQUISITES_FRAME_UNAVAILABLE", `The requisites frame failed: ${reason}`));
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (options.isActive && !options.isActive())
|
|
169
|
+
return;
|
|
170
|
+
if (refreshTimer !== null)
|
|
171
|
+
return;
|
|
172
|
+
const max = options.maxRefreshes ?? FRAME_MAX_REFRESHES;
|
|
173
|
+
if (refreshes >= max) {
|
|
174
|
+
unavailable();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
refreshes += 1;
|
|
178
|
+
const delay = (options.refreshBackoffMs ?? FRAME_REFRESH_BACKOFF_MS) * 2 ** (refreshes - 1);
|
|
179
|
+
const reloadAt = attempt;
|
|
180
|
+
refreshTimer = timers.setTimeout(() => {
|
|
181
|
+
refreshTimer = null;
|
|
182
|
+
if (destroyed || (options.isActive && !options.isActive()))
|
|
183
|
+
return;
|
|
184
|
+
tryOrigin(reloadAt);
|
|
185
|
+
}, delay);
|
|
186
|
+
}
|
|
187
|
+
tryOrigin(0);
|
|
188
|
+
return {
|
|
189
|
+
retry: () => {
|
|
190
|
+
if (destroyed)
|
|
191
|
+
return;
|
|
192
|
+
refreshes = 0;
|
|
193
|
+
tryOrigin(0);
|
|
194
|
+
},
|
|
195
|
+
destroy: () => {
|
|
196
|
+
if (destroyed)
|
|
197
|
+
return;
|
|
198
|
+
destroyed = true;
|
|
199
|
+
teardownFrame();
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const EVM_ADDRESS = /^0x[a-fA-F0-9]{40}$/;
|
|
2
|
+
const SOLANA_ADDRESS = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
3
|
+
const TRON_ADDRESS = /^T[a-km-zA-HJ-NP-Z1-9]{33}$/;
|
|
4
|
+
const BTC_BECH32 = /^(bc1|tb1)[a-z0-9]{25,87}$/;
|
|
5
|
+
const BTC_LEGACY = /^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$/;
|
|
6
|
+
export const addressFamily = (chainId) => {
|
|
7
|
+
if (chainId === "bitcoin")
|
|
8
|
+
return "bitcoin";
|
|
9
|
+
if (chainId === "solana")
|
|
10
|
+
return "solana";
|
|
11
|
+
if (chainId === "tron")
|
|
12
|
+
return "tron";
|
|
13
|
+
return "evm";
|
|
14
|
+
};
|
|
15
|
+
export const isValidWithdrawAddress = (chainId, address) => {
|
|
16
|
+
const value = address.trim();
|
|
17
|
+
if (!value)
|
|
18
|
+
return false;
|
|
19
|
+
switch (addressFamily(chainId)) {
|
|
20
|
+
case "bitcoin":
|
|
21
|
+
return BTC_BECH32.test(value) || BTC_LEGACY.test(value);
|
|
22
|
+
case "solana":
|
|
23
|
+
return SOLANA_ADDRESS.test(value);
|
|
24
|
+
case "tron":
|
|
25
|
+
return TRON_ADDRESS.test(value);
|
|
26
|
+
default:
|
|
27
|
+
return EVM_ADDRESS.test(value);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface NativeBounds {
|
|
2
|
+
minNative?: string | null;
|
|
3
|
+
maxNative?: string | null;
|
|
4
|
+
minUsd: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const formatMinLimit: (bounds: NativeBounds, currency: string) => string;
|
|
7
|
+
export type NativeRange = {
|
|
8
|
+
kind: "range";
|
|
9
|
+
text: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "from";
|
|
12
|
+
amount: string;
|
|
13
|
+
} | {
|
|
14
|
+
kind: "upTo";
|
|
15
|
+
amount: string;
|
|
16
|
+
};
|
|
17
|
+
export declare const formatNativeRange: (bounds: NativeBounds, currency: string) => NativeRange | null;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { trimNumber } from "../money.js";
|
|
2
|
+
const present = (value) => value !== null && value !== undefined && value !== "";
|
|
3
|
+
const native = (value, currency) => `${trimNumber(value)} ${currency}`;
|
|
4
|
+
export const formatMinLimit = (bounds, currency) => present(bounds.minNative) ? native(bounds.minNative, currency) : `$${Number(bounds.minUsd).toFixed(2)}`;
|
|
5
|
+
export const formatNativeRange = (bounds, currency) => {
|
|
6
|
+
const hasMin = present(bounds.minNative);
|
|
7
|
+
const hasMax = present(bounds.maxNative);
|
|
8
|
+
if (hasMin && hasMax) {
|
|
9
|
+
return {
|
|
10
|
+
kind: "range",
|
|
11
|
+
text: `${trimNumber(bounds.minNative)}–${native(bounds.maxNative, currency)}`,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
if (hasMin)
|
|
15
|
+
return { kind: "from", amount: native(bounds.minNative, currency) };
|
|
16
|
+
if (hasMax)
|
|
17
|
+
return { kind: "upTo", amount: native(bounds.maxNative, currency) };
|
|
18
|
+
return null;
|
|
19
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MessageKey } from "../i18n/index.js";
|
|
2
|
+
import type { RequisitesKind } from "./requisites.js";
|
|
3
|
+
export type PayStatusTone = "pending" | "completed" | "failed";
|
|
4
|
+
export declare const FINAL_STATES: readonly string[];
|
|
5
|
+
export declare const SUCCESS_STATES: readonly string[];
|
|
6
|
+
export declare const statusTone: (status: string) => PayStatusTone;
|
|
7
|
+
export declare const COUNTDOWN_MAX_MS: number;
|
|
8
|
+
export declare const showsCountdown: (expiresAt: number | string | null | undefined, nowMs: number) => boolean;
|
|
9
|
+
export declare const formatExpires: (expiresAt: number | string | null | undefined, nowMs: number) => string;
|
|
10
|
+
export declare const payToLabelKey: (kind: RequisitesKind) => MessageKey;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const FINAL_STATES = ["completed", "failed", "expired"];
|
|
2
|
+
export const SUCCESS_STATES = ["completed", "credited", "success"];
|
|
3
|
+
export const statusTone = (status) => {
|
|
4
|
+
if (SUCCESS_STATES.includes(status))
|
|
5
|
+
return "completed";
|
|
6
|
+
if (status === "failed" || status === "expired")
|
|
7
|
+
return "failed";
|
|
8
|
+
return "pending";
|
|
9
|
+
};
|
|
10
|
+
// Without a provider deadline paymeta keeps a one-day fallback; a "1439:59" countdown would read as a promise.
|
|
11
|
+
export const COUNTDOWN_MAX_MS = 60 * 60000;
|
|
12
|
+
const toMs = (value) => typeof value === "number" ? value : typeof value === "string" ? new Date(value).getTime() : Number.NaN;
|
|
13
|
+
export const showsCountdown = (expiresAt, nowMs) => {
|
|
14
|
+
const ms = toMs(expiresAt) - nowMs;
|
|
15
|
+
return Number.isFinite(ms) && ms <= COUNTDOWN_MAX_MS;
|
|
16
|
+
};
|
|
17
|
+
export const formatExpires = (expiresAt, nowMs) => {
|
|
18
|
+
const ms = toMs(expiresAt) - nowMs;
|
|
19
|
+
if (!Number.isFinite(ms) || ms <= 0)
|
|
20
|
+
return "00:00";
|
|
21
|
+
const total = Math.floor(ms / 1000);
|
|
22
|
+
const minutes = Math.floor(total / 60);
|
|
23
|
+
const seconds = total % 60;
|
|
24
|
+
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
|
|
25
|
+
};
|
|
26
|
+
const PAY_TO_LABEL = {
|
|
27
|
+
upi: "fiat.payToUpi",
|
|
28
|
+
card: "fiat.payToCard",
|
|
29
|
+
phone: "fiat.payToPhone",
|
|
30
|
+
account: "fiat.payToAccount",
|
|
31
|
+
};
|
|
32
|
+
export const payToLabelKey = (kind) => PAY_TO_LABEL[kind];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export interface ReceiptRule {
|
|
2
|
+
pdfOnly: boolean;
|
|
3
|
+
}
|
|
4
|
+
export declare const getRuReceiptRule: (provider: string | null | undefined, methodId: string | null | undefined) => ReceiptRule | null;
|
|
5
|
+
export declare const requiresRuReceipt: (provider: string | null | undefined, methodId: string | null | undefined) => boolean;
|
|
6
|
+
export declare const getRuReceiptRuleForMethod: (methodId: string, providerOptions: readonly string[] | null | undefined) => ReceiptRule | null;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const RECEIPT_RULES = {
|
|
2
|
+
paayzen: {
|
|
3
|
+
sbp: { pdfOnly: false },
|
|
4
|
+
c2c: { pdfOnly: false },
|
|
5
|
+
},
|
|
6
|
+
paylee: {
|
|
7
|
+
any_qr: { pdfOnly: true },
|
|
8
|
+
alfa_transgran: { pdfOnly: true },
|
|
9
|
+
alfa: { pdfOnly: true },
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
// Paylee stores the rail as `any-qr` while the picker card id is `any_qr`; both must hit the same rule.
|
|
13
|
+
const normalizeMethodKey = (methodId) => methodId.replace(/-/g, "_");
|
|
14
|
+
export const getRuReceiptRule = (provider, methodId) => {
|
|
15
|
+
if (!provider || !methodId)
|
|
16
|
+
return null;
|
|
17
|
+
return RECEIPT_RULES[provider]?.[normalizeMethodKey(methodId)] ?? null;
|
|
18
|
+
};
|
|
19
|
+
export const requiresRuReceipt = (provider, methodId) => getRuReceiptRule(provider, methodId) !== null;
|
|
20
|
+
export const getRuReceiptRuleForMethod = (methodId, providerOptions) => {
|
|
21
|
+
if (!providerOptions || providerOptions.length === 0)
|
|
22
|
+
return null;
|
|
23
|
+
let agreed = null;
|
|
24
|
+
for (const provider of providerOptions) {
|
|
25
|
+
const rule = getRuReceiptRule(provider, methodId);
|
|
26
|
+
if (!rule)
|
|
27
|
+
return null;
|
|
28
|
+
if (agreed && agreed.pdfOnly !== rule.pdfOnly)
|
|
29
|
+
return null;
|
|
30
|
+
agreed = rule;
|
|
31
|
+
}
|
|
32
|
+
return agreed;
|
|
33
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { FiatDeposit, FiatDepositStatus, Requisites, RequisitesFrameRef } from "../types.js";
|
|
2
|
+
export type RequisitesKind = "card" | "phone" | "account" | "upi";
|
|
3
|
+
export interface RequisitesView {
|
|
4
|
+
kind: RequisitesKind;
|
|
5
|
+
display: string;
|
|
6
|
+
copyValue: string;
|
|
7
|
+
amount: string;
|
|
8
|
+
amountCopy: string;
|
|
9
|
+
bank: string | null;
|
|
10
|
+
recipient: string | null;
|
|
11
|
+
country: string | null;
|
|
12
|
+
}
|
|
13
|
+
export type PayView = {
|
|
14
|
+
kind: "requisites";
|
|
15
|
+
requisites: RequisitesView;
|
|
16
|
+
} | {
|
|
17
|
+
kind: "frame";
|
|
18
|
+
frame: RequisitesFrameRef | null;
|
|
19
|
+
} | {
|
|
20
|
+
kind: "redirect";
|
|
21
|
+
url: string;
|
|
22
|
+
} | {
|
|
23
|
+
kind: "qr";
|
|
24
|
+
payload: string | null;
|
|
25
|
+
imageUrl: string | null;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "closed";
|
|
28
|
+
} | {
|
|
29
|
+
kind: "none";
|
|
30
|
+
};
|
|
31
|
+
export declare const groupCardNumber: (value: string) => string;
|
|
32
|
+
export declare const amountForCopy: (value: string) => string;
|
|
33
|
+
export declare const requisitesOf: (requisites: Requisites | null | undefined, requestedAmount: string) => RequisitesView | null;
|
|
34
|
+
export type PayFields = Pick<FiatDeposit, "status" | "amount" | "expiresAt" | "nextAction">;
|
|
35
|
+
export declare const freshRequisitesOf: (deposit: PayFields) => RequisitesView | null;
|
|
36
|
+
export declare const getPayView: (deposit: PayFields, known?: RequisitesView | null, nowMs?: number) => PayView;
|
|
37
|
+
export declare const shownPayStatus: (deposit: {
|
|
38
|
+
status: FiatDepositStatus;
|
|
39
|
+
creditedUsd?: string | null;
|
|
40
|
+
}, view: PayView) => FiatDepositStatus;
|
|
41
|
+
export declare const isTerminalDepositStatus: (status: FiatDepositStatus) => boolean;
|
|
42
|
+
export declare const redirectAfterCreate: (deposit: PayFields) => string | null;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const KINDS = ["card", "phone", "account", "upi"];
|
|
2
|
+
const text = (value) => {
|
|
3
|
+
const trimmed = value?.trim() ?? "";
|
|
4
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
5
|
+
};
|
|
6
|
+
export const groupCardNumber = (value) => value.replace(/\s+/g, "").replace(/(\d{4})(?=\d)/g, "$1 ");
|
|
7
|
+
export const amountForCopy = (value) => {
|
|
8
|
+
const num = Number(value);
|
|
9
|
+
if (!Number.isFinite(num))
|
|
10
|
+
return value.replace(/\s+/g, "");
|
|
11
|
+
return Number.isInteger(num) ? String(num) : num.toFixed(2);
|
|
12
|
+
};
|
|
13
|
+
const deadlinePassed = (expiresAt, nowMs) => expiresAt !== null && Number.isFinite(expiresAt) && expiresAt <= nowMs;
|
|
14
|
+
export const requisitesOf = (requisites, requestedAmount) => {
|
|
15
|
+
if (!requisites)
|
|
16
|
+
return null;
|
|
17
|
+
const payTo = text(requisites.value);
|
|
18
|
+
if (!payTo)
|
|
19
|
+
return null;
|
|
20
|
+
const kind = KINDS.find((entry) => entry === requisites.kind) ?? "account";
|
|
21
|
+
const amount = text(requisites.amount) ?? requestedAmount;
|
|
22
|
+
return {
|
|
23
|
+
kind,
|
|
24
|
+
display: kind === "card" ? groupCardNumber(payTo) : payTo,
|
|
25
|
+
copyValue: payTo.replace(/\s+/g, ""),
|
|
26
|
+
amount,
|
|
27
|
+
amountCopy: amountForCopy(amount),
|
|
28
|
+
bank: text(requisites.bank),
|
|
29
|
+
recipient: text(requisites.recipientName),
|
|
30
|
+
country: text(requisites.country),
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export const freshRequisitesOf = (deposit) => deposit.nextAction?.type === "show_requisites"
|
|
34
|
+
? requisitesOf(deposit.nextAction.requisites, deposit.amount ?? "")
|
|
35
|
+
: null;
|
|
36
|
+
const isWithdrawn = (deposit) => deposit.nextAction === null ||
|
|
37
|
+
(deposit.nextAction?.type === "show_requisites" &&
|
|
38
|
+
deposit.nextAction.requisites === null &&
|
|
39
|
+
deposit.nextAction.frame === null);
|
|
40
|
+
export const getPayView = (deposit, known = null, nowMs = Date.now()) => {
|
|
41
|
+
const fresh = freshRequisitesOf(deposit);
|
|
42
|
+
const shown = fresh ?? known;
|
|
43
|
+
const payable = deposit.status === "pending" && !deadlinePassed(deposit.expiresAt, nowMs);
|
|
44
|
+
if (shown) {
|
|
45
|
+
const withdrawn = fresh === null && isWithdrawn(deposit);
|
|
46
|
+
return payable && !withdrawn
|
|
47
|
+
? { kind: "requisites", requisites: shown }
|
|
48
|
+
: { kind: "closed" };
|
|
49
|
+
}
|
|
50
|
+
const action = deposit.nextAction;
|
|
51
|
+
if (action?.type === "show_requisites" && action.frame) {
|
|
52
|
+
return payable ? { kind: "frame", frame: action.frame } : { kind: "closed" };
|
|
53
|
+
}
|
|
54
|
+
if (action?.type === "redirect")
|
|
55
|
+
return { kind: "redirect", url: action.url };
|
|
56
|
+
if (action?.type === "qr") {
|
|
57
|
+
return { kind: "qr", payload: action.payload, imageUrl: action.imageUrl };
|
|
58
|
+
}
|
|
59
|
+
return { kind: "none" };
|
|
60
|
+
};
|
|
61
|
+
export const shownPayStatus = (deposit, view) => deposit.status === "pending" && !deposit.creditedUsd && view.kind === "closed"
|
|
62
|
+
? "expired"
|
|
63
|
+
: deposit.status;
|
|
64
|
+
export const isTerminalDepositStatus = (status) => status === "completed" || status === "failed" || status === "expired";
|
|
65
|
+
export const redirectAfterCreate = (deposit) => {
|
|
66
|
+
const view = getPayView(deposit);
|
|
67
|
+
return view.kind === "redirect" ? view.url : null;
|
|
68
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const acceptSlippagePct: (shownPct: string | number) => number | null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const acceptSlippagePct = (shownPct) => {
|
|
2
|
+
const pct = Number(shownPct);
|
|
3
|
+
if (!Number.isFinite(pct) || pct < 0)
|
|
4
|
+
return null;
|
|
5
|
+
// One display unit above the rounded percent clears the server's bps-precision gate.
|
|
6
|
+
return Math.min(100, Number((pct + 0.01).toFixed(2)));
|
|
7
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type Listener = () => void;
|
|
2
|
+
export interface ReadableStore<T> {
|
|
3
|
+
getSnapshot: () => T;
|
|
4
|
+
subscribe: (listener: Listener) => () => void;
|
|
5
|
+
}
|
|
6
|
+
export interface Store<T> extends ReadableStore<T> {
|
|
7
|
+
set: (next: Partial<T> | ((previous: T) => Partial<T>)) => void;
|
|
8
|
+
replace: (next: T) => void;
|
|
9
|
+
}
|
|
10
|
+
export declare const createStore: <T extends object>(initial: T) => Store<T>;
|
|
11
|
+
export type Emitter<E> = {
|
|
12
|
+
emit: (event: E) => void;
|
|
13
|
+
subscribe: (listener: (event: E) => void) => () => void;
|
|
14
|
+
};
|
|
15
|
+
export declare const createEmitter: <E>() => Emitter<E>;
|