mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
2224 lines
76 KiB
JavaScript
2224 lines
76 KiB
JavaScript
const { logErrorCode, logStatusCode } = require('../../helpers/log-fields');
|
|
const helper = require('../../helpers/helper');
|
|
const winston = require('../../helpers/winston.logger');
|
|
const { validationResult } = require('express-validator');
|
|
const model = require('./reservebalance.model');
|
|
const moment = require('moment');
|
|
|
|
// Pertahankan alur validasi dan pembuatan reservasi untuk klien V1 dan V2.
|
|
const reservationValidate = async (req, res) => {
|
|
try {
|
|
// generate unique code
|
|
// unique code ini digunakan untuk keperluan log agar mudah untuk dibaca
|
|
let uniqueCode = await helper.getUniqueCode();
|
|
let result = {};
|
|
|
|
// param
|
|
let { mid, tid, card_number, amount } = req.body;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ REQUEST RESERVATION-VALIDATE ]`
|
|
);
|
|
|
|
// trim card number
|
|
card_number = card_number.trim();
|
|
|
|
// check validator
|
|
let err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
result = {
|
|
code: '400',
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// pengecekkan nomor kartu yang di request adalah kartu production atau bukan berdasarkan settingan di .ENV dengan constanta SERVER_FOR.
|
|
// Jika settingan .ENV SERVER_FOR adalah production dan nomor kartu KMT yang di request adalah kartu development maka sistem online topup akan menolaknya. Begitupun sebaliknya
|
|
// Begitupun sebaliknya, jika settingan .ENV SERVER_FOR adalah development dan nomor kartu KMT yang di request adalah kartu production maka sistem online topup akan menolaknya
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking production card number or not...`
|
|
// );
|
|
|
|
let cardBlacklist = await model.getCardBlacklist(card_number);
|
|
if (cardBlacklist) {
|
|
if (cardBlacklist.c_status == 'B') {
|
|
result = {
|
|
code: '445',
|
|
message: 'Card is Blacklisted',
|
|
data: {},
|
|
};
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// jika settingan diperuntukan untuk production
|
|
// if (process.env.SERVER_FOR == 'production') {
|
|
// // kartu yang di request bukan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) != '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message: "It's not production card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ]: ${JSON.stringify(
|
|
// result
|
|
// )}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// } else {
|
|
// // jika settingan bukan diperuntukan untuk production, bisa development atau local atau yang lainnya dan kemudian kartu yang di request merupakan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) == '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message:
|
|
// "It's production card number. Please use the development card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ]: ${JSON.stringify(
|
|
// result
|
|
// )}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// }
|
|
|
|
// time
|
|
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
let today = moment().format('YYYY-MM-DD');
|
|
let year = moment().format('Y');
|
|
let month = moment().format('M');
|
|
|
|
// log debug
|
|
// proses pengecekkan data partner berdasarkan mid dan tid yang di request, apakah terdaftar di sistem CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking partner...`
|
|
// );
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
|
|
// jika partner (mid atau tid) tidak ditemukan atau tidak terdaftar di sistem CMS
|
|
if (!getPartner) {
|
|
result = {
|
|
code: '412',
|
|
message: 'terminal id is not valid',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (getPartner.i_partner != req.partner_id_token) {
|
|
result = {
|
|
code: '412',
|
|
message: 'partner is not match',
|
|
data: {},
|
|
};
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// get settingan dari parameter apakah nomor kartu yang di request harus di cek dengan data card product CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] getting checking card number by card product CMS status on parameter...`
|
|
// );
|
|
|
|
// get parameter to check with card product cms or not
|
|
let getCheckByCardProductStatusValue =
|
|
await model.getCheckByCardProductStatusValue();
|
|
let checkByCardProductStatusValue =
|
|
getCheckByCardProductStatusValue?.c_value;
|
|
|
|
// jika settingan pengecekkan nomor kartu yang di request dengan data card product yang ada di sistem CMS aktif
|
|
if (checkByCardProductStatusValue == 'A') {
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking card number...`
|
|
// );
|
|
|
|
// check card number yang di request terdaftar di card product CMS atau tidak
|
|
let checkCardNumber = await model.checkCardNumber(card_number);
|
|
|
|
// jika nomor kartu yang di request tidak terdaftar di card prouct CMS
|
|
if (!checkCardNumber) {
|
|
result = {
|
|
code: '413',
|
|
message: 'card number is not registered',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking reservation amount...`
|
|
// );
|
|
|
|
// check reservation amount
|
|
// amount reservasi harus lebih dari 0
|
|
if (parseInt(amount) <= 0) {
|
|
result = {
|
|
code: '415',
|
|
message: 'amount must be greater than 0',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// proses pencarian saldo terakhir dari nomor kartu yang di request berdasarkan data log hitory
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] getting last balance on the card by card history...`
|
|
// );
|
|
|
|
// get last balance on the card
|
|
let balance = 0;
|
|
let getLastBalanceFromCardHistory =
|
|
await model.getLastBalanceFromCardHistory(card_number);
|
|
if (getLastBalanceFromCardHistory) {
|
|
balance = getLastBalanceFromCardHistory.m_balance_after;
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking BI regulation status...`
|
|
// );
|
|
|
|
// get settingan terkait pengecekkan regulasi BI dari parameter, apakah diaktifkan atau tidak
|
|
let getBIRegulationStatus = await model.getBIRegulationStatus();
|
|
|
|
// jika pengecekkan regulasi BI diaktifkan
|
|
if (getBIRegulationStatus.c_value == 'A') {
|
|
// log debug
|
|
// get maksimum saldo yang harus ada di dalam kartu berdasarkan settingan parameter di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking max balance on the card...`
|
|
// );
|
|
|
|
// get max saldo on the card
|
|
let getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard();
|
|
|
|
// jika settingan paremeter maksimum saldo di dalam kartu ditemukan
|
|
if (getMaxAmountOnTheCard.c_value) {
|
|
// jika saldo dalam kartu diakumulasikan dengan amount reservasi dan jumlah nya ternyata melebihi jumlah maksimum saldo berdasarkan settingan parameter, maka akan transaksi akan di tolak
|
|
if (
|
|
parseInt(balance) + parseInt(amount) >
|
|
parseInt(getMaxAmountOnTheCard.c_value)
|
|
) {
|
|
result = {
|
|
code: '416',
|
|
message: 'your card number has reached the maximum balance',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan akumulasi maksimum topup di bulan berjalan berdasarkan parameter setting di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking max topup per month...`
|
|
// );
|
|
|
|
// get accumulated balance in this month dari nomor kartu yang di request
|
|
let accumulatedBalance = 0;
|
|
let getAccumulationBalanceInThisMonthFromCardHistory =
|
|
await model.getAccumulationBalanceInThisMonthFromCardHistory(
|
|
card_number,
|
|
year,
|
|
month
|
|
);
|
|
|
|
// jika ditemukan history akumulasi topup dari nomor kartu yang di request, maka set akumulasi saldo di bulan berjalan sama dengan history yang ditemukan
|
|
if (getAccumulationBalanceInThisMonthFromCardHistory) {
|
|
accumulatedBalance =
|
|
getAccumulationBalanceInThisMonthFromCardHistory.accumulation_balance;
|
|
}
|
|
|
|
// validate max topup per month berdasarkan parameter setting
|
|
let getMaxTopupPerMonth = await model.getMaxTopupPerMonth();
|
|
|
|
// jika settingan parameter maksimum akumulasi topup dalam sebulan ditemukan
|
|
if (getMaxTopupPerMonth.c_value) {
|
|
// jika akumulasi saldo kartu yang di request dijumlahkan dengan amount reservasi dan ternyata melebihi jumlah akumulasi maksumum topup dalam sebelum, maka transaksi akan di tolak
|
|
if (
|
|
parseInt(accumulatedBalance) + parseInt(amount) >
|
|
parseInt(getMaxTopupPerMonth.c_value)
|
|
) {
|
|
result = {
|
|
code: '417',
|
|
message: 'your card number has been limit top up for this month',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan maksimum reservasi dengna status pending di parameter setting sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking max reservation...`
|
|
// );
|
|
|
|
// get maksimum reservation dengan status pending dari parameter setting
|
|
let getMaxReservation = await model.getMaxReservation();
|
|
|
|
// jika parameter setting maksimum reservasi dengan status pending tidak ditemukan maka set menjadi 0
|
|
let maxReservation = getMaxReservation.c_value
|
|
? parseInt(getMaxReservation.c_value)
|
|
: 0;
|
|
|
|
// log debug
|
|
// proses pencarian transaki dari table reservasi balance berdasarkan nomor kartu yang di request
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] checking total reservation pending...`
|
|
// );
|
|
|
|
// get total reservation pending bersarkan nomor kartu yang di request yaitu total transaksi yang status nya pending beserta jumlah amount nya
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now
|
|
);
|
|
|
|
// jika nomor kartu yang di request ditemukan transaksi reservasi dengan status pending pada transaksi sebelumnya, maka jumlahkan total amount reservasi sebelumnya dengan amount reservasi yang dilakukan sekarang
|
|
let totalReservationPending =
|
|
getReservationPending.total_nominal_reservation > 0
|
|
? parseInt(getReservationPending.total_nominal_reservation) +
|
|
parseInt(amount)
|
|
: parseInt(amount);
|
|
|
|
// jika total amount reservasi melebihi maksimum reservasi sesuai dengan parameter setting di sistem CMS maka transaksi akan di tolak
|
|
if (totalReservationPending > maxReservation) {
|
|
result = {
|
|
code: '421',
|
|
message:
|
|
'your card number has some pending transaction. Please finished it!',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// process mendapatkan persentase issuer fee dan channel free berdasarkan settingan dari sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] getting issuer fee and channel fee...`
|
|
// );
|
|
|
|
// set default issuer fee
|
|
let issuerFee = 0;
|
|
let channelFee = 0;
|
|
|
|
// get issuer fee dan channel fee dari table public.t_c_partner_fee
|
|
let getIssuer = await model.getIssuer(getPartner.i_partner, now);
|
|
|
|
if (getIssuer) {
|
|
// per tanggal 1 November 2023 fee sharing mulai menggunakan amount nominal dan bukan persentase lagi
|
|
if (today >= '2023-11-01') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// ======================= KHUSUS LRT JABODEBEK =============================
|
|
if (getPartner.partner_code == 'P05') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// SELAIN LRT JABODEBEK
|
|
issuerFee =
|
|
(getIssuer.i_issuer_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
channelFee =
|
|
(getIssuer.i_channel_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
}
|
|
}
|
|
}
|
|
// if (!getIssuer) {
|
|
// return res.status(200).json({
|
|
// code: "403",
|
|
// message: "issuer doesn't exist",
|
|
// data: {},
|
|
// });
|
|
// }
|
|
|
|
result = {
|
|
code: '00',
|
|
message: 'validation successfully.',
|
|
data: {
|
|
issuer_fee: issuerFee,
|
|
channel_fee: channelFee,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`[ RESPONSE RESERVATION-VALIDATE ] code=500 error_code=${logErrorCode(error)}`
|
|
);
|
|
|
|
return res.status(200).json({
|
|
code: '500',
|
|
message:
|
|
process.env.NODE_ENV != 'production'
|
|
? error.message
|
|
: '500 internal server error - backend server.',
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
const reservationValidateV2 = async (req, res) => {
|
|
try {
|
|
// generate unique code
|
|
// unique code ini digunakan untuk keperluan log agar mudah untuk dibaca
|
|
let uniqueCode = await helper.getUniqueCode();
|
|
let result = {};
|
|
|
|
// param
|
|
let { mid, tid, card_number, amount } = req.body;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ REQUEST RESERVATION-VALIDATE-V2 ]`
|
|
);
|
|
|
|
// trim card number
|
|
// card_number = card_number.trim();
|
|
|
|
// check validator
|
|
let err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
result = {
|
|
code: '400',
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
let cardBlacklist = await model.getCardBlacklist(card_number);
|
|
if (cardBlacklist) {
|
|
if (cardBlacklist.c_status == 'B') {
|
|
result = {
|
|
code: '445',
|
|
message: 'Card is Blacklisted',
|
|
data: {},
|
|
};
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// pengecekkan nomor kartu yang di request adalah kartu production atau bukan berdasarkan settingan di .ENV dengan constanta SERVER_FOR.
|
|
// Jika settingan .ENV SERVER_FOR adalah production dan nomor kartu KMT yang di request adalah kartu development maka sistem online topup akan menolaknya. Begitupun sebaliknya
|
|
// Begitupun sebaliknya, jika settingan .ENV SERVER_FOR adalah development dan nomor kartu KMT yang di request adalah kartu production maka sistem online topup akan menolaknya
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking production card number or not...`
|
|
// );
|
|
|
|
// jika settingan diperuntukan untuk production
|
|
// if (process.env.SERVER_FOR == 'production') {
|
|
// // kartu yang di request bukan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) != '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message: "It's not production card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ]: ${JSON.stringify(
|
|
// result
|
|
// )}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// } else {
|
|
// // jika settingan bukan diperuntukan untuk production, bisa development atau local atau yang lainnya dan kemudian kartu yang di request merupakan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) == '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message:
|
|
// "It's production card number. Please use the development card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ]: ${JSON.stringify(
|
|
// result
|
|
// )}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// }
|
|
|
|
// time
|
|
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
let today = moment().format('YYYY-MM-DD');
|
|
let year = moment().format('Y');
|
|
let month = moment().format('M');
|
|
|
|
// log debug
|
|
// proses pengecekkan data partner berdasarkan mid dan tid yang di request, apakah terdaftar di sistem CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking partner...`
|
|
// );
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartner(mid, tid);
|
|
|
|
// jika partner (mid atau tid) tidak ditemukan atau tidak terdaftar di sistem CMS
|
|
if (!getPartner) {
|
|
result = {
|
|
code: '412',
|
|
message: 'terminal id is not valid',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (getPartner.i_partner != req.partner_id_token) {
|
|
result = {
|
|
code: '412',
|
|
message: 'partner is not match',
|
|
data: {},
|
|
};
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// get settingan dari parameter apakah nomor kartu yang di request harus di cek dengan data card product CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] getting checking card number by card product CMS status on parameter...`
|
|
// );
|
|
|
|
// get parameter to check with card product cms or not
|
|
let getCheckByCardProductStatusValue =
|
|
await model.getCheckByCardProductStatusValue();
|
|
let checkByCardProductStatusValue =
|
|
getCheckByCardProductStatusValue?.c_value;
|
|
|
|
// jika settingan pengecekkan nomor kartu yang di request dengan data card product yang ada di sistem CMS aktif
|
|
if (checkByCardProductStatusValue == 'A') {
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking card number...`
|
|
// );
|
|
|
|
// check card number yang di request terdaftar di card product CMS atau tidak
|
|
let checkCardNumber = await model.checkCardNumber(card_number);
|
|
|
|
// jika nomor kartu yang di request tidak terdaftar di card prouct CMS
|
|
if (!checkCardNumber) {
|
|
result = {
|
|
code: '413',
|
|
message: 'card number is not registered',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking reservation amount...`
|
|
// );
|
|
|
|
// check reservation amount
|
|
// amount reservasi harus lebih dari 0
|
|
if (parseInt(amount) <= 0) {
|
|
result = {
|
|
code: '415',
|
|
message: 'amount must be greater than 0',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// proses pencarian saldo terakhir dari nomor kartu yang di request berdasarkan data log hitory
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] getting last balance on the card by card history...`
|
|
// );
|
|
|
|
// get last balance on the card
|
|
let balance = 0;
|
|
let getLastBalanceFromCardHistory =
|
|
await model.getLastBalanceFromCardHistory(card_number);
|
|
if (getLastBalanceFromCardHistory) {
|
|
balance = getLastBalanceFromCardHistory.m_balance_after;
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking BI regulation status...`
|
|
// );
|
|
|
|
// get settingan terkait pengecekkan regulasi BI dari parameter, apakah diaktifkan atau tidak
|
|
let getBIRegulationStatus = await model.getBIRegulationStatus();
|
|
|
|
// jika pengecekkan regulasi BI diaktifkan
|
|
if (getBIRegulationStatus.c_value == 'A') {
|
|
// log debug
|
|
// get maksimum saldo yang harus ada di dalam kartu berdasarkan settingan parameter di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking max balance on the card...`
|
|
// );
|
|
|
|
// get max saldo on the card
|
|
let getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard();
|
|
|
|
// jika settingan paremeter maksimum saldo di dalam kartu ditemukan
|
|
if (getMaxAmountOnTheCard.c_value) {
|
|
// jika saldo dalam kartu diakumulasikan dengan amount reservasi dan jumlah nya ternyata melebihi jumlah maksimum saldo berdasarkan settingan parameter, maka akan transaksi akan di tolak
|
|
if (
|
|
parseInt(balance) + parseInt(amount) >
|
|
parseInt(getMaxAmountOnTheCard.c_value)
|
|
) {
|
|
result = {
|
|
code: '416',
|
|
message: 'your card number has reached the maximum balance',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan akumulasi maksimum topup di bulan berjalan berdasarkan parameter setting di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking max topup per month...`
|
|
// );
|
|
|
|
// get accumulated balance in this month dari nomor kartu yang di request
|
|
let accumulatedBalance = 0;
|
|
let getAccumulationBalanceInThisMonthFromCardHistory =
|
|
await model.getAccumulationBalanceInThisMonthFromCardHistory(
|
|
card_number,
|
|
year,
|
|
month
|
|
);
|
|
|
|
// jika ditemukan history akumulasi topup dari nomor kartu yang di request, maka set akumulasi saldo di bulan berjalan sama dengan history yang ditemukan
|
|
if (getAccumulationBalanceInThisMonthFromCardHistory) {
|
|
accumulatedBalance =
|
|
getAccumulationBalanceInThisMonthFromCardHistory.accumulation_balance;
|
|
}
|
|
|
|
// validate max topup per month berdasarkan parameter setting
|
|
let getMaxTopupPerMonth = await model.getMaxTopupPerMonth();
|
|
|
|
// jika settingan parameter maksimum akumulasi topup dalam sebulan ditemukan
|
|
if (getMaxTopupPerMonth.c_value) {
|
|
// jika akumulasi saldo kartu yang di request dijumlahkan dengan amount reservasi dan ternyata melebihi jumlah akumulasi maksumum topup dalam sebelum, maka transaksi akan di tolak
|
|
if (
|
|
parseInt(accumulatedBalance) + parseInt(amount) >
|
|
parseInt(getMaxTopupPerMonth.c_value)
|
|
) {
|
|
result = {
|
|
code: '417',
|
|
message: 'your card number has been limit top up for this month',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan maksimum reservasi dengna status pending di parameter setting sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking max reservation...`
|
|
// );
|
|
|
|
// get maksimum reservation dengan status pending dari parameter setting
|
|
let getMaxReservation = await model.getMaxReservation();
|
|
|
|
// jika parameter setting maksimum reservasi dengan status pending tidak ditemukan maka set menjadi 0
|
|
let maxReservation = getMaxReservation.c_value
|
|
? parseInt(getMaxReservation.c_value)
|
|
: 0;
|
|
|
|
// log debug
|
|
// proses pencarian transaki dari table reservasi balance berdasarkan nomor kartu yang di request
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] checking total reservation pending...`
|
|
// );
|
|
|
|
// get total reservation pending bersarkan nomor kartu yang di request yaitu total transaksi yang status nya pending beserta jumlah amount nya
|
|
let getReservationPending = await model.getReservationPendingV2(
|
|
card_number,
|
|
now
|
|
);
|
|
|
|
let dataReservationPending = [];
|
|
let totalNominalReservationPending = 0;
|
|
if (getReservationPending.length > 0) {
|
|
for (let item of getReservationPending) {
|
|
// hitung total reservasi yang masih pending / belum di topup
|
|
totalNominalReservationPending =
|
|
totalNominalReservationPending + parseInt(item.i_reservation_amount);
|
|
|
|
dataReservationPending.push({
|
|
reservation_date: item.d_reservation,
|
|
amount: parseInt(item.i_reservation_amount),
|
|
partner_name: item.c_partner_name,
|
|
});
|
|
}
|
|
}
|
|
|
|
// jika nomor kartu yang di request ditemukan transaksi reservasi dengan status pending pada transaksi sebelumnya, maka jumlahkan total amount reservasi sebelumnya dengan amount reservasi yang dilakukan sekarang
|
|
let totalReservationPending =
|
|
totalNominalReservationPending > 0
|
|
? parseInt(totalNominalReservationPending) + parseInt(amount)
|
|
: parseInt(amount);
|
|
|
|
// jika total amount reservasi melebihi maksimum reservasi sesuai dengan parameter setting di sistem CMS maka transaksi akan di tolak
|
|
if (totalReservationPending > maxReservation) {
|
|
result = {
|
|
code: '421',
|
|
message:
|
|
'your card number has some pending transaction. Please finished it!',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// process mendapatkan persentase issuer fee dan channel free berdasarkan settingan dari sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] getting issuer fee and channel fee...`
|
|
// );
|
|
|
|
// set default issuer fee
|
|
let issuerFee = 0;
|
|
let channelFee = 0;
|
|
|
|
// get issuer fee dan channel fee dari table public.t_c_partner_fee
|
|
let getIssuer = await model.getIssuer(getPartner.i_partner, now);
|
|
|
|
if (getIssuer) {
|
|
// per tanggal 1 November 2023 fee sharing mulai menggunakan amount nominal dan bukan persentase lagi
|
|
if (today >= '2023-11-01') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// ======================= KHUSUS LRT JABODEBEK =============================
|
|
if (getPartner.partner_code == 'P05') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// SELAIN LRT JABODEBEK
|
|
issuerFee =
|
|
(getIssuer.i_issuer_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
channelFee =
|
|
(getIssuer.i_channel_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
// if (!getIssuer) {
|
|
// return res.status(200).json({
|
|
// code: "403",
|
|
// message: "issuer doesn't exist",
|
|
// data: {},
|
|
// });
|
|
// }
|
|
|
|
// log debug
|
|
// process enkripsi data reservasi dengan HMAC yang nanti nya akan di kirim ke end point reservation api sebagai pembanding bahwa data yang sudah di enkripsi tidak dimanipulasi
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] generating security code from data reservation`
|
|
// );
|
|
|
|
// create security code yang isinya data reservasi yang akan di compare nantinya saat melakukan insert reservation
|
|
let securityCode = await helper.createHMAC(
|
|
mid + '|' + tid + '|' + card_number + '|' + amount
|
|
);
|
|
|
|
let reservationData = {
|
|
mid: mid,
|
|
tid: tid,
|
|
card_number: card_number,
|
|
amount: amount,
|
|
transaction_date: now,
|
|
security_code: securityCode,
|
|
};
|
|
|
|
// log debug
|
|
// process enkripsi data reservasi dengan AES yang nanti nya akan di kirim ke end point reservation api untuk menyimpan data reservasi
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE-V2 ] encrypting data reservation: ${JSON.stringify(
|
|
// reservationData
|
|
// )}`
|
|
// );
|
|
|
|
// enkripsi data reservasi sebelum melakukan transaksi reservasi
|
|
let hash = helper.encryptText(JSON.stringify(reservationData));
|
|
|
|
result = {
|
|
code: '00',
|
|
message: 'validation successfully.',
|
|
data: {
|
|
reservation_data: hash,
|
|
reservation_detail: {
|
|
card_number: card_number,
|
|
amount: amount,
|
|
issuer_fee: issuerFee,
|
|
channel_fee: channelFee,
|
|
},
|
|
reservation_pending: dataReservationPending,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-VALIDATE-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`[ RESPONSE RESERVATION-VALIDATE-V2 ] code=500 error_code=${logErrorCode(error)}`
|
|
);
|
|
|
|
return res.status(200).json({
|
|
code: '500',
|
|
message:
|
|
process.env.NODE_ENV != 'production'
|
|
? error.message
|
|
: '500 internal server error - backend server.',
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
const reservation = async (req, res) => {
|
|
try {
|
|
// generate unique code
|
|
// unique code ini digunakan untuk keperluan log agar mudah untuk dibaca
|
|
let uniqueCode = await helper.getUniqueCode();
|
|
let result = {};
|
|
|
|
// param
|
|
let { mid, tid, card_number, amount, ref_no, customer_name, phone_number } =
|
|
req.body;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ REQUEST RESERVATION ]`
|
|
);
|
|
|
|
// trim card number
|
|
card_number = card_number.trim();
|
|
|
|
// check validator
|
|
let err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
result = {
|
|
code: '400',
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// pengecekkan nomor kartu yang di request adalah kartu production atau bukan berdasarkan settingan di .ENV dengan constanta SERVER_FOR.
|
|
// Jika settingan .ENV SERVER_FOR adalah production dan nomor kartu KMT yang di request adalah kartu development maka sistem online topup akan menolaknya. Begitupun sebaliknya
|
|
// Begitupun sebaliknya, jika settingan .ENV SERVER_FOR adalah development dan nomor kartu KMT yang di request adalah kartu production maka sistem online topup akan menolaknya
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking production card number or not...`
|
|
// );
|
|
|
|
let cardBlacklist = await model.getCardBlacklist(card_number);
|
|
if (cardBlacklist) {
|
|
if (cardBlacklist.c_status == 'B') {
|
|
result = {
|
|
code: '445',
|
|
message: 'Card is Blacklisted',
|
|
data: {},
|
|
};
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// jika settingan diperuntukan untuk production
|
|
// if (process.env.SERVER_FOR == 'production') {
|
|
// // kartu yang di request bukan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) != '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message: "It's not production card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION ]: ${JSON.stringify(result)}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// } else {
|
|
// // jika settingan bukan diperuntukan untuk production, bisa development atau local atau yang lainnya dan kemudian kartu yang di request merupakan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) == '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message:
|
|
// "It's production card number. Please use the development card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION ]: ${JSON.stringify(result)}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// }
|
|
|
|
// time
|
|
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
let today = moment().format('YYYY-MM-DD');
|
|
let year = moment().format('Y');
|
|
let month = moment().format('M');
|
|
|
|
// log debug
|
|
// proses pengecekkan data partner berdasarkan mid dan tid yang di request, apakah terdaftar di sistem CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] 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 RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (getPartner.i_partner != req.partner_id_token) {
|
|
result = {
|
|
code: '412',
|
|
message: 'partner is not match',
|
|
data: {},
|
|
};
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// get settingan dari parameter apakah nomor kartu yang di request harus di cek dengan data card product CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] getting checking card number by card product CMS status on parameter...`
|
|
// );
|
|
|
|
// get parameter to check with card product cms or not
|
|
let getCheckByCardProductStatusValue =
|
|
await model.getCheckByCardProductStatusValue();
|
|
let checkByCardProductStatusValue =
|
|
getCheckByCardProductStatusValue?.c_value;
|
|
|
|
// jika settingan pengecekkan nomor kartu yang di request dengan data card product yang ada di sistem CMS aktif
|
|
if (checkByCardProductStatusValue == 'A') {
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking card number...`
|
|
// );
|
|
|
|
// check card number yang di request terdaftar di card product CMS atau tidak
|
|
let checkCardNumber = await model.checkCardNumber(card_number);
|
|
if (!checkCardNumber) {
|
|
result = {
|
|
code: '413',
|
|
message: 'card number is not registered',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking reservation amount...`
|
|
// );
|
|
|
|
// check reservation amount
|
|
// amount reservasi harus lebih dari 0
|
|
if (parseInt(amount) <= 0) {
|
|
result = {
|
|
code: '415',
|
|
message: 'amount must be greater than 0',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// proses pencarian saldo terakhir dari nomor kartu yang di request berdasarkan data log hitory
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] getting last balance on the card by card history...`
|
|
// );
|
|
|
|
// get last balance on the card
|
|
let balance = 0;
|
|
let getLastBalanceFromCardHistory =
|
|
await model.getLastBalanceFromCardHistory(card_number);
|
|
if (getLastBalanceFromCardHistory) {
|
|
balance = getLastBalanceFromCardHistory.m_balance_after;
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking BI regulation status...`
|
|
// );
|
|
|
|
// get settingan terkait pengecekkan regulasi BI dari parameter, apakah diaktifkan atau tidak
|
|
let getBIRegulationStatus = await model.getBIRegulationStatus();
|
|
if (getBIRegulationStatus.c_value == 'A') {
|
|
// log debug
|
|
// get maksimum saldo yang harus ada di dalam kartu berdasarkan settingan parameter di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking max balance on the card...`
|
|
// );
|
|
|
|
// get max saldo on the card
|
|
let getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard();
|
|
// jika settingan paremeter maksimum saldo di dalam kartu ditemukan
|
|
if (getMaxAmountOnTheCard.c_value) {
|
|
// jika saldo dalam kartu diakumulasikan dengan amount reservasi dan jumlah nya ternyata melebihi jumlah maksimum saldo berdasarkan settingan parameter, maka akan transaksi akan di tolak
|
|
if (
|
|
parseInt(balance) + parseInt(amount) >
|
|
parseInt(getMaxAmountOnTheCard.c_value)
|
|
) {
|
|
result = {
|
|
code: '416',
|
|
message: 'your card number has reached the maximum balance',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan akumulasi maksimum topup di bulan berjalan berdasarkan parameter setting di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking max topup per month...`
|
|
// );
|
|
|
|
// get accumulated balance in this month dari nomor kartu yang di request
|
|
let accumulatedBalance = 0;
|
|
let getAccumulationBalanceInThisMonthFromCardHistory =
|
|
await model.getAccumulationBalanceInThisMonthFromCardHistory(
|
|
card_number,
|
|
year,
|
|
month
|
|
);
|
|
|
|
// jika ditemukan history akumulasi topup dari nomor kartu yang di request, maka set akumulasi saldo di bulan berjalan sama dengan history yang ditemukan
|
|
if (getAccumulationBalanceInThisMonthFromCardHistory) {
|
|
accumulatedBalance =
|
|
getAccumulationBalanceInThisMonthFromCardHistory.accumulation_balance;
|
|
}
|
|
|
|
// validate max topup per month berdasarkan parameter setting
|
|
let getMaxTopupPerMonth = await model.getMaxTopupPerMonth();
|
|
|
|
// jika settingan parameter maksimum akumulasi topup dalam sebulan ditemukan
|
|
if (getMaxTopupPerMonth.c_value) {
|
|
// jika akumulasi saldo kartu yang di request dijumlahkan dengan amount reservasi dan ternyata melebihi jumlah akumulasi maksumum topup dalam sebelum, maka transaksi akan di tolak
|
|
if (
|
|
parseInt(accumulatedBalance) + parseInt(amount) >
|
|
parseInt(getMaxTopupPerMonth.c_value)
|
|
) {
|
|
result = {
|
|
code: '417',
|
|
message: 'your card number has been limit top up for this month',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan maksimum reservasi dengna status pending di parameter setting sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking max reservation...`
|
|
// );
|
|
|
|
// get maksimum reservation dengan status pending dari parameter setting
|
|
let getMaxReservation = await model.getMaxReservation();
|
|
let maxReservation = getMaxReservation.c_value
|
|
? parseInt(getMaxReservation.c_value)
|
|
: 0;
|
|
|
|
// // log debug
|
|
// winston.logger.debug(`${uniqueCode} [ PROCESSING RESERVATION ] checking reservation with flag...`);
|
|
|
|
// // get reservation with flag
|
|
// let reservationWithFlag = await model.getReservationWithFlag(
|
|
// card_number,
|
|
// now
|
|
// );
|
|
// let totalReservationWithFlag =
|
|
// reservationWithFlag.total_reservation > 0
|
|
// ? reservationWithFlag.total_reservation
|
|
// : 0;
|
|
// if (totalReservationWithFlag > 0) {
|
|
// result = {
|
|
// code: "420",
|
|
// message:
|
|
// "your card number has incomplete transaction. Please unload your balance!",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION ]: ${JSON.stringify(result)}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
|
|
// log debug
|
|
// proses pencarian transaki dari table reservasi balance berdasarkan nomor kartu yang di request
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] checking total reservation pending...`
|
|
// );
|
|
|
|
// get total reservation pending bersarkan nomor kartu yang di request yaitu total transaksi yang status nya pending beserta jumlah amount nya
|
|
let getReservationPending = await model.getReservationPending(
|
|
card_number,
|
|
now
|
|
);
|
|
|
|
// jika nomor kartu yang di request ditemukan transaksi reservasi dengan status pending pada transaksi sebelumnya, maka jumlahkan total amount reservasi sebelumnya dengan amount reservasi yang dilakukan sekarang
|
|
let totalReservationPending =
|
|
getReservationPending.total_nominal_reservation > 0
|
|
? parseInt(getReservationPending.total_nominal_reservation) +
|
|
parseInt(amount)
|
|
: parseInt(amount);
|
|
|
|
// jika total amount reservasi melebihi maksimum reservasi sesuai dengan parameter setting di sistem CMS maka transaksi akan di tolak
|
|
if (totalReservationPending > maxReservation) {
|
|
result = {
|
|
code: '421',
|
|
message:
|
|
'your card number has some pending transaction. Please finished it!',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// process mendapatkan persentase issuer fee dan channel free berdasarkan settingan dari sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] getting issuer fee and channel fee...`
|
|
// );
|
|
|
|
// set default issuer fee
|
|
let issuerFee = 0;
|
|
let channelFee = 0;
|
|
|
|
// get issuer fee dan channel fee dari table public.t_c_partner_fee
|
|
let getIssuer = await model.getIssuer(getPartner.i_partner, now);
|
|
|
|
if (getIssuer) {
|
|
// per tanggal 1 November 2023 fee sharing mulai menggunakan amount nominal dan bukan persentase lagi
|
|
if (today >= '2023-11-01') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// ======================= KHUSUS LRT JABODEBEK =============================
|
|
if (getPartner.partner_code == 'P05') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// SELAIN LRT JABODEBEK
|
|
issuerFee =
|
|
(getIssuer.i_issuer_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
channelFee =
|
|
(getIssuer.i_channel_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
// if (!getIssuer) {
|
|
// return res.status(200).json({
|
|
// code: "403",
|
|
// message: "issuer doesn't exist",
|
|
// data: {},
|
|
// });
|
|
// }
|
|
|
|
// log debug
|
|
// proses pengecekkan value dari setting parameter untuk data reservasi yang tidak di load/topup apakah statusnya diaktifkan atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] getting reservation expiration date on parameter...`
|
|
// );
|
|
|
|
const { time, measure } = await model.getReservationExpiration();
|
|
let expired = moment(now).add(time, measure).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
// -------------------------------- NOTES --------------------------------
|
|
// for LRT JABO => customer_name diisi Device Name (versi penamaan LRT Jabo) dan phone_number diisi Device Code (versi kode LRT Jabo) sesuai MOM BI-WEEKLY MEETING (08/05/2023)
|
|
let customerName = customer_name;
|
|
let phoneNumber = phone_number;
|
|
|
|
if (!customerName || customerName == '' || customerName == '-') {
|
|
customerName = getPartner ? getPartner.terminal_name : '';
|
|
}
|
|
|
|
if (!phoneNumber || phoneNumber == '' || phoneNumber == '-') {
|
|
phoneNumber = mid + '_' + tid;
|
|
}
|
|
|
|
const { number: reservationNumber, sequence: reservationNoSequence } =
|
|
await model.allocateReservationNumber(getPartner.partner_code, moment(now).format('YYMMDD'));
|
|
|
|
// binding data
|
|
let data = {
|
|
c_reservation_no: reservationNumber,
|
|
d_reservation: now,
|
|
c_ref_no: ref_no,
|
|
i_partner: getPartner ? getPartner.i_partner : null,
|
|
c_partner_code: getPartner ? getPartner.partner_code : null,
|
|
c_partner_name: getPartner ? getPartner.partner_name : null,
|
|
i_merchant: getPartner ? getPartner.i_merchant : null,
|
|
c_merchant_code: getPartner ? getPartner.merchant_code : null,
|
|
c_merchant_name: getPartner ? getPartner.merchant_name : null,
|
|
c_mid: getPartner ? getPartner.mid : null,
|
|
i_terminal: getPartner ? getPartner.i_terminal : null,
|
|
c_terminal_code: getPartner ? getPartner.terminal_code : null,
|
|
c_terminal_name: getPartner ? getPartner.terminal_name : null,
|
|
c_tid: getPartner ? getPartner.tid : null,
|
|
c_card_number: card_number,
|
|
i_reservation_amount: amount,
|
|
// i_issuer: getIssuer ? getIssuer.i_issuer : null,
|
|
// c_issuer_code: getIssuer ? getIssuer.issuer_code : null,
|
|
// c_issuer_name: getIssuer ? getIssuer.issuer_name : null,
|
|
i_issuer_fee: issuerFee,
|
|
// i_channel: null,
|
|
// c_channel_code: null,
|
|
// c_channel_name: null,
|
|
i_channel_fee: channelFee,
|
|
c_payment_ref_no: null,
|
|
i_reservation_no_seq: reservationNoSequence,
|
|
d_expired: expired,
|
|
d_load_balance: null,
|
|
c_load_balance_no: null,
|
|
c_status: 'P',
|
|
c_status_name: 'PENDING',
|
|
d_create: now,
|
|
i_create: null,
|
|
c_create_username: null,
|
|
c_create_fullname: null,
|
|
d_modify: null,
|
|
i_modify: null,
|
|
c_modify_username: null,
|
|
c_modify_fullname: null,
|
|
d_delete: null,
|
|
i_delete: null,
|
|
c_delete_username: null,
|
|
c_delete_fullname: null,
|
|
c_flag: '-1',
|
|
c_customer_name: customerName,
|
|
c_phone_number: phoneNumber,
|
|
};
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] inserting reservation transaction...`
|
|
// );
|
|
|
|
// insert transaction
|
|
let insert = await model.insertReservation(data);
|
|
|
|
// jika proses insert data semua yang berkaitan dengan reservasi saldo mengalami kegagalan, maka lakukan rollback
|
|
// kegagalan insert data transaksi bisa disebabkan karena data ref no yang di berikan client sudah digunakan
|
|
if (insert.rowCount > 0) {
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION ] inserting log for card history...`
|
|
// );
|
|
|
|
// log card history
|
|
await model.createReserveBalanceLog({
|
|
i_log_name: 90,
|
|
d_log: now,
|
|
c_card_number: card_number,
|
|
c_source: 'CMS',
|
|
c_location: getPartner ? getPartner.partner_name : null,
|
|
m_balance_before: balance > 0 ? balance : 0,
|
|
m_amount: 0,
|
|
m_balance_after: balance > 0 ? balance : 0,
|
|
j_new_data: data,
|
|
j_old_data: null,
|
|
c_note: `RESERVED BALANCE ${amount} FROM ${getPartner ? getPartner.partner_name : null
|
|
} SUCCESSFULLY`,
|
|
c_status: 'S',
|
|
d_created: now,
|
|
i_create: null,
|
|
c_create_username: 'HSM',
|
|
c_create_fullname: 'HARDWARE SECURE MODULE',
|
|
c_category: 'T',
|
|
});
|
|
|
|
result = {
|
|
code: '00',
|
|
message: 'reservation saved successfully.',
|
|
data: {
|
|
reservation_number: reservationNumber,
|
|
reservation_time: now,
|
|
partner: getPartner ? getPartner.partner_name : null,
|
|
merchant: getPartner ? getPartner.merchant_name : null,
|
|
terminal: getPartner ? getPartner.terminal_name : null,
|
|
card_number: card_number,
|
|
reserved_amount: amount,
|
|
issuer_fee: issuerFee,
|
|
channel_fee: channelFee,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
} else {
|
|
result = {
|
|
code: '424',
|
|
message: 'refno is duplicate',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`[ RESPONSE RESERVATION ] code=500 error_code=${logErrorCode(error)}`
|
|
);
|
|
|
|
return res.status(200).json({
|
|
code: '500',
|
|
message:
|
|
process.env.NODE_ENV != 'production'
|
|
? error.message
|
|
: '500 internal server error - backend server.',
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
const reservationV2 = async (req, res) => {
|
|
let stage = 'validate_request';
|
|
try {
|
|
// generate unique code
|
|
// unique code ini digunakan untuk keperluan log agar mudah untuk dibaca
|
|
let uniqueCode = await helper.getUniqueCode();
|
|
let result = {};
|
|
|
|
// param
|
|
let {
|
|
reservation_data,
|
|
ref_no,
|
|
customer_name,
|
|
phone_number,
|
|
amount,
|
|
card_number,
|
|
} = req.body;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ REQUEST RESERVATION-V2 ]`
|
|
);
|
|
|
|
// trim card number
|
|
// card_number = card_number.trim();
|
|
|
|
// check validator
|
|
let err = validationResult(req, res);
|
|
if (!err.isEmpty()) {
|
|
result = {
|
|
code: '400',
|
|
message: err.errors[0].msg,
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// pengecekkan reservation data yang di request
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking reservation data...`
|
|
// );
|
|
|
|
stage = 'decode_reservation_data';
|
|
let reservationData;
|
|
try {
|
|
const plaintext = helper.decryptText(reservation_data);
|
|
if (typeof plaintext === 'string' && plaintext) reservationData = JSON.parse(plaintext);
|
|
} catch {
|
|
// Muatan yang gagal didekripsi atau diurai ditolak sebagai input yang tidak valid.
|
|
}
|
|
|
|
if (!reservationData || typeof reservationData !== 'object' || Array.isArray(reservationData)) {
|
|
result = {
|
|
code: '435',
|
|
message: 'bad request. reservation data is invalid.',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (
|
|
!reservationData.mid ||
|
|
!reservationData.tid ||
|
|
!reservationData.card_number ||
|
|
!reservationData.amount ||
|
|
!reservationData.transaction_date
|
|
) {
|
|
result = {
|
|
code: '436',
|
|
message: 'bad request. reservation data is empty.',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (card_number) {
|
|
if (reservationData.card_number != card_number) {
|
|
result = {
|
|
code: '441',
|
|
message:
|
|
'bad request. card number requested is different with reservation data.',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
stage = 'check_card_blacklist';
|
|
let cardBlacklist = await model.getCardBlacklist(
|
|
reservationData.card_number
|
|
);
|
|
if (cardBlacklist) {
|
|
if (cardBlacklist.c_status == 'B') {
|
|
result = {
|
|
code: '445',
|
|
message: 'Card is Blacklisted',
|
|
data: {},
|
|
};
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
if (amount) {
|
|
if (reservationData.amount != amount) {
|
|
result = {
|
|
code: '442',
|
|
message:
|
|
'bad request. amount requested is different with reservation data.',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// extract reseravtion data
|
|
let mid = reservationData.mid;
|
|
let tid = reservationData.tid;
|
|
card_number = reservationData.card_number;
|
|
amount = reservationData.amount;
|
|
let transactionDate = reservationData.transaction_date;
|
|
let securityCode = reservationData.security_code;
|
|
|
|
stage = 'verify_reservation_data';
|
|
if (securityCode) {
|
|
// log debug
|
|
// process enkripsi data reservasi dengan HMAC sebagai pembanding bahwa data yang di decrypt valid
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] comparing security code with decrypt data...}`
|
|
// );
|
|
|
|
// create security code yang isinya data reservasi yang akan di compare nantinya saat melakukan insert reservation
|
|
let securityCodeCompare = await helper.createHMAC(
|
|
mid + '|' + tid + '|' + card_number + '|' + amount
|
|
);
|
|
|
|
if (securityCode !== securityCodeCompare) {
|
|
result = {
|
|
code: '437',
|
|
message: 'bad request. reservation data has been changed.',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// pengecekkan reservation data yang di request
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] extracting reservation data : ${JSON.stringify(
|
|
// {
|
|
// mid: mid,
|
|
// tid: tid,
|
|
// card_number: card_number,
|
|
// amount: amount,
|
|
// transaction_date: transactionDate,
|
|
// security_code: securityCode,
|
|
// }
|
|
// )}`
|
|
// );
|
|
|
|
// log debug
|
|
// pengecekkan nomor kartu yang di request adalah kartu production atau bukan berdasarkan settingan di .ENV dengan constanta SERVER_FOR.
|
|
// Jika settingan .ENV SERVER_FOR adalah production dan nomor kartu KMT yang di request adalah kartu development maka sistem online topup akan menolaknya. Begitupun sebaliknya
|
|
// Begitupun sebaliknya, jika settingan .ENV SERVER_FOR adalah development dan nomor kartu KMT yang di request adalah kartu production maka sistem online topup akan menolaknya
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking production card number or not...`
|
|
// );
|
|
|
|
// jika settingan diperuntukan untuk production
|
|
// if (process.env.SERVER_FOR == 'production') {
|
|
// // kartu yang di request bukan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) != '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message: "It's not production card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION-V2 ]: ${JSON.stringify(result)}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// } else {
|
|
// // jika settingan bukan diperuntukan untuk production, bisa development atau local atau yang lainnya dan kemudian kartu yang di request merupakan kartu production maka akan di tolak
|
|
// if (card_number.substring(0, 1) == '1') {
|
|
// result = {
|
|
// code: '400',
|
|
// message:
|
|
// "It's production card number. Please use the development card number.",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION-V2 ]: ${JSON.stringify(result)}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
// }
|
|
|
|
// time
|
|
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
let today = moment().format('YYYY-MM-DD');
|
|
let year = moment().format('Y');
|
|
let month = moment().format('M');
|
|
|
|
// log debug
|
|
// proses pengecekkan data partner berdasarkan mid dan tid yang di request, apakah terdaftar di sistem CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking partner...`
|
|
// );
|
|
|
|
// get partner, merchant, terminal from tid
|
|
stage = 'lookup_partner';
|
|
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 RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (getPartner.i_partner != req.partner_id_token) {
|
|
result = {
|
|
code: '412',
|
|
message: 'partner is not match',
|
|
data: {},
|
|
};
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// get settingan dari parameter apakah nomor kartu yang di request harus di cek dengan data card product CMS atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-VALIDATE ] getting checking card number by card product CMS status on parameter...`
|
|
// );
|
|
|
|
// get parameter to check with card product cms or not
|
|
stage = 'check_reservation_limits';
|
|
let getCheckByCardProductStatusValue =
|
|
await model.getCheckByCardProductStatusValue();
|
|
let checkByCardProductStatusValue =
|
|
getCheckByCardProductStatusValue?.c_value;
|
|
|
|
// jika settingan pengecekkan nomor kartu yang di request dengan data card product yang ada di sistem CMS aktif
|
|
if (checkByCardProductStatusValue == 'A') {
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking card number...`
|
|
// );
|
|
|
|
// check card number yang di request terdaftar di card product CMS atau tidak
|
|
let checkCardNumber = await model.checkCardNumber(card_number);
|
|
if (!checkCardNumber) {
|
|
result = {
|
|
code: '413',
|
|
message: 'card number is not registered',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking reservation amount...`
|
|
// );
|
|
|
|
// check reservation amount
|
|
// amount reservasi harus lebih dari 0
|
|
if (parseInt(amount) <= 0) {
|
|
result = {
|
|
code: '415',
|
|
message: 'amount must be greater than 0',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// proses pencarian saldo terakhir dari nomor kartu yang di request berdasarkan data log hitory
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] getting last balance on the card by card history...`
|
|
// );
|
|
|
|
// get last balance on the card
|
|
let balance = 0;
|
|
let getLastBalanceFromCardHistory =
|
|
await model.getLastBalanceFromCardHistory(card_number);
|
|
if (getLastBalanceFromCardHistory) {
|
|
balance = getLastBalanceFromCardHistory.m_balance_after;
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking BI regulation status...`
|
|
// );
|
|
|
|
// get settingan terkait pengecekkan regulasi BI dari parameter, apakah diaktifkan atau tidak
|
|
let getBIRegulationStatus = await model.getBIRegulationStatus();
|
|
if (getBIRegulationStatus.c_value == 'A') {
|
|
// log debug
|
|
// get maksimum saldo yang harus ada di dalam kartu berdasarkan settingan parameter di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking max balance on the card...`
|
|
// );
|
|
|
|
// get max saldo on the card
|
|
let getMaxAmountOnTheCard = await model.getMaxAmountOnTheCard();
|
|
// jika settingan paremeter maksimum saldo di dalam kartu ditemukan
|
|
if (getMaxAmountOnTheCard.c_value) {
|
|
// jika saldo dalam kartu diakumulasikan dengan amount reservasi dan jumlah nya ternyata melebihi jumlah maksimum saldo berdasarkan settingan parameter, maka akan transaksi akan di tolak
|
|
if (
|
|
parseInt(balance) + parseInt(amount) >
|
|
parseInt(getMaxAmountOnTheCard.c_value)
|
|
) {
|
|
result = {
|
|
code: '416',
|
|
message: 'your card number has reached the maximum balance',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan akumulasi maksimum topup di bulan berjalan berdasarkan parameter setting di sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking max topup per month...`
|
|
// );
|
|
|
|
// get accumulated balance in this month dari nomor kartu yang di request
|
|
let accumulatedBalance = 0;
|
|
let getAccumulationBalanceInThisMonthFromCardHistory =
|
|
await model.getAccumulationBalanceInThisMonthFromCardHistory(
|
|
card_number,
|
|
year,
|
|
month
|
|
);
|
|
|
|
// jika ditemukan history akumulasi topup dari nomor kartu yang di request, maka set akumulasi saldo di bulan berjalan sama dengan history yang ditemukan
|
|
if (getAccumulationBalanceInThisMonthFromCardHistory) {
|
|
accumulatedBalance =
|
|
getAccumulationBalanceInThisMonthFromCardHistory.accumulation_balance;
|
|
}
|
|
|
|
// validate max topup per month berdasarkan parameter setting
|
|
let getMaxTopupPerMonth = await model.getMaxTopupPerMonth();
|
|
|
|
// jika settingan parameter maksimum akumulasi topup dalam sebulan ditemukan
|
|
if (getMaxTopupPerMonth.c_value) {
|
|
// jika akumulasi saldo kartu yang di request dijumlahkan dengan amount reservasi dan ternyata melebihi jumlah akumulasi maksumum topup dalam sebelum, maka transaksi akan di tolak
|
|
if (
|
|
parseInt(accumulatedBalance) + parseInt(amount) >
|
|
parseInt(getMaxTopupPerMonth.c_value)
|
|
) {
|
|
result = {
|
|
code: '417',
|
|
message: 'your card number has been limit top up for this month',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
// log debug
|
|
// proses pengecekkan maksimum reservasi dengna status pending di parameter setting sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking max reservation...`
|
|
// );
|
|
|
|
// get maksimum reservation dengan status pending dari parameter setting
|
|
let getMaxReservation = await model.getMaxReservation();
|
|
let maxReservation = getMaxReservation.c_value
|
|
? parseInt(getMaxReservation.c_value)
|
|
: 0;
|
|
|
|
// // log debug
|
|
// winston.logger.debug(`${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking reservation with flag...`);
|
|
|
|
// // get reservation with flag
|
|
// let reservationWithFlag = await model.getReservationWithFlag(
|
|
// card_number,
|
|
// now
|
|
// );
|
|
// let totalReservationWithFlag =
|
|
// reservationWithFlag.total_reservation > 0
|
|
// ? reservationWithFlag.total_reservation
|
|
// : 0;
|
|
// if (totalReservationWithFlag > 0) {
|
|
// result = {
|
|
// code: "420",
|
|
// message:
|
|
// "your card number has incomplete transaction. Please unload your balance!",
|
|
// data: {},
|
|
// };
|
|
|
|
// // log warn
|
|
// winston.logger.warn(
|
|
// `${uniqueCode} [ RESPONSE RESERVATION-V2 ]: ${JSON.stringify(result)}`
|
|
// );
|
|
|
|
// return res.status(200).json(result);
|
|
// }
|
|
|
|
// log debug
|
|
// proses pencarian transaki dari table reservasi balance berdasarkan nomor kartu yang di request
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] checking total reservation pending...`
|
|
// );
|
|
|
|
// get total reservation pending bersarkan nomor kartu yang di request yaitu total transaksi yang status nya pending beserta jumlah amount nya
|
|
let getReservationPending = await model.getReservationPendingV2(
|
|
card_number,
|
|
now
|
|
);
|
|
|
|
let dataReservationPending = [];
|
|
let totalNominalReservationPending = 0;
|
|
if (getReservationPending.length > 0) {
|
|
for (let item of getReservationPending) {
|
|
// hitung total reservasi yang masih pending / belum di topup
|
|
totalNominalReservationPending =
|
|
totalNominalReservationPending + parseInt(item.i_reservation_amount);
|
|
|
|
dataReservationPending.push({
|
|
reservation_date: item.d_reservation,
|
|
amount: parseInt(item.i_reservation_amount),
|
|
partner_name: item.c_partner_name,
|
|
});
|
|
}
|
|
}
|
|
|
|
// jika nomor kartu yang di request ditemukan transaksi reservasi dengan status pending pada transaksi sebelumnya, maka jumlahkan total amount reservasi sebelumnya dengan amount reservasi yang dilakukan sekarang
|
|
let totalReservationPending =
|
|
totalNominalReservationPending > 0
|
|
? parseInt(totalNominalReservationPending) + parseInt(amount)
|
|
: parseInt(amount);
|
|
|
|
// jika total amount reservasi melebihi maksimum reservasi sesuai dengan parameter setting di sistem CMS maka transaksi akan di tolak
|
|
if (totalReservationPending > maxReservation) {
|
|
result = {
|
|
code: '421',
|
|
message:
|
|
'your card number has some pending transaction. Please finished it!',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// process mendapatkan persentase issuer fee dan channel free berdasarkan settingan dari sistem CMS
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] getting issuer fee and channel fee...`
|
|
// );
|
|
|
|
// set default issuer fee
|
|
let issuerFee = 0;
|
|
let channelFee = 0;
|
|
|
|
// get issuer fee dan channel fee dari table public.t_c_partner_fee
|
|
stage = 'lookup_issuer_fee';
|
|
let getIssuer = await model.getIssuer(getPartner.i_partner, now);
|
|
|
|
if (getIssuer) {
|
|
// per tanggal 1 November 2023 fee sharing mulai menggunakan amount nominal dan bukan persentase lagi
|
|
if (today >= '2023-11-01') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// ======================= KHUSUS LRT JABODEBEK =============================
|
|
if (getPartner.partner_code == 'P05') {
|
|
issuerFee = getIssuer.i_issuer_fee_nominal;
|
|
channelFee = getIssuer.i_channel_fee_nominal;
|
|
} else {
|
|
// SELAIN LRT JABODEBEK
|
|
issuerFee =
|
|
(getIssuer.i_issuer_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
channelFee =
|
|
(getIssuer.i_channel_fee_perc / 100) * parseInt(getIssuer.i_amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
// if (!getIssuer) {
|
|
// return res.status(200).json({
|
|
// code: "403",
|
|
// message: "issuer doesn't exist",
|
|
// data: {},
|
|
// });
|
|
// }
|
|
|
|
// log debug
|
|
// proses pengecekkan value dari setting parameter untuk data reservasi yang tidak di load/topup apakah statusnya diaktifkan atau tidak
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] getting reservation expiration date on parameter...`
|
|
// );
|
|
|
|
stage = 'lookup_reservation_expiration';
|
|
const { time, measure } = await model.getReservationExpiration();
|
|
let expired = moment(now).add(time, measure).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
// -------------------------------- NOTES --------------------------------
|
|
// for LRT JABO => customer_name diisi Device Name (versi penamaan LRT Jabo) dan phone_number diisi Device Code (versi kode LRT Jabo) sesuai MOM BI-WEEKLY MEETING (08/05/2023)
|
|
let customerName = customer_name;
|
|
let phoneNumber = phone_number;
|
|
|
|
if (!customerName || customerName == '' || customerName == '-') {
|
|
customerName = getPartner ? getPartner.terminal_name : '';
|
|
}
|
|
|
|
if (!phoneNumber || phoneNumber == '' || phoneNumber == '-') {
|
|
phoneNumber = mid + '_' + tid;
|
|
}
|
|
|
|
stage = 'allocate_reservation_number';
|
|
const { number: reservationNumber, sequence: reservationNoSequence } =
|
|
await model.allocateReservationNumber(getPartner.partner_code, moment(now).format('YYMMDD'));
|
|
|
|
// binding data
|
|
let data = {
|
|
c_reservation_no: reservationNumber,
|
|
d_reservation: now,
|
|
c_ref_no: ref_no,
|
|
i_partner: getPartner ? getPartner.i_partner : null,
|
|
c_partner_code: getPartner ? getPartner.partner_code : null,
|
|
c_partner_name: getPartner ? getPartner.partner_name : null,
|
|
i_merchant: getPartner ? getPartner.i_merchant : null,
|
|
c_merchant_code: getPartner ? getPartner.merchant_code : null,
|
|
c_merchant_name: getPartner ? getPartner.merchant_name : null,
|
|
c_mid: getPartner ? getPartner.mid : null,
|
|
i_terminal: getPartner ? getPartner.i_terminal : null,
|
|
c_terminal_code: getPartner ? getPartner.terminal_code : null,
|
|
c_terminal_name: getPartner ? getPartner.terminal_name : null,
|
|
c_tid: getPartner ? getPartner.tid : null,
|
|
c_card_number: card_number,
|
|
i_reservation_amount: amount,
|
|
// i_issuer: getIssuer ? getIssuer.i_issuer : null,
|
|
// c_issuer_code: getIssuer ? getIssuer.issuer_code : null,
|
|
// c_issuer_name: getIssuer ? getIssuer.issuer_name : null,
|
|
i_issuer_fee: issuerFee,
|
|
// i_channel: null,
|
|
// c_channel_code: null,
|
|
// c_channel_name: null,
|
|
i_channel_fee: channelFee,
|
|
c_payment_ref_no: null,
|
|
i_reservation_no_seq: reservationNoSequence,
|
|
d_expired: expired,
|
|
d_load_balance: null,
|
|
c_load_balance_no: null,
|
|
c_status: 'P',
|
|
c_status_name: 'PENDING',
|
|
d_create: now,
|
|
i_create: null,
|
|
c_create_username: null,
|
|
c_create_fullname: null,
|
|
d_modify: null,
|
|
i_modify: null,
|
|
c_modify_username: null,
|
|
c_modify_fullname: null,
|
|
d_delete: null,
|
|
i_delete: null,
|
|
c_delete_username: null,
|
|
c_delete_fullname: null,
|
|
c_flag: '-1',
|
|
c_customer_name: customerName,
|
|
c_phone_number: phoneNumber,
|
|
};
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] inserting reservation transaction...`
|
|
// );
|
|
|
|
// (belum) ada issue disini, jadi ada case reservation number nya sama, terus error query harusnya ada handle response message nya, yg sekarang malah masuk ke blok reservation saved successfully.
|
|
|
|
// insert transaction
|
|
stage = 'insert_reservation';
|
|
let insert = await model.insertReservation(data);
|
|
|
|
// jika proses insert data semua yang berkaitan dengan reservasi saldo mengalami kegagalan, maka lakukan rollback
|
|
// kegagalan insert data transaksi bisa disebabkan karena data ref no yang di berikan client sudah digunakan
|
|
if (insert.rowCount > 0) {
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING RESERVATION-V2 ] inserting log for card history...`
|
|
// );
|
|
|
|
// log card history
|
|
stage = 'write_reservation_history';
|
|
await model.createReserveBalanceLog({
|
|
i_log_name: 90,
|
|
d_log: now,
|
|
c_card_number: card_number,
|
|
c_source: 'CMS',
|
|
c_location: getPartner ? getPartner.partner_name : null,
|
|
m_balance_before: balance > 0 ? balance : 0,
|
|
m_amount: 0,
|
|
m_balance_after: balance > 0 ? balance : 0,
|
|
j_new_data: data,
|
|
j_old_data: null,
|
|
c_note: `RESERVED BALANCE ${amount} FROM ${getPartner ? getPartner.partner_name : null
|
|
} SUCCESSFULLY`,
|
|
c_status: 'S',
|
|
d_created: now,
|
|
i_create: null,
|
|
c_create_username: 'HSM',
|
|
c_create_fullname: 'HARDWARE SECURE MODULE',
|
|
c_category: 'T',
|
|
});
|
|
|
|
result = {
|
|
code: '00',
|
|
message: 'reservation saved successfully.',
|
|
data: {
|
|
reservation_number: reservationNumber,
|
|
reservation_time: now,
|
|
partner: getPartner ? getPartner.partner_name : null,
|
|
merchant: getPartner ? getPartner.merchant_name : null,
|
|
terminal: getPartner ? getPartner.terminal_name : null,
|
|
card_number: card_number,
|
|
reserved_amount: amount,
|
|
issuer_fee: issuerFee,
|
|
channel_fee: channelFee,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
} else {
|
|
result = {
|
|
code: '424',
|
|
message: 'refno is duplicate',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE RESERVATION-V2 ] code=${logStatusCode(result?.code)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
} catch (error) {
|
|
// Simpan tahap dan kode aman tersedia di audit tanpa menyalin exception mentah.
|
|
const errorCode = logErrorCode(error);
|
|
res.locals.reservationFailure = { stage, code: errorCode };
|
|
winston.logger.error(
|
|
`[ RESPONSE RESERVATION-V2 ] code=500 error_code=${errorCode} error_stage=${stage}`
|
|
);
|
|
|
|
return res.status(200).json({
|
|
code: '500',
|
|
message:
|
|
process.env.NODE_ENV != 'production'
|
|
? error.message
|
|
: '500 internal server error - backend server.',
|
|
data: {},
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = { reservationValidate, reservationValidateV2, reservation, reservationV2 };
|