mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 10:56:27 +00:00
464 lines
13 KiB
JavaScript
464 lines
13 KiB
JavaScript
const { param, body, validationResult } = require('express-validator');
|
|
const helper = require('../../helpers/helper');
|
|
const winston = require('../../helpers/winston.logger');
|
|
const model = require('./paymentrecon.model');
|
|
const queue = require('../../helpers/queue');
|
|
const moment = require('moment');
|
|
const { default: axios } = require('axios');
|
|
const { allocatePaymentReconNumber } = require('./payment-recon-number.service');
|
|
moment.locale('id');
|
|
|
|
var dataReconQueue = [];
|
|
var isProccessingRecon = false;
|
|
|
|
// VALIDATION
|
|
const validate = (method) => {
|
|
switch (method) {
|
|
case 'paymentRecon':
|
|
return [
|
|
body('date')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Date is required')
|
|
.isDate({ format: 'YYYY-MM-DD' })
|
|
.withMessage('Invalid format date. Format is YYYY-MM-DD'),
|
|
body('reservation_number')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Reservation Number is required'),
|
|
body('bank_ref').not().isEmpty().withMessage('Bank Ref is required'),
|
|
body('amount')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Amount is required')
|
|
.isNumeric()
|
|
.withMessage('Amount is invalid format'),
|
|
];
|
|
case 'paymentReconSemiTopupOnline':
|
|
return [
|
|
body('date')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Date is required')
|
|
.isDate({ format: 'YYYY-MM-DD' })
|
|
.withMessage('Invalid format date. Format is YYYY-MM-DD'),
|
|
body('settle_raw')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Settle raw is required'),
|
|
body('amount')
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage('Amount is required')
|
|
.isNumeric()
|
|
.withMessage('Amount is invalid format'),
|
|
];
|
|
case 'paymentReconBulk':
|
|
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 RECON =================================================================
|
|
// *Created By: Mohamad Ihsan*
|
|
// *Description:*
|
|
// proses yang digunakan untuk melaporkan data transaksi topup online dari partner ke sistem CMS sebagai data rekonsialisasi
|
|
|
|
function alreadyReconciledResponse(reconciliation) {
|
|
return {
|
|
code: '04',
|
|
message: `success. Transaction has been recon at ${reconciliation.d_payment_recon}`,
|
|
data: {
|
|
payment_recon_no: reconciliation.c_payment_recon_no,
|
|
recon_date: reconciliation.d_payment_recon,
|
|
},
|
|
};
|
|
}
|
|
|
|
const paymentRecon = 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 { date, bank_ref, reservation_number, amount } = req.body;
|
|
let partner_id = req.partner_id_token;
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ REQUEST PAYMENT-RECON ]: ${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 PAYMENT-RECON ]: ${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} [ PROCESSING PAYMENT-RECON ]: checking partner...`
|
|
// );
|
|
|
|
// get partner, merchant, terminal from tid
|
|
let getPartner = await model.getPartnerByID(partner_id);
|
|
if (!getPartner) {
|
|
result = {
|
|
code: '426',
|
|
message: "Partner doesn't exists.",
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// check payment recon status is enable
|
|
if (getPartner.b_payment_recon != true) {
|
|
result = {
|
|
code: '444',
|
|
message: 'Partner unable to receive payment reconciliations',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// check partner reservation number yang di request sama dengan partner yang di token
|
|
let checkReservation = await model.checkReservation(reservation_number);
|
|
|
|
if (!checkReservation) {
|
|
result = {
|
|
code: '400',
|
|
message: 'Reservation number does not exist.',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
if (checkReservation.i_partner != partner_id) {
|
|
result = {
|
|
code: '443',
|
|
message:
|
|
'Partner for payment reconciliation and transaction does not match',
|
|
data: {},
|
|
};
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// check reservation number yang di request sudah berhasil di recon atau belum sebelumnnya
|
|
let checkReservationNumberAndAmountAlreadyRecon =
|
|
await model.checkReservationNumberAndAmountAlreadyRecon(
|
|
reservation_number
|
|
);
|
|
|
|
// jika reservastion number yang di request sudah berhasil di recon sebelumnya
|
|
if (checkReservationNumberAndAmountAlreadyRecon) {
|
|
result = alreadyReconciledResponse(checkReservationNumberAndAmountAlreadyRecon);
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
const partnerCode = getPartner.partner_code;
|
|
const PaymentReconNumber = await allocatePaymentReconNumber(partnerCode, now);
|
|
|
|
if (PaymentReconNumber === 'XX') {
|
|
result = {
|
|
code: '500',
|
|
message: 'server is busy. please try again.',
|
|
data: {},
|
|
};
|
|
|
|
// log error
|
|
winston.logger.error(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING PAYMENT-RECON ]: checking reservation status has been cancelled or not...`
|
|
// );
|
|
|
|
// check reservation number yang di request apakah masih aktif atau sudah di cancel
|
|
let checkReservationCanceledStatus =
|
|
await model.checkReservationCanceledStatus(reservation_number);
|
|
|
|
// jika reservasi number yang di request status nya di cancel
|
|
if (checkReservationCanceledStatus) {
|
|
result = {
|
|
code: '431',
|
|
message: 'transaction has been canceled',
|
|
data: {
|
|
payment_recon_no: PaymentReconNumber,
|
|
recon_date: moment().format('YYYY-MM-DD HH:mm:ss'),
|
|
},
|
|
};
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING PAYMENT-RECON ]: inserting payment recon transaction...`
|
|
// );
|
|
|
|
// insert payment recon transaction
|
|
await model.insertPaymentReconTransaction(
|
|
date,
|
|
moment().format('YYYY-MM-DD HH:mm:ss'),
|
|
getPartner.partner_code,
|
|
PaymentReconNumber,
|
|
bank_ref,
|
|
reservation_number,
|
|
amount,
|
|
'F',
|
|
'FAILED',
|
|
result.code,
|
|
result.message.toUpperCase(),
|
|
result.data
|
|
);
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING PAYMENT-RECON ]: checking reservation number in CMS...`
|
|
// );
|
|
|
|
// cek reservation number tercatat di sistem CMS atau tidak
|
|
let check = await model.checkReservationNumberAndAmount(reservation_number);
|
|
|
|
// jika reservation number tidak ada di sistem CMS
|
|
if (!check) {
|
|
result = {
|
|
code: '427',
|
|
message: "transaction doesn't exists",
|
|
data: {
|
|
payment_recon_no: PaymentReconNumber,
|
|
recon_date: now,
|
|
},
|
|
};
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING PAYMENT-RECON ]: inserting payment recon transaction...`
|
|
// );
|
|
|
|
// insert payment recon transaction
|
|
await model.insertPaymentReconTransaction(
|
|
date,
|
|
now,
|
|
getPartner.partner_code,
|
|
PaymentReconNumber,
|
|
bank_ref,
|
|
reservation_number,
|
|
amount,
|
|
'F',
|
|
'FAILED',
|
|
result.code,
|
|
result.message.toUpperCase(),
|
|
result.data
|
|
);
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
let totalTransactions = check.total_transaction;
|
|
|
|
// jika reservation number ditemukan lebih dari 1 data transaksi
|
|
if (totalTransactions > 1) {
|
|
result = {
|
|
code: '424',
|
|
message: 'Duplicate Ref No.',
|
|
data: {
|
|
payment_recon_no: PaymentReconNumber,
|
|
recon_date: now,
|
|
},
|
|
};
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING PAYMENT-RECON ]: inserting payment recon transaction...`
|
|
// );
|
|
|
|
// insert payment recon transaction
|
|
await model.insertPaymentReconTransaction(
|
|
date,
|
|
now,
|
|
getPartner.partner_code,
|
|
PaymentReconNumber,
|
|
bank_ref,
|
|
reservation_number,
|
|
amount,
|
|
'F',
|
|
'FAILED',
|
|
result.code,
|
|
result.message.toUpperCase(),
|
|
result.data
|
|
);
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
let totalAmount = check.total_amount;
|
|
|
|
// jika total amount yang di temukan di sistem CMS berdasarkan reservation number berbeda dengan amount yang di request
|
|
if (parseInt(totalAmount) != parseInt(amount)) {
|
|
result = {
|
|
code: '425',
|
|
message: 'Total amount is different.',
|
|
data: {
|
|
payment_recon_no: PaymentReconNumber,
|
|
recon_date: now,
|
|
},
|
|
};
|
|
|
|
// log debug
|
|
// winston.logger.debug(
|
|
// `${uniqueCode} [ PROCESSING PAYMENT-RECON ]: inserting payment recon transaction...`
|
|
// );
|
|
|
|
// insert payment recon transaction
|
|
await model.insertPaymentReconTransaction(
|
|
date,
|
|
now,
|
|
getPartner.partner_code,
|
|
PaymentReconNumber,
|
|
bank_ref,
|
|
reservation_number,
|
|
amount,
|
|
'F',
|
|
'FAILED',
|
|
result.code,
|
|
result.message.toUpperCase(),
|
|
result.data
|
|
);
|
|
|
|
// log warn
|
|
winston.logger.warn(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
const responseData = { payment_recon_no: PaymentReconNumber, recon_date: now };
|
|
const committed = await model.completePaymentRecon({
|
|
date, time: now, partnerId: partner_id, partnerCode: getPartner.partner_code,
|
|
paymentReconNumber: PaymentReconNumber, bankRef: bank_ref, reservationNumber: reservation_number,
|
|
amount, expectedAmount: totalAmount, responseData,
|
|
});
|
|
|
|
if (!committed) {
|
|
const already = await model.checkReservationNumberAndAmountAlreadyRecon(reservation_number);
|
|
result = already ? alreadyReconciledResponse(already) : {
|
|
code: '428',
|
|
message: 'failed recon. The other transaction with same data is running.',
|
|
data: responseData,
|
|
};
|
|
winston.logger.warn(`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`);
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
result = { code: '00', message: 'success', data: responseData };
|
|
|
|
// log info
|
|
winston.logger.info(
|
|
`${uniqueCode} [ RESPONSE PAYMENT-RECON ]: ${JSON.stringify(result)}`
|
|
);
|
|
|
|
return res.status(200).json(result);
|
|
} catch (error) {
|
|
// create log
|
|
winston.logger.error(
|
|
`[ RESPONSE PAYMENT-RECON ]: 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,
|
|
paymentRecon,
|
|
};
|