How to Test Stripe Payments Before Real Money Moves

· 8 min read ·

A man in a green sweater taps a chunky toy cash register with a yellow tag on it, while a small white robot in a detective cap stands on a stool holding a clipboard checklist and ticking off boxes

Test Stripe in a sandbox, using its sk_test_ and pk_test_ keys and Stripe’s published test card numbers. No real money moves, no real card is touched, and you can make every kind of payment fail on purpose. Then work through the card cases in the table below, plus the flows no card can make for you: an abandoned checkout, a refund, and a subscription renewing. The one that matters most is the unhappy path: a payment that did not go through must never unlock the paid plan.

AI tools wire up the happy path fast, because it is the one they can see working. The bugs live in the other five.

Are you actually in test mode?

Stripe’s docs say every new account starts in a sandbox, and your keys decide what your code does, not the dashboard toggle: the Dashboard you are looking at “doesn’t affect your integration code”. The prefixes tell you which world a key belongs to. Per Stripe’s key docs, test keys start with pk_test_, rk_test_ and sk_test_, live keys with pk_live_, rk_live_ and sk_live_.

Two details that catch people:

  • Webhook secrets are separate from API keys. Stripe’s docs say to find them under each webhook endpoint in the Dashboard. A test-mode webhook secret in production, or the other way round, fails every signature check. Webhooks get their own post: why customers paid and got nothing.
  • Legacy test mode shares some settings with live mode. Stripe’s testing docs warn that changes in the old “test mode sandbox” might also affect live mode on pages that do not show a banner, and say to use a general sandbox for new integrations. Create a separate sandbox for development, and another for any automated tests.

Do not test with your own real card

It is tempting to “just buy your own product once”. Stripe’s docs say do not use real card details in live mode: the Stripe Services Agreement prohibits it. Use the test cards. If you run a real live-mode purchase at launch, that is a real sale of your product, not a test of your code. Know too that refunding a real payment does not give back the fee: Stripe’s processing fees from the original transaction aren’t returned.

The scenarios, and the card for each

Use any future expiry date and any three-digit CVC. These numbers come from Stripe’s testing page, and they are the only numbers that work in a sandbox.

ScenarioCardWhat to check in your app
Payment succeeds4242 4242 4242 4242The user gets the paid plan, once
Generic decline4000 0000 0000 0002The user sees a clear message and gets nothing
Insufficient funds4000 0000 0000 9995Same, with a “try another card” path
Always asks for 3D Secure4000 0027 6000 3184The extra authentication step appears and works
Asks for 3D Secure unless set up4000 0025 0000 3155Authenticates on the first payment, then not for later off-session charges once the card is saved
Card saved, then charges fail4000 0000 0000 0341Attaching works, the later charge fails, the user is told
Wrong CVC4000 0000 0000 0127Fails. Note Stripe skips the CVC check if you leave the CVC out, so type one

Run each one through your real checkout, all the way, and then look in two places: what the user sees, and what your database now says about that user. The second look is the one people skip.

For subscriptions, Stripe’s test clocks let you move time forward to see a renewal or a failed renewal without waiting a month. Run one renewal and one failed renewal before launch.

The bug to look for first: the success page grants the plan

The most common version of this bug is: after Checkout, Stripe sends the user to your success_url, and your code on that page marks the user as paid. It works every time you test it with the good card. It also marks someone as paid if they open that URL without paying, if their card was declined, or if they guess a session id.

Stripe’s own fulfillment guide describes the safe shape. Your fulfillment function should handle being called multiple times with the same Checkout Session, retrieve the session from the API, check its payment_status, and only then deliver. Two more checks belong in it. The session must be this user’s: create it with a client_reference_id and compare it, or whoever opens a paid session’s URL first gets the plan. And read payment_status the way Stripe’s Checkout Session reference defines it: paid means the funds are available (for a free trial, that the $0 trial invoice was processed), and no_payment_required means a setup-mode session or a billing cycle anchor with payment collected later, so this post’s check does not unlock the paid plan on it by itself. Stripe’s own fulfillment example tests payment_status !== 'unpaid' instead, which accepts no_payment_required too. That is a fair choice for a trial product, because your code then has to know the session is a trial or a setup. If you sell a free trial, decide it on purpose and add a test for it. The same page says automatic fulfillment needs webhooks if you accept payment methods that confirm later, because their state changes after Checkout has already finished. Cards usually confirm at once. Some bank payment methods do not.

Tests that say what the app does for a user

Here is a test file with eight tests. They do not know how your code is written. They ask what a user gets after each outcome, and what the app does when it starts with the wrong kind of key. Save it as pay.test.js and run node --test (Node 18 or newer).

