Stripe Webhook 给我一个 308 重定向错误

Stripe Webhook is giving me a 308 redirecting error

提问人:sam 提问时间:11/14/2023 更新时间:11/14/2023 访问量:21

问:

enter image description here

我确实参考了条带文档堆栈溢出

但是在生产和开发中,我仍然遇到同样的问题。正如你所看到的,它用 308 重定向了我。

我当前的 webhook 端点格式如下所示:

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

该网站托管在 vercel 上,我正在使用 nextjs。我没有使用过 webhook 或 stripe。这对我来说都是新的,并且已经被困在这里很长时间了。

我也在使用文员 webhook。这就是为什么我使用一个名为 webhooks 的父目录,它有两个子目录和 ./clerk/route.ts/stripe/route.ts

这是我的api路由:/api/webhooks/stripe

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 });
}

提前致谢:)

条带支付 next.js13

评论

0赞 soma 11/14/2023
308 错误表示您的服务器正在尝试执行永久重定向,而 Stripe webhook 不支持此重定向。您需要确保您的 Webhook 端点 URL 没有重定向并返回 200 状态。
0赞 sam 11/14/2023
感谢您抽出宝贵时间回复。我该怎么做@soma。

答: 暂无答案