Guide for integrating Stripe payments into an existing project. Covers one-time payments, subscriptions, and advanced patterns with security best practices.
npx playbooks add skill eng0ai/eng0-template-skills --skill stripe-skill
This skill helps you integrate Stripe into an existing project. It provides decision trees, code patterns, and security checklists.
Ask the user these questions to determine the right pattern:
| Answer | Go to |
|--------|-------|
| One-time payment (buy a product/service once) | patterns/one-time-checkout.md |
| Subscription (recurring billing) | patterns/subscription.md |
| Usage-based billing (metered) | patterns/usage-based.md |
| Custom payment flow (full control) | patterns/payment-intent.md |
| Answer | Action |
|--------|--------|
| Yes (marketplace, platform fees) | Also read patterns/connect.md |
| No | Skip Connect |
| Stack | Additional Guide |
|-------|------------------|
| Next.js (App Router) | stacks/nextjs.md |
| Express.js | stacks/express.md |
| Serverless (Vercel/Netlify Functions) | stacks/serverless.md |
| Other | Use Express guide as reference |
Before writing any code, understand these non-negotiable rules:
// WRONG - Client-side (attackers can modify amount)
const paymentIntent = await stripe.paymentIntents.create({
amount: userProvidedAmount, // DANGEROUS!
currency: 'usd',
});
// CORRECT - Server-side with validated amount
app.post('/create-payment-intent', async (req, res) => {
const { productId } = req.body;
const product = await db.products.findById(productId);
const paymentIntent = await stripe.paymentIntents.create({
amount: product.priceInCents, // From YOUR database
currency: 'usd',
});
res.json({ clientSecret: paymentIntent.client_secret });
});
Stripe may send the same event multiple times. You MUST handle duplicates:
app.post('/webhook', async (req, res) => {
const event = stripe.webhooks.constructEvent(/*...*/);
// Check if already processed
const existing = await db.processedEvents.findById(event.id);
if (existing) {
return res.status(200).json({ received: true }); // Already handled
}
// Process the event
await handleEvent(event);
// Mark as processed
await db.processedEvents.create({ id: event.id, processedAt: new Date() });
res.status(200).json({ received: true });
});
Return 2xx immediately, process business logic asynchronously:
// WRONG - Synchronous processing (may timeout)
app.post('/webhook', async (req, res) => {
const event = verifyEvent(req);
await sendEmail(event); // Slow!
await updateDatabase(event); // Slow!
await notifyExternalService(event); // May fail!
res.status(200).send();
});
// CORRECT - Queue for async processing
app.post('/webhook', async (req, res) => {
const event = verifyEvent(req);
await queue.add('stripe-event', event); // Fast enqueue
res.status(200).json({ received: true }); // Return immediately
});
Always use cents (or smallest unit), never dollars:
// WRONG
amount: 19.99 // Floating point errors!
// CORRECT
amount: 1999 // $19.99 in cents
Never hardcode API keys:
# .env
STRIPE_SECRET_KEY=sk_test_xxx
STRIPE_PUBLISHABLE_KEY=pk_test_xxx
STRIPE_WEBHOOK_SECRET=whsec_xxx
// WRONG
const stripe = require('stripe')('sk_test_xxx');
// CORRECT
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
After implementing, verify these items:
4242 4242 4242 4242 (success)4000 0000 0000 0002 (decline)4000 0025 0000 3155 (requires 3D Secure)stripe listen --forward-to localhost:3000/webhookBefore switching to live mode:
| Card Number | Scenario |
|-------------|----------|
| 4242 4242 4242 4242 | Success |
| 4000 0000 0000 0002 | Decline |
| 4000 0025 0000 3155 | Requires 3D Secure |
| 4000 0000 0000 9995 | Insufficient funds |
| 4000 0000 0000 0069 | Expired card |
Any future expiry date and any 3-digit CVC will work in test mode.
essentials/webhook-handling.md - Deep dive into webhooksessentials/error-handling.md - Error categorization and recoveryessentials/idempotency.md - Preventing duplicate operationsessentials/security-checklist.md - Full security audit checklistWeekly roundup of top Claude Code skills, MCP servers, and AI coding tips.