node.jsexpresspaypal-ipncoinpayments-api

Express JS and CoinPayments: coudn't get the request body of IPN notification request


So here is my problem, I work with Express JS, I am setting up payments with coinPayments, everything work npm coinpayments, however I couldn't get any body with the 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();
  }

I always get an empty req.body

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

Does anyone have an idea why , and how can I resolve it ?


Solution

  • From the coinpayment documentation

    It is implemented by making a standard HTTP POST (application/x-www-form-urlencoded) call over a https:// or http:// URL to a script or CGI program on your server.

    In your Express server, you may need to add the middleware to parse requests in urlencoded format:

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