FreeStack

Stripe Checkout

Complete Stripe checkout flow with subscription management, webhook handling, and customer portal integration. Includes pricing page component and usage-based billing support.

Next.jsPaymentsBuilt with Claude
2.8k
Stars
13.6k
Installs
2
Deps
2
Comments

Install / Copy

npx create-freestack-module stripe-checkout

Code Preview

index.tsx
import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST(request: Request) {
  const { priceId, customerId } = await request.json();

  const session = await stripe.checkout.sessions.create({
    customer: customerId,
    line_items: [{ price: priceId, quantity: 1 }],
    mode: "subscription",
    success_url: `${process.env.NEXT_PUBLIC_URL}/success?session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${process.env.NEXT_PUBLIC_URL}/pricing`,
  });

  return Response.json({ url: session.url });
}

export async function handleWebhook(event: Stripe.Event) {
  switch (event.type) {
    case "checkout.session.completed":
      await activateSubscription(event.data.object);
      break;
    case "invoice.payment_failed":
      await handleFailedPayment(event.data.object);
      break;
  }
}
SA
saasbuilder2 days ago

The webhook handler covers all the edge cases I always forget about. Saved me a weekend.

FI
fintech-dev5 days ago

Added usage-based metering on top of this. The billing portal integration is seamless.