OrahKokos / coinpayments-ipn

Module for verifing Coinpaymets Instant Payment notifications.
MIT License
7 stars 4 forks source link

coinpayment fails to send my server ipn #6

Closed casha1995 closed 5 years ago

casha1995 commented 5 years ago

after upgrading to the new version , and following the steps , i get ipn from coinpayment api , with no problems , but in my coin-payment account , sent successfully appears as no (num tries left) , even when i get ipn calls to the back-end and receiving it successfully with no errors or no problems , but this didn't happen in older version , so can some one help in this matter here is the code that i'm trying after update to version 2.x.x

coinpayment api ipn send my server ipn successfully but in ipn-history sent successfully still no , this will cause coinpayment ipn api to send retries according to their docs https://www.coinpayments.net/merchant-tools-ipn and this , may create a duplicate or cause errors , i can check for duplicates , but i think there is something have gone wrong especially this didn't happen in the old version , i'm using http://url , but according to docs this isn't the problem , so can some one help me here to understand what is the problem

const { verify } = require(`coinpayments-ipn`);
const CoinpaymentsIPNError = require(`coinpayments-ipn/lib/error`);

const {
  MERCHANT_ID,
  IPN_SECRET
} = process.env;

app.post('/notify', function (req, res, next) {
  if(!req.get(`HMAC`) || !req.body || !req.body.ipn_mode || req.body.ipn_mode !== `hmac` || MERCHANT_ID !== req.body.merchant) {
    return next(new Error(`Invalid request`));
  }

  let isValid, error;

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

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

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

  return next();
}, function (req, res, next) {
  console.log(`Process payment notification`);
  console.log(req.body);
  // then some operations in db
  return next();
});
OrahKokos commented 5 years ago

@casha1995 Thanks for the feedback. Have you tried doing res.end() when the verification middleware is done and then call next? You can response with a 200 right after verification and make a check on your last middleware for res.headerSent to avoid error messages.

OrahKokos commented 5 years ago

Closing issue. Presumed fixed.

Reopen if needed.