【问题标题】:Stripe webhook returning 308 error when calling Vercel serverless function调用 Vercel 无服务器函数时,Stripe webhook 返回 308 错误
【发布时间】:2023-01-10 03:53:20
【问题描述】:

我已经使用 Stripe 设置了一个 webhook,它在触发时调用无服务器函数。

该函数旨在在调用时更新我的​​数据库中的条目,表明用户已注册高级帐户。

当我在本地运行它时,webhook 完美运行。它触发 API、更新用户并处理付款。

但是,当它实时运行时,我不断收到 308 错误消息:

重定向到 my-app-url.com

这是我的函数的代码:

import { buffer } from "micro"
import { createClient } from "@supabase/supabase-js";

require("dotenv").config();

const stripe = require("stripe")(process.env.STRIPE_LIVE_KEY)

const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET

const supabaseUrl = process.env.REACT_APP_SUPABASE_URL
const supabaseAnonKey = process.env.REACT_APP_SUPABASE_ANON_KEY

const supabase = createClient(supabaseUrl, supabaseAnonKey)

module.exports = async (req, res) => {

   const signature = req.headers["stripe-signature"]
   const reqBuffer = await buffer(req)

   let event 

   try {
    event = stripe.webhooks.constructEvent(reqBuffer, signature, endpointSecret)
   } catch (err) {
    console.log(err)
    return res.status(400).send(`Webhook error: ${err.message}`)
   }

   if (event.type === "checkout.session.completed") {
    console.log("Checkout completed!")
    const userId = String(event.data.object.client_reference_id)

    console.log(userId)

    const { error } = await supabase.from('profiles').update({ premium: 'true' }).eq('id', userId) 
    
    if (error) {
      console.log(error)
    }
   }

   res.send({ received: true })
}

当我检查我的函数日志时,它似乎根本没有触发/到达我的 API - 没有日志。

有没有人有什么建议?

【问题讨论】:

    标签: node.js stripe-payments serverless vercel


    【解决方案1】:

    正如 308 错误所示,看起来您的服务器正在接收 webhook,但试图将其重定向到另一个 URL。它甚至可能是相同的 URL,但通过 HTTPS。

    Stripe 无法遵循重定向,因此这是服务器端的错误配置。您必须为您的服务器期望接收 webhook 的位置提供确切的 URL。

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 2020-08-03
      • 2021-01-12
      • 2021-09-17
      • 2020-12-18
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      相关资源
      最近更新 更多