When inserting a new subscription cycle, read the actual subscription start/end period, not invoice period#3899
Merged
jurgenwerk merged 4 commits intomainfrom Feb 4, 2026
Merged
Conversation
514c7db to
c1dff08
Compare
Preview deployments |
lukemelia
approved these changes
Feb 2, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes subscription cycle period_start/period_end being derived from the invoice period (lagging behind) by instead using the subscription line-item period from Stripe invoice webhooks, and backfills existing incorrect rows.
Changes:
- Update
invoice.payment_succeededhandling to read subscription cycle periods from the invoice line item’speriod.start/end. - Update webhook/billing tests to include Stripe line-item fields (
type,proration,period) and assert corrected cycle periods. - Add a Postgres data-fix migration to update existing
subscription_cyclesperiods using stored Stripe event payloads.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/billing/stripe-webhook-handlers/payment-succeeded.ts | Reads subscription cycle period from invoice subscription line item and passes it into subscription/subscription-cycle creation. |
| packages/billing/stripe-webhook-handlers/index.ts | Extends invoice line typing to include type and proration. |
| packages/postgres/migrations/1769517089459_fix-subscription-cycle-dates.js | Data migration to rewrite incorrect subscription_cycles.period_start/end based on stored Stripe event line-item periods. |
| packages/realm-server/tests/server-endpoints/stripe-webhook-test.ts | Updates mocked Stripe webhook payloads to include required line-item fields for period extraction. |
| packages/realm-server/tests/billing-test.ts | Updates billing tests to assert that cycle periods come from line-item period, not invoice period_start/end. |
| packages/host/config/schema/1769517089459_schema.sql | Adds the latest generated SQLite schema snapshot corresponding to the newest migration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
packages/postgres/migrations/1769517089459_fix-subscription-cycle-dates.js
Outdated
Show resolved
Hide resolved
packages/postgres/migrations/1769517089459_fix-subscription-cycle-dates.js
Outdated
Show resolved
Hide resolved
Comment on lines
+39
to
+40
| line.period?.start && | ||
| line.period?.end, |
There was a problem hiding this comment.
The predicate line.period?.start && line.period?.end treats 0 as “missing” because it relies on truthiness. Use an explicit null/undefined check (e.g., != null) so valid numeric timestamps aren’t accidentally rejected.
Suggested change
| line.period?.start && | |
| line.period?.end, | |
| line.period?.start != null && | |
| line.period?.end != null, |
… start/end period, not invoice period
52171b3 to
8c5c7e0
Compare
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.
This PR fixes a bug where we process a webhook coming from Stripe when a new subscription is made, or when it renews.
At that time we create a new
subscription_cyclesrecord, which has fieldsperiod_start,period_end, which we read from the event's payload. The issue is that the source of where we are reading these values is wrong - we are reading the invoice period while we should be reading the subscription period which is included in the invoice's line item.So the result of this error is that the subscription cycle periods are lagging 1 month behind. This PR fixes this with a data fix migration and adjusted logic to read the periods from the correct attributes in stripe event's payload.