Use it in Next.js
app/pricing/page.tsx
"use client";
import PricingSwitch, { type Plan } from "@/components/pricing-switch/PricingSwitch";
import { startSubscription } from "./actions";
const plans: Plan[] = [
{ id: "starter", name: "Starter", description: "For side projects", monthly: 9, yearly: 7, cta: "Start Starter", features: ["1 project", "Email support"] },
{ id: "pro", name: "Pro", description: "For a real business", monthly: 29, yearly: 23, cta: "Start Pro", features: ["10 projects", "Priority support"], featured: true },
];
export default function Pricing() {
return (
<PricingSwitch
plans={plans}
onSelect={async (plan, yearly) => {
const url = await startSubscription(plan.id, yearly);
window.location.assign(url);
}}
/>
);
}
app/pricing/actions.ts
"use server";
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
// one recurring Price per plan and period, made in the Stripe dashboard
const PRICES: Record<string, { monthly: string; yearly: string }> = {
starter: { monthly: "price_starter_m", yearly: "price_starter_y" },
pro: { monthly: "price_pro_m", yearly: "price_pro_y" },
};
export async function startSubscription(planId: string, yearly: boolean) {
const prices = PRICES[planId];
if (!prices) throw new Error("Unknown plan");
const session = await stripe.checkout.sessions.create({
mode: "subscription",
line_items: [{ price: yearly ? prices.yearly : prices.monthly, quantity: 1 }],
success_url: `${process.env.NEXT_PUBLIC_SITE_URL}/welcome?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.NEXT_PUBLIC_SITE_URL}/pricing`,
});
if (!session.url) throw new Error("Stripe returned no checkout URL");
return session.url;
}