Hir*_*uri 8 node.js serverless vercel
我正在 Zeit Now 上从 Express 迁移到无服务器功能。
Stripe webhook 文档要求提供原始正文请求,使用 Express 时我可以通过 bodyParser 获取它,但是它如何在无服务器功能上工作?如何以字符串格式接收正文以验证条带签名?
支持团队将我重定向到这个文档链接,我很困惑,据我所知,我必须传入text/plain请求标头,但我无法控制它,因为 Stripe 是发送 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);
...
}
Run Code Online (Sandbox Code Playgroud)
在我的函数中,我req.body作为一个对象接收,我该如何解决这个问题?
以下代码片段对我有用(从此来源修改):
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,
},
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
573 次 |
| 最近记录: |