Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 38 ยท Testing and Fraud

Test Mode and Test Cards

Learn how Stripe test mode keeps real money safe, how test and live keys differ, which published test card numbers trigger success, declines and 3D Secure, and how to build a test matrix.

Payments code is the last place you want to learn by experiment on real customers. Stripe solves this with test mode: a complete copy of the API where nothing touches real cards, banks or money. Objects you create in test mode are separate from live objects, and the same code works in both. Only the keys change.

Keys and modes

Stripe gives you two pairs of keys:

Key Starts with Where it lives Purpose
Publishable (test) pk_test_ Browser, safe to expose Initialize Stripe.js
Secret (test) sk_test_ Server only Create charges, customers
Publishable (live) pk_live_ Browser Real payments
Secret (live) sk_live_ Server only, tightly guarded Real payments

Newer accounts can also create sandboxes, isolated test environments with their own keys, so a team can keep several experiments apart. The idea is the same: never mix test and live data.

Never commit a secret key to Git. Load keys from environment variables, use different values per environment, and use restricted keys with only the permissions a service needs. If a key leaks, roll it immediately.

Node.js Guard against using the wrong key

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Test card numbers

In test mode, real card numbers are rejected. Stripe publishes special numbers that behave in predictable ways. Use any future expiry date, any three-digit CVC and any postal code. A few well-known ones:

Number Behaviour
4242 4242 4242 4242 Succeeds (Visa)
4000 0000 0000 0002 Declined: generic decline
4000 0000 0000 9995 Declined: insufficient funds
4000 0025 0000 3155 Requires 3D Secure authentication
4000 0000 0000 0341 Attaching works, but later charges fail

Stripe's testing documentation lists many more (disputes, specific countries, other brands). Check it for the full current list rather than relying on memory. These numbers are public and are only valid in test mode.

All of them pass the Luhn check, the checksum every real card number satisfies. It catches typing mistakes before a bank is asked.

Node.js The Luhn check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Build a test matrix

A good integration is tested against more than the happy path. Write down each scenario, the card that triggers it, and what your app should do.

Node.js A scenario matrix as data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Other test tools

  • Test clocks simulate time so you can watch a subscription renew, fail and recover without waiting a month.
  • Test bank accounts and identity data exercise payouts and Connect onboarding.
  • The Dashboard shows test events, logs and failed requests; a toggle switches between test and live views.
  • Request logs show the exact parameters you sent, which is often the fastest way to debug an invalid_request_error.

Test mode is generous but not identical to live: real banks add timing quirks and unusual decline codes. Plan a small live smoke test with a real card of your own and refund it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key may appear in browser code?

    Publishable keys are designed for the browser. Secret keys must stay on the server.
  2. 2. What does the card 4000 0025 0000 3155 test?

    It requires authentication, so it exercises your requires_action handling.
  3. 3. What does the Luhn check do?

    It is a simple local checksum that catches most typing errors.
  4. 4. How do you test a subscription renewal without waiting a month?

    Test clocks advance simulated time for test-mode objects.

Summary

  • Test mode is a separate copy of Stripe; keys decide which one you use.
  • Use published test cards to trigger success, declines and 3D Secure.
  • Keep a written scenario matrix and test failures, not only success.
  • Test clocks, test bank data and the request log speed up debugging.

Next: Testing Webhooks Locally