Repository navigation
feat(billing): email people 3 days before their free trial ends - #2467
Merged
Merged
Conversation
Handle Stripe's customer.subscription.trial_will_end in the webhook and send Resend the `trial.ending` event for a branded trial reminder, like the welcome's `user.signed_up`. Cloud only; only for a subscription that is still trialing and will be charged (not set to cancel). To the Stripe customer's e-mail, else the organization's admins, with the workspace, plan, trial end, the first charge from Stripe's invoice preview (left out when unknown) and the Billing page's link. Once per trial without a migration: claimed on the subscription's metadata (trial_reminder_sent_at, the trial end, a claim id), read back, then sent. Accepted or unknown (5xx, timeout): kept. Refused (4xx): released and the webhook answers 500, so Stripe's retry sends it. PostHog gets trial_ending_notified with the plan and outcome. Help: the trial article says we email 3 days before the trial ends. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
A 429 still releases the claim and answers Stripe 500, so Stripe's retry sends it. Any other 4xx (a bad address, request or key) won't go through on a retry: the claim stays, marked trial_reminder_outcome: refused, and the webhook answers 200, so Stripe doesn't retry for 3 days. Clearing the trial_reminder_* metadata and resending the event sends it again. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
gkhngyk
approved these changes
Nov 17, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
Looks good: the claim/settle/read-back guard, the 429-only retry and the cancel checks all hold up. Merging once CI is green.
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
Nothing told people their free trial was ending. The webhook did not handle Stripe's
customer.subscription.trial_will_end, so the card was charged on day 15 without warning.This PR sends our own branded reminder 3 days before the trial ends. It works like the welcome email: the app sends Resend a custom event,
trial.ending, and a Resend Automation sends the email. It runs on Cloud only.Once the Automation is live, Stripe's own trial reminder can be turned off.
Changes
web/src/lib/billing/trial-ending.ts(new): handlestrial_will_end.trialing, the trial ends later than now, and a charge is coming. Nothing is sent whencancel_at_period_endis on, or whencancel_atis on or before the trial end.trial_ending_notifiedwithplan,outcome,recipient,recipient_count,has_amountandorganization_id. It is sent as the person who started the subscription, or else the first admin.[trial-ending]line with a reason code and the subscription id. Email addresses and names are never logged.web/src/app/api/stripe/webhook/route.ts: a newcustomer.subscription.trial_will_endcase. It answers 500 only when Resend rate limited the event (429), so Stripe retries.help/settings/trial-ai-credit.md) gets a "Before your trial ends" section with one sentence.approved-articles.jsonis regenerated, not hand-edited.web/.env.example: a note that the webhook also sendstrial.ending.en.jsonchange, no new server action.Exactly once
There is no table for this. The webhook does not dedupe event ids, so the send is recorded on the Stripe subscription's metadata:
trial_reminder_sent_atis set for this trial end, nothing is sent.trial_reminder_sent_at,trial_reminder_trial_endand a newtrial_reminder_claimid. After 1.5 s the claim is read back, and only the delivery whose claim id stayed sends. This stops two deliveries that arrive at the same moment from both sending.send-outcome.ts):trial_reminder_outcome: refused. The webhook answers 200, so Stripe doesn't retry for 3 days. One[trial-ending]line logs the status, and PostHog getsoutcome: refused.If some admins' sends are refused and others accepted, the reminder counts as sent. Writing the metadata makes Stripe send
customer.subscription.updated. That handler writes the same values again, so nothing changes.Ops steps
Stripe
/api/stripe/webhook.customer.subscription.trial_will_end.Resend
trial.ending.first_name(may be missing)workspace_nameplan_name(may be missing)trial_ends_at(ISO time, UTC)trial_ends_on(a readable date in UTC, e.g. "November 20, 2026")amount(formatted, e.g. "$249.00"; may be missing)currency(e.g. "USD"; may be missing)manage_url(the app's Billing page, where the plan is changed or canceled)first_nameand "your plan's price" foramount.manage_url, not the Stripe portal. Cancel lives in our app.RESEND_API_KEYis already set for the welcome email. Nothing new is needed.Sending a reminder again by hand
A reminder Resend refused for good (
trial_reminder_outcome: refusedon the subscription) is not retried. To send it again after fixing the cause (for example the customer's email):trial_reminder_sent_at,trial_reminder_trial_end,trial_reminder_claimandtrial_reminder_outcome.customer.subscription.trial_will_endevent under Developers › Events and click Resend.Stripe's own trial reminder
Example event payload
What the app posts to
https://api.resend.com/events/send:{ "event": "trial.ending", "email": "[email protected]", "payload": { "first_name": "Ada", "workspace_name": "Example Workspace", "plan_name": "Growth", "trial_ends_at": "2026-11-20T10:00:00.000Z", "trial_ends_on": "November 20, 2026", "amount": "$249.00", "currency": "USD", "manage_url": "https://app.ansvisor.com/dashboard/settings/billing" } }How to test (Stripe test mode)
NEXT_PUBLIC_IS_CLOUD=true,RESEND_API_KEY,NEXT_PUBLIC_APP_URLand the test-modeSTRIPE_*keys. For a local app, runstripe listen --forward-to localhost:3000/api/stripe/webhookand use the secret it prints asSTRIPE_WEBHOOK_SECRET.4242 4242 4242 4242.stripe subscriptions update sub_... -d trial_end=<unix time>. Stripe sendscustomer.subscription.trial_will_endwhen 3 days or less are left. You can also use a test clock on a test customer.[trial-ending] claimed: customer(1) sub=sub_..., then[trial-ending] sent: accepted sub=sub_...trial_reminder_sent_at,trial_reminder_trial_endandtrial_reminder_claim.trial.endingevent with the payload above, the Automation run and the email.trial_ending_notifiedwithoutcome: accepted.[trial-ending] resend_refused:<status> sub=sub_.... A 429 adds(released: Stripe sends the event again). Any other 4xx adds(claim kept, not retried: ...).[trial-ending] skip: already_sent sub=sub_...and no second email is sent.skip: cancel_at_period_endand no email is sent.stripe trigger customer.subscription.trial_will_endonly checks the wiring. Its subscription belongs to no workspace, so you should see a skip line such asskip: no_org.Choices made
first_name,plan_name,amountandcurrencycan be missing, so give them fallback values in the template.Checks
npx tsc --noEmit -p .: clean.src/app/api/stripe,src/lib/billing, signup-event, resend-event, posthog-server,src/lib/help,src/i18n,use-server-exportsandsubscription-guard-coverage.cancel_at_period_end, a cancel before the trial end, not trialing, trial over, self-hosted or no key.git merge-tree --write-tree HEAD upstream/main: clean.Generated by Claude Code