-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix(billing): reset usage on transition from free -> paid plan #1397
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
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.
Greptile Summary
This PR addresses a billing issue where users upgrading from free to paid plans would retain their accumulated usage statistics instead of starting fresh with their new subscription. The change implements a usage reset mechanism that triggers when users transition from free tier to paid plans (pro, team, or enterprise).
The implementation involves three key changes:
-
Exporting the usage reset function (
apps/sim/lib/billing/webhooks/invoices.ts): Makes the existingresetUsageForSubscriptionfunction exportable so it can be reused across the billing system. This function moves current period costs to last period costs and sets current usage to 0. -
Adding subscription creation handling (
apps/sim/lib/auth.ts): Integrates a call tohandleSubscriptionCreatedin theonSubscriptionCompletehook to trigger usage reset when subscriptions are created through the authentication flow. -
Creating the subscription webhook handler (
apps/sim/lib/billing/webhooks/subscription.ts): Implements the core logic that detects free-to-paid transitions by checking if users have other active subscriptions. When a user with no previous active subscriptions creates a paid plan subscription, their usage gets reset.
This change integrates with the existing usage monitoring system documented in sim/lib/usage-monitor.ts, which tracks user usage against subscription limits and provides the UsageData interface used throughout the application. The reset ensures that paying customers get the full value of their subscription limits from day one rather than being penalized for free tier usage.
Confidence score: 4/5
- This PR addresses a legitimate billing fairness issue with a well-structured solution
- Score reflects solid implementation using existing patterns and proper error handling
- Pay close attention to the subscription webhook handler logic and database query conditions
3 files reviewed, 1 comment
| status: string | ||
| }) { | ||
| try { | ||
| const otherActiveSubscriptions = await db |
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.
it shouldn't be possible to have multiple active subscriptions tbh -- is this the case rn if I a pro user is added to a team?
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.
or is this something to do with cancelling paid subscriptions?
# Conflicts: # apps/sim/lib/auth.ts
Summary
reset usage on transition from free -> paid plan, so if someone goes from free to pro plan, their usage restarts at 0, previously it would stay where it was
Type of Change
Testing
N/A
Checklist