Repository navigation
fix(billing): keep Stripe subscriptions in sync with tenants - #1760
Merged
Merged
Conversation
- Activate the tenant when a subscription created `incomplete` is paid later (customer.subscription.updated, previous status incomplete). Re-fetch the subscription so late/out-of-order events act on its current state. Fixes #1740. - Only downgrade on customer.subscription.deleted when the deleted subscription is the tenant's stored one. - Reuse a subscription already on the Checkout customer instead of creating another: the idempotency key expires after 24h but webhooks are retried for 3 days and can be resent for 15. - Never create or activate a second live subscription for a tenant; log it for manual refund instead of orphaning the first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed checkout Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #1740.
Problem
After setup-mode Checkout, the webhook creates the subscription itself. If the first off-session charge is declined or needs 3-D Secure, the subscription comes back
incompleteand we stored nothing. If the customer later paid the open invoice, the subscription wentactiveand was billed every period, but the tenant stayed on Free and "Manage billing" couldn't find it. Stripe doesn't retry that first charge, so the old comment's assumption ("Stripe will keep retrying and eventually fire subscription.deleted") was wrong.The same review turned up other ways billing could get out of sync with tenants:
customer.subscription.deleteddowngraded the tenant for any subscription with itstenant_id, including a duplicate, while the real one was still being paid.session.ID) only lasts 24h, but Stripe retries webhooks for 3 days and the Dashboard can resend them for 15. A late redelivery created and charged a second subscription.Changes
customer.subscription.updatedwhenprevious_attributes.statusisincomplete. The handler re-fetches the subscription so late or out-of-order events act on its current state, then activates the tenant the same way checkout does.CancelStripeSubscriptiononly clears the stored subscription and downgrades when the deleted subscription matches it (Resultreports whether it did).CustomerCreation=always), so any subscription found there came from an earlier delivery of the same event.canceled,incomplete_expiredor missing at Stripe) never gets a second one. No new subscription is created (the card is saved but nothing is charged), and an already-charged one isn't activated. Both cases are logged at error level for a manual refund.Deploy
customer.subscription.updatedon the live Stripe webhook endpoint. Without it the [BUG] Stripe: a subscription createdincompleteis never activated when its first invoice is paid later #1740 fix never runs.Failed-payment settings are already compatible: retries leave the subscription
past_due, then cancel it. That firesdeleted, which downgrades the tenant.Testing
incompletesubscription's invoice firescustomer.subscription.updatedwith previous statusincomplete. Expiry toincomplete_expiredfires onlyupdated, neverdeleted.stripe listen, sandbox):app/handlers/webhooks/stripe_test.go(fake Stripe API, signed payloads) and a DB test for the cancel guard. The [BUG] Stripe: a subscription createdincompleteis never activated when its first invoice is paid later #1740 test fails onmain.🤖 Generated with Claude Code