I'm currently in the process of integrating Stripe into my SaaS platform, and I've encountered an issue with calculating the quota based on the payment period. Despite setting the plan as monthly, Stripe is returning a period from the 70s through the webhook, starting and ending on the same day.
// webhook
import Stripe from "stripe";
import { headers } from "next/headers";
import {
processWebhookInvoiceSuccess,
processWebhookSubscription,
stripe,
} from "@/services/stripe";
export async function POST(req: Request) {
const body = await req.text();
const signature = headers().get("Stripe-Signature") as string;
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_WEBHOOK_SECRET
);
} catch (error: any) {
console.error(`Webhook Error: ${error.message}`);
return new Response(`Webhook Error: ${error.message}`, { status: 400 });
}
switch (event.type) {
case "customer.subscription.created":
case "customer.subscription.updated":
await processWebhookSubscription(event.data.object);
break;
case "invoice.payment_succeeded":
await processWebhookInvoiceSuccess(event.data.object);
default:
console.log(`Unhandled event type ${event.type}`);
}
return Response.json({ ok: true }, { status: 200 });
}