【问题标题】:How can I access the raw data from the Stripe API in Sails?如何从 Sails 中的 Stripe API 访问原始数据?
【发布时间】:2021-10-12 23:23:37
【问题描述】:

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

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 中间件,如here 所述,但如果我这样做,那么this.req.body 是:

undefined

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

【问题讨论】:

    标签: stripe-payments sails.js


    【解决方案1】:

    答案在这里:https://stackoverflow.com/a/68236261/6569847

    将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);  
    },
    

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2019-06-18
      • 2012-11-02
      • 1970-01-01
      • 2018-02-14
      • 2011-01-07
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      相关资源
      最近更新 更多