This template includes a complete Stripe integration for handling payments, subscriptions, and billing management.
The payments system is built on Stripe's API and provides a comprehensive solution for handling various aspects of payments and subscriptions. It includes server-side integration with Stripe's API and client-side components for payment collection and management.
The integration is designed to be developer-friendly while providing all the features needed for a production-ready payment system.
Built-in handlers for Stripe webhooks to process events such as payments, subscription updates, and invoice processing.
Tools for managing user subscriptions including pausing, canceling, and upgrading subscription plans.
Customizable banner alerts with CTAs for customers with errored payments or subscription issues.
Automatic payment retry system to recover failed payments and reduce churn.
Complete subscription lifecycle management from creation to cancellation.
The Stripe integration is implemented in the src/server/stripe
directory and includes:
To use Stripe webhooks in your development environment, you'll need to set up the Stripe CLI to forward events to your local server:
# Install the Stripe CLI
brew install stripe/stripe-cli/stripe
# Login to your Stripe account
stripe login
# Forward events to your local server
stripe listen --forward-to localhost:3000/api/webhooks/stripe
For production, you'll need to configure the webhook URL in your Stripe dashboard and set the webhook secret in your environment variables.
Here's an example of how to create a checkout session:
// Server-side API route to create a checkout session
import { stripe } from "@/server/stripe/client";
import { NextResponse } from "next/server";
export async function POST(req: Request) {
try {
const { priceId, customerId } = await req.json();
const session = await stripe.checkout.sessions.create({
customer: customerId,
line_items: [{ price: priceId, quantity: 1 }],
mode: "subscription",
success_url: `${process.env.NEXT_PUBLIC_APP_URL}/payment/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.NEXT_PUBLIC_APP_URL}/payment/canceled`,
});
return NextResponse.json({ url: session.url });
} catch (error) {
return NextResponse.json({ error: "Failed to create checkout session" }, { status: 500 });
}
}
Stripe settings can be configured in the .env
file. The following environment variables are required:
STRIPE_SECRET_KEY
: Your Stripe secret keySTRIPE_WEBHOOK_SECRET
: The signing secret for your Stripe webhookNEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
: Your Stripe publishable key