Billing
One place to change billing
People changed billing in Stripe because it was the fastest place to do it, and every change left the contract and the books behind. I moved each of those changes onto the contract.
The business problem
An earlier piece of work made the signed contract decide what a client is charged. That only holds if nobody changes the charge somewhere else, and people did, for good reasons. A client asks for a discount. A one-off fee needs charging. A card fails and someone wants to try again. Stripe's dashboard was the quickest place to do each of those.
Stripe accepted the change and nothing else heard about it. The contract still showed the old deal, the books still expected the old amount, and at month end somebody had to work out by hand why the money didn't match.
Nobody was being careless. The right place to make a change was slower than the wrong one.
So I stopped thinking of it as a Stripe integration. It was four business problems, and each needed its own answer.
A change has to reach every record
If a change starts in Stripe, the contract finds out late or never. If it starts on the contract, the contract can carry it everywhere. So every billing change now starts on the contract's Billing tab.
- Cancel in one of three ways: immediately, after a final payment, or with a refund of the last payment.
- Pause billing, and resume it later.
- Each action is limited to organization admins and asks for a reason.
A price change is a new agreement
A discount typed into Stripe changes what a client pays without changing what they agreed to. I made a price change an amendment instead. The signed contract is copied into a new one, the client signs it, and only then does a background job reprice the Stripe subscription. The new price starts on the next billing date, with no partial-month math.
The amendment is not marked done when Stripe accepts the request. It is marked done when Stripe's next invoice shows the new amount.
A charge needs the customer's yes
A one-off fee or a project milestone goes to the customer by email for approval and is charged only after they agree. For a charge the client has already agreed to, staff can charge straight away. They choose which saved card or bank account to use, and that choice is pinned to the request so a later change to the client's default method can't redirect it.
Getting paid is part of billing
Chasing an overdue payment used to mean leaving the app. Now one email can cover several overdue payments, each with its own pay link. Reminders follow on days the sender chooses, and an internal alert goes out on day 28. An open invoice can be marked paid outside Stripe or waived, with undo where that is possible.
The changes already made in Stripe
People did not stop using the dashboard on day one, and years of earlier changes were already there. The app had to absorb them without pretending they were errors.
- An existing subscription can be attached to a contract, and its paid history comes with it.
- A payment or invoice created in the dashboard can be linked to its contract, and unlinked if it was linked to the wrong one.
- A discount or account credit applied in Stripe is shown and explained. A real price difference is still flagged.
- A Stripe object that belongs to no contract is ignored, so a subscription someone set up by hand can't overwrite a contract's records.
Stripe reports, a person decides
A nightly sync once moved a client to canceled because their card had failed, while they were still being invoiced another way. The software had made a business decision from a payment event.
I changed the rule. When Stripe stops billing, the contract raises a flag and a person decides what the contract's status should be. The subscription's history is kept, and Stripe billing can restart on a chosen date.
The technical approach
Four ideas carry most of it. The first is that every billing action is one function with the same shape: load the contract's billing record, refuse what makes no sense, call Stripe, write the log entry with who did it and why, then read the result back from Stripe so the app shows what Stripe actually holds.
export async function pauseBilling({ db, organizationId, contractId, behavior, reason, actor }: PauseInput) {
const { billing, stripe, account } = await loadBillingContext(db, organizationId, contractId);
if (billing.status === 'canceled') throw conflict('Billing is canceled and cannot be paused.');
if (isPaused(billing)) throw conflict('Billing is already paused.');
if (!billing.subscriptionId) throw conflict('There is no active subscription to pause.');
await stripe.subscriptions.update(
billing.subscriptionId,
{ pause_collection: { behavior } },
// Pausing can happen more than once. A fixed key would make Stripe replay
// the first pause for a day and turn every later one into a silent no-op,
// so the key is unique to this call and only covers its own retries.
{ stripeAccount: account, idempotencyKey: `pause-${billing.id}-${behavior}-${crypto.randomUUID()}` },
);
await recordChange(db, {
contractId,
action: 'billing_paused',
actor,
before: { paused: false },
after: { paused: true, behavior },
details: { reason },
});
// Read the result back from Stripe instead of assuming it.
return reconcileBilling(db, billing);
}The key on that Stripe call is the detail I got wrong first. A fixed key is right for an action that must happen once, such as applying an amendment. For an action a person can repeat, it quietly swallows the second attempt.
The second is to verify instead of assume. Applying an amendment writes the new amount to our record, and nothing would otherwise re-read it from Stripe. So after it applies, a separate step asks Stripe to preview the next invoice and checks three things: one line, the amended price, and a total that matches.
// Applied is not the same as billed.
if (items.length !== 1) {
return mismatch(`Stripe still carries ${items.length} line items; expected one.`);
}
if (items[0].priceId !== expected.priceId) {
return mismatch('The subscription is not on the amended price.');
}
let upcoming;
try {
upcoming = await stripe.invoices.createPreview({ subscription: subscription.id });
} catch (error) {
if (isNoUpcomingInvoice(error)) {
// Fine only if the contract was signed to end here.
return endsAsSigned(subscription, termEnd)
? confirmed()
: mismatch('Stripe has no upcoming invoice, so the amended amount will never bill.');
}
// A timeout or outage proves nothing either way: fail and retry.
throw error;
}
// One-time lines added by this amendment ride on the invoice once.
const recurring = upcoming.total - oneTimeLinesFor(upcoming, amendment.id);
return recurring === expected.amountCents
? confirmed()
: mismatch(`Stripe's next invoice totals ${recurring} but the contract bills ${expected.amountCents}.`);The third is that Stripe events are not trusted to belong to us. A connected Stripe account also holds subscriptions and invoices that people manage by hand. An event is matched to a billing record only by an id we put on the Stripe object or an id we stored, inside the same organization and account. Anything else changes nothing.
async function findBilling(db: Db, connection: Connection, object: StripeObject) {
// Our own id, stamped on the object when we created it...
const ours = object.metadata?.[BILLING_ID_KEY];
if (ours) return loadBillingById(db, connection.organizationId, ours);
// ...or a Stripe id an admin attached to a contract.
return loadBillingByStripeId(db, {
organizationId: connection.organizationId,
account: connection.account,
subscriptionId: subscriptionIdOf(object),
});
}
export async function onStripeEvent(db: Db, connection: Connection, event: StripeEvent) {
const billing = await findBilling(db, connection, event.data.object);
if (!billing) return; // not ours: a strict no-op
await reconcileBilling(db, billing);
}The flag for Stripe stopping billing does not come from this handler. A nightly job reads our own copy of the billing state, finds contracts still in force whose subscription was canceled or paused, and writes the flag to the contract's activity. It never changes the contract's status.
The fourth is the log. Every change to a contract or its billing is appended to one record that says who made it and where it came from: the web app, an AI agent, Slack, a background job or Stripe itself. Nothing in it is edited afterwards.
What I owned
I designed and built this, across eight pull requests between late August and late November 2026. About 171 of the 174 commits on the Stripe code, the billing routes and the Stripe webhooks are mine. A teammate built the original contract editor it sits on.
What it doesn't do
It does not lock anyone out of Stripe. The app notices dashboard changes and absorbs them, and it makes the contract the easier place to work, but a person can still open Stripe and change something. I also haven't measured how often that still happens, so I can't tell you the drift is gone. I can tell you where it now shows up, and who has to decide what to do about it.