Skip to content

Commit de5bc42

Browse files
committed
chore: 🤖 on upgrade failure, retry up to 3 times
1 parent f63b56a commit de5bc42

File tree

3 files changed

+67
-50
lines changed

3 files changed

+67
-50
lines changed

.changeset/empty-buses-deny.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fleek-platform/dashboard": patch
3+
---
4+
5+
Retry on upgrade failure attempt up to 3 times

src/hooks/useFleekCheckout.ts

+61-50
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useCookies } from '@/providers/CookiesProvider';
77
import { CheckoutResponse, PlanResponse } from '@/types/Billing';
88

99
import { useRouter } from './useRouter';
10+
import { sleep } from '../utils/timeout';
1011

1112
declare global {
1213
interface Window {
@@ -23,6 +24,9 @@ const getCookie = (name: string): string | null => {
2324
return cookie ? decodeURIComponent(cookie) : null;
2425
};
2526

27+
const FLEEK_CHECKOUT_MAX_RETRIES = 3;
28+
const FLEEK_CHECKOUT_RETRY_DELAY_MS = 1800;
29+
2630
export const useFleekCheckout = () => {
2731
const router = useRouter();
2832
const projectId = router.query.projectId!;
@@ -32,56 +36,63 @@ export const useFleekCheckout = () => {
3236
});
3337

3438
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+
}
8596
};
8697

8798
return useMutation({

src/utils/timeout.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

0 commit comments

Comments
 (0)