如何从 Sails 中的 Stripe API 访问原始数据?

How can I access the raw data from the Stripe API in Sails?

我正在尝试在 Sails 中设置 Stripe webhook,按照 here 所述检查 webhook 签名。签名以与 Stripe 示例相同的格式返回,但每个测试 webhook returns:

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

为了尝试获取原始请求正文,我尝试绕过 sails.config.http 中此路由的 bodyParser 中间件,如 所述,但如果我这样做,则 this.req.body 是:

undefined

顺便说一句,我可以看到 Sails 中有一些与 Stripe 的集成,但我找不到预构建的 webhook。如果我要重新发明轮子,请告诉我,关于将 Stripe 与 Sails 集成的文档很少。

答案在这里:

将config/http.js下注释掉的bodyParser更新为:

bodyParser: function(req, res, next) {
  var skipper = require('skipper')();
  var rawParser = require("body-parser").raw({type: "*/*"});

  // Create and return the middleware function
  sails.log.debug(req.headers);
  if (req.headers && req.headers['stripe-signature']) {
    sails.log.info('request using raw parser middleware');
    return rawParser(req, res, next);
  }
  // Otherwise use Skipper to parse the body
  sails.log.info('request using skipper middleware');
  return skipper(req, res, next);  
},