Repository navigation
feat(billing): take payment through Stripe (reopens #352 against develop) - #355
Merged
Merged
Conversation
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]>
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.
Reopens #352 against
develop.#352 says MERGED and its code never reached
develop. It was stacked onartempartos/domain-verification, which had already merged intodevelopas#351 — so merging #352 put the Stripe work into a branch nothing pulls from.
app/services/billing/stripe_client.rbandapp/controllers/webhooks/stripe_controller.rbare absent from
developtoday. Same five commits, replayed onto currentdevelop; the three that were already upstream dropped out of the rebase bythemselves.
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::StripeClientBilling::Meter::StripeWeb::Company::BillingCheckoutsControllerWebhooks::StripeControllerTrialBannerReplays 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_duekeeps a customer running on purpose: a failed payment starts adunning 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 totrialing— the free allowance was spent once andcancelling does not return it.
Registration cannot open without a way to pay
REGISTRATION_ENABLED=trueon a hosted deployment with no Stripe key refusesto 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 inassets:precompile. It runs inafter_initializenow, and a test pins thedeferral.
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
activewithout a Stripecustomer (ours, and every legacy one) is counted as
unbilledrather than loggedas an error every hour.
Deployed alongside this
Staging is configured and waiting for this code:
/aixle/staging/app/STRIPE_SECRET_KEYandSTRIPE_WEBHOOK_SECRETare in SSM,the ExternalSecret is synced,
STRIPE_PRICE_IDis in the ConfigMap, andhttps://webhooks-staging.flow.aixle.com/webhooks/stripeanswers 404 todaypurely 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 therewould let a real customer appear to pay.
Checks
rubocop(2073 files),brakeman(0 warnings),tsc,eslint,steigerclean. The touched suites 138/138 on the rebased branch.
🤖 Generated with Claude Code