Repository navigation
Server: resubscribing on an existing Stripe customer - #262
Merged
Merged
Conversation
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.
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.
What was wrong
A reader who already has a Stripe customer (anyone who subscribed before) could not subscribe again:
POST /web/account/checkoutanswered502 billing_unavailable.createCheckoutSessionsentcustomer_update=auto, a string where Stripe's Checkout Sessions API expects an object, and Stripe refused the request.The fix
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, soaddressis the only sub-field Stripe requires (nameis only required withtax_id_collection).customer_email, nocustomerand nocustomer_update. The teacher offer is unchanged too: it always opens Checkout without a customer, with the coupon and no promotion-code field.stripe_request_failedwith the endpoint (Stripe ids replaced by:id), the HTTP status, Stripe's errortype, itscode, and the name of the refused parameter infields. Stripe's free-textmessageis deliberately not logged: it can echo the values we sent (an email address, for instance), andLogFieldshas no free-text field by design. Type, code and parameter name are enough to tell what was refused.Tests
server/tests/helpers.tsnow refuses a barecustomer_update, and refuses an existing customer with automatic tax but nocustomer_update[address]=auto, as Stripe does. That is why the bug was never caught.web-account.test.ts: the exactcustomer*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 nocustomerand nocustomer_update.npm test -w @squiggle/server(414 passed) andnpm run typecheck -w @squiggle/serverare clean.Deploy
From the repository root (
docs/DEPLOYING.md):No migration.
Checking it after the deploy
server/, runnpx wrangler tail --env production --format prettyand leave it open./tarifsor/compte: Stripe Checkout opens instead of the "service de paiement" error. Complete it (a promotion code works) and/compteshows the subscription as active once the webhook arrives.stripe_request_failedline for that request. If one appears, itsstripeType,codeandfieldssay what Stripe refused.