diff --git a/partner-simplepay.js b/partner-simplepay.js index e10af26..456f5b7 100644 --- a/partner-simplepay.js +++ b/partner-simplepay.js @@ -310,6 +310,43 @@ function reverseReportHandler(body) { logger.info('Got reverse report', {body: body}); } +function _parseInfoFromReverseReport(info) { + let result; + try { + result = JSON.parse(info); + } + catch (e) { + logger.warn('Exception on parsing qs info as JSON', {info: info}); + } + + return result; +} + +function _createSnFromReverseReport(qs) { + let token = qs.serial; + if (token && typeof token === 'string') { + token = token.replace(/\W+/g, '-'); + + } + + let sn = token || '-'; + + const info = _parseInfoFromReverseReport(qs.info); + if (info) { + const cust_name = (typeof info.cust_name === 'string') ? info.cust_name.replace(/\W+/g, '-').replace(/^\W+/, '').replace(/\W+$/, '') : '-'; + const kelas = (typeof info.kelas === 'string') ? info.kelas.replace(/\W+/g, '-').replace(/^\W+/, '').replace(/\W+$/, '') : '-'; + const kwh = (typeof info.size === 'string') ? info.size.replace(/\W+/g, '-').replace(/^\W+/, '').replace(/\W+$/, '') : '-'; + + sn = [ + token || '-', + cust_name || '-', + kwh || '-' + ].join('/'); + } + + return sn; +} + function createReverseHttpServer() { var httpServer = http.createServer(function(req, res) { @@ -328,6 +365,32 @@ function createReverseHttpServer() { reverseReportHandler(body); }); + if (!qs || !qs.request_id) { + return; + } + + let rc = '68'; + if (qs.trx_status === 'S') { + rc = '00'; + } + else if (qs.trx_status === 'R') { + rc = '40'; + } + + const sn = (rc === '00') ? _createSnFromReverseReport(qs) : null; + let msg = [ + qs.diag, + 'Status: ' + (qs.trx_status || '-') + 'Balance: ' + (qs.balance || '-'), + 'Harga: ' + (qs.harga || '-') + ].join('. '); + + if (sn) { + msg = 'SN=' + sn + '; ' + msg; + } + + callbackReport(qs.request_id, rc, msg); + }); httpServer.listen(config.h2h_out.listen_port, function() {