-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix race condition on stripe subscription #9629
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR addresses a race condition in the Stripe subscription handling process by implementing better synchronization between frontend and backend states.
- Added new
RefreshSubscriptionStatusEffect
component in/modules/billing/components/RefreshSubscriptionStatusEffect.tsx
that polls subscription status up to 5 times with 1-second intervals - Modified
PaymentSuccess
page to includeRefreshSubscriptionStatusEffect
, preventing premature navigation before subscription status is confirmed - Updated navigation logic in
usePageChangeEffectNavigateLocation
to treatPlanRequiredSuccess
as an open signed-in route - Adjusted test cases to reflect new navigation behavior, particularly for
PlanRequiredSuccess
states
4 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile
packages/twenty-front/src/modules/billing/components/RefreshSubscriptionStatusEffect.tsx
Outdated
Show resolved
Hide resolved
packages/twenty-front/src/modules/billing/components/RefreshSubscriptionStatusEffect.tsx
Outdated
Show resolved
Hide resolved
packages/twenty-front/src/modules/billing/components/RefreshSubscriptionStatusEffect.tsx
Outdated
Show resolved
Hide resolved
packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts
Outdated
Show resolved
Hide resolved
packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts
Outdated
Show resolved
Hide resolved
packages/twenty-front/src/modules/billing/components/RefreshSubscriptionStatusEffect.tsx
Outdated
Show resolved
Hide resolved
packages/twenty-front/src/modules/billing/components/RefreshSubscriptionStatusEffect.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left comments! I think we can merge the change on PaymentSuccess and on RefreshSubStatusEffect.
Questioning the change in usePageChangeEffect: I don't get how it works + will be overridden by @etiennejouan change
if (!isDefined(subscriptionStatus)) { | ||
const result = await getCurrentUser({ fetchPolicy: 'network-only' }); | ||
const currentUser = result.data?.currentUser; | ||
if (isDefined(currentUser)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's make a deep equal here (make it a useRecoilCallback and check the currentUser in the snapshot, this will avoid unnecessary re-renders)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a final comment :) LGTM on the rest, much better indeed!
0ad1d9e
to
769723e
Compare
return; | ||
} | ||
|
||
throw new Error( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we use a Snackbar here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but we also want the error to be send to sentry, so I throw an error (in add snackBar and send error to sentry)
if (currentUser?.onboardingStatus === OnboardingStatus.Completed) { | ||
const navigateWithSubscriptionCheck = async () => { | ||
if (isDefined(subscriptionStatus)) { | ||
navigate(AppPath.CreateWorkspace); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get this, won't it always redirect here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, if we receive the stripe subscription create event after the stripe checkout redirection, subscriptionStatus will be undefined.
In that case, we refresh subscriptionStatus, redirect if not undefined, and throw error if undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok!
Fixes twentyhq/core-team-issues#191
Observation: Locally, I had to delay the stripe webhook subscription created endpoint by 7s to see race condition issue
Enregistrement.de.l.ecran.2025-01-15.a.15.56.49.mov