Skip to content

Server: resubscribing on an existing Stripe customer - #262

Merged
TheGlitching merged 1 commit into
mainfrom
fm/squiggle-checkout-fix
Nov 23, 2026
Merged

TheGlitching merged 1 commit into
mainfrom
fm/squiggle-checkout-fix

Conversation

@TheGlitching

Copy link
Copy Markdown
Owner

What was wrong

A reader who already has a Stripe customer (anyone who subscribed before) could not subscribe again: POST /web/account/checkout answered 502 billing_unavailable. createCheckoutSession sent customer_update=auto, a string where Stripe's Checkout Sessions API expects an object, and Stripe refused the request.

The fix

  • With an existing customer, the session now carries customer_update[address]=auto. Automatic tax needs an address on the customer, and Checkout saves the one it collects. No tax id and no shipping address are collected, so address is the only sub-field Stripe requires (name is only required with tax_id_collection).
  • A new reader is unchanged: customer_email, no customer and no customer_update. The teacher offer is unchanged too: it always opens Checkout without a customer, with the coupon and no promotion-code field.
  • Any failed Stripe call now logs stripe_request_failed with the endpoint (Stripe ids replaced by :id), the HTTP status, Stripe's error type, its code, and the name of the refused parameter in fields. Stripe's free-text message is deliberately not logged: it can echo the values we sent (an email address, for instance), and LogFields has no free-text field by design. Type, code and parameter name are enough to tell what was refused.

Tests

  • The fake Stripe in server/tests/helpers.ts now refuses a bare customer_update, and refuses an existing customer with automatic tax but no customer_update[address]=auto, as Stripe does. That is why the bug was never caught.
  • web-account.test.ts: the exact customer* fields sent for a new reader and for a former subscriber (failed with a 502 before the fix), and the log line of a Stripe refusal, which must not contain the message.
  • teacher.test.ts: the teacher checkout sends no customer and no customer_update.

npm test -w @squiggle/server (414 passed) and npm run typecheck -w @squiggle/server are clean.

Deploy

From the repository root (docs/DEPLOYING.md):

SQUIGGLE_WEB_ANALYTICS_TOKEN=… npm run deploy:production

No migration.

Checking it after the deploy

  1. In server/, run npx wrangler tail --env production --format pretty and leave it open.
  2. Signed in on the site with an account that has subscribed before and has no subscription now, start the subscription from /tarifs or /compte: Stripe Checkout opens instead of the "service de paiement" error. Complete it (a promotion code works) and /compte shows the subscription as active once the webhook arrives.
  3. The tail shows no stripe_request_failed line for that request. If one appears, its stripeType, code and fields say what Stripe refused.

A reader who had subscribed before, and so already has a Stripe customer,
could not subscribe again: POST /web/account/checkout answered 502
billing_unavailable. The Checkout session was sent with
`customer_update=auto`, a string where Stripe expects an object, and Stripe
refused the request. With automatic tax on an existing customer, Stripe
needs `customer_update[address]=auto` so that Checkout saves the address it
collects; no tax id or shipping address is collected, so `address` is the
only sub-field required. A new reader is still sent as `customer_email`,
with no `customer_update`, and the teacher offer is unchanged.

A refusal from Stripe was invisible in the logs. Any failed Stripe call now
logs `stripe_request_failed` with the endpoint (Stripe ids replaced), the
HTTP status, Stripe's error `type`, its `code` and the name of the
parameter it refused. Stripe's free-text `message` is still never logged,
since it can echo the values we sent.

The fake Stripe in the tests now refuses this request the way Stripe does,
which is how the bug had gone unnoticed.
@TheGlitching
TheGlitching merged commit 00b4473 into main Nov 23, 2026
1 check passed
@TheGlitching
TheGlitching deleted the fm/squiggle-checkout-fix branch November 23, 2026 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant