-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseTransakRouting.ts
More file actions
1015 lines (948 loc) · 35.1 KB
/
Copy pathuseTransakRouting.ts
File metadata and controls
1015 lines (948 loc) · 35.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { useCallback, useRef } from 'react';
import { useNavigation, type NavigationProp } from '@react-navigation/native';
import { useSelector } from 'react-redux';
import type { CaipChainId } from '@metamask/utils';
import { strings } from '../../../../../locales/i18n';
import { useTheme } from '../../../../util/theme';
import {
RampsOrderStatus,
type TransakBuyQuote,
} from '@metamask/ramps-controller';
import { REDIRECTION_URL } from '../constants';
import { generateThemeParameters } from '../utils/depositUtils';
import type {
AddressFormData,
BasicInfoFormData,
} from '../types/transakNativeForms';
import { createCheckoutNavDetails } from '../Views/Checkout';
import { createV2EnterEmailNavDetails } from '../Views/NativeFlow/EnterEmail';
import { createKycWebviewNavDetails } from '../Views/NativeFlow/KycWebview';
import useAnalytics from './useAnalytics';
import { showV2OrderToast } from '../utils/v2OrderToast';
import Logger from '../../../../util/Logger';
import Routes from '../../../../constants/navigation/Routes';
import { useTransakController } from './useTransakController';
import { useRampsUserRegion } from './useRampsUserRegion';
import { useRampsPaymentMethods } from './useRampsPaymentMethods';
import { useRampsProviders } from './useRampsProviders';
import { selectTokens } from '../../../../selectors/rampsController';
import useRampAccountAddress from './useRampAccountAddress';
import { isHttpUnauthorized } from '../utils/isHttpUnauthorized';
import { parseUserFacingError } from '../utils/parseUserFacingError';
import { useRampsOrders } from './useRampsOrders';
import {
closeSession,
failSession,
getSession,
} from '../headless/sessionRegistry';
import { dismissHeadlessFlow } from '../headless/headlessEntryNavigation';
import { getChainIdFromAssetId } from '../headless';
import { setHeadlessOrderContext } from '../../../../core/Engine/controllers/ramps-controller/headlessOrderContextRegistry';
import { emitTerminalOrderAnalyticsFromCallback } from '../../../../core/Engine/controllers/ramps-controller/event-handlers/analytics';
interface RampStackParamList {
/** `baseRouteParams` (e.g. `headlessSessionId`) are merged onto this route in resets — see `navigateToVerifyIdentityCallback`. */
RampVerifyIdentity: {
quote: TransakBuyQuote;
headlessSessionId?: string;
amount?: string;
currency?: string;
assetId?: string;
};
/** `baseRouteParams` are merged here too — see `navigateToBasicInfoCallback`. */
RampBasicInfo: {
quote: TransakBuyQuote;
previousFormData?: BasicInfoFormData & AddressFormData;
headlessSessionId?: string;
amount?: string;
currency?: string;
assetId?: string;
};
RampBankDetails: { orderId: string; shouldUpdate?: boolean };
RampOrderProcessing: { orderId: string };
RampAdditionalVerification: {
quote: TransakBuyQuote;
kycUrl: string;
workFlowRunId: string;
/** User-entered fiat from BuildQuote; used when resetting stack so amount screen keeps the typed value. */
amount?: number;
};
RampKycProcessing: { headlessSessionId?: string } | undefined;
RampEnterEmail: undefined;
Checkout: {
url: string;
providerName: string;
userAgent?: string;
onNavigationStateChange?: (navState: { url: string }) => void;
};
[key: string]: object | undefined;
}
class LimitExceededError extends Error {
readonly headlessBuyErrorCode = 'LIMIT_EXCEEDED';
readonly details?: Record<string, unknown>;
constructor(message: string, details?: Record<string, unknown>) {
super(message);
this.name = 'LimitExceededError';
this.details = details;
}
}
interface UseTransakRoutingConfig {
screenLocation?: string;
/**
* Stack base route for navigation resets done after auth/KYC. Defaults to
* `Routes.RAMP.AMOUNT_INPUT` (BuildQuote) — that's what the regular
* Unified Buy v2 flow expects today.
*
* Headless callers (Phase 5) override this to `Routes.RAMP.HEADLESS_HOST`
* so post-auth resets land back on the Host instead of dropping the user
* onto BuildQuote, which a headless consumer never opened.
*/
baseRoute?: string;
/**
* Static params for the stack base route. For BuildQuote we still want to
* keep the per-call `amount` thread (so the input is pre-filled after a
* reset) — that's the default behavior. For the Host we pass
* `{ headlessSessionId }` so the Host can re-pick up the live session
* after a reset.
*/
baseRouteParams?: Record<string, unknown>;
}
export const useTransakRouting = (config?: UseTransakRoutingConfig) => {
const baseRoute = config?.baseRoute;
const baseRouteParams = config?.baseRouteParams;
// Headless-mode marker extracted from the caller's config. When the
// Host wires `baseRouteParams: { headlessSessionId }` through, this
// lets post-checkout success paths hand the orderId to the session's
// `onOrderCreated` callback and unwind the ramp stack instead of
// resetting to `RAMPS_ORDER_DETAILS`.
const headlessSessionId = (
baseRouteParams as { headlessSessionId?: string } | undefined
)?.headlessSessionId;
// Composes the stack base entry used by `navigation.reset` calls below.
// - When the caller didn't override `baseRoute`, we keep BuildQuote as the
// base and merge the per-call `amount` (so the input is pre-filled when
// the user lands back).
// - When `baseRoute` was overridden (headless flow's HEADLESS_HOST), we
// pass the static `baseRouteParams` as-is — `amount` is irrelevant
// because the Host carries the full quote on `session.params.quote`.
const buildBaseRouteEntry = useCallback(
({ amount }: { amount?: number }) => {
if (baseRoute) {
return {
name: baseRoute,
params: baseRouteParams,
};
}
return {
name: Routes.RAMP.AMOUNT_INPUT,
params: { amount, ...(baseRouteParams ?? {}) },
};
},
[baseRoute, baseRouteParams],
);
const navigation = useNavigation<NavigationProp<RampStackParamList>>();
const { themeAppearance, colors } = useTheme();
const trackEvent = useAnalytics();
const processingOrderIdRef = useRef<string | null>(null);
const { addOrder, refreshOrder } = useRampsOrders();
const dismissActiveHeadlessFlow = useCallback(() => {
dismissHeadlessFlow(navigation);
}, [navigation]);
const {
logoutFromProvider,
getUserDetails,
getKycRequirement,
getAdditionalRequirements,
createOrder: transakCreateOrder,
getOrder,
getUserLimits,
requestOtt,
generatePaymentWidgetUrl,
submitPurposeOfUsageForm,
} = useTransakController();
const { userRegion } = useRampsUserRegion();
const { selectedPaymentMethod } = useRampsPaymentMethods();
const { selectedProvider } = useRampsProviders();
const { selected: selectedToken } = useSelector(selectTokens);
const headlessSessionParams = getSession(headlessSessionId)?.params;
const headlessAssetId = headlessSessionParams?.assetId;
const walletAddressChainId =
(headlessAssetId ? getChainIdFromAssetId(headlessAssetId) : null) ??
(selectedToken?.chainId as CaipChainId | undefined);
const resolvedWalletAddress = useRampAccountAddress(walletAddressChainId);
const walletAddress =
headlessSessionParams?.walletAddress ?? resolvedWalletAddress;
const fiatCurrency = userRegion?.country?.currency || '';
const regionIsoCode = userRegion?.regionCode || '';
/**
* Emits HEADLESS `RAMPS_ORDER_FAILED` for headless buy failures (TRAM-3623
* §7): `failSession` (non-React) can't emit, so this host does, around that
* call. No-op without a live session; `quote` (when in scope) seeds amount.
*/
const emitHeadlessOrderFailed = useCallback(
(error: unknown, quote?: TransakBuyQuote, providerOrderId?: string) => {
const session = getSession(headlessSessionId);
if (!session) {
return;
}
trackEvent('RAMPS_ORDER_FAILED', {
ramp_type: 'HEADLESS',
ramp_surface: session.params?.rampSurface,
// TRAM-3696: present when the failure occurs after an order exists.
...(providerOrderId && { provider_order_id: providerOrderId }),
amount_source: Number(quote?.fiatAmount ?? session.params?.amount ?? 0),
amount_destination: 0,
payment_method_id: selectedPaymentMethod?.id || '',
region: regionIsoCode,
chain_id: (selectedToken?.chainId as string) || '',
currency_destination: selectedToken?.assetId || '',
currency_destination_symbol: selectedToken?.symbol || undefined,
currency_source: quote?.fiatCurrency || fiatCurrency || '',
error_message: parseUserFacingError(
error,
strings('deposit.buildQuote.unexpectedError'),
),
is_authenticated: true,
});
},
[
headlessSessionId,
trackEvent,
selectedPaymentMethod?.id,
regionIsoCode,
selectedToken?.chainId,
selectedToken?.assetId,
selectedToken?.symbol,
fiatCurrency,
],
);
const checkUserLimits = useCallback(
async (quote: TransakBuyQuote, kycType: string) => {
try {
const userLimits = await getUserLimits(
fiatCurrency,
selectedPaymentMethod?.id || '',
kycType,
);
if (!userLimits?.remaining) {
return;
}
const { remaining } = userLimits;
const dailyLimit = remaining['1'];
const monthlyLimit = remaining['30'];
const yearlyLimit = remaining['365'];
if (
dailyLimit === undefined ||
monthlyLimit === undefined ||
yearlyLimit === undefined
) {
return;
}
const depositAmount = quote.fiatAmount;
if (depositAmount > dailyLimit) {
throw new LimitExceededError(
strings('deposit.buildQuote.limitExceeded', {
period: 'daily',
remaining: `${dailyLimit} ${fiatCurrency}`,
}),
{
period: 'daily',
remaining: dailyLimit,
currency: fiatCurrency,
},
);
}
if (depositAmount > monthlyLimit) {
throw new LimitExceededError(
strings('deposit.buildQuote.limitExceeded', {
period: 'monthly',
remaining: `${monthlyLimit} ${fiatCurrency}`,
}),
{
period: 'monthly',
remaining: monthlyLimit,
currency: fiatCurrency,
},
);
}
if (depositAmount > yearlyLimit) {
throw new LimitExceededError(
strings('deposit.buildQuote.limitExceeded', {
period: 'yearly',
remaining: `${yearlyLimit} ${fiatCurrency}`,
}),
{
period: 'yearly',
remaining: yearlyLimit,
currency: fiatCurrency,
},
);
}
} catch (error) {
if (error instanceof LimitExceededError) {
// Surfaced to the Headless Host as recoverable data, not a terminal
// failure - intentionally no RAMPS_ORDER_FAILED here.
throw error;
}
if (headlessSessionId) {
// Infra failure during the limits check: emit the HEADLESS failure
// event (TRAM-3623 §7) before failSession closes the session.
emitHeadlessOrderFailed(error, quote);
failSession(headlessSessionId, error);
throw error;
}
Logger.error(error as Error, 'Failed to check user limits');
}
},
[
getUserLimits,
fiatCurrency,
selectedPaymentMethod?.id,
headlessSessionId,
emitHeadlessOrderFailed,
],
);
const navigateToVerifyIdentityCallback = useCallback(
({ quote, amount }: { quote: TransakBuyQuote; amount?: number }) => {
const baseEntry = buildBaseRouteEntry({ amount });
navigation.reset({
index: 1,
routes: [
baseEntry,
{
name: Routes.RAMP.VERIFY_IDENTITY,
params: {
quote,
...(baseRouteParams ?? {}),
},
},
],
});
},
[navigation, buildBaseRouteEntry, baseRouteParams],
);
const navigateToBasicInfoCallback = useCallback(
({
quote,
previousFormData,
amount,
}: {
quote: TransakBuyQuote;
previousFormData?: BasicInfoFormData & AddressFormData;
amount?: number;
}) => {
const baseEntry = buildBaseRouteEntry({ amount });
navigation.reset({
index: 1,
routes: [
baseEntry,
{
name: Routes.RAMP.BASIC_INFO,
params: {
quote,
previousFormData,
...(baseRouteParams ?? {}),
},
},
],
});
},
[navigation, buildBaseRouteEntry, baseRouteParams],
);
const navigateToBankDetailsCallback = useCallback(
({
orderId,
shouldUpdate,
}: {
orderId: string;
shouldUpdate?: boolean;
}) => {
navigation.reset({
index: 0,
routes: [
{
name: Routes.RAMP.BANK_DETAILS,
params: { orderId, shouldUpdate },
},
],
});
},
[navigation],
);
const navigateToOrderProcessingCallback = useCallback(
({ orderId }: { orderId: string }) => {
// Headless mode: fire `onOrderCreated`, close the session, and pop
// out of the ramp stack so the caller regains foreground. The
// consumer drives post-order UI themselves — no RAMPS_ORDER_DETAILS.
const session = getSession(headlessSessionId);
if (headlessSessionId && session) {
try {
session.callbacks.onOrderCreated(orderId);
} catch (callbackError) {
Logger.error(
callbackError as Error,
'useTransakRouting: onOrderCreated callback threw',
);
}
closeSession(headlessSessionId, { reason: 'completed' });
dismissActiveHeadlessFlow();
return;
}
navigation.reset({
index: 0,
routes: [
{
name: Routes.RAMP.RAMPS_ORDER_DETAILS,
params: { orderId, showCloseButton: true },
},
],
});
},
[navigation, headlessSessionId, dismissActiveHeadlessFlow],
);
const navigateToAdditionalVerificationCallback = useCallback(
({
quote,
kycUrl,
workFlowRunId,
amount,
}: {
quote: TransakBuyQuote;
kycUrl: string;
workFlowRunId: string;
amount?: number;
}) => {
navigation.reset({
index: 1,
routes: [
buildBaseRouteEntry({ amount }),
{
name: Routes.RAMP.ADDITIONAL_VERIFICATION,
params: { quote, kycUrl, workFlowRunId, amount },
},
],
});
},
[navigation, buildBaseRouteEntry],
);
const handleNavigationStateChange = useCallback(
async ({ url }: { url: string }) => {
if (!url.startsWith(REDIRECTION_URL)) return;
let orderId: string | null = null;
try {
const urlObj = new URL(url);
orderId = urlObj.searchParams.get('orderId');
} catch (e) {
Logger.error(
e as Error,
'useTransakRouting: Error parsing redirect URL',
);
return;
}
if (!orderId) {
if (headlessSessionId) {
closeSession(headlessSessionId, { reason: 'user_dismissed' });
dismissActiveHeadlessFlow();
}
return;
}
if (processingOrderIdRef.current === orderId) {
return;
}
processingOrderIdRef.current = orderId;
const session = getSession(headlessSessionId);
if (headlessSessionId && session) {
// Headless mode: fetch the order, hand the orderId to the
// consumer, close the session, and unwind out of the ramp stack
// so the caller regains foreground. Skip RAMPS_ORDER_DETAILS —
// the consumer drives its own UI.
try {
const depositOrder = await getOrder(orderId, walletAddress || '');
if (!depositOrder) {
throw new Error('Missing order');
}
const providerCode = String(
depositOrder.provider ?? 'transak-native',
);
const rampsOrder = await refreshOrder(
providerCode,
depositOrder.providerOrderId,
walletAddress || depositOrder.walletAddress,
);
addOrder({
...rampsOrder,
paymentDetails: depositOrder.paymentDetails,
});
// Snapshot the headless context BEFORE closeSession tears it down
// (TRAM-3623 §4) so the terminal RAMPS_TRANSACTION_CONFIRMED carries
// ramp_type HEADLESS + the seeded ramp_surface.
const confirmSession = getSession(headlessSessionId);
const wasHeadless = Boolean(confirmSession);
const rampSurface = confirmSession?.params?.rampSurface;
// Carry the headless context (surface + region) to the controller-side
// terminal-failed subscriber (TRAM-3623 §1/§5). GUARD: this write MUST
// stay inside the headless gate (`if (wasHeadless)`); this hook also
// serves non-headless UB2-native, and an unconditional write at the
// `addOrder(...)` above would make those orders wrongly emit a HEADLESS
// RAMPS_TRANSACTION_FAILED. Keyed by providerOrderId (the same id the
// subscriber receives), normalized via extractOrderCode on both sides.
if (wasHeadless) {
setHeadlessOrderContext(rampsOrder.providerOrderId, {
rampSurface,
region: regionIsoCode,
});
}
try {
session.callbacks.onOrderCreated(rampsOrder.providerOrderId);
} catch (callbackError) {
Logger.error(
callbackError as Error,
'useTransakRouting: onOrderCreated callback threw',
);
}
closeSession(headlessSessionId, { reason: 'completed' });
// @ts-expect-error `pop` exists on the parent stack navigator at
// runtime but is not surfaced on the generic `NavigationProp`
// type returned by `getParent()`.
navigation.getParent()?.pop();
// TRAM-3691: a widget payment that came back terminal-failed must NOT
// report as confirmed/placed. Emit the terminal failure instead — the
// order is terminal so polling never observes it, and the headless
// context set above tags it HEADLESS.
if (
rampsOrder.status === RampsOrderStatus.Failed ||
rampsOrder.status === RampsOrderStatus.IdExpired
) {
emitTerminalOrderAnalyticsFromCallback(rampsOrder);
} else {
trackEvent('RAMPS_TRANSACTION_CONFIRMED', {
ramp_type: wasHeadless ? 'HEADLESS' : 'DEPOSIT',
ramp_surface: rampSurface,
provider_order_id: rampsOrder.providerOrderId,
amount_source: Number(rampsOrder.fiatAmount),
amount_destination: Number(rampsOrder.cryptoAmount),
exchange_rate: Number(rampsOrder.exchangeRate),
gas_fee: rampsOrder.networkFees
? Number(rampsOrder.networkFees)
: 0,
processing_fee: rampsOrder.partnerFees
? Number(rampsOrder.partnerFees)
: 0,
total_fee: Number(rampsOrder.totalFeesFiat),
payment_method_id: rampsOrder.paymentMethod?.id || '',
country: regionIsoCode,
region: regionIsoCode,
chain_id: rampsOrder.network?.chainId || '',
currency_destination: rampsOrder.cryptoCurrency?.assetId || '',
currency_destination_symbol:
rampsOrder.cryptoCurrency?.symbol || '',
currency_destination_network: rampsOrder.network?.name || '',
currency_source: rampsOrder.fiatCurrency?.symbol || '',
});
}
} catch (error) {
processingOrderIdRef.current = null;
Logger.error(error as Error, {
message:
'useTransakRouting: Failed to process order after checkout',
});
// Emit the HEADLESS failure event BEFORE failSession tears the
// session down (TRAM-3623 §7) so the surface snapshot is available.
// orderId (from the callback URL) is the provider order id (TRAM-3696).
emitHeadlessOrderFailed(error, undefined, orderId);
if (failSession(headlessSessionId, error)) {
// @ts-expect-error `pop` exists on the parent stack navigator at
// runtime but is not surfaced on the generic `NavigationProp`
// type returned by `getParent()`.
navigation.getParent()?.pop();
}
}
return;
}
// Same pattern as unified Buy WebView Checkout: leave the webview
// immediately; OrderDetails resolves the order via callback params.
if (!selectedProvider?.id) {
processingOrderIdRef.current = null;
Logger.error(
new Error('Missing selected provider'),
'useTransakRouting: cannot open OrderDetails without provider',
);
return;
}
const cryptoSymbol = selectedToken?.symbol;
navigation.reset({
index: 0,
routes: [
{
name: Routes.RAMP.RAMPS_ORDER_DETAILS,
params: {
callbackUrl: url,
providerCode: selectedProvider.id,
walletAddress: walletAddress || '',
showCloseButton: true,
...(cryptoSymbol ? { cryptocurrency: cryptoSymbol } : {}),
},
},
],
});
},
[
navigation,
walletAddress,
headlessSessionId,
getOrder,
addOrder,
refreshOrder,
regionIsoCode,
trackEvent,
selectedToken,
selectedProvider,
dismissActiveHeadlessFlow,
emitHeadlessOrderFailed,
],
);
const navigateToWebviewModalCallback = useCallback(
({ paymentUrl, amount }: { paymentUrl: string; amount?: number }) => {
const [routeName, routeParams] = createCheckoutNavDetails({
url: paymentUrl,
providerName: 'Transak',
onNavigationStateChange: handleNavigationStateChange,
headlessSessionId,
});
const baseEntry = buildBaseRouteEntry({ amount });
navigation.reset({
index: 1,
routes: [baseEntry, { name: routeName, params: routeParams }],
});
},
[
navigation,
handleNavigationStateChange,
buildBaseRouteEntry,
headlessSessionId,
],
);
const navigateToKycProcessingCallback = useCallback(
({ amount }: { amount?: number }) => {
const baseEntry = buildBaseRouteEntry({ amount });
navigation.reset({
index: 1,
routes: [
baseEntry,
{
name: Routes.RAMP.KYC_PROCESSING,
// Thread the headless session id so KycProcessing (no params of its
// own) can flip its KYC analytics to `ramp_type: 'HEADLESS'` (TRAM-3623).
params: headlessSessionId ? { headlessSessionId } : undefined,
},
],
});
},
[navigation, buildBaseRouteEntry, headlessSessionId],
);
const navigateToKycWebviewCallback = useCallback(
({
quote,
kycUrl,
workFlowRunId,
amount,
}: {
quote: TransakBuyQuote;
kycUrl: string;
workFlowRunId: string;
amount?: number;
}) => {
const [routeName, routeParams] = createKycWebviewNavDetails({
url: kycUrl,
providerName: 'Transak',
workFlowRunId,
quote,
amount,
});
navigation.reset({
index: 2,
routes: [
buildBaseRouteEntry({ amount }),
{
name: Routes.RAMP.KYC_PROCESSING,
// Thread the headless session id (TRAM-3623) - see the reset above.
params: headlessSessionId ? { headlessSessionId } : undefined,
},
{ name: routeName, params: routeParams },
],
});
},
[navigation, buildBaseRouteEntry, headlessSessionId],
);
const routeAfterAuthentication = useCallback(
async (quote: TransakBuyQuote, amount?: number, depth = 0) => {
try {
const userDetails = await getUserDetails();
const previousFormData = {
firstName: userDetails?.firstName || '',
lastName: userDetails?.lastName || '',
mobileNumber: userDetails?.mobileNumber || '',
dob: userDetails?.dob || '',
addressLine1: userDetails?.address?.addressLine1 || '',
addressLine2: userDetails?.address?.addressLine2 || '',
city: userDetails?.address?.city || '',
state: userDetails?.address?.state || '',
postCode: userDetails?.address?.postCode || '',
countryCode: userDetails?.address?.countryCode || '',
};
const requirements = await getKycRequirement(quote.quoteId);
if (!requirements) {
throw new Error('Missing KYC requirements');
}
switch (requirements.status) {
case 'APPROVED': {
try {
if (!userDetails) {
throw new Error('Missing user details');
}
await checkUserLimits(quote, requirements.kycType);
if (selectedPaymentMethod?.isManualBankTransfer) {
const depositOrder = await transakCreateOrder(
quote.quoteId,
walletAddress || '',
selectedPaymentMethod.id,
);
if (!depositOrder) {
throw new Error('Missing order');
}
const providerCode = String(
depositOrder.provider ?? 'transak-native',
);
const rampsOrder = await refreshOrder(
providerCode,
depositOrder.providerOrderId,
walletAddress || depositOrder.walletAddress,
);
addOrder({
...rampsOrder,
paymentDetails: depositOrder.paymentDetails,
});
// Manual-bank-transfer headless branch (TRAM-3623 §4): snapshot
// the surface before navigateToOrderProcessingCallback closes
// the session, then emit the confirmed event.
const manualBankSession = getSession(headlessSessionId);
if (manualBankSession) {
const rampSurface = manualBankSession.params?.rampSurface;
// Carry the headless context to the terminal-failed
// subscriber (TRAM-3623 §1/§5). GUARD: inside the
// `if (manualBankSession)` headless gate only - never at the
// unconditional `addOrder(...)` above - so non-headless
// UB2-native manual-bank orders never get an entry.
setHeadlessOrderContext(rampsOrder.providerOrderId, {
rampSurface,
region: regionIsoCode,
});
navigateToOrderProcessingCallback({
orderId: rampsOrder.providerOrderId,
});
trackEvent('RAMPS_TRANSACTION_CONFIRMED', {
ramp_type: 'HEADLESS',
ramp_surface: rampSurface,
provider_order_id: rampsOrder.providerOrderId,
amount_source: Number(rampsOrder.fiatAmount),
amount_destination: Number(rampsOrder.cryptoAmount),
exchange_rate: Number(rampsOrder.exchangeRate),
gas_fee: rampsOrder.networkFees
? Number(rampsOrder.networkFees)
: 0,
processing_fee: rampsOrder.partnerFees
? Number(rampsOrder.partnerFees)
: 0,
total_fee: Number(rampsOrder.totalFeesFiat),
payment_method_id: rampsOrder.paymentMethod?.id || '',
country: regionIsoCode,
region: regionIsoCode,
chain_id: rampsOrder.network?.chainId || '',
currency_destination:
rampsOrder.cryptoCurrency?.assetId || '',
currency_destination_symbol:
rampsOrder.cryptoCurrency?.symbol || '',
currency_destination_network:
rampsOrder.network?.name || '',
currency_source: rampsOrder.fiatCurrency?.symbol || '',
});
return true;
}
showV2OrderToast({
orderId: rampsOrder.providerOrderId,
cryptocurrency: rampsOrder.cryptoCurrency?.symbol ?? '',
cryptoAmount: rampsOrder.cryptoAmount,
status: rampsOrder.status,
});
navigateToBankDetailsCallback({
orderId: rampsOrder.providerOrderId,
shouldUpdate: false,
});
} else {
const ottResponse = await requestOtt();
if (!ottResponse) {
throw new Error('Failed to get OTT token');
}
const paymentUrl = generatePaymentWidgetUrl(
ottResponse.ott,
quote,
walletAddress || '',
generateThemeParameters(themeAppearance, colors),
);
if (!paymentUrl) {
throw new Error('Failed to generate payment URL');
}
navigateToWebviewModalCallback({
paymentUrl,
amount,
});
}
return true;
} catch (error) {
if (error instanceof LimitExceededError) {
throw error;
}
throw new Error(
parseUserFacingError(
error,
strings('deposit.buildQuote.unexpectedError'),
),
);
}
}
case 'NOT_SUBMITTED': {
// Snapshot the headless session BEFORE navigating so KYC_STARTED
// carries `ramp_type: 'HEADLESS'` + the seeded `ramp_surface`
// (TRAM-3623); the regular flow keeps `'DEPOSIT'`.
const kycStartedSession = getSession(headlessSessionId);
trackEvent('RAMPS_KYC_STARTED', {
ramp_type: kycStartedSession ? 'HEADLESS' : 'DEPOSIT',
ramp_surface: kycStartedSession?.params?.rampSurface,
kyc_type: requirements.kycType || '',
region: regionIsoCode,
});
navigateToBasicInfoCallback({ quote, previousFormData, amount });
return;
}
case 'ADDITIONAL_FORMS_REQUIRED': {
const additionalRequirements = await getAdditionalRequirements(
quote.quoteId,
);
const formsRequired = additionalRequirements?.formsRequired || [];
const purposeOfUsageForm = formsRequired.find(
(f) => f.type === 'PURPOSE_OF_USAGE',
);
if (purposeOfUsageForm) {
if (depth < 5) {
await submitPurposeOfUsageForm([
'Buying/selling crypto for investments',
]);
await routeAfterAuthentication(quote, amount, depth + 1);
} else {
Logger.error(
new Error(`Submit of purpose depth exceeded: ${depth}`),
);
}
return;
}
const idProofForm = formsRequired.find((f) => f.type === 'IDPROOF');
if (idProofForm) {
const { metadata } = idProofForm;
if (!metadata) {
throw new Error('Missing ID proof metadata');
}
// Same headless snapshot as the NOT_SUBMITTED branch above
// (TRAM-3623): HEADLESS + ramp_surface on the headless path.
const idProofKycSession = getSession(headlessSessionId);
trackEvent('RAMPS_KYC_STARTED', {
ramp_type: idProofKycSession ? 'HEADLESS' : 'DEPOSIT',
ramp_surface: idProofKycSession?.params?.rampSurface,
kyc_type: 'STANDARD',
region: regionIsoCode,
});
navigateToAdditionalVerificationCallback({
quote,
kycUrl: metadata.kycUrl,
workFlowRunId: metadata.workFlowRunId,
amount,
});
return;
}
navigateToKycProcessingCallback({ amount });
return;
}
case 'SUBMITTED': {
navigateToKycProcessingCallback({ amount });
return;
}
default:
throw new Error(strings('deposit.buildQuote.unexpectedError'));
}
} catch (error) {
if (isHttpUnauthorized(error)) {
await logoutFromProvider(false);
const hid = baseRouteParams?.headlessSessionId;
if (typeof hid === 'string' && hid.length > 0) {
// Match BasicInfo logout: OtpCode requires amount, currency, and
// assetId in params to re-fetch the Transak quote after re-auth.
// Without them it navigates to HEADLESS_HOST while the session is
// already `continued`, so the Host effect never re-runs (stuck loader).
const resolvedAmount =
amount != null
? String(amount)
: quote.fiatAmount != null
? String(quote.fiatAmount)
: undefined;
navigation.navigate(
...createV2EnterEmailNavDetails({
headlessSessionId: hid,
amount: resolvedAmount,
currency: quote.fiatCurrency || fiatCurrency || undefined,
assetId: selectedToken?.assetId,
}),
);
} else {
navigation.navigate(Routes.RAMP.ENTER_EMAIL);
}
return;
}
throw error;
}
},
[
navigation,
baseRouteParams,
fiatCurrency,
selectedToken?.assetId,
getKycRequirement,
getAdditionalRequirements,
getUserDetails,
regionIsoCode,
selectedPaymentMethod?.isManualBankTransfer,
selectedPaymentMethod?.id,
addOrder,
refreshOrder,
navigateToBankDetailsCallback,
navigateToWebviewModalCallback,
navigateToKycProcessingCallback,
navigateToOrderProcessingCallback,
headlessSessionId,
submitPurposeOfUsageForm,
logoutFromProvider,
navigateToBasicInfoCallback,
trackEvent,
navigateToAdditionalVerificationCallback,
transakCreateOrder,