Receber por email
← Voltar para o catálogo

nextjs-stripe-integration

Add Stripe payment processing to Next.js projects. Implement checkout sessions, payment handling, subscriptions, webhooks, and customer management. Use when adding Stripe to a Next.js project, building payment flows, implementing subscriptions, or integrating payment processing.

239estrelas
Atualizado há 6 meses

Ver no GitHub ↗ Licença: NOASSERTION

Como adicionar

/plugin marketplace add Microck/ordinary-claude-skills

O comando exato pode variar conforme o repositório. Confira o README no GitHub.

Para o autor da skill

Cole no README do seu repo

Mostra que sua skill está catalogada na Skillteca, gera backlink e tráfego rastreável.

[![Skillteca](https://www.skillteca.com.br/api/badge/nextjs-stripe-integration/svg)](https://www.skillteca.com.br/skills/nextjs-stripe-integration)

Alerta por categoria

Receba novas skills de Design e Frontend toda segunda

1 email curto, só com as skills novas de Design e Frontend. 4 minutos de leitura, sem spam, cancela com 1 clique.

Você confirma o email no primeiro envio. Sem spam. Cancela com 1 clique.

Ler descrição completa

Next.js + Stripe Integration

This Skill teaches Claude how to implement Stripe payment processing in Next.js projects, including one-time payments, subscriptions, webhooks, and customer management. Based on real-world implementation experience with modern Stripe APIs and authentication frameworks.

⚠️ CRITICAL: Breaking Changes in Modern Stripe.js

stripe.redirectToCheckout() is DEPRECATED and no longer works!

Modern Stripe implementations use the checkout session URL directly:

// ❌ OLD (BROKEN)
const { error } = await stripe.redirectToCheckout({ sessionId });

// ✅ NEW (CORRECT)
const session = await stripe.checkout.sessions.create({...});
window.location.href = session.url; // Use the URL directly!

Quick Start Checklist

When implementing Stripe in a Next.js project:

  1. Install dependencies: stripe and @stripe/stripe-js
  2. Configure environment: Add NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY to .env.local
  3. Access env vars correctly: Load inside functions, NOT at module level (critical for runtime)
  4. Create API routes: Build endpoints for checkout sessions, webhooks, and customer portal
  5. Build UI: Create checkout forms and payment pages
  6. Handle webhooks: Set up secure webhook handlers for payment events
  7. Update middleware: Add payment routes to unauthenticatedPaths if using auth middleware
  8. Test locally: Use Stripe CLI for webhook testing

Core Implementation Patterns

1. Environment Setup & Runtime Loading

# .env.local
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...

CRITICAL: Access environment variables inside API route functions, NOT at module initialization:

// ❌ WRONG - Fails at build/startup
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST() { ... }

// ✅ CORRECT - Variables loaded at runtime
export async function POST(request: NextRequest) {
  const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
  if (!stripeSecretKey) {
    return NextResponse.json({ error: 'API key not configured' }, { status: 500 });
  }
  const stripe = new Stripe(stripeSecretKey);
  // ... rest of function
}

Important: Only use NEXT_PUBLIC_ prefix for publishable keys. Secret keys stay server-side only.

2. One-Time Payments (Checkout) - Modern Approach

API Route (app/api/checkout/route.ts):

  • Load Stripe with secret key inside the function
  • Create a Stripe checkout session with mode: 'payment'
  • Return the full session URL (not just session ID)
  • Verify webhook signatures on payment success
// ✅ CORRECT: Load env vars inside function
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
const session = await stripe.checkout.sessions.create({...});
return NextResponse.json({ url: session.url }); // Return URL directly

Client Side (Simplified):

  • NO need to load Stripe.js for basic checkout
  • Call checkout API route
  • Redirect to session.url directly from response
  • Handle success/cancel redirects via query parameters

3. Subscriptions

Differences from one-time payments:

  • Create products in Stripe Dashboard with recurring pricing
  • Use mode: 'subscription' when creating checkout sessions
  • Manage customer subscriptions in database
  • Handle multiple lifecycle events via webhooks

Key workflow:

  1. Fetch available subscription tiers from Stripe API
  2. Display pricing page with subscription options
  3. Create checkout session with subscription mode
  4. Handle customer.subscription.created webhook
  5. Sync subscription status to your database

4. Webhook Handling

Critical security requirements:

  • Verify webhook signatures using Stripe's libraries
  • Use raw request body for signature validation (disable body parsing)
  • Handle these key events:
  • payment_intent.succeeded — one-time payment confirmed
  • customer.subscription.created — new subscription
  • customer.subscription.updated — subscription changes
  • customer.subscription.deleted — cancellation
  • invoice.payment_succeeded — renewal payment

Webhook endpoint (app/api/webhooks/stripe/route.ts):

  • Accept POST requests from Stripe
  • Verify signature: stripe.webhooks.constructEvent(body, signature, secret)
  • Process event and update database
  • Return 200 status to acknowledge

5. Authentication Middleware Configuration

When using WorkOS or similar auth frameworks, explicitly allow payment routes:

// middleware.ts
export default authkitMiddleware({
  eagerAuth: true,
  middlewareAuth: {
    enabled: true,
    unauthenticatedPaths: [
      '/',
      '/sign-in',
      '/sign-up',
      '/api/checkout',              // Allow unauthenticated checkout
      '/api/webhooks/stripe',       // Allow webhook delivery
      '/payment-success',
      '/payment-cancel',
    ],
  },
});

Why: Without this, auth middleware intercepts payment routes, causing CORS errors when the frontend tries to call them.

6. Customer Portal

Enable users to manage subscriptions without custom code:

  • Configure Customer Portal in Stripe Dashboard
  • Create API route that generates portal sessions
  • Redirect users to portal for managing subscriptions, payment methods, and invoices

Implementation Guide

Setup Phase

  1. Create Next.js project (or use existing)
  2. Install Stripe packages:

``bash npm install stripe @stripe/stripe-js ``

  1. Get API keys from Stripe Dashboard → Developers → API Keys
  2. Add keys to .env.local
  3. Add .env.local to .gitignore

Build Checkout Flow (One-Time Payments)

  1. Create app/api/checkout/route.ts:
  2. Load Stripe with secret key inside the function
  3. Accept POST with amount and metadata
  4. Create checkout session
  5. Return session.url directly (not just session ID)
  6. See [API_ROUTES.md](API_ROUTES.md) for complete code
  1. Create checkout page:
  2. Simple button component (no Stripe.js needed for basic flow)
  3. Call checkout API route on button click
  4. Redirect to response.url directly
  5. Handle success/cancel via query parameters
  1. Create success page:
  2. Accepts session_id query parameter
  3. Retrieves session details from Stripe (optional - for confirmation display)
  4. Displays confirmation message
  5. Can fetch order details from your database

Build Subscription Flow

  1. Create product in Stripe Dashboard (recurring pricing)
  2. Create app/api/subscriptions/list/route.ts:
  3. Fetch products and prices from Stripe API
  4. Return formatted subscription tiers
  1. Create app/api/checkout-subscription/route.ts:
  2. Similar to checkout flow but use mode: 'subscription'
  3. Link to price ID instead of amount
  1. Create subscriptions page:
  2. Fetch available tiers from API
  3. Display subscription cards with pricing
  4. Implement checkout on selection
  1. Create app/api/customer-portal/route.ts:
  2. Accept POST request
  3. Create portal session with customer ID
  4. Return portal URL

Webhook Integration

  1. Create app/api/webhooks/stripe/route.ts:
  2. Disable body parsing: export const config = { api: { bodyParser: false } }
  3. Extract raw body and signature from headers
  4. Verify: stripe.webhooks.constructEvent(body, signature, webhookSecret)
  5. Handle subscription and payment events
  6. Update database based on event type
  1. Test locally with Stripe CLI:

``bash stripe listen --forward-to localhost:3000/api/webhooks/stripe stripe trigger payment_intent.succeeded ``

  1. Deploy webhook endpoint to production
  2. Add webhook endpoint URL in Stripe Dashboard → Webhooks
  3. Use production secret key for production webhooks

Best Practices

  • PCI Compliance: Always load Stripe.js from Stripe's CDN, never bundle it
  • Singleton Pattern: Lazy-load Stripe.js only when needed (performance optimization)
  • **Environment Variabl
Compartilhar X LinkedIn