diff --git a/src/controllers/imps.controller.js b/src/controllers/imps.controller.js new file mode 100644 index 0000000..0eb933d --- /dev/null +++ b/src/controllers/imps.controller.js @@ -0,0 +1,43 @@ +const axios = require('axios'); +const { logger } = require('../util/logger'); + +async function send( + fromAccount, + toAccount, + amount, + ifscCode, + beneficiaryName, + beneficiaryAcctType = 'SAVING', + remarks = '' +) { + try { + const reqData = { + stBenAccNo: toAccount, + stBeneName: beneficiaryName, + stBenAccType: beneficiaryAcctType, + stBenIFSC: ifscCode, + stFromAccDetails: fromAccount, + stTransferAmount: amount, + stRemarks: remarks, + }; + logger.info(reqData, 'request data to be sent to IMPS server'); + const response = await axios.post( + 'http://localhost:6768/kccb/api/IMPS/Producer', + reqData, + { + headers: { + 'Content-Type': 'application/json', + }, + } + ); + logger.info(response, 'response from IMPS'); + return response.data; + } catch (error) { + logger.error(error, 'error from IMPS'); + throw new Error( + 'API call failed: ' + (error.response?.data?.message || error.message) + ); + } +} + +module.exports = { send }; diff --git a/src/routes/imps.route.js b/src/routes/imps.route.js new file mode 100644 index 0000000..7f6a31d --- /dev/null +++ b/src/routes/imps.route.js @@ -0,0 +1,36 @@ +const express = require('express'); +const impsController = require('../controllers/imps.controller'); +const { logger } = require('../util/logger'); +const impsValidator = require('../validators/imps.validator'); +const paymentSecretValidator = require('../validators/payment.secret.validator'); + +const router = express.Router(); +router.use(impsValidator, paymentSecretValidator); + +const impsRoute = async (req, res) => { + const { fromAccount, toAccount, ifscCode, amount, beneficiaryName } = + req.body; + + try { + const result = await impsController.send( + fromAccount, + toAccount, + amount, + ifscCode, + beneficiaryName, + 'SAVING', + 'check' + ); + + if (result.startsWith('Message produced successfully')) { + return res.json({ message: 'SUCCESS' }); + } else { + return res.json({ error: 'INVALID_REQUEST' }); + } + } catch (error) { + logger.error(error, 'error occured while doing IMPS'); + return res.json({ error: 'INTERVAL_SERVER_ERROR' }); + } +}; + +module.exports = impsRoute; diff --git a/src/routes/index.js b/src/routes/index.js index 9599fbd..4819c87 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -7,6 +7,7 @@ const transferRoute = require('./transfer.route'); const beneficiaryRoute = require('./beneficiary.route'); const neftRoute = require('./neft.route'); const rtgsRoute = require('./rtgs.route'); +const impsRoute = require('./imps.route'); const { npciResponse } = require('../controllers/npci.controller'); const router = express.Router(); @@ -17,6 +18,7 @@ router.use('/transactions/account/:accountNo', authenticate, transactionRoute); router.use('/payment/transfer', authenticate, transferRoute); router.use('/payment/neft', authenticate, neftRoute); router.use('/payment/rtgs', authenticate, rtgsRoute); +router.use('/payment/imps', authenticate, impsRoute); router.use('/beneficiary', authenticate, beneficiaryRoute); router.use('/npci/beneficiary-response', npciResponse); diff --git a/src/validators/imps.validator.js b/src/validators/imps.validator.js new file mode 100644 index 0000000..76d787d --- /dev/null +++ b/src/validators/imps.validator.js @@ -0,0 +1,36 @@ +const impsValidator = (req, res, next) => { + const { + fromAccount, + toAccount, + amount, + remitterName, + beneficiaryName, + ifscCode, + } = req.body; + + if (!isAccountNumbersValid(fromAccount, toAccount)) { + return res.status(400).json({ error: 'INVALID_ACCOUNT_NUMBER_FORMAT' }); + } + + if (amount < 1) { + return res.status(400).json({ error: 'INVALID_AMOUNT' }); + } + + if (!remitterName || !beneficiaryName) { + return res + .status(400) + .json({ error: 'REMITTER_NAME AND BENEFICIARY_NAME REQUIRED' }); + } + + if (!ifscCode || !/^[A-Z]{4}0[0-9]{6}$/.test(ifscCode)) { + return res.status(400).json({ error: 'INVALID_IFSC_CODE' }); + } + + next(); +}; + +const isAccountNumbersValid = (fromAcct, toAcct) => { + return !(!fromAcct || !toAcct || fromAcct.length != 11 || toAcct.length < 7); +}; + +module.exports = impsValidator;