Back to Articles

Automated Invoicing with Stripe Billing and Next.js Webhooks

How to build an automated invoicing SaaS using Stripe Billing for subscription management, Prisma for database operations, and Next.js webhooks for event handling.

Automated Invoicing with Stripe Billing and Next.js Webhooks

Manual invoicing wastes hours every month. InvoiceFlow automates the entire billing lifecycle using Stripe's subscription APIs.


1. Stripe Subscription Setup

We create subscription plans with metered billing using Stripe's API:

typescript
const subscription = await stripe.subscriptions.create({ customer: customerId, items: [{ price: priceId }], payment_behavior: 'default_incomplete', expand: ['latest_invoice.payment_intent'], });

2. Webhook Event Handling

A Next.js API route processes Stripe webhook events to update invoice states in our database:

typescript
export async function POST(req: Request) { const event = stripe.webhooks.constructEvent(body, sig, webhookSecret); switch (event.type) { case 'invoice.paid': await prisma.invoice.update({ where: { stripeId: event.data.object.id }, data: { status: 'PAID' } }); break; case 'invoice.payment_failed': await sendPaymentFailureEmail(event.data.object.customer_email); break; } }

3. PDF Generation

Paid invoices auto-generate branded PDF documents using Puppeteer, which are emailed to clients and stored in S3.


Summary

Stripe webhooks + Prisma + automated PDF generation creates an invoicing pipeline that runs itself, letting freelancers focus on billable work.