@@ -7,6 +7,7 @@ import { useCookies } from '@/providers/CookiesProvider';
7
7
import { CheckoutResponse , PlanResponse } from '@/types/Billing' ;
8
8
9
9
import { useRouter } from './useRouter' ;
10
+ import { sleep } from '../utils/timeout' ;
10
11
11
12
declare global {
12
13
interface Window {
@@ -23,6 +24,9 @@ const getCookie = (name: string): string | null => {
23
24
return cookie ? decodeURIComponent ( cookie ) : null ;
24
25
} ;
25
26
27
+ const FLEEK_CHECKOUT_MAX_RETRIES = 3 ;
28
+ const FLEEK_CHECKOUT_RETRY_DELAY_MS = 1800 ;
29
+
26
30
export const useFleekCheckout = ( ) => {
27
31
const router = useRouter ( ) ;
28
32
const projectId = router . query . projectId ! ;
@@ -32,56 +36,63 @@ export const useFleekCheckout = () => {
32
36
} ) ;
33
37
34
38
const checkout = async ( ) => {
35
- try {
36
- const plan = await backendApi . fetch ( {
37
- url : '/api/v1/plans' ,
38
- } ) ;
39
-
40
- if ( ! plan . ok ) {
41
- throw plan . statusText ;
42
- }
43
-
44
- const PlansResponse : PlanResponse [ ] = await plan . json ( ) ;
45
-
46
- // always keep the plan name aligned with what's on stripe plan name
47
- const planId = PlansResponse . find (
48
- ( plan ) => plan . name . toUpperCase ( ) === 'PRO' ,
49
- ) ?. id ;
50
-
51
- if ( ! planId ) {
52
- throw new Error ( 'Plan not found' ) ;
53
- }
54
-
55
- const referralId =
56
- window . tolt_referral || getCookie ( 'tolt_referral' ) || '' ;
57
-
58
- console . log ( `[🤖 debug]: ${ referralId } ` ) ;
59
-
60
- const response = await backendApi . fetch ( {
61
- url : '/api/v1/subscriptions/checkout' ,
62
- method : 'POST' ,
63
- redirect : 'follow' ,
64
- body : JSON . stringify ( {
65
- projectId,
66
- planId,
67
- metadata : {
68
- referralId,
69
- } ,
70
- } ) ,
71
- } ) ;
72
-
73
- if ( ! response . ok ) {
74
- throw new Error (
75
- 'There was an error trying to upgrade your plan. Please try again.' ,
76
- ) ;
77
- }
78
-
79
- const result : CheckoutResponse = await response . json ( ) ;
80
-
81
- return result ;
82
- } catch ( error ) {
83
- throw error ;
84
- }
39
+ let attempts = 0 ;
40
+
41
+ while ( attempts < FLEEK_CHECKOUT_MAX_RETRIES ) {
42
+ try {
43
+ const plan = await backendApi . fetch ( {
44
+ url : '/api/v1/plans' ,
45
+ } ) ;
46
+
47
+ if ( ! plan . ok ) {
48
+ throw plan . statusText ;
49
+ }
50
+
51
+ const PlansResponse : PlanResponse [ ] = await plan . json ( ) ;
52
+ const planId = PlansResponse . find (
53
+ ( plan ) => plan . name . toUpperCase ( ) === 'PRO' ,
54
+ ) ?. id ;
55
+
56
+ if ( ! planId ) {
57
+ throw new Error ( 'Plan not found' ) ;
58
+ }
59
+
60
+ const referralId =
61
+ window . tolt_referral || getCookie ( 'tolt_referral' ) || '' ;
62
+
63
+ const response = await backendApi . fetch ( {
64
+ url : '/api/v1/subscriptions/checkout' ,
65
+ method : 'POST' ,
66
+ redirect : 'follow' ,
67
+ body : JSON . stringify ( {
68
+ projectId,
69
+ planId,
70
+ metadata : {
71
+ referralId,
72
+ } ,
73
+ } ) ,
74
+ } ) ;
75
+
76
+ if ( ! response . ok ) {
77
+ throw new Error (
78
+ 'There was an error trying to upgrade your plan. Please try again.' ,
79
+ ) ;
80
+ }
81
+
82
+ const result : CheckoutResponse = await response . json ( ) ;
83
+
84
+ return result ;
85
+ } catch ( error ) {
86
+ attempts ++ ;
87
+
88
+ if ( attempts >= FLEEK_CHECKOUT_MAX_RETRIES ) {
89
+ console . error ( `Checkout failed after ${ attempts } attempts:` , error ) ;
90
+ throw error ;
91
+ }
92
+
93
+ await sleep ( FLEEK_CHECKOUT_RETRY_DELAY_MS ) ;
94
+ }
95
+ }
85
96
} ;
86
97
87
98
return useMutation ( {
0 commit comments