Repository navigation
fix(billing): start the first tracking run from the webhook too, once per purchase - #1772
Merged
Merged
Conversation
The first run after a payment only started when the Stripe return page loaded. A person who closed the tab before it did had the prompts saved in onboarding wait for the nightly run, up to a day. The return page and the checkout.session.completed webhook now share one helper (web/src/lib/billing/first-run.ts) that starts the first run for the organization's first brand, after the plan alignment, and only for an active or trialing subscription. The API keeps it to one run per checkout (server/src/lib/first-run.js): calls for a brand are taken one at a time, and one that finds a full run already waiting or running, or one started since the checkout began, gets that run's id back with created: false. The return page carries that id to the dashboard as before, and only the caller that started the run also starts the volume analysis. A failure in the webhook is logged and Stripe still gets its 200. Deploy the API before the web app: an older API starts a run for every call, so the two callers would start two. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ne run per purchase Review follow-ups to the first run after a payment: - A delayed payment (SEPA, for one) leaves the checkout's subscription incomplete, so nothing started the first run. The webhook now starts it on customer.subscription.updated when the status was incomplete, since the subscription's creation; never on renewals or other changes. - The volume analysis starts in the API, with the run it creates (routes/internal-trigger-tracking.js, moved out of server.js and tested), not from the web app, so a caller that gives up waiting still gets it. The analyze-volumes endpoint had no other caller and is gone. - The jobs read for an existing run go back to the purchase when it is older than 20 hours, so a return page opened again a day later finds the finished run instead of starting a second one. - The first brand is the first active one: a paused brand is skipped. - The job's flag is afterPayment, not firstRun. Co-Authored-By: Claude Opus 5.5 <[email protected]>
6 tasks done
Stripe doesn't order its events, so the subscription's incomplete to active update can come before the checkout's, whose alignment then hasn't run. Align here too before the first run starts, so it tracks only the plan's engines.
gkhngyk
approved these changes
Oct 29, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
Looks good with 5f0a273 (branch take-1772, on this PR's head ce7cf99).
What I changed
customer.subscription.updated(incomplete → active) now aligns the prompts to the plan before it starts the first run, unless the plan-change branch just did. Stripe doesn't order its events: this update can arrive beforecheckout.session.completed, whose alignment then hasn't run, and the worker takes each prompt'splatformsas stored (only models are filtered by plan at run time). Idempotent, like the other alignments.- Two webhook tests: same-plan update aligns before the API call; a plan-change update aligns once. The first fails without the fix.
Checked
- One run per purchase: both callers pick the same brand (oldest active). The API takes calls for a brand one at a time, finds a live full run or one completed since the purchase, and hands its id back with
created: false. Only the creating call starts the job and the volume analysis, so no second Cloro batch. This relies on the API being one process, which the in-memory job queue already assumes. - Stripe signature path: unchanged.
constructEventon the raw body, 400 on a missing or bad signature. The new calls never throw, have a 5 s timeout and return 200. - Plan limits: the run is started only for
active/trialing, and only after the organization row has the new status and plan (the run'sskipUnpaidreads it). With this fix, every caller aligns the prompts first. - Embeds: none added (
jobs,brandsread flat columns). - Read-only gate: no new web write route or action; the internal route keeps CRON_SECRET.
- Migrations: none.
Follow-ups (not blocking)
- A failed or cancelled first run gets restarted by reopening the return page. That's fine, and it's the same as before.
- As before, any checkout (a re-subscribe too) starts a run when none ran since the session began.
Deploy: API before web, as the PR says.
Tests: on the stack tip merged with main 3b7611c: web stripe + billing (with the new tests), subscription-guard coverage and embed-hints pass; tsc is clean. Server first-run + internal-trigger-tracking pass. Full server suite: 290/298 files pass. The 8 that fail are the missing framer-api package in the local install (5 files) and the known timing tests (content-agent, content-people, traffic-events). eslint/prettier are clean on the touched files.
5 of 6 tasks
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
The first tracking run after a purchase now starts even when the person never reaches the Stripe return page. It starts exactly once.
Before:
/api/stripe/successstarted the first run, but the webhook (checkout.session.completed) didn't. If the person closed the tab before the return page loaded, the subscription and the plan's engines were saved, but the prompts set up during onboarding waited for the nightly run, up to 24 h.incomplete→active) never started it.Now
web/src/lib/billing/first-run.tsis shared by the success route and the webhook. It:activeortrialing, after the prompts are aligned to the plan's engines;null, so Stripe always gets its 200.checkout.session.completedstarts it.customer.subscription.updatedstarts it when the previous status wasincompleteand the new one isactive.past_dueandincomplete_expireddon't.server/src/lib/first-run.js, behind/api/internal/trigger-tracking(moved toroutes/internal-trigger-tracking.js):created: falseinstead of opening another. The lower bound ismin(since, now − 20 h), so reopening the return page a day later doesn't open a second run.analyzeBrandVolumes(…, { onlyMissing: true })), so a slow response can't lose them. The web no longer calls/api/internal/analyze-volumes, which had no other caller and is removed.Deploy: the API before the web. The old API opens a run per call, so in between, the webhook and the success route would start two runs.
Merge note (demo brand chain #1734–#1740):
firstBrandIdinweb/src/lib/billing/first-run.tsmust add.eq('is_demo', false)oncebrands.is_demoexists.hideDemoBrandAfterPurchase.Related issue
None.
Type of change
fix— Bug fixHow to test
cd server && npx vitest run src/lib/first-run.test.js src/routes/internal-trigger-tracking.test.js.cd web && yarn vitest run src/lib/billing/ src/app/api/stripe/. These cover:incomplete→activeupdate starts it;For the maintainer (Stripe test mode): try a checkout and close the tab on Stripe's success redirect. The first run should still start, once.
Checklist
🤖 Generated with Claude Code