Get a free 3-min fix video

Our tests were green. Real Stripe paid out anyway.

Every test passed. Then I pointed the same payout code at a real Stripe test account, and it paid a seller EUR 100 for an invoice the customer had not paid yet. Nobody had chosen to.

Nothing was wrong with the test suite. It did exactly what we built it to do. That turned out to be the problem.

How we got here

I built this test suite before coding agents got this good, around the Opus 4.5 days. Agents could write code, but they could not check their own work well. The tooling and skills were not as advanced as they are now. So I wanted a safety net in CI: unit tests, integration tests and end-to-end tests.

For Stripe, I first tried Stripe's own mock server, stripe-mock. It is stateless, and its README says: "Testing for specific responses and errors is currently not supported. It will return a success response instead of the desired error response." I needed the failures too. So I had AI build our own fake: many response types, the error cases, shapes copied from Stripe's API fixtures.

I did not want a real Stripe sandbox in CI. It shares state between runs, and I expected it to drift. So we went further: an in-memory database and an in-memory Stripe fake. The end-to-end tests ran with no outside services at all, and their run time dropped from about 10 minutes to about 7.

Fast, green, no flaky network calls. I trusted it.

The money flow

The platform is a marketplace. A customer pays an invoice for an order. Sellers do the work. When the work is approved and the money is in, the platform pays each seller through Stripe Connect.

Two rules must hold:

  1. 1No payout before the customer has paid, unless an admin chooses to pay early and sees a warning first.
  2. 2No payout from an order that is cancelled or closed.

The exception in rule 1 is real. Sometimes a seller needs the money fast, and the business is willing to front it. That is a business decision. A person makes it, on purpose, for one payout. If the platform pays early by default and the customer never pays, that money is gone.

Bug 1: the button was hidden, the server did not check

The code in this post is a simplified demo, not the client's code. The shape of the bug was this:

{invoice.status === "paid" && <ReleaseButton payout={payout} />}
export async function releasePayout(payout: Payout) {
  if (!(await balanceCovers(payout.amount))) {
    return { status: "deferred", reason: "Balance too low" } as const;
  }

  await stripe.transfers.create({
    amount: payout.amount,
    currency: payout.currency,
    destination: payout.sellerAccountId,
  });
  return { status: "released" } as const;
}

The screen checks the invoice. The server only checks the balance.

The admin screen only showed "Release payout" when the invoice was paid. That looked safe. But the server never checked the invoice itself. For some currencies, it used the computed invoice total as "money received". If the platform's Stripe balance could cover the transfer, the transfer went out.

Paying sellers from the pooled balance was on purpose, once the customer had paid. Card payments take days to settle, and sellers should not wait for that. Paying before the customer had paid was allowed too, but only as a choice an admin makes, after a warning. The code skipped the choice and the warning. The exception had become the default.

A stale browser tab could trigger it. So could a bulk release, or any automatic release. Nobody chose any of those payouts.

The UI is a suggestion. The server is the rule. And an exception is a choice, never a default.

Bug 2: one door locked, three still open

We had already fixed one hole: the manual release button refused closed orders. But money can leave a platform through more than one door. Ours had four:

  1. 1The manual release button
  2. 2Automatic release after the work is approved
  3. 3The invoice.paid webhook from Stripe
  4. 4A sweep that runs when Stripe makes balance available (balance.available)

Only the first door checked the lock. In the sandbox, a closed order paid out anyway, while the screen said its payouts were locked.

Count the doors before you lock one.

Why the tests missed it

The fake did not lie. Every response it gave was a response Stripe could give. The problem was the questions.

Our tests set up the happy path: a paid invoice, an open order. Nobody wrote "what if the invoice is still open?" or "what if the webhook arrives after the order is closed?" Each test checked one function on one path. No test checked a rule across all four doors.

When I finally went looking, I started from the rules instead of the functions. For each rule, I set up the state that should block a payout, an open invoice or a closed order, and ran the real payout code end to end against a real Stripe sandbox. Both rules broke.

