【发布时间】:2019-05-22 18:52:49
【问题描述】:
我有一个调用 Firebase 函数的条带网络钩子。在这个函数中,我需要验证这个请求是否来自 Stripe 服务器。这是代码:
const functions = require('firebase-functions');
const bodyParser = require('body-parser');
const stripe = require("stripe")("sk_test_****");
const endpointSecret = 'whsec_****';
const app = require('express')();
app.use(bodyParser.json({
verify: function (req, res, buf) {
var url = req.originalUrl;
if (url.startsWith('/webhook')) {
req.rawBody = buf.toString()
}
}
}));
app.post('/webhook/example', (req, res) => {
let sig = req.headers["stripe-signature"];
try {
console.log(req.bodyRaw)
let event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
console.log(event);
res.status(200).end()
// Do something with event
}
catch (err) {
console.log(err);
res.status(400).end()
}
});
exports.app = functions.https.onRequest(app);
正如Stripe Documentation 中提到的,我必须使用原始正文来执行此安全检查。
我已尝试使用我当前的代码和:
app.use(require('body-parser').raw({type: '*/*'}));
但我总是收到这个错误:
Error: 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
【问题讨论】:
标签: google-cloud-platform google-cloud-functions stripe-payments