mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
238 lines
6.5 KiB
JavaScript
238 lines
6.5 KiB
JavaScript
const { param, body, validationResult } = require('express-validator');
|
|
const winston = require('../../../helpers/winston.logger');
|
|
const generate = require('../utils/utils');
|
|
const loadBalance = require('../../loadbalance/loadbalance.controller');
|
|
const { requestHsm, hsmErrorMessage, parseHsmTransactionId } = require('../services/requestHsm');
|
|
const selectHsmClient = require('../services/selectHsmClient');
|
|
|
|
// VALIDATION
|
|
const validate = (method) => {
|
|
switch (method) {
|
|
case 'topupResponse':
|
|
return [
|
|
body('mid').not().isEmpty().withMessage('MID is required'),
|
|
body('tid').not().isEmpty().withMessage('TID is required'),
|
|
body('uid').not().isEmpty().withMessage('UID is required'),
|
|
body('flag').not().isEmpty().withMessage('Flag is required'),
|
|
body('card_number')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Card Number is required'),
|
|
body('amount')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Amount is required')
|
|
.isNumeric()
|
|
.withMessage('Amount is invalid format'),
|
|
body('reservation_number')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Reservation Number is required')
|
|
.isArray()
|
|
.withMessage('Reservation Number is invalid request format'),
|
|
body('load_balance_number')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Load Balance Number is required'),
|
|
];
|
|
default:
|
|
break;
|
|
}
|
|
};
|
|
|
|
const controller = async (req, res) => {
|
|
try {
|
|
// param
|
|
let {
|
|
mid,
|
|
tid,
|
|
card_number,
|
|
amount,
|
|
reservation_number,
|
|
ref_no,
|
|
uid,
|
|
balance,
|
|
flag,
|
|
unique_code,
|
|
load_balance_number,
|
|
trxID,
|
|
requestData,
|
|
} = req.body;
|
|
|
|
let uniqueCode = unique_code;
|
|
let result = {};
|
|
|
|
const { client: useClient } = selectHsmClient(uniqueCode);
|
|
|
|
// case success write to the card but loss contact with device
|
|
// result = {
|
|
// code: "100",
|
|
// message: "failed topup",
|
|
// data: {},
|
|
// };
|
|
// return res.status(200).send(result);
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} [ REQUEST LOADING-BALANCE TOPUP-7 ]`
|
|
);
|
|
|
|
let transactionId = parseHsmTransactionId(trxID);
|
|
let packet = Buffer.from(generate.hexToBytes(requestData));
|
|
if (packet[0] != 0x47) {
|
|
result = {
|
|
code: '03',
|
|
message: 'Respone Card Wrong',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE LOADING-BALANCE TOPUP-7 ]: ${result.code}`
|
|
);
|
|
|
|
return res.status(200).send(result);
|
|
}
|
|
|
|
let apiId = Buffer.from([0x05]);
|
|
let reqData = generate.makeRequestData(transactionId, apiId, packet);
|
|
const respData = await requestHsm(useClient?.ws, reqData);
|
|
|
|
transactionId[0] = respData[0];
|
|
transactionId[1] = respData[1];
|
|
const respCode = generate.getResponseCode(respData);
|
|
const respData1 = generate.getResponseData(respData);
|
|
|
|
let data = {};
|
|
|
|
if (
|
|
respData[6] != 0x47 ||
|
|
(respData1[9] != 0x00 && respData1[10] != 0x00)
|
|
) {
|
|
data = {
|
|
mid: mid,
|
|
tid: tid,
|
|
card_number: card_number,
|
|
amount: amount,
|
|
reservation_number: reservation_number,
|
|
ref_no: ref_no,
|
|
write_status: 'FW',
|
|
uid: uid,
|
|
balance: parseInt(balance) + parseInt(amount),
|
|
flag: parseInt(flag),
|
|
unique_code: unique_code,
|
|
load_balance_number: load_balance_number,
|
|
};
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} [ PROCESSING LOADING-BALANCE TOPUP-7 ]: updating load balance transaction is FW...`
|
|
);
|
|
|
|
// update data load balance
|
|
let update = await loadBalance.loadingBalanceUpdate(data);
|
|
if (update) {
|
|
if (update.code == '00') {
|
|
result = {
|
|
code: '5002',
|
|
message: 'failed from HSM',
|
|
data: {
|
|
trxID: generate.buf2hex(transactionId),
|
|
responseCode: respData1.slice(6, 10),
|
|
transcode: null,
|
|
load_balance_number: update.data.load_balance_number,
|
|
},
|
|
};
|
|
} else {
|
|
result = {
|
|
code: update.code,
|
|
message: update.message,
|
|
data: {},
|
|
};
|
|
}
|
|
} else {
|
|
result = {
|
|
code: '423',
|
|
message: 'update/insert failed',
|
|
data: {},
|
|
};
|
|
}
|
|
} else {
|
|
// jika sudah mencapai maksimum counting flag, maka reset flag ke awal
|
|
if (flag >= 255) {
|
|
flag = -1;
|
|
}
|
|
|
|
data = {
|
|
mid: mid,
|
|
tid: tid,
|
|
card_number: card_number,
|
|
amount: amount,
|
|
reservation_number: reservation_number,
|
|
ref_no: ref_no,
|
|
write_status: null,
|
|
uid: uid,
|
|
balance: balance,
|
|
flag: flag ? parseInt(flag) + 1 : flag,
|
|
unique_code: unique_code,
|
|
load_balance_number: load_balance_number,
|
|
};
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} [ PROCESSING LOADING-BALANCE TOPUP-7 ]: updating load balance transaction...`
|
|
);
|
|
|
|
// update data load balance
|
|
let update = await loadBalance.loadingBalanceUpdate(data);
|
|
if (update) {
|
|
if (update.code == '00') {
|
|
result = {
|
|
code: '00',
|
|
message: 'Topup Succeess',
|
|
data: {
|
|
trxID: generate.buf2hex(transactionId),
|
|
responseCode: respCode,
|
|
responseData: respData1,
|
|
responseLength: respData1.length / 2,
|
|
load_balance_number: update.data.load_balance_number,
|
|
},
|
|
};
|
|
} else {
|
|
result = {
|
|
code: update.code,
|
|
message: update.message,
|
|
data: {},
|
|
};
|
|
}
|
|
} else {
|
|
result = {
|
|
code: '423',
|
|
message: 'update/insert failed',
|
|
data: {},
|
|
};
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
winston.logger.debug(
|
|
`${uniqueCode} [ RESPONSE LOADING-BALANCE TOPUP-7 ]: ${result.code}`
|
|
);
|
|
|
|
return res.status(200).send(result);
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`[ RESPONSE LOADING-BALANCE TOPUP-7 ]: 500 internal server error - backend server | ${error?.code === 'HSM_TIMEOUT' ? 'HSM_TIMEOUT' : 'HSM_REQUEST_FAILED'}`
|
|
);
|
|
|
|
return res.status(200).send({
|
|
code: '5001',
|
|
message: hsmErrorMessage(error),
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = { validate, controller };
|