【问题标题】:Express JS and CoinPayments: coudn't get the request body of IPN notification requestExpress JS 和 CoinPayments:无法获取 IPN 通知请求的请求正文
【发布时间】:2021-08-30 06:05:59
【问题描述】:

所以这是我的问题,我使用 Express JS,我正在使用 coinPayments 设置付款,一切正常 npm coinpayments,但是我无法使用 IPN 获得任何主体

router.post(
  `/notify`,
  (req, res, next) => {
    res.send('ok');
    console.log('------------------------------ipn--------------------------------------');
    console.log('body', req.body);
    console.log('------------------------------ipn--------------------------------------');
    if (
      !req.get(`HMAC`) ||
      !req.body.ipn_mode ||
      req.body.ipn_mode !== `hmac` ||
      MERCHANT_ID !== req.body.merchant
    ) {
      return next(new Error(`Invalid request`));
    }

    let isValid;
    let error;

    try {
      isValid = verify(req.get(`HMAC`), IPN_SECRET, req.body);
    } catch (e) {
      error = e;
    }

    if (error && error) {
      return next(error);
    }

    if (!isValid) {
      return next(new Error(`Hmac calculation does not match`));
    }

    return next();
  }

我总是得到一个空的 req.body

 ------------------------------ipn--------------------------------------
 body {}
 ------------------------------ipn--------------------------------------
Invalid request at router.post.txn_id.txn_id

有谁知道为什么,我该如何解决?

【问题讨论】:

    标签: node.js express paypal-ipn coinpayments-api


    【解决方案1】:

    来自coinpayment documentation

    它是通过制作一个标准的HTTP POST来实现的 (application/x-www-form-urlencoded) 通过 https:// 或 http:// 调用 您服务器上的脚本或 CGI 程序的 URL。

    在您的 Express 服务器中,您可能需要添加中间件来解析 urlencoded 格式的请求:

    app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
    

    【讨论】:

    • 它有效!非常感谢您的支持和解释 Đăng Khoa Đinh
    猜你喜欢
    • 2016-08-01
    • 2015-06-27
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2015-10-15
    • 2022-12-03
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多