Skip to content

feat(billing): take payment through Stripe (reopens #352 against develop) - #355

Merged
artempartos merged 5 commits into
developfrom
artempartos/stripe-billing-develop
Sep 29, 2026
Merged

artempartos merged 5 commits into
developfrom
artempartos/stripe-billing-develop

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Reopens #352 against develop.

#352 says MERGED and its code never reached develop. It was stacked on
artempartos/domain-verification, which had already merged into develop as
#351 — so merging #352 put the Stripe work into a branch nothing pulls from.
app/services/billing/stripe_client.rb and app/controllers/webhooks/stripe_controller.rb
are absent from develop today. Same five commits, replayed onto current
develop; the three that were already upstream dropped out of the rebase by
themselves.

Everything below is unchanged from #352.


Why

Capacity has been measured and recorded since the metering work. Nothing was ever
sent anywhere, and there was nowhere for a customer to pay: a workspace that spent
its free allowance stopped, and the only way out was a platform administrator
moving a column by hand.

What

Billing::StripeClient The one place this application talks to Stripe
Billing::Meter::Stripe Stops logging; sends one meter event per company per hour
Web::Company::BillingCheckoutsController Opens Stripe's own card page
Webhooks::StripeController Acts on what comes back
TrialBanner Offers Add a card where there is somewhere to pay

Replays cannot bill twice

The part that had to be proved against the real API rather than reasoned
about.
Each meter event carries an identifier built from the company and the
hour. Stripe answers a repeat with a 400 carrying neither a code nor a param —
the message is the only signal there is — and our delivery loop treats any raised
error as a failure and replays the hour from the ledger.

Left alone, the first partial failure would have replayed that hour on every run
until the six-hour window closed, failing every time and reporting a broken
provider. The duplicate is now read as recorded and counted separately on the
ledger row (sent=N:already=M:unbilled=K). If Stripe ever changes the wording,
the hour looks failed, is replayed, is refused again and is abandoned at the
window: noise, never a second bill.

Found by sending the same hour twice at the real test account. No mock would have
shown it.

The signature is the authentication

The webhook URL appears in Stripe's dashboard and in delivery logs, so it grants
nothing on its own. A body not signed with this endpoint's secret is refused
before it is read as anything, and an installation with no secret configured
accepts nothing at all. The tests sign real bodies and let the real adapter
verify them — the one path not driven through the fake.

past_due keeps a customer running on purpose: a failed payment starts a
dunning cycle that usually ends in payment, and stopping their work on the first
retry is the worse mistake. A company stopped by cancellation or non-payment goes
to blocked, not back to trialing — the free allowance was spent once and
cancelling does not return it.

Registration cannot open without a way to pay

REGISTRATION_ENABLED=true on a hosted deployment with no Stripe key refuses
to boot
. The two switches drift apart quietly and are found out by a customer:
people sign up, spend the allowance, and reach a stop with no card to add.

The first version of that guard named both predicates from an initializer, where
Zeitwerk has not made them yet — NameError, and the image build died in
assets:precompile. It runs in after_initialize now, and a test pins the
deferral.

Configured, or not at all

The Stripe customer is created lazily, on the first checkout rather than at
signup: signing up already waits on an email round trip, and a Stripe outage must
not be able to stop somebody registering.

With no credentials — every self-hosted installation, permanently — nothing calls
Stripe, the banner offers no button, the checkout endpoint refuses, and capacity
is still measured and recorded. A company that is active without a Stripe
customer (ours, and every legacy one) is counted as unbilled rather than logged
as an error every hour.

Deployed alongside this

Staging is configured and waiting for this code:
/aixle/staging/app/STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET are in SSM,
the ExternalSecret is synced, STRIPE_PRICE_ID is in the ConfigMap, and
https://webhooks-staging.flow.aixle.com/webhooks/stripe answers 404 today
purely because this endpoint does not exist in the deployed image
(palad-ai/aixle-infra#49).

Prod is deliberately untouched: the Stripe account is not activated
(charges_enabled: false), so there is no usable live key, and a test key there
would let a real customer appear to pay.

Checks

rubocop (2073 files), brakeman (0 warnings), tsc, eslint, steiger
clean. The touched suites 138/138 on the rebased branch.

🤖 Generated with Claude Code

artempartos and others added 5 commits September 29, 2026 12:54
Capacity has been measured and recorded since the metering work; nothing was
ever sent anywhere, and there was nowhere for a customer to pay. A workspace that
spent its free allowance stopped, and the only way out was a platform
administrator moving a column by hand.

Four parts. Billing::StripeClient is the one place this application talks to
Stripe, so the decision "is Stripe even configured here" is asked once rather
than at every call site, and the tests below it drive a fake of it rather than
the vendor's classes. Billing::Meter::Stripe stops logging and sends one meter
event per company per hour. A checkout controller opens Stripe's own card page.
A webhook receiver acts on what comes back.

REPLAYS CANNOT BILL TWICE, and this is the part that had to be proved against
the real API rather than reasoned about. Each event carries an identifier built
from the company and the hour. Stripe answers a repeat with a 400 carrying
neither a code nor a param — the message is the only signal there is — and our
delivery loop treats any raised error as a failure and replays the hour. Left
alone, the first partial failure would have replayed that hour every run until
the six-hour window closed, failing every time. The duplicate is now read as
"recorded" and counted separately on the ledger row. A message that changes makes
the hour look failed and be abandoned at the window: noise, never a second bill.

THE SIGNATURE IS THE AUTHENTICATION on the webhook. The URL appears in Stripe's
dashboard and in delivery logs, so a body that is not signed with this endpoint's
secret is refused before it is read as anything, and an installation with no
secret configured accepts nothing at all. `past_due` deliberately keeps a
customer running: a failed payment starts a dunning cycle that usually ends in
payment, and stopping their work on the first retry is the worse mistake. A
company stopped this way goes to `blocked` rather than back to `trialing` — the
free allowance was spent once and cancelling does not return it.

The Stripe customer is made lazily, on the first checkout rather than at signup:
signing up already waits on an email round trip, and a Stripe outage must not be
able to stop somebody registering. With no credentials configured — every
self-hosted installation, permanently — nothing calls Stripe, the banner offers
no button, and capacity is still measured and recorded.

Test-mode meter, product and metered price created and recorded in the runbook.
The webhook endpoint is not: it needs a public host, which is a deployment
decision.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The banner told a blocked workspace to talk to us while showing it an "Add a
card" button. Two different answers in one line.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The two switches drift apart quietly and are found out by a customer: open
signup on a hosted deployment with no Stripe key, and people sign up, spend the
free allowance, and reach a stop with no card to add and no button to press.
Nothing in the application would have said so until it happened to somebody.

So the deploy refuses instead. Beside the encryption keys in required_env,
because it is the same kind of misconfiguration: harmless at boot, expensive
later. In the initializer rather than in Deployment.self_serve_signup? on
purpose — a key that goes missing must fail loudly rather than quietly closing
the door on new customers and leaving the ones inside wondering.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
It named Deployment and Billing::StripeClient from an initializer, where
Zeitwerk has not made them yet — NameError, and the image build died in
assets:precompile. The guard meant to stop a bad deploy stopped every deploy.

Moved into after_initialize rather than rewritten against ENV: a second copy of
the rule in environment-variable terms is a second thing to keep in step with
Deployment.self_serve_signup?, and it would drift the first time either changes.

Verified both ways against a production boot with the image build variables: it
boots clean where registration is closed, and refuses where it is open with no
Stripe key. A test pins the deferral so nobody moves it back.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Rubocop caught it, mine did not run: I edited the file after the last full pass
and went straight to the next thing.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@artempartos
artempartos merged commit 48e4596 into develop Sep 29, 2026
7 checks passed
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