You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stripe Webhook触发308重定向错误求助(Next.js+Vercel)

Stripe Webhook 308重定向问题求助

错误截图

我已经参考过Stripe官方文档和Stack Overflow相关帖子,但无论生产还是开发环境,都被同一个问题卡住——请求被308重定向,如截图所示。

当前我的Webhook端点格式:

https://mydomain.vercel.app/api/webhooks/stripe/

我的站点部署在Vercel,使用Next.js框架,此前从未接触过Webhook和Stripe,已经卡了很久。

同时我还在用Clerk Webhook,所以创建了webhooks父目录,里面包含两个子目录:/clerk/route.ts和/stripe/route.ts。

以下是/api/webhooks/stripe的API路由代码:

import Stripe from "stripe";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { db } from "@/db";

export async function POST(req: Request) {
  const body = await req.text();
  const signature = headers().get("Stripe-Signature") as string;
  const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

  let event: Stripe.Event;

  try {
    event = stripe.webhooks.constructEvent(
      body,
      signature,
      process.env.STRIPE_WEBHOOK_SECRET!
    );
  } catch (error: any) {
    console.error(error);
    return new NextResponse(`Webhook Error: ${error.message}`, { status: 400 });
  }

  const session = event.data.object as Stripe.Checkout.Session;

  const subscription = await stripe.subscriptions.retrieve(
    session.subscription as string
  );
  if (event.type === "charge.succeeded") {
    console.log("====================================");
    console.log("Charge succeeded");
    console.log("====================================");
  }
  if (event.type === "checkout.session.completed") {
    const subscription = await stripe.subscriptions.retrieve(
      session.subscription as string
    );

    await db.user.update({
      where: {
        id: session.client_reference_id as string,
      },
      data: {
        credits: {
          increment: 5,
        },
      },
    });
  }

  if (event.type === "payment_intent.payment_failed") {
    console.log("====================================");
    console.log("Payment failed");
    console.log("====================================");
  }

  if (event.type === "payment_intent.succeeded") {
    console.log("====================================");
    console.log("Payment succeeded");
    console.log("====================================");
  }

  if (event.type === "payment_intent.created") {
    console.log("====================================");
    console.log("Payment created");
    console.log("====================================");
  }

  if (event.type === "customer.created") {
    console.log("====================================");
    console.log("Customer created");
    console.log("====================================");
  }

  if (event.type === "invoice.payment_succeeded") {
    const subscription = await stripe.subscriptions.retrieve(
      session.subscription as string
    );
  }

  return new NextResponse(null, { status: 200 });
}

提前感谢各位的帮助:)


内容的提问来源于stack exchange,提问作者sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:01:17