我现在正在从Express迁移到Zeit上的无服务器功能。
当使用webhook docs时,我可以通过bodyParser获得它,但是它如何在无服务器函数上工作呢?如何接收字符串格式的body以验证条带签名?
支持团队将我重定向到这个documentation link,我很困惑,据我所知,我必须将text/plain传递到请求标头中,但我无法控制它,因为条纹是发送webhook的那个人。
export default async (req, res) => {
let sig = req.headers["stripe-signature"];
let rawBody = req.body;
let event = stripe.webhooks.constructEvent(rawBody, sig, process.env.STRIPE_SIGNING_SECRET);
...
}在我的函数中,我接收req.body作为一个对象,我如何解决这个问题?
发布于 2020-01-24 11:21:06
下面的代码片段适用于我(从这个source修改):
const endpointSecret = process.env.STRIPE_SIGNING_SECRET;
export default async (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
let bodyChunks = [];
req
.on('data', chunk => bodyChunks.push(chunk))
.on('end', async () => {
const rawBody = Buffer.concat(bodyChunks).toString('utf8');
try {
event = stripe.webhooks.constructEvent(rawBody, sig, endpointSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle event here
...
// Return a response to acknowledge receipt of the event
res.json({ received: true });
});
};
export const config = {
api: {
bodyParser: false,
},
};https://stackoverflow.com/questions/59566185
复制相似问题