获取对 SvelteKit 端点的请求的原始正文

问题描述

我的 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

经过一番研究,我发现在调用 this function 之前,正文已被 SvelteKit hooks 解析为 JSON,这意味着无法直接获取原始正文,因此我决定我最好的选择是尝试重建原始身体:

event = stripe.webhooks.constructEvent(
  JSON.stringify(body),WH_SECRET
);

我不完全确定为什么这不起作用,因为在 the relevant code in the Stripe library 中挖掘之后,它似乎可以很好地处理字符串。我最好的猜测是编码会在某些时候搞砸。

对此的任何帮助将不胜感激,因为我真的很想避免放弃 svelteKit,因为我实际上已经用它完成了我的项目(回想起来,这不是一个好主意)。

>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...