Files
CatoandClaude Opus 5 cdb78d2142 fix(billing): the first paid Cloud subscription can be silently lost (#109)
* fix(hooks): run eslint from the workspace that actually has it

lint-staged runs from the repo root, but eslint is a dependency of apps/web
only — pnpm does not hoist it — so `eslint --fix` exited ENOENT on every
staged apps/web TS/TSX file. lint-staged then killed the remaining tasks and
reverted the stash, so the pre-commit hook failed for anyone touching the web
app and the only way to commit was --no-verify.

Invoke it through the owning workspace instead.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(billing): merge company settings in Postgres, not read-modify-write

Stripe delivers checkout.session.completed and customer.subscription.updated
for a new subscription within milliseconds of each other, and Next.js serves
them as concurrent requests. Both handlers went through updateCompanyBilling,
which did SELECT settings -> spread the patch in JS -> UPDATE the whole JSONB
column. Two writers that read before either writes each persist their own
merge, and whoever commits last erases the other's fields.

On the conversion path that is the worst possible failure: the subscriber is
charged, checkout.session.completed writes plan "premium" plus the subscription
id, customer.subscription.updated overwrites it from a stale read, and the
paying customer is left on the free plan with no stripeSubscriptionId — so the
billing portal cannot find their subscription either.

Replace it with mergeCompanySettings(), a single UPDATE using Postgres
`jsonb ||` against the row's current value under the UPDATE's row lock.
Concurrent merges of disjoint keys both survive. `jsonb - key` preserves the
old removal semantics, where an explicit undefined dropped the key via
JSON.stringify.

The checkout action's stripeCustomerId write had the same shape and is moved
onto the helper too — it races the very webhooks it triggers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs(billing): document the three Stripe env vars in .env.example

apps/web reads STRIPE_SECRET_KEY, STRIPE_PRICE_ID and STRIPE_WEBHOOK_SECRET,
and none of the three appeared in .env.example or any doc in the repo. The
only way to learn that the hosted plan needs a price ID was to read
actions/billing.ts — which is exactly the step blocking the first paid Cloud
subscription.

Record all three with where to get the value and what breaks without it,
including the webhook event list the endpoint must subscribe to. Note that
self-hosted installs need none of them.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-20 21:10:29 -07:00
..
2026-08-29 10:31:23 +00:00