Skip to content
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

Merged
merged 5 commits into from
Jan 15, 2025

Conversation

martmull
Copy link
Contributor

@martmull martmull commented Jan 15, 2025

Fixes twentyhq/core-team-issues#191

  • remove automatic redirection on payment success page when subscription status is undefined
  • add an effect component to refresh the subscription status on payment success page

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

Copy link
Contributor

@greptile-apps greptile-apps bot left a 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 include RefreshSubscriptionStatusEffect, preventing premature navigation before subscription status is confirmed
  • Updated navigation logic in usePageChangeEffectNavigateLocation to treat PlanRequiredSuccess 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

Copy link
Member

@charlesBochet charlesBochet left a 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)) {
Copy link
Member

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)

Copy link
Member

@charlesBochet charlesBochet left a 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!

@martmull martmull force-pushed the fix-stripe-webhook-race-condition branch from 0ad1d9e to 769723e Compare January 15, 2025 14:53
return;
}

throw new Error(
Copy link
Member

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?

Copy link
Contributor Author

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);
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!

@charlesBochet charlesBochet merged commit c01e3af into main Jan 15, 2025
30 of 33 checks passed
@charlesBochet charlesBochet deleted the fix-stripe-webhook-race-condition branch January 15, 2025 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition Stripe signup
3 participants