mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 22:56:26 +00:00
Add invoice service
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
const { param, body, validationResult } = require("express-validator");
|
||||
const helper = require("../../helpers/helper");
|
||||
const winston = require("../../helpers/winston.logger");
|
||||
const model = require("./invoice.model");
|
||||
const moment = require("moment");
|
||||
|
||||
const knex = require("../../../infrastructure/database/knex");
|
||||
|
||||
moment.locale("id");
|
||||
|
||||
var result = {};
|
||||
var uniqueCode;
|
||||
|
||||
// VALIDATION
|
||||
const validate = (method) => {
|
||||
switch (method) {
|
||||
case "invoiceVerification":
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
// POSTING INVOICE
|
||||
const postingInvoice = async (req, res) => {
|
||||
try {
|
||||
// generate unique code
|
||||
uniqueCode = await helper.getUniqueCode();
|
||||
|
||||
// param
|
||||
let { log } = req.body;
|
||||
let tid = req.tid_token;
|
||||
|
||||
// log info
|
||||
winston.logger.info(
|
||||
`${uniqueCode} REQUEST posting invoice : ${JSON.stringify(req.body)}`
|
||||
);
|
||||
|
||||
// check validator
|
||||
const err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
result = {
|
||||
code: "400",
|
||||
message: err.errors[0].msg,
|
||||
data: {},
|
||||
};
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE posting invoice : ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// time
|
||||
let invoiceDate = moment().subtract(1, "days").format("YYYYMMDD");
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`${uniqueCode} checking partner...`);
|
||||
|
||||
await db.transaction(async trx => {
|
||||
|
||||
let invoice = await model.getInvoice(invoiceDate, trx);
|
||||
})
|
||||
|
||||
|
||||
// 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: {},
|
||||
};
|
||||
// insert into posting_et
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment verification: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let data = [];
|
||||
|
||||
// log info
|
||||
winston.logger.info(
|
||||
`${uniqueCode} RESPONSE payment verification: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
winston.logger.error(
|
||||
`500 internal server error - backend server | ${error.message}`
|
||||
);
|
||||
|
||||
return res.status(200).json({
|
||||
code: "500",
|
||||
message:
|
||||
process.env.NODE_ENV != "production"
|
||||
? error.message
|
||||
: "500 internal server error - backend server.",
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
validate,
|
||||
postingInvoice,
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// GET PARTNER, MERCHANT, TERMINAL FROM TID
|
||||
const generateInvoice = (invoiceDate, trx) => {
|
||||
|
||||
let invoice = trx.select(trx.raw(`payment.sp_payment_req_topup_online('${invoiceDate}')`)).get()
|
||||
|
||||
invoice.forEach(function (item) {
|
||||
const url = 'http://piqas.kereta-api.co.id:51500/XISOAPAdapter/MessageServlet?senderParty=&senderService=KCI_ETICKETING_2&receiverParty=&receiverService=&interface=SI_KCI_CMS_FI01&interfaceNamespace=urn:Posting_ETicketing_2'
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'text/xml'
|
||||
},
|
||||
body: xmlRequest
|
||||
});
|
||||
|
||||
console.log(item);
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
generateInvoice,
|
||||
};
|
||||
Reference in New Issue
Block a user