Payments
Stripe integration with checkout, webhooks, and customer portal.
NextSaas provides a complete Stripe integration, including a pricing plugin system, checkout sessions, webhook handling, and a customer portal.
Setup
-
Create Stripe Account: Sign up at Stripe.
-
Get API Keys: Retrieve your Publishable Key and Secret Key from the Stripe Dashboard.
-
Configure Environment:
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="pk_test_..." STRIPE_SECRET_KEY="sk_test_..."
Webhooks
Webhooks are crucial for listening to payment events (e.g., checkout.session.completed, invoice.paid).
-
Local Development: Use the Stripe CLI to forward events to your local instance.
stripe listen --forward-to localhost:3000/api/stripe/webhookCopy the Webhook Signing Secret (
whsec_...) from the CLI output to your.envfile:STRIPE_WEBHOOK_SECRET="whsec_..." -
Production: In your Stripe Dashboard, go to Developers > Webhooks, add an endpoint:
https://your-domain.com/api/stripe/webhook, and select events likecheckout.session.completedandcustomer.subscription.updated.
Pricing Plugins
NextSaas uses a pricing plugin system to manage different pricing models. Each plugin encapsulates its own UI, configuration, and translations.
Activating a Plugin
Set the active plugin via environment variable:
NEXT_PUBLIC_PRICING_PLUGIN="lifetime"Available Plugins
| Plugin | ID | Type | Description |
|---|---|---|---|
| Lifetime | lifetime | One-time | Pay once, own forever |
Product Configuration
For the built-in Lifetime plugin, create a Product and Price in Stripe, then configure:
STRIPE_LIFETIME_PRODUCT_ID="prod_..."
STRIPE_LIFETIME_PRICE_ID="price_..."Creating a New Plugin
To add a new pricing model (e.g., monthly subscription):
-
Create a new directory under
src/lib/pricing/plugins/{name}/with these files:config.ts— Plan pricing, features, and Stripe IDsi18n.ts— Translations forenandzh-CNcomponent.tsx— Server component for the pricing UIpricing-cta.tsx— Client component for checkout interactionindex.ts— Plugin export
-
Register the plugin in
src/lib/pricing/registry.ts. -
Add any new Stripe env vars to
src/env.ts. -
Set
NEXT_PUBLIC_PRICING_PLUGIN=your-pluginin.envto activate.
Checkout Flow
We provide a ready-to-use checkout API at src/app/api/stripe/checkout/route.ts.
To initiate a checkout session from the client:
const response = await fetch("/api/stripe/checkout", {
method: "POST",
body: JSON.stringify({
priceId: "price_...",
successUrl: window.location.origin + "/dashboard",
cancelUrl: window.location.origin + "/pricing",
}),
});
const { url } = await response.json();
window.location.href = url;