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

Next.js 14集成Stripe Webhook遇404错误求助

Next.js 14.0.3 搭建Stripe Webhook遇404错误求助

在Next.js 14.0.3中搭建Stripe Webhook以实现支付功能,已编写stripe-webhook接口代码并确认文件路径无误,但测试时请求地址http://localhost:3000/pages/api/stripe-webhook返回404错误,以下是接口代码及错误日志,恳请帮忙排查解决。

Stripe Webhook接口代码

该文件名为stripe-webhook,已反复确认文件路径:

import { NextApiRequest, NextApiResponse } from "next"
import { buffer } from "stream/consumers"
import Stripe from "stripe"

export const config = {
    api:{
        bodyParser: false
    }
}

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string,{
    apiVersion: '2023-10-16'
})

export default async function handler(
    req: NextApiRequest,
    res: NextApiResponse
){

    const buf = await buffer(req)
    const sig = req.headers['stripe-signature']

    if(!sig){
        return res.status(400).send("Missing the stripe signature!")
    }

    let event: Stripe.Event

    try{
        event = stripe.webhooks.constructEvent(
            buf, sig, process.env.STRIPE_WEBHOOK_SECRET!
        )
    }catch(err){
        return res.status(400).send("Webhook error" + err)
    }

    switch(event.type){
        case 'charge.succeeded':
            const charge: any = event.data.object as Stripe.Charge;
            if(typeof charge.payment_intent === 'string'){
                await prisma?.order.update({
                    where: {paymentIntentId: charge.payment_intent},
                    data: {status: "complete", address: charge.shipping?.address},
                });
            }
            break;
        default:
            console.log("Unhandled event type:" + event.type)
    }
    res.json({received: true})
}

错误日志

> Ready! You are using Stripe API Version [2023-10-16]. Your webhook signing secret is whsec_ae0f6c1ba90b1c434264f36f618f9238a3ecb8e6b8de8a5f6ff278d99f30d0cd (^C to quit)
2023-12-04 18:06:24   --> payment_intent.created [evt_3OJbNsGRtGZJ0dW50BaY3sq1]
2023-12-04 18:06:25  <--  [404] POST http://localhost:3000/pages/api/stripe-webhook [evt_3OJbNsGRtGZJ0dW50BaY3sq1]
2023-12-04 18:06:26   --> payment_intent.created [evt_3OJbNtGRtGZJ0dW50ZnH99Dz]
2023-12-04 18:06:26  <--  [404] POST http://localhost:3000/pages/api/stripe-webhook [evt_3OJbNtGRtGZJ0dW50ZnH99Dz]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 22:12:47