小编Max*_*man的帖子

获取 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
}
Run Code Online (Sandbox Code Playgroud)

但是当它收到来自 Stripe 的事件时,我收到此错误:

No signatures found matching the expected signature for payload. Are …
Run Code Online (Sandbox Code Playgroud)

node.js stripe-payments sveltekit

10
推荐指数
2
解决办法
2360
查看次数

标签 统计

node.js ×1

stripe-payments ×1

sveltekit ×1