My AI assistant, the Pocket Operator, lives in Telegram, and my family and a few team workspaces already use it for free. Now it needs paying customers. Over three days I wired in Stripe Managed Payments, and the bot’s main file got seven marked insertions. None of them changes anything for anyone until one flag, BILLING_ENFORCE=1, is set. Today the billing route still answers 404, on purpose. No key, price id or customer appears on this page.
The assistant runs one sandboxed silo per person behind a shared Telegram bot, plus family bots and team workspaces. A billing gate is a door that says no, and if it is wired in wrong, the first deploy tells my own family their plan is paused. So the design starts from what Stripe must never be able to do. Stripe never talks to the bot. It talks to a Cloudflare Worker, which checks the signature, maps Stripe’s events to six neutral ones, and sends them on to POST /v1/billing on the gateway’s loopback listener. The ingest is idempotent on Stripe’s event id: every event id goes into a ledger, and a replay answers 200 with duplicate: true and changes nothing. So a webhook that is retried three times still grants one month, not three. Checkout and account links carry a signed uid.exp.mac token that is valid for 72 hours, and the Worker checks the same bytes (one shared test vector in both test suites). The plan file sits outside the user’s silo, in a ledger folder the model cannot see. Inside the silo, any user could ask their own operator to mark them as comped.
_ledger/billing-events.jsonl{"ok": true, "duplicate": true}, plan unchangedtest_replayed_event_200_no_change, test_bad_bearer_401 and test_unknown_user_404. Not a recorded run.
The bot’s main file already serves people every day, so billing stays out of it. The logic lives in a new module, billing_commands.py (268 lines). main.py gets seven insertions, each tagged # W1 billing: the billing route and its ingest, the admin commands, /plan and the “cancel my subscription” phrase, the may_talk gate, placed before the usage limits, the schedule skip for a paused silo, and the daily trial and payment cards. The whole change to main.py is 35 lines. One grep for the tag finds every insertion, so they are easy to review and easy to rip out. Then the switch. BILLING_ENFORCE is off unless it is set. While it is off, the gate, the cards and the schedule skip do nothing. The ingest, /plan, /comp and /refund work either way, so plans get recorded and beta users get comped before anyone can be stopped. The gate is one pure function, may_talk(plan, now), over the states trial, active, past due (with 7 days’ grace), paused and canceled, plus a comped override. A paused user gets one message a day with an account button and never reaches the model.
POST /v1/billing/comp, /plan <uid>, /refund/plan and the cancel phrase/v1/billing on the loopback listener
Merging dark gives bugs a place to show up while they still cost nothing. Four did. One: the gateway’s own small Stripe client, used only for refunds, pinned API version 2025-03-31.basil, while the Worker and the account run 2026-08-26.dahlia. That is one account with two shapes of the same object. Both now pin dahlia. Two: /refund printed every amount with a hard-coded “kr”, after the prices had moved to USD. A refund preview in the wrong currency is exactly the kind of line you approve without reading. It now prints the charge’s own currency. Three: the billing tests passed on their own branch. Then a parallel branch, bring-your-own-AI, made the bot refuse to answer for any silo that has neither its own AI sign-in nor a comp. The tests now stub that credential check as comped, so they hold after the merge, and the trial merge ran 416 passed, 1 skipped. Four came from a review the next day. The billing ingest’s bearer fell back to the connect route’s secret, so one leaked secret could forge billing events. The ingest now needs its own secret. Without one, the route answers 503, not 404, because the Worker retries on a 5xx while a 404 would end Stripe’s retries.
^/v1/connect$. The billing code already runs in the gateway release, and BILLING_ENFORCE is not in its env, so it is off.
Everything on this page is merged, and the code runs in production, switched off. The tunnel route is closed, the live prices and the live webhook endpoint do not exist yet, and BILLING_ENFORCE is unset. The sandbox proved the loop end to end in Stripe test mode on 2026-10-01. Still to do before the first live charge: accept the Managed Payments terms on the live account, name Stripe and Link as processors on the privacy page, comp every beta silo, then buy one plan with my own card and refund it. Family bots and team workspaces stay outside billing; the gate covers only personal silos on the shared bot.
/plan <uid> shows one user’s state. /comp gives a beta tester free time. Since the merge it also takes a silo name, because family bots and team workspaces only run on the shared key when they are comped. /refund only shows a preview, and acts when the same line is sent again with --yes. All of these work in the admin chat only. Below that: the states the gate decides between, and the hand-over that was written before go-live. Tap any image to enlarge it and read the exact prompt that drew it.




The safest time to find a billing bug is while billing cannot stop anyone. The code is deployed, the gate is off and the route is closed. The hand-over already says how to undo each step on its own: restore the tunnel config, set the flag back to 0 and restart, roll back the gateway release with the one line its deploy script prints, roll back the Worker, and disable the webhook while Stripe holds the events for three days. Go-live is now flipping a switch I already know how to flip back.
The seven tags in the diff, a replayed webhook answered with duplicate, a refund preview in the right currency on a test account, and the route that still says 404.
Take the pattern, not my product: a neutral billing ingest that is idempotent on the event id, a pure gate function, an enforcement flag that is off by default, and a hand-over with a rollback for every step. Everything in this episode is free and open — clone it, run it, make it yours.
may_talk(plan, now): one pure function, a named test for every state
cmd grep -n "# W1 billing": every seam in one list
template hand-over: deploy, live switch, rollback per step, merge note
assets a ledger of seen event ids: a replay answers 200 and changes nothing
# repo soon. the idempotency rule in one line: if ledger.seen(event_id): return 200, {"ok": True, "duplicate": True}
No GitHub? Comment DARK on the post and the bot DMs you the link.
Next: a wrapper that runs my jobs reported a timeout as “file lock, exit 1”, and I went hunting for a lock that did not exist. Now it exits 124, says TIMED OUT, and a test pins it.