首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >条带Webhook验证

条带Webhook验证
EN

Stack Overflow用户
提问于 2020-09-29 10:08:48
回答 1查看 247关注 0票数 0

我一直收到这个错误:

代码语言:javascript
运行
复制
No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing

我相信这与我使用的中间件有关,但我不确定是哪一个。这是我的中间件

代码语言:javascript
运行
复制
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "../views"));
app.use(
  favicon(path.join(__dirname, "../public/img/logo", "logo-bg-round.png"))
);
app.use(express.static("public"));
app.use(
  session({
    name: "oauth",
    secret: "PlincoOAUTH2",
    resave: true,
    saveUninitialized: true,
  })
);
app.use(passport.initialize());
app.use(passport.session());
app.use(express.json());
app.configure(socketio());
app.configure(express.rest());
app.use("/projects", new DashboardService());
app.on("connection", (conn) => app.channel("stream").join(conn));
app.publish(() => app.channel("stream"));
app.use("/auth", require("./routes/auth"));
app.use(cors());

这是我的路线:

代码语言:javascript
运行
复制
const Stripe = require("stripe");
const stripe = Stripe(
  "sk_test_..."
);

// Stripe requires the raw body to construct the event
app.post(
  "/payments/webhook",
  bodyParser.raw({ type: "application/json" }),
  (req, res) => {
    const sig = req.headers["stripe-signature"];
    const webhookSecret = "whsec_...";
    let event;

    try {
      event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
    } catch (err) {
      // On error, log and return the error message
      console.log(`❌ Error message: ${err.message}`);
      return res.status(400).send(`Webhook Error: ${err.message}`);
    }

    // Successfully constructed event
    console.log("✅ Success:", event.id);

    // Return a response to acknowledge receipt of the event
    res.json({ received: true });
  }
);

我尝试了很多方法,但找不到解决方案,我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-29 10:29:48

你这样做是不会得到肉体的。您需要从express.json()主体解析器中“排除”webhook路由,如下所示:https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/node-express/express.js

因此,不是

代码语言:javascript
运行
复制
app.use(express.json());

你需要这样的东西:

代码语言:javascript
运行
复制
app.use((req, res, next) => {
  if (req.originalUrl === '/payments/webhook') {
    next();
  } else {
    express.json()(req, res, next);
  }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64111880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档