const test = require("node:test");
const assert = require("node:assert");
const { makeApp } = require("./pay.js");
// A stand-in for Stripe's answers. Each test card from Stripe's testing page gives one of these outcomes.
const stripe = {
sessions: {
paid: { payment_status: "paid", client_reference_id: "u1" }, // 4242 4242 4242 4242, bought by user u1
bobs_paid:{ payment_status: "paid", client_reference_id: "bob" }, // a different user's paid session
setup: { payment_status: "no_payment_required", client_reference_id: "u1" }, // card saved, nothing charged
declined: { payment_status: "unpaid" }, // 4000 0000 0000 0002 (never completes)
abandoned:{ payment_status: "unpaid" }, // closed the tab
},
async retrieveSession(id) { return this.sessions[id]; },
};
const newApp = (env = { NODE_ENV: "development", STRIPE_SECRET_KEY: "sk_test_x" }) => makeApp({ stripe, env });
test("paying with a good card gives the user Pro", async () => {
const app = newApp();
const r = await app.successPage({ userId: "u1", sessionId: "paid" });
assert.equal(r.status, 200);
assert.equal(app.plans.get("u1"), "pro");
});
test("a declined card does not give Pro, even if the user opens the success URL", async () => {
const app = newApp();
await app.successPage({ userId: "u1", sessionId: "declined" });
assert.equal(app.plans.get("u1"), undefined);
});
test("opening the success URL for an abandoned checkout does not give Pro", async () => {
const app = newApp();
await app.successPage({ userId: "u1", sessionId: "abandoned" });
assert.equal(app.plans.get("u1"), undefined);
});
test("a made-up session id does not give Pro", async () => {
const app = newApp();
await app.successPage({ userId: "u1", sessionId: "cs_test_guess" });
assert.equal(app.plans.get("u1"), undefined);
});
test("one user cannot claim Pro with another user's paid session", async () => {
const app = newApp();
await app.successPage({ userId: "alice", sessionId: "bobs_paid" });
assert.equal(app.plans.get("alice"), undefined);
});
test("a session where nothing was charged does not give Pro", async () => {
const app = newApp();
await app.successPage({ userId: "u1", sessionId: "setup" });
assert.equal(app.plans.get("u1"), undefined);
});
test("production refuses to start with a test key", () => {
assert.throws(() => newApp({ NODE_ENV: "production", STRIPE_SECRET_KEY: "sk_test_x" }).start());
});
test("a developer machine refuses to start with a live key", () => {
assert.throws(() => newApp({ NODE_ENV: "development", STRIPE_SECRET_KEY: "sk_live_x" }).start());
});

The Stripe answers in the file are a stand-in, one per outcome, so you can run it with no Stripe account. Run in a scratch folder (Node 22) against the naive flow below, saved as pay.js, which grants Pro whenever the success page loads, it prints:

// NAIVE: grants the plan when the customer lands on the success page.
function makeApp({ stripe, env }) {
const plans = new Map(); // userId -> plan
return {
plans,
start() { /* no check on which key is loaded */ },
async successPage({ userId, sessionId }) {
plans.set(userId, "pro");
return { status: 200, body: "Welcome to Pro!" };
},
};
}
module.exports = { makeApp };

It prints:

ok 1 - paying with a good card gives the user Pro
not ok 2 - a declined card does not give Pro, even if the user opens the success URL
not ok 3 - opening the success URL for an abandoned checkout does not give Pro
not ok 4 - a made-up session id does not give Pro
not ok 5 - one user cannot claim Pro with another user's paid session
not ok 6 - a session where nothing was charged does not give Pro
not ok 7 - production refuses to start with a test key
not ok 8 - a developer machine refuses to start with a live key
# pass 1
# fail 7

Test 1 passing is the whole story. The happy path works, so nothing looks wrong until someone is not happy. Tests 5 and 6 matter most for money: a paid session belonging to someone else, and a session where nothing was charged. Here is the version that follows Stripe’s guide, saved as pay.js:

// FIXED: asks Stripe whether the session is paid, grants once, and refuses mismatched keys.
function makeApp({ stripe, env }) {
const plans = new Map();
const granted = new Set();
return {
plans,
start() {
const key = env.STRIPE_SECRET_KEY || "";
if (env.NODE_ENV === "production" && !/^(sk|rk)_live_/.test(key)) throw new Error("production needs a live Stripe key");
if (env.NODE_ENV !== "production" && /^(sk|rk)_live_/.test(key)) throw new Error("refusing to run a live Stripe key outside production");
},
async successPage({ userId, sessionId }) {
const s = await stripe.retrieveSession(sessionId);
if (!s || s.payment_status !== "paid") return { status: 402, body: "Payment not completed" };
if (s.client_reference_id !== userId) return { status: 403, body: "This payment belongs to someone else" };
if (!granted.has(sessionId)) { granted.add(sessionId); plans.set(userId, "pro"); }
return { status: 200, body: "Welcome to Pro!" };
},
};
}
module.exports = { makeApp };

With that, the same file prints # pass 8, # fail 0.

Do not paste that pay.js into your app. Ask your AI tool to add an adapter so the same eight tests run against your real success-page code, with a fake Stripe client in place of the real one. When a later change breaks one of these behaviors, the test fails, which is the point. The tests only protect what they cover: they do not check webhooks, subscriptions or 3D Secure, and they do not replace one real pass through Stripe’s sandbox with the card numbers above. The cards were not run through a live sandbox for this post, so the table is from Stripe’s docs, read on 6 December 2026. Stripe’s guide says to use webhooks for payment methods that confirm later, and a user can pay and then close the tab before ever reaching your success page. So treat this success-page path as one of two triggers, not the only one. Webhooks are the next post: why customers paid and got nothing.

A prompt to paste into your AI tool

Show me every place in this app where a user gets access to something paid. For each, tell me what event triggers it (a redirect to a success page, a webhook, or something else), whether it asks Stripe for the payment status first, whether it checks that the session belongs to the signed-in user, whether running it twice gives the user two of the thing, and which Stripe keys the app loads in development and in production. Do not change any code yet.

The check an engineer does by hand

Start a checkout with the declined card (4000 0000 0000 0002). When it fails, type your own success URL into the address bar (if your URL includes the session id, use the one from the failed attempt). If the account now says paid, you found it. Then run a second check: buy something as account A, copy the success URL, sign in as account B in another browser, and open it. B must not get the plan.

Payments are on the launch checklist and among the things that break first with real users, and it pairs with keeping secret keys out of the browser.

The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.

Comments