fix(subscription): incomplete team subscription race condition - #2381
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
Greptile OverviewGreptile SummaryThis PR fixes a race condition in the team subscription flow by deferring organization creation until after Stripe payment succeeds, rather than creating it pre-emptively on the client. Key changes:
Impact: Prevents orphaned organizations when payments fail and ensures consistent subscription-to-organization linking. Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant Client as upgrade.ts
participant API as /api/organizations
participant Stripe
participant Webhook as auth.ts webhooks
participant OrgService as organization.ts
User->>Client: Upgrade to Team Plan
Client->>API: GET /api/organizations
API-->>Client: { organizations, isMemberOfAnyOrg }
alt Has existing org (owner/admin)
Client->>Client: Use existing org as referenceId
else Is member of another org
Client-->>User: Error: Leave org or ask admin
else No org membership
Client->>Client: Use userId as referenceId
end
Client->>Stripe: Create subscription checkout
Stripe-->>User: Payment page
User->>Stripe: Complete payment
Stripe->>Webhook: onSubscriptionComplete
Webhook->>OrgService: ensureOrganizationForTeamSubscription()
alt referenceId starts with "org_"
OrgService-->>Webhook: Return subscription unchanged
else userId as referenceId
OrgService->>OrgService: Check existing membership
alt User owns/admins org
OrgService->>OrgService: Update subscription referenceId
else User is non-admin member
OrgService-->>Webhook: Throw error
else No membership
OrgService->>OrgService: Create new organization
OrgService->>OrgService: Update subscription referenceId
end
end
Webhook->>Webhook: handleSubscriptionCreated()
Webhook->>Webhook: syncSubscriptionUsageLimits()
Webhook->>Webhook: sendPlanWelcomeEmail()
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Incomplete Team Subscription flow requires reference id to be updated with org AFTER team subscription is correctly made. Not pre-emptively making org.
Type of Change
Testing
Tested manually using Stripe CLI
Checklist