我的 SvelteKit 应用程序中有一个端点,用于处理来自 Stripe 的 webhook 请求。每个请求都经过签名,以便可以验证它来自 Stripe。
我必须验证事件是否来自 Stripe 的代码如下所示:
import Stripe from "stripe";
const WEBHOOK_SECRET = process.env["STRIPE_WH_SECRET"];
const stripe = new Stripe(process.env["STRIPE_SECRET"], {
apiVersion: "2020-08-27",
});
export async function post({ headers, body }) {
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(
body,
headers["stripe-signature"],
WEBHOOK_SECRET
);
} catch (err) {
return {
status: 400,
body: err,
};
}
// Do stuff with the event
}
但是当它从 Stripe 接收到一个事件时,我得到了这个错误:
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
经过一番研究,我发现这个函数在调用SvelteKit 钩子之前将 body 解析为 JSON,这意味着无法直接获取原始 body,所以我决定最好的选择是尝试重建原始 body:
event = stripe.webhooks.constructEvent(
JSON.stringify(body),
headers["stripe-signature"],
WH_SECRET
);
我不完全确定为什么这不起作用,因为在挖掘了 Stripe 库中的相关代码之后,它似乎可以很好地处理字符串。我最好的猜测是,在某些时候编码会变得混乱。
对此的任何帮助将不胜感激,因为我真的很想避免离开 SvelteKit,因为我实际上已经完成了我的项目(回想起来,这不是一个好主意)。