@mintmoney/react 0.2.0-alpha.0 → 0.2.0-alpha.2
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/dist/css/styles.css +10 -0
- package/dist/docs/customisation.md +8 -0
- package/dist/esm/api/generated.js.map +1 -1
- package/dist/esm/api/types.js +3 -0
- package/dist/esm/api/types.js.map +1 -1
- package/dist/esm/checkout/claimable-funds.js +26 -0
- package/dist/esm/checkout/claimable-funds.js.map +1 -0
- package/dist/esm/checkout/creation-error-modal.js +6 -0
- package/dist/esm/checkout/creation-error-modal.js.map +1 -0
- package/dist/esm/checkout/index.js +29 -3
- package/dist/esm/checkout/index.js.map +1 -1
- package/dist/esm/checkout/modal.js +71 -68
- package/dist/esm/checkout/modal.js.map +1 -1
- package/dist/esm/checkout/payment-status.js +55 -0
- package/dist/esm/checkout/payment-status.js.map +1 -0
- package/dist/esm/checkout/providers/checkout-wallet.js +219 -0
- package/dist/esm/checkout/providers/checkout-wallet.js.map +1 -0
- package/dist/esm/checkout/providers/wagmi-config.js +49 -46
- package/dist/esm/checkout/providers/wagmi-config.js.map +1 -1
- package/dist/esm/checkout/refund-availability.js +16 -0
- package/dist/esm/checkout/refund-availability.js.map +1 -0
- package/dist/esm/checkout/resume-state.js +5 -14
- package/dist/esm/checkout/resume-state.js.map +1 -1
- package/dist/esm/checkout/views/claimable-funds.js +29 -0
- package/dist/esm/checkout/views/claimable-funds.js.map +1 -0
- package/dist/esm/checkout/views/crypto-intent/direct-details.js +13 -2
- package/dist/esm/checkout/views/crypto-intent/direct-details.js.map +1 -1
- package/dist/esm/checkout/views/crypto-intent/methods.js +22 -123
- package/dist/esm/checkout/views/crypto-intent/methods.js.map +1 -1
- package/dist/esm/checkout/views/crypto-intent/processing.js +45 -58
- package/dist/esm/checkout/views/crypto-intent/processing.js.map +1 -1
- package/dist/esm/checkout/views/crypto-intent/success.js +23 -38
- package/dist/esm/checkout/views/crypto-intent/success.js.map +1 -1
- package/dist/esm/checkout/views/crypto-intent/test-payment.js +11 -1
- package/dist/esm/checkout/views/crypto-intent/test-payment.js.map +1 -1
- package/dist/esm/checkout/views/crypto-intent/wallet-payment-options.js +54 -565
- package/dist/esm/checkout/views/crypto-intent/wallet-payment-options.js.map +1 -1
- package/dist/esm/checkout/views/crypto-intent/wallet-select.js +10 -160
- package/dist/esm/checkout/views/crypto-intent/wallet-select.js.map +1 -1
- package/dist/esm/checkout/views/failed.js +22 -39
- package/dist/esm/checkout/views/failed.js.map +1 -1
- package/dist/esm/checkout/views/initial.js +13 -9
- package/dist/esm/checkout/views/initial.js.map +1 -1
- package/dist/esm/checkout/views/method-select.js +12 -7
- package/dist/esm/checkout/views/method-select.js.map +1 -1
- package/dist/esm/checkout/views/restart.js +4 -1
- package/dist/esm/checkout/views/restart.js.map +1 -1
- package/dist/esm/refund/index.js +1 -1
- package/dist/esm/refund/index.js.map +1 -1
- package/dist/esm/state/checkout/checkout-state.js +60 -0
- package/dist/esm/state/checkout/checkout-state.js.map +1 -0
- package/dist/esm/state/checkout/payment-attempt.js +6 -0
- package/dist/esm/state/checkout/payment-attempt.js.map +1 -1
- package/dist/esm/state/checkout/payment-lifecycle.js +13 -1
- package/dist/esm/state/checkout/payment-lifecycle.js.map +1 -1
- package/dist/esm/state/checkout/quote-lifecycle.js +377 -0
- package/dist/esm/state/checkout/quote-lifecycle.js.map +1 -0
- package/dist/esm/state/checkout/store.js +169 -495
- package/dist/esm/state/checkout/store.js.map +1 -1
- package/dist/esm/state/checkout/wallet-payment-options.js +0 -32
- package/dist/esm/state/checkout/wallet-payment-options.js.map +1 -1
- package/dist/esm/state/checkout/wallet-payment.js +158 -0
- package/dist/esm/state/checkout/wallet-payment.js.map +1 -0
- package/dist/types/api/generated.d.ts +55 -7
- package/dist/types/api/generated.d.ts.map +1 -1
- package/dist/types/api/types.d.ts +4 -1
- package/dist/types/api/types.d.ts.map +1 -1
- package/dist/types/checkout/claimable-funds.d.ts +9 -0
- package/dist/types/checkout/claimable-funds.d.ts.map +1 -0
- package/dist/types/checkout/creation-error-modal.d.ts +9 -0
- package/dist/types/checkout/creation-error-modal.d.ts.map +1 -0
- package/dist/types/checkout/index.d.ts.map +1 -1
- package/dist/types/checkout/modal.d.ts +1 -1
- package/dist/types/checkout/modal.d.ts.map +1 -1
- package/dist/types/checkout/payment-status.d.ts +23 -0
- package/dist/types/checkout/payment-status.d.ts.map +1 -0
- package/dist/types/checkout/providers/checkout-wallet.d.ts +24 -0
- package/dist/types/checkout/providers/checkout-wallet.d.ts.map +1 -0
- package/dist/types/checkout/providers/wagmi-config.d.ts +8 -2
- package/dist/types/checkout/providers/wagmi-config.d.ts.map +1 -1
- package/dist/types/checkout/refund-availability.d.ts +2 -0
- package/dist/types/checkout/refund-availability.d.ts.map +1 -0
- package/dist/types/checkout/resume-state.d.ts +0 -4
- package/dist/types/checkout/resume-state.d.ts.map +1 -1
- package/dist/types/checkout/views/claimable-funds.d.ts +2 -0
- package/dist/types/checkout/views/claimable-funds.d.ts.map +1 -0
- package/dist/types/checkout/views/crypto-intent/direct-details.d.ts.map +1 -1
- package/dist/types/checkout/views/crypto-intent/methods.d.ts.map +1 -1
- package/dist/types/checkout/views/crypto-intent/processing.d.ts +3 -5
- package/dist/types/checkout/views/crypto-intent/processing.d.ts.map +1 -1
- package/dist/types/checkout/views/crypto-intent/success.d.ts +1 -2
- package/dist/types/checkout/views/crypto-intent/success.d.ts.map +1 -1
- package/dist/types/checkout/views/crypto-intent/test-payment.d.ts.map +1 -1
- package/dist/types/checkout/views/crypto-intent/wallet-payment-options.d.ts.map +1 -1
- package/dist/types/checkout/views/crypto-intent/wallet-select.d.ts.map +1 -1
- package/dist/types/checkout/views/failed.d.ts.map +1 -1
- package/dist/types/checkout/views/initial.d.ts.map +1 -1
- package/dist/types/checkout/views/method-select.d.ts +2 -1
- package/dist/types/checkout/views/method-select.d.ts.map +1 -1
- package/dist/types/checkout/views/restart.d.ts.map +1 -1
- package/dist/types/components/wallet-selector/wallets/wagmi/index.d.ts +1 -1
- package/dist/types/state/checkout/checkout-state.d.ts +70 -0
- package/dist/types/state/checkout/checkout-state.d.ts.map +1 -0
- package/dist/types/state/checkout/payment-attempt.d.ts +1 -0
- package/dist/types/state/checkout/payment-attempt.d.ts.map +1 -1
- package/dist/types/state/checkout/payment-lifecycle.d.ts +2 -0
- package/dist/types/state/checkout/payment-lifecycle.d.ts.map +1 -1
- package/dist/types/state/checkout/quote-lifecycle.d.ts +28 -0
- package/dist/types/state/checkout/quote-lifecycle.d.ts.map +1 -0
- package/dist/types/state/checkout/store.d.ts +17 -84
- package/dist/types/state/checkout/store.d.ts.map +1 -1
- package/dist/types/state/checkout/wallet-payment-options.d.ts +0 -16
- package/dist/types/state/checkout/wallet-payment-options.d.ts.map +1 -1
- package/dist/types/state/checkout/wallet-payment.d.ts +13 -0
- package/dist/types/state/checkout/wallet-payment.d.ts.map +1 -0
- package/package.json +1 -1
- package/dist/esm/checkout/providers/wallet-connect-resume.js +0 -129
- package/dist/esm/checkout/providers/wallet-connect-resume.js.map +0 -1
- package/dist/types/checkout/providers/wallet-connect-resume.d.ts +0 -9
- package/dist/types/checkout/providers/wallet-connect-resume.d.ts.map +0 -1
|
@@ -6,121 +6,44 @@ import { createPaymentAttempt, createTestPaymentAttempt, getPaymentById, getRequ
|
|
|
6
6
|
import { PaymentStatus, } from "../../api/types.js";
|
|
7
7
|
import { WagmiWalletInterface } from "../../components/wallet-selector/wallets/wagmi/index.js";
|
|
8
8
|
import { resolveEvmChainId, switchEvmWalletChain, } from "../../integrations/evm-chain.js";
|
|
9
|
+
import { ACTIONS, createInitialCheckoutState, } from "./checkout-state.js";
|
|
9
10
|
import { buildWalletPaymentAttemptPayload } from "./payment-attempt.js";
|
|
10
|
-
import { canCreateFreshAttempt,
|
|
11
|
+
import { canCreateFreshAttempt, isAttemptStale, isPaymentPollingTerminal, } from "./payment-lifecycle.js";
|
|
12
|
+
import { createQuoteLifecycle } from "./quote-lifecycle.js";
|
|
11
13
|
const PAYMENT_NO_LONGER_PAYABLE_MESSAGE = "This payment is no longer payable.";
|
|
12
|
-
|
|
13
|
-
export
|
|
14
|
-
(function (ACTIONS) {
|
|
15
|
-
ACTIONS["GET_CHECKOUT_CONFIG"] = "GET_CHECKOUT_CONFIG";
|
|
16
|
-
ACTIONS["GET_CRYPTO_CURRENCIES"] = "GET_CRYPTO_CURRENCIES";
|
|
17
|
-
ACTIONS["CREATE_PAYMENT"] = "CREATE_PAYMENT";
|
|
18
|
-
ACTIONS["GET_PAYMENT"] = "GET_PAYMENT";
|
|
19
|
-
ACTIONS["CREATE_PAYMENT_ATTEMPT"] = "CREATE_PAYMENT_ATTEMPT";
|
|
20
|
-
ACTIONS["CREATE_TEST_PAYMENT_ATTEMPT"] = "CREATE_TEST_PAYMENT_ATTEMPT";
|
|
21
|
-
ACTIONS["EXECUTE_TRANSFER"] = "EXECUTE_TRANSFER";
|
|
22
|
-
ACTIONS["POLL_PAYMENT"] = "POLL_PAYMENT";
|
|
23
|
-
ACTIONS["CHECK_CHAIN_STATUS"] = "CHECK_CHAIN_STATUS";
|
|
24
|
-
})(ACTIONS || (ACTIONS = {}));
|
|
25
|
-
const initialWalletPaymentState = () => ({
|
|
26
|
-
status: "idle",
|
|
27
|
-
options: [],
|
|
28
|
-
walletAddress: null,
|
|
29
|
-
selectedOptionId: null,
|
|
30
|
-
quoteId: null,
|
|
31
|
-
routeId: null,
|
|
32
|
-
sourceTxHash: null,
|
|
33
|
-
destinationTxHash: null,
|
|
34
|
-
quoteStatus: "idle",
|
|
35
|
-
quote: null,
|
|
36
|
-
quoteError: null,
|
|
37
|
-
error: null,
|
|
38
|
-
});
|
|
39
|
-
const initialActionState = (initialValue) => Object.values(ACTIONS).reduce((acc, action) => {
|
|
40
|
-
acc[action] = initialValue;
|
|
41
|
-
return acc;
|
|
42
|
-
}, {});
|
|
43
|
-
export const useCheckoutStore = create()(immer((_set, _get) => ({
|
|
44
|
-
checkoutId: null,
|
|
45
|
-
selectedMethod: null,
|
|
46
|
-
selectedCurrency: null,
|
|
47
|
-
retrySession: false,
|
|
48
|
-
methods: null,
|
|
49
|
-
cryptoCurrency: null,
|
|
50
|
-
checkout: null,
|
|
51
|
-
payment: null,
|
|
52
|
-
cryptoPaymentAttempt: null,
|
|
53
|
-
loading: initialActionState(false),
|
|
54
|
-
error: initialActionState(null),
|
|
55
|
-
processing: false,
|
|
56
|
-
transfer: {
|
|
57
|
-
status: "idle",
|
|
58
|
-
error: null,
|
|
59
|
-
transactionHash: null,
|
|
60
|
-
chainStatus: "pending",
|
|
61
|
-
},
|
|
62
|
-
polling: {
|
|
63
|
-
isPolling: false,
|
|
64
|
-
pollCount: 0,
|
|
65
|
-
lastPollTime: null,
|
|
66
|
-
},
|
|
67
|
-
quote: {
|
|
68
|
-
status: "idle",
|
|
69
|
-
error: null,
|
|
70
|
-
expiresAt: null,
|
|
71
|
-
remainingMs: null,
|
|
72
|
-
totalDurationMs: null,
|
|
73
|
-
},
|
|
74
|
-
walletPayment: initialWalletPaymentState(),
|
|
75
|
-
})));
|
|
14
|
+
export { ACTIONS } from "./checkout-state.js";
|
|
15
|
+
export const useCheckoutStore = create()(immer(() => createInitialCheckoutState()));
|
|
76
16
|
const set = (fn) => {
|
|
77
17
|
useCheckoutStore.setState(fn);
|
|
78
18
|
};
|
|
79
19
|
const get = () => useCheckoutStore.getState();
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
if (quoteCountdownInterval) {
|
|
99
|
-
clearInterval(quoteCountdownInterval);
|
|
100
|
-
quoteCountdownInterval = null;
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
const clearQuoteAutoRefreshTimer = () => {
|
|
104
|
-
if (quoteExpiryTimer) {
|
|
105
|
-
clearTimeout(quoteExpiryTimer);
|
|
106
|
-
quoteExpiryTimer = null;
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
const getPreservedRefreshingQuoteState = () => {
|
|
110
|
-
const currentQuote = get().quote;
|
|
111
|
-
if (currentQuote.remainingMs === null) {
|
|
112
|
-
return { status: "refreshing", error: null, ...clearQuoteTiming() };
|
|
113
|
-
}
|
|
114
|
-
return {
|
|
115
|
-
status: "refreshing",
|
|
116
|
-
error: null,
|
|
117
|
-
expiresAt: currentQuote.expiresAt,
|
|
118
|
-
remainingMs: currentQuote.expiresAt !== null
|
|
119
|
-
? Math.max(0, currentQuote.expiresAt - Date.now())
|
|
120
|
-
: currentQuote.remainingMs,
|
|
121
|
-
totalDurationMs: currentQuote.totalDurationMs,
|
|
20
|
+
let checkoutOpening = 0;
|
|
21
|
+
let checkoutOwner = null;
|
|
22
|
+
// Store requests always belong to the opening that started them. Wallet and
|
|
23
|
+
// quote lifetimes may narrow ownership, but cannot bypass this boundary.
|
|
24
|
+
export const captureCheckoutOpening = (additional) => {
|
|
25
|
+
const opening = checkoutOpening;
|
|
26
|
+
return () => opening === checkoutOpening && (additional?.() ?? true);
|
|
27
|
+
};
|
|
28
|
+
export const beginCheckoutOpening = () => {
|
|
29
|
+
const owner = Symbol();
|
|
30
|
+
checkoutOwner = owner;
|
|
31
|
+
checkoutOpening += 1;
|
|
32
|
+
return () => {
|
|
33
|
+
if (checkoutOwner !== owner)
|
|
34
|
+
return;
|
|
35
|
+
checkoutOwner = null;
|
|
36
|
+
resetCheckoutStore();
|
|
122
37
|
};
|
|
123
38
|
};
|
|
39
|
+
const quoteLifecycle = createQuoteLifecycle({
|
|
40
|
+
get,
|
|
41
|
+
set,
|
|
42
|
+
captureOpening: captureCheckoutOpening,
|
|
43
|
+
createWalletAttempt: (...args) => createWalletOptionPaymentAttempt(...args),
|
|
44
|
+
createManualAttempt: (...args) => createManualDirectPaymentAttempt(...args),
|
|
45
|
+
});
|
|
46
|
+
export const { refreshQuote, syncQuote, configureWalletQuoteRefresh, configureManualQuoteRefresh, prepareWalletQuoteReplacement, clearQuoteRefresh, loadWalletPaymentQuote, setWalletPaymentOptions, resetWalletPaymentState, setWalletPaymentQuote, } = quoteLifecycle;
|
|
124
47
|
const getCurrentCheckoutAttempt = (state = get()) => {
|
|
125
48
|
return state.cryptoPaymentAttempt ?? state.payment?.payment_attempt ?? null;
|
|
126
49
|
};
|
|
@@ -134,6 +57,8 @@ const getReusableCurrentAttemptForSource = (source) => {
|
|
|
134
57
|
const attempt = getCurrentCheckoutAttempt();
|
|
135
58
|
if (!attempt || isAttemptStale(attempt))
|
|
136
59
|
return null;
|
|
60
|
+
if (attempt.quote_request_id !== source.quoteRequestId)
|
|
61
|
+
return null;
|
|
137
62
|
if (!attemptMatchesSource(attempt, source))
|
|
138
63
|
return null;
|
|
139
64
|
return normalizeWalletAddress(attempt.refund_to) ===
|
|
@@ -164,272 +89,9 @@ const getPaymentSnapshotAfterAttempt = async (paymentId, attempt, fallbackPaymen
|
|
|
164
89
|
return await getPaymentById(paymentId);
|
|
165
90
|
}
|
|
166
91
|
catch {
|
|
167
|
-
return withFreshAttempt(
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
const buildDirectWalletQuote = (option, attempt) => {
|
|
171
|
-
const amount = String(attempt.amount_formatted);
|
|
172
|
-
return {
|
|
173
|
-
quoteId: option.quoteId,
|
|
174
|
-
routeId: option.routeId,
|
|
175
|
-
toolName: "Direct transfer",
|
|
176
|
-
fromAmount: amount,
|
|
177
|
-
fromAmountDisplay: amount,
|
|
178
|
-
toAmount: amount,
|
|
179
|
-
toAmountDisplay: amount,
|
|
180
|
-
estimatedDurationMinutes: 1,
|
|
181
|
-
};
|
|
182
|
-
};
|
|
183
|
-
const setQuote = (update, walletUpdate) => {
|
|
184
|
-
set((state) => {
|
|
185
|
-
Object.assign(state.quote, update);
|
|
186
|
-
if (walletUpdate) {
|
|
187
|
-
Object.assign(state.walletPayment, walletUpdate);
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
};
|
|
191
|
-
const clearQuoteTiming = () => ({
|
|
192
|
-
expiresAt: null,
|
|
193
|
-
remainingMs: null,
|
|
194
|
-
totalDurationMs: null,
|
|
195
|
-
});
|
|
196
|
-
const refreshQuoteCountdown = (expiresAt) => {
|
|
197
|
-
if (expiresAt === null)
|
|
198
|
-
return;
|
|
199
|
-
setQuote({
|
|
200
|
-
remainingMs: Math.max(0, expiresAt - Date.now()),
|
|
201
|
-
});
|
|
202
|
-
};
|
|
203
|
-
const getWalletQuoteLoadingState = () => ({
|
|
204
|
-
quoteStatus: "loading",
|
|
205
|
-
quote: null,
|
|
206
|
-
quoteError: null,
|
|
207
|
-
quoteId: null,
|
|
208
|
-
routeId: null,
|
|
209
|
-
});
|
|
210
|
-
const getWalletQuoteReadyState = (quote) => ({
|
|
211
|
-
quoteStatus: "ready",
|
|
212
|
-
quote,
|
|
213
|
-
quoteId: quote.quoteId,
|
|
214
|
-
routeId: quote.routeId,
|
|
215
|
-
quoteError: null,
|
|
216
|
-
});
|
|
217
|
-
const getWalletQuoteFailedState = (message) => ({
|
|
218
|
-
quoteStatus: "failed",
|
|
219
|
-
quote: null,
|
|
220
|
-
quoteError: message,
|
|
221
|
-
});
|
|
222
|
-
const getWalletAttemptRefreshContext = () => {
|
|
223
|
-
const state = get();
|
|
224
|
-
const payment = state.payment;
|
|
225
|
-
const checkout = state.checkout;
|
|
226
|
-
const settlementCurrency = state.cryptoCurrency;
|
|
227
|
-
const selectedOption = state.walletPayment.options.find((option) => option.id === state.walletPayment.selectedOptionId);
|
|
228
|
-
const walletAddress = state.walletPayment.walletAddress;
|
|
229
|
-
if (!payment || !checkout || !settlementCurrency) {
|
|
230
|
-
throw new Error("Wallet payment context is not ready");
|
|
231
|
-
}
|
|
232
|
-
if (!selectedOption || !walletAddress) {
|
|
233
|
-
throw new Error("Missing wallet payment selection for quote refresh");
|
|
234
|
-
}
|
|
235
|
-
return {
|
|
236
|
-
payment,
|
|
237
|
-
checkout,
|
|
238
|
-
settlementCurrency,
|
|
239
|
-
selectedOption,
|
|
240
|
-
walletAddress,
|
|
241
|
-
};
|
|
242
|
-
};
|
|
243
|
-
const resolveRefreshedWalletQuote = async ({ selectedOption, checkout, payment, settlementCurrency, walletAddress, attempt, }) => {
|
|
244
|
-
if (selectedOption.kind === "direct") {
|
|
245
|
-
return buildDirectWalletQuote(selectedOption, attempt);
|
|
246
|
-
}
|
|
247
|
-
// Keep routed-quote loading behind a dynamic import so the checkout store
|
|
248
|
-
// does not eagerly pull the LI.FI integration path into manual-only flows.
|
|
249
|
-
const { fetchWalletPaymentQuote } = await import("../../integrations/index.js");
|
|
250
|
-
return fetchWalletPaymentQuote({
|
|
251
|
-
option: selectedOption,
|
|
252
|
-
context: {
|
|
253
|
-
checkout,
|
|
254
|
-
payment: get().payment ?? payment,
|
|
255
|
-
settlementCurrency,
|
|
256
|
-
walletAddress,
|
|
257
|
-
cryptoPaymentAttempt: attempt,
|
|
258
|
-
},
|
|
259
|
-
});
|
|
260
|
-
};
|
|
261
|
-
const refreshWalletPaymentAttempt = async () => {
|
|
262
|
-
const context = getWalletAttemptRefreshContext();
|
|
263
|
-
const attempt = await createWalletOptionPaymentAttempt(context.selectedOption, context.walletAddress, null);
|
|
264
|
-
const quote = await resolveRefreshedWalletQuote({
|
|
265
|
-
...context,
|
|
266
|
-
attempt,
|
|
267
|
-
});
|
|
268
|
-
// Only the wallet-quote payload is set here; refreshQuote settles quote.status
|
|
269
|
-
// (ready + expiry timer) from the freshly created attempt once the rebuild ends.
|
|
270
|
-
setWalletPaymentState(getWalletQuoteReadyState(quote));
|
|
271
|
-
};
|
|
272
|
-
/**
|
|
273
|
-
* Settle the displayed quote state from the active attempt and arm a single
|
|
274
|
-
* expiry timer for a live quote. It never triggers a refresh — the caller
|
|
275
|
-
* decides what to do with a "stale" result — so it is safe to reuse mid-refresh
|
|
276
|
-
* without recursion.
|
|
277
|
-
*/
|
|
278
|
-
const settleQuoteFromAttempt = (payment, attempt) => {
|
|
279
|
-
clearQuoteExpiryTimer();
|
|
280
|
-
if (refreshMode === "none" || !attempt || !canCreateFreshAttempt(payment)) {
|
|
281
|
-
setQuote({ status: "idle", error: null, ...clearQuoteTiming() });
|
|
282
|
-
return "idle";
|
|
283
|
-
}
|
|
284
|
-
if (isAttemptStale(attempt)) {
|
|
285
|
-
return "stale";
|
|
286
|
-
}
|
|
287
|
-
const timing = getAttemptQuoteTiming(attempt);
|
|
288
|
-
setQuote({ status: "ready", error: null, ...timing });
|
|
289
|
-
// One-shot timer: when the live quote crosses its window we refresh
|
|
290
|
-
// so the payer always sees a payable amount instead of an expiry dead-end.
|
|
291
|
-
const expiresAt = timing.expiresAt ?? getAttemptExpiry(attempt);
|
|
292
|
-
if (expiresAt !== null) {
|
|
293
|
-
quoteExpiryTimer = setTimeout(() => {
|
|
294
|
-
void refreshQuote();
|
|
295
|
-
}, Math.max(0, expiresAt - Date.now()));
|
|
296
|
-
quoteCountdownInterval = setInterval(() => {
|
|
297
|
-
refreshQuoteCountdown(expiresAt);
|
|
298
|
-
}, 1000);
|
|
299
|
-
}
|
|
300
|
-
return "ready";
|
|
301
|
-
};
|
|
302
|
-
/**
|
|
303
|
-
* Fetch a fresh payable amount for the active flow. Driven both by the silent
|
|
304
|
-
* expiry timer and by the payer's explicit "Try again" — a stale or missing
|
|
305
|
-
* quote is always recoverable, never terminal.
|
|
306
|
-
*/
|
|
307
|
-
export const refreshQuote = async () => {
|
|
308
|
-
if (refreshMode === "none" || isRefreshing)
|
|
309
|
-
return;
|
|
310
|
-
if (refreshMode === "wallet" && !get().walletPayment.selectedOptionId) {
|
|
311
|
-
// Wallet refresh needs a chosen option to know which quote to rebuild, so
|
|
312
|
-
// skip ticks that land before the payer picked one (or after returning to
|
|
313
|
-
// the option list).
|
|
314
|
-
setQuote({ status: "idle", error: null, ...clearQuoteTiming() });
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
if (!canCreateFreshAttempt(get().payment)) {
|
|
318
|
-
setQuote({
|
|
319
|
-
status: "failed",
|
|
320
|
-
error: PAYMENT_NO_LONGER_PAYABLE_MESSAGE,
|
|
321
|
-
...clearQuoteTiming(),
|
|
322
|
-
});
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
// Never refresh out from under an in-flight payment: the execution already
|
|
326
|
-
// captured the quote it is paying, so swapping the attempt would churn the UI
|
|
327
|
-
// and orphan an extra attempt. The view tears down the refresh context when it
|
|
328
|
-
// leaves the pay screen.
|
|
329
|
-
if (get().processing)
|
|
330
|
-
return;
|
|
331
|
-
isRefreshing = true;
|
|
332
|
-
clearQuoteExpiryTimer();
|
|
333
|
-
setQuote(getPreservedRefreshingQuoteState(), refreshMode === "wallet" ? getWalletQuoteLoadingState() : undefined);
|
|
334
|
-
try {
|
|
335
|
-
if (refreshMode === "wallet") {
|
|
336
|
-
await refreshWalletPaymentAttempt();
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
339
|
-
// The replacement attempt must keep the refund recipient the payer bound
|
|
340
|
-
// to the expiring attempt: refund_to exists precisely for the expiry case,
|
|
341
|
-
// and an automatic refresh gives the payer no chance to re-enter it.
|
|
342
|
-
const currentAttempt = get().cryptoPaymentAttempt;
|
|
343
|
-
await createManualDirectPaymentAttempt("", 1, currentAttempt?.refund_to ?? null, {
|
|
344
|
-
...(currentAttempt?.payment_provider === "relay"
|
|
345
|
-
? {
|
|
346
|
-
mode: "relay_deposit",
|
|
347
|
-
source: {
|
|
348
|
-
asset: currentAttempt.routing.source.token,
|
|
349
|
-
chain: currentAttempt.routing.source.chain,
|
|
350
|
-
},
|
|
351
|
-
}
|
|
352
|
-
: {}),
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
catch (error) {
|
|
357
|
-
isRefreshing = false;
|
|
358
|
-
const message = error instanceof Error
|
|
359
|
-
? error.message
|
|
360
|
-
: "Failed to refresh payment quote";
|
|
361
|
-
setQuote({ status: "failed", error: message, ...clearQuoteTiming() }, refreshMode === "wallet" ? getWalletQuoteFailedState(message) : undefined);
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
isRefreshing = false;
|
|
365
|
-
if (settleQuoteFromAttempt(get().payment, get().cryptoPaymentAttempt) ===
|
|
366
|
-
"stale") {
|
|
367
|
-
setQuote({
|
|
368
|
-
status: "failed",
|
|
369
|
-
error: STALE_REPLACEMENT_QUOTE_MESSAGE,
|
|
370
|
-
...clearQuoteTiming(),
|
|
371
|
-
});
|
|
92
|
+
return withFreshAttempt(fallbackPayment, attempt);
|
|
372
93
|
}
|
|
373
94
|
};
|
|
374
|
-
/**
|
|
375
|
-
* Re-sync the quote lifecycle whenever the payment snapshot or active attempt
|
|
376
|
-
* changes. A fresh attempt arms the expiry refresh timer; a stale or missing
|
|
377
|
-
* one triggers an immediate refresh. This is the only entrypoint used
|
|
378
|
-
* after loading a payment, creating an attempt, or configuring a refresh mode.
|
|
379
|
-
*/
|
|
380
|
-
export const syncQuote = (payment, attempt) => {
|
|
381
|
-
// A refresh in flight settles the final state itself; ignore the intermediate
|
|
382
|
-
// snapshot it emits while rebuilding the attempt.
|
|
383
|
-
if (isRefreshing)
|
|
384
|
-
return;
|
|
385
|
-
if (settleQuoteFromAttempt(payment, attempt ?? null) === "stale") {
|
|
386
|
-
void refreshQuote();
|
|
387
|
-
}
|
|
388
|
-
};
|
|
389
|
-
export const configureWalletQuoteRefresh = (walletAddress) => {
|
|
390
|
-
// Wallet refresh replaces a stale attempt by creating a fresh crypto payment
|
|
391
|
-
// attempt and reloading the wallet quote for the selected route.
|
|
392
|
-
refreshMode = "wallet";
|
|
393
|
-
set((state) => {
|
|
394
|
-
state.quote.error = null;
|
|
395
|
-
state.walletPayment.walletAddress = walletAddress;
|
|
396
|
-
});
|
|
397
|
-
syncQuote(get().payment, get().cryptoPaymentAttempt);
|
|
398
|
-
};
|
|
399
|
-
export const prepareWalletQuoteReplacement = (walletAddress = null) => {
|
|
400
|
-
// A payer-triggered wallet quote replacement is already fetching a new
|
|
401
|
-
// attempt/route, so pause only the automatic expiry refresh. Keep the
|
|
402
|
-
// countdown visible until the replacement quote is ready or fails.
|
|
403
|
-
refreshMode = "wallet";
|
|
404
|
-
clearQuoteAutoRefreshTimer();
|
|
405
|
-
set((state) => {
|
|
406
|
-
state.quote.error = null;
|
|
407
|
-
state.walletPayment.walletAddress =
|
|
408
|
-
walletAddress ?? state.walletPayment.walletAddress;
|
|
409
|
-
});
|
|
410
|
-
};
|
|
411
|
-
export const configureManualQuoteRefresh = () => {
|
|
412
|
-
// Manual refresh reuses the same lifecycle but only needs a fresh direct/relay
|
|
413
|
-
// address — no wallet quote metadata.
|
|
414
|
-
refreshMode = "manual";
|
|
415
|
-
set((state) => {
|
|
416
|
-
state.quote.error = null;
|
|
417
|
-
});
|
|
418
|
-
syncQuote(get().payment, get().cryptoPaymentAttempt);
|
|
419
|
-
};
|
|
420
|
-
export const clearQuoteRefresh = (options = {}) => {
|
|
421
|
-
// Drop the refresh mode before returning to method selection or closing the
|
|
422
|
-
// checkout so future expiry events do not reuse stale wallet/manual context.
|
|
423
|
-
refreshMode = "none";
|
|
424
|
-
isRefreshing = false;
|
|
425
|
-
clearQuoteExpiryTimer();
|
|
426
|
-
set((state) => {
|
|
427
|
-
state.quote = { status: "idle", error: null, ...clearQuoteTiming() };
|
|
428
|
-
if (!options.preserveWalletAddress) {
|
|
429
|
-
state.walletPayment.walletAddress = null;
|
|
430
|
-
}
|
|
431
|
-
});
|
|
432
|
-
};
|
|
433
95
|
const updatePaymentSnapshot = (payment, attempt, extra) => {
|
|
434
96
|
set((state) => {
|
|
435
97
|
state.payment = payment;
|
|
@@ -451,14 +113,18 @@ export const clearErrors = () => {
|
|
|
451
113
|
});
|
|
452
114
|
};
|
|
453
115
|
export const initialiseStore = async (payment) => {
|
|
454
|
-
resetCheckoutStore();
|
|
455
|
-
|
|
116
|
+
resetCheckoutStore(payment);
|
|
117
|
+
const canUpdate = captureCheckoutOpening();
|
|
456
118
|
try {
|
|
457
119
|
const refreshedPayment = await getPaymentById(payment.id);
|
|
120
|
+
if (!canUpdate())
|
|
121
|
+
return;
|
|
458
122
|
updatePaymentSnapshot(refreshedPayment);
|
|
459
123
|
hydrateCheckoutFromPayment(refreshedPayment);
|
|
460
124
|
}
|
|
461
125
|
catch (err) {
|
|
126
|
+
if (!canUpdate())
|
|
127
|
+
return;
|
|
462
128
|
set((state) => {
|
|
463
129
|
state.error[ACTIONS.GET_CHECKOUT_CONFIG] = "Failed to load checkout";
|
|
464
130
|
});
|
|
@@ -467,7 +133,10 @@ export const initialiseStore = async (payment) => {
|
|
|
467
133
|
export const createWalletOptionPaymentAttempt = async (
|
|
468
134
|
// "direct" is a routing classification: the payer sends the settlement
|
|
469
135
|
// asset on the settlement chain. It is not tied to manual vs wallet UX.
|
|
470
|
-
option, sendingAddress, quote) => {
|
|
136
|
+
option, sendingAddress, quote, ownsWallet) => {
|
|
137
|
+
const canUpdate = captureCheckoutOpening(ownsWallet);
|
|
138
|
+
if (!canUpdate())
|
|
139
|
+
throw new Error("Checkout is no longer active");
|
|
471
140
|
const payment = useCheckoutStore.getState().payment;
|
|
472
141
|
const paymentId = payment?.id;
|
|
473
142
|
if (!paymentId) {
|
|
@@ -476,11 +145,13 @@ option, sendingAddress, quote) => {
|
|
|
476
145
|
if (!canCreateFreshAttempt(payment)) {
|
|
477
146
|
throw Error(PAYMENT_NO_LONGER_PAYABLE_MESSAGE);
|
|
478
147
|
}
|
|
148
|
+
const quoteRequestId = quoteLifecycle.getWalletQuoteRequestId();
|
|
479
149
|
if (option.kind === "direct") {
|
|
480
150
|
const reusableAttempt = getReusableCurrentAttemptForSource({
|
|
481
151
|
token: option.fromToken,
|
|
482
152
|
chain: option.fromChain,
|
|
483
153
|
sendingAddress,
|
|
154
|
+
quoteRequestId,
|
|
484
155
|
});
|
|
485
156
|
if (reusableAttempt) {
|
|
486
157
|
return reuseCurrentAttempt(reusableAttempt);
|
|
@@ -496,9 +167,17 @@ option, sendingAddress, quote) => {
|
|
|
496
167
|
sendingAddress,
|
|
497
168
|
quote,
|
|
498
169
|
requiredConfirmations: 1,
|
|
170
|
+
quoteRequestId,
|
|
499
171
|
});
|
|
500
172
|
const attempt = await createPaymentAttempt(paymentId, attemptPayload, 1);
|
|
173
|
+
if (!canUpdate())
|
|
174
|
+
return attempt;
|
|
175
|
+
if (attempt.quote_request_id !== quoteRequestId) {
|
|
176
|
+
throw new Error("Could not get a fresh payment quote. Please try again.");
|
|
177
|
+
}
|
|
501
178
|
const refreshedPayment = await getPaymentSnapshotAfterAttempt(paymentId, attempt, payment);
|
|
179
|
+
if (!canUpdate())
|
|
180
|
+
return attempt;
|
|
502
181
|
updatePaymentSnapshot(refreshedPayment, attempt, (state) => {
|
|
503
182
|
state.retrySession = false;
|
|
504
183
|
state.error[ACTIONS.CREATE_PAYMENT_ATTEMPT] = null;
|
|
@@ -507,6 +186,8 @@ option, sendingAddress, quote) => {
|
|
|
507
186
|
return attempt;
|
|
508
187
|
}
|
|
509
188
|
catch (err) {
|
|
189
|
+
if (!canUpdate())
|
|
190
|
+
throw err;
|
|
510
191
|
const error = err;
|
|
511
192
|
set((state) => {
|
|
512
193
|
state.error[ACTIONS.CREATE_PAYMENT_ATTEMPT] = error.message;
|
|
@@ -515,7 +196,10 @@ option, sendingAddress, quote) => {
|
|
|
515
196
|
throw error;
|
|
516
197
|
}
|
|
517
198
|
};
|
|
518
|
-
export const createManualDirectPaymentAttempt = async (sendingAddress = "", confirmations = 1, refundTo, options = {}) => {
|
|
199
|
+
export const createManualDirectPaymentAttempt = async (sendingAddress = "", confirmations = 1, refundTo, options = {}, ownsQuote) => {
|
|
200
|
+
const canUpdate = captureCheckoutOpening(ownsQuote);
|
|
201
|
+
if (!canUpdate())
|
|
202
|
+
throw new Error("Checkout is no longer active");
|
|
519
203
|
const payment = useCheckoutStore.getState().payment;
|
|
520
204
|
const paymentId = payment?.id;
|
|
521
205
|
if (!paymentId) {
|
|
@@ -558,13 +242,18 @@ export const createManualDirectPaymentAttempt = async (sendingAddress = "", conf
|
|
|
558
242
|
},
|
|
559
243
|
};
|
|
560
244
|
const attempt = await createPaymentAttempt(paymentId, attemptPayload);
|
|
245
|
+
if (!canUpdate())
|
|
246
|
+
return attempt;
|
|
561
247
|
const refreshedPayment = await getPaymentSnapshotAfterAttempt(paymentId, attempt, payment);
|
|
248
|
+
if (!canUpdate())
|
|
249
|
+
return attempt;
|
|
562
250
|
updatePaymentSnapshot(refreshedPayment, attempt, (state) => {
|
|
563
251
|
state.retrySession = false;
|
|
564
252
|
});
|
|
565
253
|
return attempt;
|
|
566
254
|
};
|
|
567
255
|
export const createSuccessfulTestPaymentAttempt = async () => {
|
|
256
|
+
const canUpdate = captureCheckoutOpening();
|
|
568
257
|
const paymentId = useCheckoutStore.getState().payment?.id;
|
|
569
258
|
if (!paymentId) {
|
|
570
259
|
throw Error("Cannot create test payment attempt without payment object.");
|
|
@@ -579,7 +268,11 @@ export const createSuccessfulTestPaymentAttempt = async () => {
|
|
|
579
268
|
throw Error("Cannot create test payment attempt without payment object.");
|
|
580
269
|
}
|
|
581
270
|
const attempt = await createTestPaymentAttempt(paymentId, "0xsuccess", currentPayment);
|
|
271
|
+
if (!canUpdate())
|
|
272
|
+
return;
|
|
582
273
|
const payment = await getPaymentById(paymentId);
|
|
274
|
+
if (!canUpdate())
|
|
275
|
+
return;
|
|
583
276
|
updatePaymentSnapshot(payment, attempt, (state) => {
|
|
584
277
|
state.retrySession = false;
|
|
585
278
|
state.error[ACTIONS.CREATE_TEST_PAYMENT_ATTEMPT] = null;
|
|
@@ -587,6 +280,8 @@ export const createSuccessfulTestPaymentAttempt = async () => {
|
|
|
587
280
|
});
|
|
588
281
|
}
|
|
589
282
|
catch (err) {
|
|
283
|
+
if (!canUpdate())
|
|
284
|
+
return;
|
|
590
285
|
const error = err;
|
|
591
286
|
set((state) => {
|
|
592
287
|
state.error[ACTIONS.CREATE_TEST_PAYMENT_ATTEMPT] = error.message;
|
|
@@ -596,6 +291,7 @@ export const createSuccessfulTestPaymentAttempt = async () => {
|
|
|
596
291
|
}
|
|
597
292
|
};
|
|
598
293
|
export const createFailedTestPaymentAttempt = async () => {
|
|
294
|
+
const canUpdate = captureCheckoutOpening();
|
|
599
295
|
const paymentId = useCheckoutStore.getState().payment?.id;
|
|
600
296
|
if (!paymentId) {
|
|
601
297
|
throw Error("Cannot create test payment attempt without payment object.");
|
|
@@ -610,7 +306,11 @@ export const createFailedTestPaymentAttempt = async () => {
|
|
|
610
306
|
throw Error("Cannot create test payment attempt without payment object.");
|
|
611
307
|
}
|
|
612
308
|
const attempt = await createTestPaymentAttempt(paymentId, "0xfail", currentPayment);
|
|
309
|
+
if (!canUpdate())
|
|
310
|
+
return;
|
|
613
311
|
const payment = await getPaymentById(paymentId);
|
|
312
|
+
if (!canUpdate())
|
|
313
|
+
return;
|
|
614
314
|
updatePaymentSnapshot(payment, attempt, (state) => {
|
|
615
315
|
state.retrySession = false;
|
|
616
316
|
state.error[ACTIONS.CREATE_TEST_PAYMENT_ATTEMPT] = null;
|
|
@@ -618,6 +318,8 @@ export const createFailedTestPaymentAttempt = async () => {
|
|
|
618
318
|
});
|
|
619
319
|
}
|
|
620
320
|
catch (err) {
|
|
321
|
+
if (!canUpdate())
|
|
322
|
+
return;
|
|
621
323
|
const error = err;
|
|
622
324
|
set((state) => {
|
|
623
325
|
state.error[ACTIONS.CREATE_TEST_PAYMENT_ATTEMPT] = error.message;
|
|
@@ -667,6 +369,7 @@ export const hydrateCheckoutFromPayment = (payment) => {
|
|
|
667
369
|
}
|
|
668
370
|
};
|
|
669
371
|
export const getPayment = async () => {
|
|
372
|
+
const canUpdate = captureCheckoutOpening();
|
|
670
373
|
const ACTION = ACTIONS.GET_PAYMENT;
|
|
671
374
|
set((state) => {
|
|
672
375
|
state.loading[ACTION] = true;
|
|
@@ -682,6 +385,8 @@ export const getPayment = async () => {
|
|
|
682
385
|
}
|
|
683
386
|
try {
|
|
684
387
|
const resp = await getPaymentById(payment.id);
|
|
388
|
+
if (!canUpdate())
|
|
389
|
+
return null;
|
|
685
390
|
if (resp) {
|
|
686
391
|
updatePaymentSnapshot(resp);
|
|
687
392
|
set((state) => {
|
|
@@ -700,6 +405,8 @@ export const getPayment = async () => {
|
|
|
700
405
|
}
|
|
701
406
|
}
|
|
702
407
|
catch (error) {
|
|
408
|
+
if (!canUpdate())
|
|
409
|
+
return null;
|
|
703
410
|
set((state) => {
|
|
704
411
|
state.loading[ACTION] = false;
|
|
705
412
|
state.error[ACTION] = "Failed to load payment";
|
|
@@ -707,7 +414,10 @@ export const getPayment = async () => {
|
|
|
707
414
|
return null;
|
|
708
415
|
}
|
|
709
416
|
};
|
|
710
|
-
export const executeTransfer = async (wallet) => {
|
|
417
|
+
export const executeTransfer = async (wallet, ownsWallet) => {
|
|
418
|
+
const canExecute = captureCheckoutOpening(ownsWallet);
|
|
419
|
+
if (!canExecute())
|
|
420
|
+
throw new Error("Checkout is no longer active");
|
|
711
421
|
const ACTION = ACTIONS.EXECUTE_TRANSFER;
|
|
712
422
|
const state = get();
|
|
713
423
|
if (!state.cryptoPaymentAttempt) {
|
|
@@ -743,7 +453,12 @@ export const executeTransfer = async (wallet) => {
|
|
|
743
453
|
assetName: state.cryptoPaymentAttempt.settlement_config.asset_name,
|
|
744
454
|
},
|
|
745
455
|
};
|
|
456
|
+
if (!canExecute()) {
|
|
457
|
+
throw new Error("Wallet connection changed. Connect again before paying.");
|
|
458
|
+
}
|
|
746
459
|
const txHash = await wallet.executeTransfer(transfer);
|
|
460
|
+
if (!canExecute())
|
|
461
|
+
return txHash;
|
|
747
462
|
if (!txHash) {
|
|
748
463
|
throw new Error("Failed to get transaction hash");
|
|
749
464
|
}
|
|
@@ -756,6 +471,8 @@ export const executeTransfer = async (wallet) => {
|
|
|
756
471
|
return txHash;
|
|
757
472
|
}
|
|
758
473
|
catch (error) {
|
|
474
|
+
if (!canExecute())
|
|
475
|
+
throw error;
|
|
759
476
|
const errorMessage = error instanceof Error ? error.message : "Failed to execute transfer";
|
|
760
477
|
set((state) => {
|
|
761
478
|
state.transfer.status = "failed";
|
|
@@ -766,52 +483,59 @@ export const executeTransfer = async (wallet) => {
|
|
|
766
483
|
throw error instanceof Error ? error : new Error(errorMessage);
|
|
767
484
|
}
|
|
768
485
|
};
|
|
769
|
-
export const checkChainStatus = async (wallet) => {
|
|
486
|
+
export const checkChainStatus = async (wallet, submitted) => {
|
|
770
487
|
const ACTION = ACTIONS.CHECK_CHAIN_STATUS;
|
|
771
488
|
const state = get();
|
|
772
|
-
|
|
489
|
+
const hash = submitted?.hash ?? state.transfer.transactionHash;
|
|
490
|
+
const canUpdate = captureCheckoutOpening(submitted?.canUpdate);
|
|
491
|
+
if (!hash || (!submitted && state.transfer.status !== "success")) {
|
|
773
492
|
return;
|
|
774
493
|
}
|
|
775
494
|
// Check if the wallet is a WagmiWalletInterface
|
|
776
495
|
if (!(wallet instanceof WagmiWalletInterface)) {
|
|
777
496
|
const error = new Error("Wallet does not support chain status checks");
|
|
778
497
|
console.error(error.message);
|
|
779
|
-
|
|
780
|
-
state
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
498
|
+
if (canUpdate())
|
|
499
|
+
set((state) => {
|
|
500
|
+
state.transfer.status = "failed";
|
|
501
|
+
state.transfer.error = error.message;
|
|
502
|
+
state.transfer.chainStatus = "failed";
|
|
503
|
+
state.error[ACTION] = error.message;
|
|
504
|
+
});
|
|
785
505
|
throw error;
|
|
786
506
|
}
|
|
787
|
-
|
|
788
|
-
state
|
|
789
|
-
|
|
790
|
-
|
|
507
|
+
if (canUpdate())
|
|
508
|
+
set((state) => {
|
|
509
|
+
state.loading[ACTION] = true;
|
|
510
|
+
state.error[ACTION] = null;
|
|
511
|
+
});
|
|
791
512
|
try {
|
|
792
513
|
const receipt = await waitForTransactionReceipt(wallet.wagmiConfig, {
|
|
793
|
-
hash:
|
|
514
|
+
hash: hash,
|
|
515
|
+
chainId: submitted?.chainId,
|
|
794
516
|
confirmations: 1,
|
|
795
517
|
});
|
|
796
518
|
if (receipt.status === "reverted") {
|
|
797
519
|
throw new Error("Transaction reverted before confirmation");
|
|
798
520
|
}
|
|
799
|
-
|
|
800
|
-
state
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
521
|
+
if (canUpdate())
|
|
522
|
+
set((state) => {
|
|
523
|
+
state.transfer.chainStatus = "confirmed";
|
|
524
|
+
state.loading[ACTION] = false;
|
|
525
|
+
state.error[ACTION] = null;
|
|
526
|
+
});
|
|
804
527
|
}
|
|
805
528
|
catch (error) {
|
|
806
529
|
console.error("Error checking transaction status:", error);
|
|
807
530
|
const errorMessage = error instanceof Error ? error.message : "Failed to check chain status";
|
|
808
|
-
|
|
809
|
-
state
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
531
|
+
if (canUpdate())
|
|
532
|
+
set((state) => {
|
|
533
|
+
state.transfer.status = "failed";
|
|
534
|
+
state.transfer.error = errorMessage;
|
|
535
|
+
state.transfer.chainStatus = "failed";
|
|
536
|
+
state.loading[ACTION] = false;
|
|
537
|
+
state.error[ACTION] = errorMessage;
|
|
538
|
+
});
|
|
815
539
|
throw error instanceof Error ? error : new Error(errorMessage);
|
|
816
540
|
}
|
|
817
541
|
};
|
|
@@ -829,7 +553,13 @@ export const startPaymentPolling = (onPaymentStatusChange, confirmationStatus =
|
|
|
829
553
|
const ACTION = ACTIONS.POLL_PAYMENT;
|
|
830
554
|
const POLL_INTERVAL = 5000;
|
|
831
555
|
let pollInterval = null;
|
|
556
|
+
let stopped = false;
|
|
557
|
+
const canUpdate = captureCheckoutOpening(() => !stopped);
|
|
832
558
|
const pollPaymentStatus = async () => {
|
|
559
|
+
if (!canUpdate()) {
|
|
560
|
+
stopPolling();
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
833
563
|
const state = get();
|
|
834
564
|
if (!state.polling.isPolling)
|
|
835
565
|
return;
|
|
@@ -839,16 +569,12 @@ export const startPaymentPolling = (onPaymentStatusChange, confirmationStatus =
|
|
|
839
569
|
});
|
|
840
570
|
try {
|
|
841
571
|
const updatedPayment = await getPayment();
|
|
842
|
-
if (!updatedPayment) {
|
|
572
|
+
if (!canUpdate() || !updatedPayment) {
|
|
843
573
|
return;
|
|
844
574
|
}
|
|
845
|
-
|
|
846
|
-
? [PaymentStatus.PAID]
|
|
847
|
-
: [confirmationStatus, PaymentStatus.PAID];
|
|
848
|
-
if (successStatuses.includes(updatedPayment.status) ||
|
|
849
|
-
updatedPayment.status === PaymentStatus.FAILED) {
|
|
575
|
+
if (isPaymentPollingTerminal(updatedPayment.status, confirmationStatus)) {
|
|
850
576
|
onPaymentStatusChange(updatedPayment.status);
|
|
851
|
-
|
|
577
|
+
stopPolling();
|
|
852
578
|
}
|
|
853
579
|
}
|
|
854
580
|
catch (error) {
|
|
@@ -856,13 +582,14 @@ export const startPaymentPolling = (onPaymentStatusChange, confirmationStatus =
|
|
|
856
582
|
}
|
|
857
583
|
};
|
|
858
584
|
const stopPolling = () => {
|
|
585
|
+
const owned = canUpdate();
|
|
586
|
+
stopped = true;
|
|
859
587
|
if (pollInterval) {
|
|
860
588
|
clearInterval(pollInterval);
|
|
861
589
|
pollInterval = null;
|
|
862
590
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
});
|
|
591
|
+
if (owned)
|
|
592
|
+
stopPaymentPolling();
|
|
866
593
|
};
|
|
867
594
|
set((state) => {
|
|
868
595
|
state.polling.isPolling = true;
|
|
@@ -871,10 +598,9 @@ export const startPaymentPolling = (onPaymentStatusChange, confirmationStatus =
|
|
|
871
598
|
state.loading[ACTION] = true;
|
|
872
599
|
state.error[ACTION] = null;
|
|
873
600
|
});
|
|
874
|
-
// Start polling immediately
|
|
875
|
-
void pollPaymentStatus();
|
|
876
601
|
// Set up interval for subsequent polls
|
|
877
602
|
pollInterval = setInterval(pollPaymentStatus, POLL_INTERVAL);
|
|
603
|
+
void pollPaymentStatus();
|
|
878
604
|
return {
|
|
879
605
|
stop: stopPolling,
|
|
880
606
|
};
|
|
@@ -887,89 +613,42 @@ export const stopPaymentPolling = () => {
|
|
|
887
613
|
state.loading[ACTIONS.POLL_PAYMENT] = false;
|
|
888
614
|
});
|
|
889
615
|
};
|
|
890
|
-
export const resetCheckoutStore = () => {
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
clearQuoteExpiryTimer();
|
|
616
|
+
export const resetCheckoutStore = (payment = null) => {
|
|
617
|
+
checkoutOpening += 1;
|
|
618
|
+
quoteLifecycle.dispose();
|
|
894
619
|
set((state) => {
|
|
895
|
-
Object.assign(state, {
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
selectedMethod: null,
|
|
899
|
-
selectedCurrency: null,
|
|
900
|
-
retrySession: false,
|
|
901
|
-
methods: null,
|
|
902
|
-
cryptoCurrency: null,
|
|
903
|
-
checkout: null,
|
|
904
|
-
payment: null,
|
|
905
|
-
cryptoPaymentAttempt: null,
|
|
906
|
-
quote: {
|
|
907
|
-
status: "idle",
|
|
908
|
-
error: null,
|
|
909
|
-
expiresAt: null,
|
|
910
|
-
remainingMs: null,
|
|
911
|
-
totalDurationMs: null,
|
|
912
|
-
},
|
|
913
|
-
loading: initialActionState(false),
|
|
914
|
-
error: initialActionState(null),
|
|
915
|
-
transfer: {
|
|
916
|
-
status: "idle",
|
|
917
|
-
error: null,
|
|
918
|
-
transactionHash: null,
|
|
919
|
-
chainStatus: "pending",
|
|
920
|
-
},
|
|
921
|
-
polling: {
|
|
922
|
-
isPolling: false,
|
|
923
|
-
pollCount: 0,
|
|
924
|
-
lastPollTime: null,
|
|
925
|
-
},
|
|
926
|
-
walletPayment: initialWalletPaymentState(),
|
|
620
|
+
Object.assign(state, createInitialCheckoutState(), {
|
|
621
|
+
payment,
|
|
622
|
+
cryptoPaymentAttempt: payment?.payment_attempt ?? null,
|
|
927
623
|
});
|
|
928
624
|
});
|
|
929
625
|
};
|
|
930
626
|
export const resetCheckoutSessionForRetry = () => {
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
627
|
+
checkoutOpening += 1;
|
|
628
|
+
quoteLifecycle.dispose();
|
|
629
|
+
const initial = createInitialCheckoutState();
|
|
934
630
|
set((state) => {
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
state.quote = {
|
|
957
|
-
status: "idle",
|
|
958
|
-
error: null,
|
|
959
|
-
expiresAt: null,
|
|
960
|
-
remainingMs: null,
|
|
961
|
-
totalDurationMs: null,
|
|
962
|
-
};
|
|
963
|
-
state.error[ACTIONS.CREATE_PAYMENT_ATTEMPT] = null;
|
|
964
|
-
state.error[ACTIONS.CREATE_TEST_PAYMENT_ATTEMPT] = null;
|
|
965
|
-
state.error[ACTIONS.EXECUTE_TRANSFER] = null;
|
|
966
|
-
state.error[ACTIONS.POLL_PAYMENT] = null;
|
|
967
|
-
state.error[ACTIONS.CHECK_CHAIN_STATUS] = null;
|
|
968
|
-
state.loading[ACTIONS.CREATE_PAYMENT_ATTEMPT] = false;
|
|
969
|
-
state.loading[ACTIONS.CREATE_TEST_PAYMENT_ATTEMPT] = false;
|
|
970
|
-
state.loading[ACTIONS.EXECUTE_TRANSFER] = false;
|
|
971
|
-
state.loading[ACTIONS.POLL_PAYMENT] = false;
|
|
972
|
-
state.loading[ACTIONS.CHECK_CHAIN_STATUS] = false;
|
|
631
|
+
Object.assign(state, {
|
|
632
|
+
selectedMethod: initial.selectedMethod,
|
|
633
|
+
selectedCurrency: initial.selectedCurrency,
|
|
634
|
+
retrySession: true,
|
|
635
|
+
cryptoPaymentAttempt: initial.cryptoPaymentAttempt,
|
|
636
|
+
processing: initial.processing,
|
|
637
|
+
transfer: initial.transfer,
|
|
638
|
+
polling: initial.polling,
|
|
639
|
+
walletPayment: initial.walletPayment,
|
|
640
|
+
quote: initial.quote,
|
|
641
|
+
});
|
|
642
|
+
for (const action of [
|
|
643
|
+
ACTIONS.CREATE_PAYMENT_ATTEMPT,
|
|
644
|
+
ACTIONS.CREATE_TEST_PAYMENT_ATTEMPT,
|
|
645
|
+
ACTIONS.EXECUTE_TRANSFER,
|
|
646
|
+
ACTIONS.POLL_PAYMENT,
|
|
647
|
+
ACTIONS.CHECK_CHAIN_STATUS,
|
|
648
|
+
]) {
|
|
649
|
+
state.error[action] = initial.error[action];
|
|
650
|
+
state.loading[action] = initial.loading[action];
|
|
651
|
+
}
|
|
973
652
|
});
|
|
974
653
|
};
|
|
975
654
|
export const setCheckoutMethod = (method) => {
|
|
@@ -1002,9 +681,4 @@ export const setWalletPaymentState = (update) => {
|
|
|
1002
681
|
Object.assign(state.walletPayment, update);
|
|
1003
682
|
});
|
|
1004
683
|
};
|
|
1005
|
-
export const resetWalletPaymentState = () => {
|
|
1006
|
-
set((state) => {
|
|
1007
|
-
state.walletPayment = initialWalletPaymentState();
|
|
1008
|
-
});
|
|
1009
|
-
};
|
|
1010
684
|
//# sourceMappingURL=store.js.map
|