小编qui*_*gly的帖子

我正在尝试使用 Stripe 和 NextJS 13.2.3 设置 webhook

我能够通过内置 Stripe 页面成功结帐我的购物车,并且我会被重定向到我的 successUrl 路线。我的本地测试 webhook 正在按预期被调用。但是,当我包含代码来验证请求是否来自 Stripe 时,我收到一些错误。

这是我在 NextJS 中的 webhook

import { stripe } from "@/lib/stripe"

export async function POST(req: Request){

    const payload = await req.json()
    
    const sig = req.headers.get('stripe-signature') as string
    console.log("sig: ", sig)

    let event;

    const endpointSecret = process.env.WEBHOOK_SECRET_LOCAL as string

    try {
        console.log("constructing event...")
        event = stripe.webhooks.constructEvent(payload, sig, endpointSecret)
    } catch (error) {
        console.error(error)
        return new Response(`Webhook error: ${error}`, {
            status: 400,
        })
    }
    
    return new Response("payment confirmation route received", {
        status: 200,
    }) …
Run Code Online (Sandbox Code Playgroud)

stripe-payments typescript next.js next.js13

7
推荐指数
3
解决办法
6246
查看次数

标签 统计

next.js ×1

next.js13 ×1

stripe-payments ×1

typescript ×1