Stripe
Configure Stripe for billing, subscriptions, and the customer billing portal.
Prerequisites
- Stripe CLI installed:
npm install -g stripe
Step 1: Set environment variables
STRIPE_SECRET_KEY=sk_live_xxxxxxxxxxxxxxxxxxxx
STRIPE_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxxxxxxxxxUse sk_test_ keys during development.
Step 2: Create products in Stripe
Nebutra's billing model maps to three Stripe products:
# Create products
stripe products create --name="Nebutra FREE" --description="Free tier"
stripe products create --name="Nebutra PRO" --description="$29/month"
stripe products create --name="Nebutra ENTERPRISE" --description="Custom pricing"
# Create prices
stripe prices create \
--product=prod_xxx \
--unit-amount=2900 \
--currency=usd \
--recurring[interval]=monthStep 3: Configure webhook endpoint
In your Stripe Dashboard: Developers → Webhooks → Add endpoint
- URL:
https://api.nebutra.com/api/webhooks/stripe - Events to listen for:
checkout.session.completedcustomer.subscription.updatedcustomer.subscription.deletedinvoice.paidinvoice.payment_failed
Test webhooks locally
stripe listen --forward-to localhost:3000/api/webhooks/stripeStep 4: Webhook handler
The Stripe webhook handler is pre-built in the monorepo:
import { handleStripeWebhook } from "@nebutra/billing/stripe";
export async function POST(req: Request) {
return handleStripeWebhook(req);
}Step 5: Billing portal
Customers can manage their subscription via the Stripe customer portal:
const session = await stripe.billingPortal.sessions.create({
customer: org.stripeCustomerId,
return_url: `${process.env.WEB_URL}/settings/billing`,
});
redirect(session.url);This is pre-wired in Settings → Billing → Manage Subscription.
Stripe test cards
| Card number | Scenario |
|---|---|
4242 4242 4242 4242 | Successful payment |
4000 0000 0000 9995 | Insufficient funds |
4000 0025 0000 3155 | 3D Secure authentication |
Use any future expiry date and any 3-digit CVC.
Related
How is this guide?
Edit on GitHub
Last updated on