mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
215 lines
5.4 KiB
JavaScript
215 lines
5.4 KiB
JavaScript
const { param, body, validationResult } = require('express-validator');
|
|
const helper = require('../../helpers/helper');
|
|
const winston = require('../../helpers/winston.logger');
|
|
const model = require('./refundkmt.model');
|
|
const moment = require('moment');
|
|
moment.locale('id');
|
|
|
|
// VALIDATION
|
|
const validate = (method) => {
|
|
switch (method) {
|
|
case 'callbackOy':
|
|
return [
|
|
body('partner_trx_id')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Partner Trx Id is required'),
|
|
body('trx_id').not().isEmpty().withMessage('Trx Id is required'),
|
|
body('amount').not().isEmpty().withMessage('Amount is required'),
|
|
body('recipient_bank')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Recipient Bank is required'),
|
|
body('recipient_account')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Recipient Account is required'),
|
|
];
|
|
default:
|
|
break;
|
|
}
|
|
};
|
|
|
|
const callbackOy = async (req, res) => {
|
|
try {
|
|
// generate unique code
|
|
// unique code ini digunakan untuk keperluan log agar mudah untuk dibaca
|
|
let uniqueCode = helper.getUniqueCode();
|
|
let result = {};
|
|
|
|
// param
|
|
let {
|
|
status,
|
|
amount,
|
|
recipient_name,
|
|
recipient_bank,
|
|
recipient_account,
|
|
trx_id,
|
|
partner_trx_id,
|
|
timestamp,
|
|
created_date,
|
|
last_updated_date,
|
|
} = req.body;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ REQUEST REFUND-KMT ]: ${JSON.stringify(req.body)}`
|
|
);
|
|
|
|
// 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 REFUND-KMT ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// time
|
|
let now = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE REFUND-KMT ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
let recipient_bank_code = recipient_bank.toLowerCase();
|
|
// check trx_id yang di request match
|
|
let checkRequest = await model.checkRedeem(
|
|
partner_trx_id,
|
|
trx_id,
|
|
amount,
|
|
recipient_bank_code,
|
|
recipient_account
|
|
);
|
|
|
|
if (!checkRequest) {
|
|
result = {
|
|
code: '400',
|
|
message: 'Refund Request Transaction is not found.',
|
|
data: {},
|
|
};
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE REFUND-KMT ]: ${JSON.stringify(result)}`
|
|
);
|
|
return res.status(200).json(result);
|
|
} else if (checkRequest.c_status?.trim() == 'A') {
|
|
result = {
|
|
code: '400',
|
|
message: 'Refund transaction is already completed.',
|
|
data: {},
|
|
};
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE REFUND-KMT ]: ${JSON.stringify(result)}`
|
|
);
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ checkRequest ]: ${JSON.stringify(checkRequest)}`
|
|
);
|
|
|
|
if (status.code != '000') {
|
|
result = {
|
|
code: '400',
|
|
message: status.message,
|
|
data: {
|
|
trx_id: trx_id,
|
|
partner_trx_id: partner_trx_id,
|
|
amount: amount,
|
|
recipient_bank: recipient_bank_code,
|
|
recipient_account: recipient_account,
|
|
},
|
|
};
|
|
}
|
|
|
|
const unixTimeZero = Date.parse(last_updated_date);
|
|
const date_reimbursement = moment(unixTimeZero).format(
|
|
'YYYY-MM-DD HH:mm:ss'
|
|
);
|
|
|
|
//Update Card Replacement
|
|
await model.updateReplacementComplete(
|
|
checkRequest.i_card_replacement,
|
|
checkRequest.c_request_no,
|
|
date_reimbursement
|
|
);
|
|
|
|
//Notif Email
|
|
let urlCMS = '';
|
|
if (process.env.SERVER_FOR == 'production') {
|
|
urlCMS = 'https://cms.kci.id';
|
|
} else if (process.env.SERVER_FOR == 'development') {
|
|
urlCMS = 'http://10.20.14.69';
|
|
} else {
|
|
urlCMS = 'http://localhost:8000';
|
|
}
|
|
|
|
let axios = require('axios');
|
|
|
|
const url = urlCMS + '/api/redeem/notifemail';
|
|
console.log('urlCMS', url);
|
|
|
|
// let resp = await axios.post(url, body, config);
|
|
const responseMail = await axios({
|
|
method: 'post',
|
|
url: url,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
data: {
|
|
i_card_replacement: checkRequest.i_card_replacement?.trim(),
|
|
c_request_no: checkRequest.c_request_no?.trim(),
|
|
c_card_no: checkRequest.c_card_no?.trim(),
|
|
status: 'complete',
|
|
},
|
|
});
|
|
console.log('urlCMS', url);
|
|
|
|
result = {
|
|
code: '00',
|
|
message: 'success',
|
|
data: {
|
|
trx_id: trx_id,
|
|
partner_trx_id: partner_trx_id,
|
|
amount: amount,
|
|
recipient_bank: recipient_bank,
|
|
recipient_account: recipient_account,
|
|
last_updated_date: last_updated_date,
|
|
},
|
|
};
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ RESPONSE REFUND-KMT ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`[ RESPONSE REFUND-KMT ]: 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,
|
|
callbackOy,
|
|
};
|