Hey developers! Let's talk about one of the most crucial aspects of integrating Stripe: test mode. Whether you're building a new payment integration or debugging an existing one, understanding test mode is essential for shipping with confidence.
Think of test mode as your payment integration's sandbox - a safe space where you can experiment without the fear of accidentally charging real cards or moving actual money. It's a complete simulation environment that mirrors Stripe's live environment but with zero financial risk.
When working with Stripe's API, every request happens in either test or live mode. Here's what you need to know about each:
// Test mode API key
const stripe = require('stripe')('sk_test_...');
// Live mode API key
const stripe = require('stripe')('sk_live_...');
Important note: The test mode toggle in Stripe's Dashboard doesn't affect your integration code - it's your API keys that determine whether you're in test or live mode.
The real power of test mode comes from simulating different payment scenarios. Here's how to test some common cases:
// Test card numbers for different scenarios
const testCards = {
success: '4242424242424242',
declined: '4000000000000002',
insufficientFunds: '4000000000000341'
};
// Immediate capture
const charge = await stripe.paymentIntents.create({
amount: 2000,
currency: 'usd',
payment_method: 'pm_card_visa',
confirm: true
});
// Authorization with delayed capture
const authIntent = await stripe.paymentIntents.create({
amount: 2000,
currency: 'usd',
capture_method: 'manual',
payment_method: 'pm_card_visa',
confirm: true
});
A more in depth list of test cases can be found here
When you're ready to go live:
Remember: test card numbers won't work in live mode, so don't accidentally leave them in your production code! Testing thoroughly in test mode can save you from many headaches down the line. Happy coding! 🚀