Why a real sandbox, and not just better tests on the fake? Because then the result is not my fake's opinion. It is a real transfer on a real Stripe account, and nobody can argue with that.

A fake answers the questions you ask it. The bugs live in the questions nobody asked.

The fix, and the proof

Both rules moved into the payout release itself. Every door now goes through the same check: single release, bulk release, release after approval, the webhook and the sweep.

type Refusal = { status: "refused"; reason: string };

function canRelease(payout: Payout): Refusal | null {
  if (payout.order.status === "closed" || payout.order.status === "cancelled") {
    return { status: "refused", reason: "The order is closed" };
  }
  if (payout.invoice.status !== "paid") {
    return { status: "refused", reason: "The invoice must be paid" };
  }
  return null;
}

export async function releasePayout(payout: Payout) {
  const refusal = canRelease(payout);
  if (refusal) return refusal;
  ...
}

Every door calls releasePayout, so no door can skip the check. A refused release returns a clear reason, and the screen shows it.

For now, every path refuses a payout on an unpaid invoice. The deliberate option, an admin who chooses to pay early after a warning, is still to build. When it comes, it will be one explicit door, not a side effect.

Each fix started as a test that failed. Here is the real payout code against the Stripe sandbox, about 35 seconds per run:

Scenario A: invoice open

Before the fixBUG PRESENT

released, transfer 100.00 EUR

After the fixFIXED

deferred ("invoice must be paid"), no transfer

Scenario B: order closed

Before the fixBUG PRESENT

reconcile, transfer 100.00 EUR

After the fixFIXED

no transfer

The same run found two smaller bugs: a self-billed VAT invoice that was 1 cent off the transfer, and a currency split that could overpay by 1 cent. Both are fixed.

And this is what "test the rule, not the function" looks like. One test per rule, run on every door, against the sandbox:

const doors = [
  releaseManually,
  releaseInBulk,
  releaseOnApproval,
  handleInvoicePaid,
  sweepAvailableBalance,
];

test.each(doors.map((door) => [door.name, door] as const))(
  "%s never pays out on an unpaid invoice",
  async (_name, door) => {
    const order = await sandbox.createOrder({ invoice: "open" });

    await door(order);

    expect(await sandbox.transfersFor(order)).toHaveLength(0);
  },
);

If someone adds a sixth door next year, it goes in the list, and the rule is checked there too.

Even a sandbox is not production

On another project, we added Stripe quotes. Everything worked on our deployed test server. In production we got a generic error. The cause: our Stripe plan did not include quotes. We had to upgrade to Invoicing Plus.

A sandbox runs your code against real Stripe behavior. It does not run against your live account's plan and settings. So I think about three layers:

LayerCatchesMisses
FakeLogic, fast, in CIWhatever the tests never ask
Real sandboxHow the whole system behaves with real eventsLive account plans and settings
Production checksAccount and plan differencesNothing before it is live

What I do now

  • Write the money rules down, with their exceptions. Short sentences: "No payout before the customer has paid." For each exception, write who may make it and what they see first. Then test the rule on every path, not the function.
  • Keep the fake, and check it. The fake stays in CI because it is fast. I run the real sandbox locally to hunt bugs, and I record its real responses to keep the fake honest.
  • Fail closed. When a rule breaks, hold the money and alert. A late payout is an email from an annoyed seller. A wrong payout is often money you never see again.
  • A person approves money changes. Agents write most of the code I ship now, payout code included. A person reviews every change to how money moves before it merges.

If your platform pays people

If your platform collects from customers and pays sellers through Stripe Connect, your money has more doors than your tests know about. I run this same check as a fixed-price audit. You get:

  • Your money rules and their exceptions, written down in plain sentences, with you.
  • Each rule tested against every path that can move money, on a real Stripe sandbox. I never touch your live account.
  • A short report of what holds and what breaks, with a failing test for every break, so your team can rerun it after the fix.

All posts

Get a free 3‑minute fix video

Send me your website or main link and one thing breaking or taking hours every week. I look at your setup from the outside and email you a 3-minute screen recording within 2 business days.