Commit afd80f70b85a745cfadc8957973af1911fa50970
1 parent
45008e9867
Exists in
master
support irs reverse report
Showing 4 changed files with 242 additions and 1 deletions Side-by-side Diff
index.js
... | ... | @@ -3,11 +3,22 @@ process.chdir(__dirname); |
3 | 3 | const fs = require('fs'); |
4 | 4 | fs.writeFileSync('pid.txt', process.pid); |
5 | 5 | |
6 | +const config = require('komodo-sdk/config'); | |
7 | +const logger = require('komodo-sdk/logger'); | |
8 | + | |
6 | 9 | require('komodo-sdk/api-server'); |
7 | 10 | const pullgw = require('komodo-sdk/gateway/pull'); |
8 | 11 | const partner = require('./lib/partner'); |
9 | -require('./lib/reverse-report'); | |
10 | 12 | const adviceServer = require('komodo-sdk/gateway/advice-push-server'); |
11 | 13 | |
14 | +if (config.partner && config.partner.reverse_report_irs) { | |
15 | + logger.info('Reverse report using IRS mode'); | |
16 | + require('./lib/irs/reverse-report'); | |
17 | +} | |
18 | +else { | |
19 | + require('./lib/reverse-report'); | |
20 | +} | |
21 | + | |
22 | + | |
12 | 23 | pullgw.setPartner(partner); |
13 | 24 | adviceServer.setPartner(partner); |
lib/irs/irs.js
... | ... | @@ -0,0 +1,103 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const rcFromMsg = require('komodo-sdk/rc-from-msg'); | |
4 | +const organicRc = require('./rc'); | |
5 | + | |
6 | +function getRcFromMessage(msg, customRc) { | |
7 | + let rc; | |
8 | + if (customRc) { | |
9 | + rc = rcFromMsg(msg, customRc); | |
10 | + } | |
11 | + | |
12 | + if (!rc) { | |
13 | + rc = rcFromMsg(msg, organicRc); | |
14 | + } | |
15 | + | |
16 | + return rc; | |
17 | +} | |
18 | + | |
19 | +function getPriceFromMessage(msg, rule) { | |
20 | + if (typeof msg !== 'string') { | |
21 | + return; | |
22 | + } | |
23 | + | |
24 | + if (process.env.DEBUG_IRS && !rule) { | |
25 | + console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console | |
26 | + } | |
27 | + | |
28 | + if (process.env.DEBUG_IRS && rule) { | |
29 | + console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console | |
30 | + } | |
31 | + | |
32 | + const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) "; | |
33 | + const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1; | |
34 | + | |
35 | + const re = new RegExp(pattern); | |
36 | + const matches = msg.match(re); | |
37 | + if (process.env.DEBUG_IRS) { | |
38 | + console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console | |
39 | + console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console | |
40 | + } | |
41 | + if (matches && matches[match_idx]) { | |
42 | + const result = Number(matches[match_idx].replace(/\./g, '')); | |
43 | + if (process.env.DEBUG_IRS) { | |
44 | + console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console | |
45 | + } | |
46 | + return result; | |
47 | + } | |
48 | +} | |
49 | + | |
50 | +function extractFromMessage(msg, rule) { | |
51 | + if (typeof msg !== 'string') { return; } | |
52 | + | |
53 | + if (!rule) { return; } | |
54 | + | |
55 | + if (typeof rule !== 'object') { | |
56 | + return; | |
57 | + } | |
58 | + | |
59 | + rule.match_idx = Number(rule.match_idx); | |
60 | + | |
61 | + if (!rule.match_idx) { | |
62 | + rule.match_idx = 1; | |
63 | + } | |
64 | + | |
65 | + const re = new RegExp(rule.pattern); | |
66 | + const matches = msg.match(re); | |
67 | + if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') { | |
68 | + return matches[rule.match_idx]; | |
69 | + } | |
70 | +} | |
71 | + | |
72 | +function getSnFromMessage(msg, rule) { | |
73 | + if (!rule) { | |
74 | + rule = { | |
75 | + pattern: "SN: (\\d+)", | |
76 | + match_idx: 1 | |
77 | + } | |
78 | + } | |
79 | + | |
80 | + let sn = extractFromMessage(msg, rule); | |
81 | + if (!sn || typeof sn !== 'string') { return; } | |
82 | + | |
83 | + return sn.toUpperCase().replace(/[^a-zA-Z0-9/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, ''); | |
84 | +} | |
85 | + | |
86 | +function getBalanceFromMessage(msg, rule) { | |
87 | + if (!rule) { | |
88 | + rule = { | |
89 | + pattern: "Sisa Saldo: .+? = ([\\d\\.]+) ", | |
90 | + match_idx: 1 | |
91 | + } | |
92 | + } | |
93 | + | |
94 | + let result = extractFromMessage(msg, rule); | |
95 | + if (!result || typeof result !== 'string') { return; } | |
96 | + | |
97 | + return Number(result.replace(/\./g, '')); | |
98 | +} | |
99 | + | |
100 | +exports.getRcFromMessage = getRcFromMessage; | |
101 | +exports.getPriceFromMessage = getPriceFromMessage; | |
102 | +exports.getSnFromMessage = getSnFromMessage; | |
103 | +exports.getBalanceFromMessage = getBalanceFromMessage; |
lib/irs/rc.js
... | ... | @@ -0,0 +1,65 @@ |
1 | +module.exports = [ | |
2 | + { | |
3 | + pattern: " BERHASIL", | |
4 | + rc: '00' | |
5 | + }, | |
6 | + { | |
7 | + pattern: "SUKSES", | |
8 | + rc: '00' | |
9 | + }, | |
10 | + { | |
11 | + pattern: " under process", | |
12 | + rc: '68' | |
13 | + }, | |
14 | + { | |
15 | + pattern: " under proses", | |
16 | + rc: '68' | |
17 | + }, | |
18 | + { | |
19 | + pattern: " sedang dalam antrian", | |
20 | + rc: '68' | |
21 | + }, | |
22 | + { | |
23 | + pattern: " sdg dalam antrian", | |
24 | + rc: '68' | |
25 | + }, | |
26 | + { | |
27 | + pattern: " sedang diproses", | |
28 | + rc: '68' | |
29 | + }, | |
30 | + { | |
31 | + pattern: " sdg diproses", | |
32 | + rc: '68' | |
33 | + }, | |
34 | + { | |
35 | + pattern: " nomor tujuan dan produk tidak sesuai", | |
36 | + rc: '14', | |
37 | + flags: 'i' | |
38 | + }, | |
39 | + { | |
40 | + pattern: " tujuan salah", | |
41 | + rc: '14' | |
42 | + }, | |
43 | + { | |
44 | + pattern: "Nomor tidak valid", | |
45 | + rc: '14' | |
46 | + }, | |
47 | + { | |
48 | + pattern: "Maaf Produk sedang gangguan", | |
49 | + rc: '90', | |
50 | + flags: 'i' | |
51 | + }, | |
52 | + { | |
53 | + pattern: "Maaf.*Sedang gangguan. Coba ulangi beberapa saat lagi", | |
54 | + rc: '90', | |
55 | + flags: 'i' | |
56 | + }, | |
57 | + { | |
58 | + pattern: " RC:61 ", | |
59 | + rc: '90' | |
60 | + }, | |
61 | + { | |
62 | + pattern: "GAGAL", | |
63 | + rc: '40' | |
64 | + } | |
65 | +] |
lib/irs/reverse-report.js
... | ... | @@ -0,0 +1,62 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const http = require('http'); | |
4 | +const url = require('url'); | |
5 | + | |
6 | +const stringify = require("json-stringify-pretty-compact"); | |
7 | + | |
8 | +const config = require('komodo-sdk/config'); | |
9 | +const logger = require('komodo-sdk/logger'); | |
10 | + | |
11 | +const partner = require('../partner'); | |
12 | +const irs = require('./irs'); | |
13 | + | |
14 | +function processPartnerReport(qs) { | |
15 | + let rc = '68'; | |
16 | + if (qs.statuscode === '1') { | |
17 | + rc = '00'; | |
18 | + } | |
19 | + else if (qs.statuscode === '2') { | |
20 | + rc = '40'; | |
21 | + } | |
22 | + | |
23 | + if (rc === '40') { | |
24 | + rc = irs.getRcFromMessage(qs.msg) || '40'; | |
25 | + } | |
26 | + | |
27 | + let amount = null; | |
28 | + if (rc === '00') { | |
29 | + amount = Number(qs.hrg); | |
30 | + if (!amount) { | |
31 | + amount = irs.getPriceFromMessage(qs.msg, config.partner.price_pattern); | |
32 | + } | |
33 | + } | |
34 | + | |
35 | + partner.report({ | |
36 | + trx_id: qs.clientid, | |
37 | + rc: rc, | |
38 | + message: 'REVERSE-REPORT: ' + stringify(qs), | |
39 | + raw: stringify(qs), | |
40 | + sn: (qs.sn ? qs.sn : null) || irs.getSnFromMessage(qs.msg, config.partner.sn_pattern) || null, | |
41 | + amount: amount, | |
42 | + balance: (rc === '00') ? irs.getBalanceFromMessage(qs.msg, config.partner.balance_pattern) : null, | |
43 | + misc: {} | |
44 | + }) | |
45 | +} | |
46 | + | |
47 | +function create() { | |
48 | + http.createServer(function (req, res) { | |
49 | + res.writeHead(200, {'Content-Type': 'text/html'}); | |
50 | + const qs = url.parse(req.url, true).query; | |
51 | + res.end('OK'); | |
52 | + | |
53 | + const remote_ip = req.connection ? req.connection.remoteAddress : null; | |
54 | + logger.verbose('REVERSE-REPORT: got report from partner', {url: req.url, remote_ip: remote_ip}); | |
55 | + | |
56 | + processPartnerReport(qs); | |
57 | + }).listen(config.reverse_report_port); | |
58 | + | |
59 | + logger.info('REVERSE-REPORT: listen on port ' + config.reverse_report_port); | |
60 | +} | |
61 | + | |
62 | +config.reverse_report_port && create(); |