我正在将 Stripe 与我的 SaaS 集成,并使用有效的付款期限来计算配额,但 Stripe 总是返回从 70 年代开始的期限,即使我已将计划定义为每月,它也会从开始到结束webhook 并在 70 年代的同一天结束
// 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 });
}
谢谢你,我已经解决了。我稍微改变了方法,我不知道这是否是最正确的方法,但它有效
async function processWebhookInvoiceSuccess(obj: Stripe.Invoice) {
if (!user) throw new Error("invoice user can't find");
await prisma.user.update({
where: { id: user.id },
data: {
latestInvoiceStart: new Date(obj.lines.data[0].period.start * 1000),
latestInvoiceEnds: new Date(obj.lines.data[0].period.end * 1000),
},
});
}