Billing
A contract that tells Stripe what to charge
The agreement, the Stripe subscription and the QuickBooks records were kept in step by hand. Here is how the contract became the one that decides, and how a warning caught a repricing bug.
The problem
As I remember it, the agency kept three records of every deal by hand: the agreement, the Stripe subscription and the QuickBooks records. Unusual deals sometimes went through DocuSign, which left no structured agreement to bill from.
A cancellation or an upsell could reach one record and miss another. When I tried to match Stripe against QuickBooks, a more basic problem showed up. Some contracts had no subscription attached, or were missing their active status. There was no reliable starting point to reconcile from.
The decision
A teammate had built the first contract editor and signing page. I rebuilt the product catalog and built the billing system on top: Stripe collection, amendments, subscription migration, QuickBooks import, tax rules and reconciliation.
The rule underneath all of it is that the contract decides. It calculates what is owed from its services and their tax rules. Stripe collects that amount. Nothing in Stripe is allowed to be the place where the price lives.
The contract amount is the price.
One amount, calculated once
Services come from the catalog, which I import from QuickBooks along with a tax rule per service. A rule is a taxable share of the price. That is how the Texas rule for data processing services is expressed: 80% of the charge is taxable.
// Tax on one dollar of a service: the share of it that is taxable,
// multiplied by the local rate. An 80% share at 8.25% is 6.6 cents.
function taxPerDollar(taxableShare: number, localRate: number): number {
return roundToMicros(taxableShare * localRate);
}So a $150 service under that rule bills $159.90. The contract works this out and hands Stripe a single price for the billing period, tax included. Stripe does not get a line per service. The breakdown is the contract's job.
An amendment is a new contract
Terms change. The easy implementation edits the signed contract in place, and then nobody can say what the client actually signed or what they had been paying.
So an amendment is a successor agreement. It starts as a draft copy that points at the original. When it takes effect, the original is retired and points forward to its successor.
if (!original.signedAt) throw conflict('Only a signed contract can be amended.');
if (original.supersededBy) throw conflict('This contract was already amended.');
// The draft copies the terms and the services.
// It resets status, amount and the signing link.
// It never copies signatures, payments, sends or activity.
const amendment = await copyAsDraft(original);
// Later, once the amendment is signed and applied to billing:
await contracts.markSuperseded(original.id, { by: amendment.id });The Stripe subscription stays the same one, with the same card and billing date. It is repriced with no proration, so the cycle already being charged finishes as signed.
Showing the disagreement
Older clients already had Stripe subscriptions. I added a way to attach one to a contract and import its paid history. Attaching takes the subscription as it is, which means the contract and Stripe can disagree from the first day.
I built a warning for that. It prices the contract exactly the way checkout would, compares the result with what Stripe will charge, and says so on the contract.
const charged = nextCharge?.amountCents ?? null;
// Allow a cent of rounding between our maths and the processor's.
if (charged === null || Math.abs(charged - contract.amountCents) <= 1) {
return null; // a matching contract shows nothing
}
return <Warning>Stripe and this contract disagree on price</Warning>;The bug it caught
As I recall it, that comparison is what exposed a mismatch on older subscriptions with several line items. The contracts we create carry one Stripe line. A legacy subscription could carry several, plus a coupon. An amendment repriced the first line and left the rest billable beside it.
The fix follows from the rule. If the contract amount is the price, the subscription should have exactly one line at that amount.
const [keep, ...extras] = subscription.items;
await stripe.subscriptions.update(subscription.id, {
items: [
{ id: keep.id, price: contractPrice, quantity: 1 },
// Every other line goes. The contract is now the only source of price.
...extras.map((item) => ({ id: item.id, deleted: true })),
],
proration_behavior: 'none', // changes start next cycle, never mid-month
});Some shapes cannot be reproduced by one flat price: metered prices, lines on different intervals, a discount on a single line. Those are refused before anything is written, with a message to adjust the billing in Stripe.
Confirmed means verified
The update returning without an error is not proof. After applying an amendment, the job reads Stripe back. The amendment is marked confirmed only once Stripe's next invoice bills the amended amount. Otherwise it is marked failed and can be retried.
- The contract calculates the amount, including tax.
- Stripe collects one price and is compared against the contract.
- A change of terms is a new contract, so signed and paid history is never rewritten.
What I can't tell you from the record: whether any customer was actually mischarged by the multi-line bug. The record shows the mismatch and the fix. It does not establish the rest.