diff --git a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json index ba0abce..9947d36 100644 --- a/.71c131f2ce727b13210eef32232709400762e5fb-audit.json +++ b/.71c131f2ce727b13210eef32232709400762e5fb-audit.json @@ -6,9 +6,9 @@ "auditLog": ".71c131f2ce727b13210eef32232709400762e5fb-audit.json", "files": [ { - "date": 1656386699784, - "name": "2022-06-28-onlinetopup.log", - "hash": "c73cd79907e574f119fdefa09ac22421faedca9a4fd00da66ddfd4d033dfc933" + "date": 1656909824891, + "name": "2022-07-04-onlinetopup.log", + "hash": "89c35db4590f70687ea9c14c6cdd614ae9588da6ff509d844520eb1526433504" } ], "hashType": "sha256" diff --git a/application/middlewares/jwt.middleware.js b/application/middlewares/jwt.middleware.js index 4f4767d..9693a1f 100644 --- a/application/middlewares/jwt.middleware.js +++ b/application/middlewares/jwt.middleware.js @@ -1,14 +1,22 @@ const jwt = require("jsonwebtoken"); +const helper = require("../helpers/helper"); const generateRefreshToken = (req) => { let expiresIn = process.env.JWT_REFRESH_EXPIRATION ? parseInt(process.env.JWT_REFRESH_EXPIRATION) : "7d"; - return jwt.sign(req, process.env.REFRESH_TOKEN_SECRET, { + let signOptions = { + issuer: 'CMS Kereta Commuter Indonesia', expiresIn: expiresIn, - }); + } + return jwt.sign(req, process.env.REFRESH_TOKEN_SECRET, signOptions); }; const authenticateRefreshToken = (req, res, next) => { - jwt.verify(req.header("refresh_token"), process.env.REFRESH_TOKEN_SECRET, (err, data) => { + + let verifyOptions = { + issuer: 'CMS Kereta Commuter Indonesia', + } + + jwt.verify(req.header("refresh_token"), process.env.REFRESH_TOKEN_SECRET, verifyOptions, (err, data) => { if (err) { return res.json({ code: "400", @@ -23,6 +31,10 @@ const authenticateRefreshToken = (req, res, next) => { const generateAccessToken = (req) => { let expiresIn = process.env.JWT_EXPIRATION ? parseInt(process.env.JWT_EXPIRATION) : "1d"; + let signOptions = { + issuer: 'CMS Kereta Commuter Indonesia', + expiresIn: expiresIn, + } if (req.user_group_id == "1") { expiresIn = parseInt(process.env.JWT_EXPIRATION_ADMIN_WEB); @@ -36,9 +48,7 @@ const generateAccessToken = (req) => { expiresIn = parseInt(process.env.JWT_EXPIRATION_TERMINAL); } - return jwt.sign(req, process.env.TOKEN_SECRET, { - expiresIn: expiresIn, - }); + return jwt.sign(req, process.env.TOKEN_SECRET, signOptions); }; const authenticateToken = async (req, res, next) => { @@ -55,7 +65,11 @@ const authenticateToken = async (req, res, next) => { }); } - await jwt.verify(token, process.env.TOKEN_SECRET, (err, data) => { + let verifyOptions = { + issuer: 'CMS Kereta Commuter Indonesia', + } + + await jwt.verify(token, process.env.TOKEN_SECRET, verifyOptions, (err, data) => { if (err) { return res.json({ code: "402", @@ -64,9 +78,8 @@ const authenticateToken = async (req, res, next) => { }); } - req.user_id = data.user_id; - req.username = data.username; - req.user_group_id = data.user_group_id; + req.mid_token = helper.decryptText(data.mid); + req.tid_token = helper.decryptText(data.tid); next(); }); diff --git a/application/modules/hsm/authenticate1.js b/application/modules/hsm/authenticate1.js index d34883a..d6d4211 100644 --- a/application/modules/hsm/authenticate1.js +++ b/application/modules/hsm/authenticate1.js @@ -1,4 +1,5 @@ const { client } = require("./services/webSocket"); +const helper = require("../../helpers/helper"); const winston = require("../../helpers/winston.logger"); const generate = require("./utils/utils"); const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); diff --git a/application/modules/hsm/service_block.js b/application/modules/hsm/service_block.js index 47b413c..bb34c78 100644 --- a/application/modules/hsm/service_block.js +++ b/application/modules/hsm/service_block.js @@ -2,8 +2,8 @@ const controller = async (req, res) => { try { let sytemCode = "939E" - let serviceReadNS = "0B104B1017208B20" - let blockReadNS = "8000810082008300" + let serviceReadNS = "4B1017208B20" + let blockReadNS = "800081008200" let serviceMutual = "102001A04C2001A0882001A0" let blockReadS = "80008200" let blockWrite = "800081008200" diff --git a/application/modules/paymentverification/paymentverification.controller.js b/application/modules/paymentverification/paymentverification.controller.js new file mode 100644 index 0000000..d1fb356 --- /dev/null +++ b/application/modules/paymentverification/paymentverification.controller.js @@ -0,0 +1,142 @@ +const { param, body, validationResult } = require("express-validator"); +const helper = require("../../helpers/helper"); +const winston = require("../../helpers/winston.logger"); +const model = require("./paymentverification.model"); +const moment = require("moment"); +moment.locale("id"); + +var result = {}; +var uniqueCode; + +// VALIDATION +const validate = (method) => { + switch (method) { + case "paymentVerification": + return [ + body("date") + .not() + .isEmpty() + .withMessage("Date is required") + .isDate({ format: "YYYY-MM-DD" }) + .withMessage("Invalid format date. Format is YYYY-MM-DD"), + body("log") + .not() + .isEmpty() + .withMessage("Log is required") + .isArray() + .withMessage("Log is invalid request"), + ]; + default: + break; + } +}; + +// PAYMENT VERIFICATION +const paymentVerification = async (req, res) => { + try { + // generate unique code + uniqueCode = await helper.getUniqueCode(); + + // param + let { log } = req.body; + let mid = req.mid_token; + let tid = req.tid_token; + + // log info + winston.logger.info( + `${uniqueCode} REQUEST payment verification: ${JSON.stringify(req.body)}` + ); + + // check validator + const err = validationResult(req, res); + if (!err.isEmpty()) { + result = { + code: "400", + message: err.errors[0].msg, + data: {}, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment verification: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + // time + let now = moment().format("YYYY-MM-DD HH:mm:ss"); + + // log debug + winston.logger.debug(`${uniqueCode} checking partner...`); + + // get partner, merchant, terminal from tid + let getPartner = await model.getPartner(mid, tid); + if (!getPartner) { + result = { + code: "412", + message: "terminal id is not valid", + data: {}, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment verification: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } + + let data = []; + + log.forEach(async (element) => { + // check ref no and amount log + let check = await model.checkRefNoAndAmount( + element.ref_no, + element.amount + ); + if (!check) { + result = { + code: "412", + message: "terminal id is not valid", + data: {}, + }; + + // log warn + winston.logger.warn( + `${uniqueCode} RESPONSE payment verification: ${JSON.stringify( + result + )}` + ); + + return res.status(200).json(result); + } + }); + + // log info + winston.logger.info( + `${uniqueCode} RESPONSE payment verification: ${JSON.stringify(result)}` + ); + + return res.status(200).json(result); + } catch (error) { + // create log + winston.logger.error( + `500 internal server error - backend server | ${error.message}` + ); + + return res.status(200).json({ + code: "500", + message: + process.env.NODE_ENV != "production" + ? error.message + : "500 internal server error - backend server.", + data: {}, + }); + } +}; + +module.exports = { + validate, + paymentVerification, +}; diff --git a/application/modules/paymentverification/paymentverification.model.js b/application/modules/paymentverification/paymentverification.model.js new file mode 100644 index 0000000..f14a99c --- /dev/null +++ b/application/modules/paymentverification/paymentverification.model.js @@ -0,0 +1,37 @@ +const knex = require("../../../infrastructure/database/knex"); + +// GET PARTNER, MERCHANT, TERMINAL FROM TID +const getPartner = (mid, tid) => { + let result = knex + .select( + "t_m_partner.i_partner", + "t_m_partner.c_code as partner_code", + "t_m_partner.c_name as partner_name", + "t_m_merchant.i_merchant", + "t_m_merchant.c_code as merchant_code", + "t_m_merchant.c_name as merchant_name", + "t_m_merchant.e_mid as mid", + "t_m_terminal.i_terminal", + "t_m_terminal.c_code as terminal_code", + "t_m_terminal.c_name as terminal_name", + "t_m_terminal.e_tid as tid" + ) + .from("t_m_terminal") + .leftJoin( + "t_m_merchant", + "t_m_terminal.i_merchant", + "t_m_merchant.i_merchant" + ) + .leftJoin("t_m_partner", "t_m_merchant.i_partner", "t_m_partner.i_partner") + .where("t_m_merchant.e_mid", mid) + .where("t_m_terminal.e_tid", tid) + .where("t_m_terminal.c_status", "A") + .where("t_m_merchant.c_status", "A") + .where("t_m_partner.c_status", "A") + .first(); + return result; +}; + +module.exports = { + getPartner, +}; diff --git a/application/modules/reservebalance/reservebalance.controller.js b/application/modules/reservebalance/reservebalance.controller.js index 7c1aa9a..089c1ca 100644 --- a/application/modules/reservebalance/reservebalance.controller.js +++ b/application/modules/reservebalance/reservebalance.controller.js @@ -100,7 +100,7 @@ const checkReservation = async (req, res) => { }; } else { result = { - code: "00", + code: "02", message: "success without reservation pending or in progress.", data: {}, }; @@ -348,10 +348,10 @@ const reservationValidate = async (req, res) => { // get issuer fee let issuerFee = 0; let channelFee = 0; - let getIssuer = await model.getIssuer(getPartner.i_partner); + let getIssuer = await model.getIssuer(getPartner.i_partner, now); if (getIssuer) { - issuerFee = (getIssuer.i_issuer_fee_perc / 100) * 1500; - channelFee = (getIssuer.i_channel_fee_perc / 100) * 1500; + issuerFee = (getIssuer.i_issuer_fee_perc / 100) * getIssuer.i_amount; + channelFee = (getIssuer.i_channel_fee_perc / 100) * getIssuer.i_amount; } // if (!getIssuer) { // return res.status(200).json({ @@ -637,10 +637,10 @@ const reservation = async (req, res) => { // get issuer fee let issuerFee = 0; let channelFee = 0; - let getIssuer = await model.getIssuer(getPartner.i_partner); + let getIssuer = await model.getIssuer(getPartner.i_partner, now); if (getIssuer) { - issuerFee = (getIssuer.i_issuer_fee_perc / 100) * 1500; - channelFee = (getIssuer.i_channel_fee_perc / 100) * 1500; + issuerFee = (getIssuer.i_issuer_fee_perc / 100) * getIssuer.i_amount; + channelFee = (getIssuer.i_channel_fee_perc / 100) * getIssuer.i_amount; } // if (!getIssuer) { // return res.status(200).json({ diff --git a/application/modules/reservebalance/reservebalance.model.js b/application/modules/reservebalance/reservebalance.model.js index e899187..00a58c3 100644 --- a/application/modules/reservebalance/reservebalance.model.js +++ b/application/modules/reservebalance/reservebalance.model.js @@ -66,11 +66,13 @@ const getPartner = (mid, tid) => { }; // GET ISSUER FEE -const getIssuer = (id) => { +const getIssuer = (id, time) => { let result = knex("t_c_partner_fee") - .select("i_issuer_fee_perc", "i_channel_fee_perc") + .select("i_issuer_fee_perc", "i_channel_fee_perc", "i_amount") .where("i_partner", id) .where("c_status", "A") + .where("d_valid", "<=", time) + .orderBy("d_valid", "desc") .first(); return result; }; diff --git a/application/routes/paymentverification.routes.js b/application/routes/paymentverification.routes.js new file mode 100644 index 0000000..5352c00 --- /dev/null +++ b/application/routes/paymentverification.routes.js @@ -0,0 +1,13 @@ +const express = require("express"); +const router = express.Router(); +const auth = require("../middlewares/jwt.middleware"); +const controller = require("../modules/paymentverification/paymentverification.controller"); + +router.post( + "/payment-verification", + auth.authenticateToken, + controller.validate("paymentVerification"), + controller.paymentVerification +); + +module.exports = router; diff --git a/docs/error_code b/docs/error_code index f9cc65f..5e30a9a 100644 --- a/docs/error_code +++ b/docs/error_code @@ -1,6 +1,8 @@ # SUCCESS MESSAGE CODE 00 Success Normal 01 Success Without Write to the card + 02 Success without reservation pending or in progress status + 03 Success with reservation pending or in progress status # ERROR MESSAGE CODE TOPUP ONLINE SERVICE 400 Bad Request Parameter or Validation Parameter Error diff --git a/index.js b/index.js index 1b26a52..861ca1f 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ const reservationRoute = require("./application/routes/reservebalance.routes"); const loadBalanceRoute = require("./application/routes/loadbalance.routes"); const hsmRoute = require("./application/routes/hsm.routes"); const historyRoute = require("./application/routes/history.routes"); +const paymentVerificationRoute = require("./application/routes/paymentverification.routes"); require("dotenv").config(); @@ -40,6 +41,7 @@ app.use("/api/", authRoute); app.use("/api/", historyRoute); app.use("/api/", hsmRoute); app.use("/api/", loadBalanceRoute); +app.use("/api/", paymentVerificationRoute); app.use("/api/", reservationRoute); // set port, listen for requests