implemented a controller for processing all the responses we will get from NPCI for beneficiary validation

This commit is contained in:
2025-08-07 02:01:03 +05:30
parent 35bd668116
commit ecb9b14629

View File

@@ -0,0 +1,45 @@
const db = require('../config/db');
const { getJson } = require('../config/redis');
const { logger } = require('../util/logger');
async function npciResponse(req, res) {
const { resp } = req.body;
if (resp.status === 'Success') {
await handleNPCISuccess(resp);
} else {
await handleNPCIFailure(resp);
}
res.send('ok');
}
async function handleNPCISuccess(response) {
const { txnid, benename } = response;
try {
const beneficiaryDetails = await getJson(txnid);
if (!beneficiaryDetails) {
logger.warn('no txnid in redis');
return false;
}
const { customerNo, accountNo, ifscCode, accountType } = beneficiaryDetails;
const query =
'INSERT INTO beneficiaries (customer_no, account_no, name, account_type, ifsc_code) VALUES ($1, $2, $3, $4, $5)';
const result = await db.query(query, [
customerNo,
accountNo,
benename,
accountType,
ifscCode,
]);
logger.info(result);
return true;
} catch (error) {
logger.error(error, 'error processing npci response');
return false;
}
}
async function handleNPCIFailure(response) {
console.log(response);
}
module.exports = { npciResponse };