Commit b56a3961e573ebb225da56f6de8fd9cd9efa9c31
1 parent
92511d68d9
Exists in
master
ignore non sn report
Showing 1 changed file with 6 additions and 1 deletions Inline Diff
partner-datacell.js
1 | var http = require('http'); | 1 | var http = require('http'); |
2 | var url = require('url'); | 2 | var url = require('url'); |
3 | var math = require('mathjs'); | 3 | var math = require('mathjs'); |
4 | var xml = require('xml'); | 4 | var xml = require('xml'); |
5 | var xml2js = require('xml2js').parseString; | 5 | var xml2js = require('xml2js').parseString; |
6 | var strftime = require('strftime'); | 6 | var strftime = require('strftime'); |
7 | var xor = require('base64-xor'); | 7 | var xor = require('base64-xor'); |
8 | var request = require('request'); | 8 | var request = require('request'); |
9 | 9 | ||
10 | var config; | 10 | var config; |
11 | var callbackReport; | 11 | var callbackReport; |
12 | 12 | ||
13 | var max_retry = 2; | 13 | var max_retry = 2; |
14 | var sleep_before_retry = 2000; | 14 | var sleep_before_retry = 2000; |
15 | 15 | ||
16 | function calculateSignature(userid, password, msisdn, timestamp) { | 16 | function calculateSignature(userid, password, msisdn, timestamp) { |
17 | var a = msisdn.substr(msisdn.length - 4) + timestamp; | 17 | var a = msisdn.substr(msisdn.length - 4) + timestamp; |
18 | var b = userid.substr(0, 4) + password; | 18 | var b = userid.substr(0, 4) + password; |
19 | 19 | ||
20 | return xor.encode(a,b); | 20 | return xor.encode(a,b); |
21 | } | 21 | } |
22 | 22 | ||
23 | function createPayload(task) { | 23 | function createPayload(task) { |
24 | var timestamp = strftime('%H%M%S'); | 24 | var timestamp = strftime('%H%M%S'); |
25 | 25 | ||
26 | var payload = { | 26 | var payload = { |
27 | datacell: [ | 27 | datacell: [ |
28 | { perintah: 'charge'}, | 28 | { perintah: 'charge'}, |
29 | {oprcode: task['remoteProduct']}, | 29 | {oprcode: task['remoteProduct']}, |
30 | {userid: config.h2h_out.userid}, | 30 | {userid: config.h2h_out.userid}, |
31 | {time: timestamp}, | 31 | {time: timestamp}, |
32 | {msisdn: task['destination']}, | 32 | {msisdn: task['destination']}, |
33 | {ref_trxid: task['requestId']}, | 33 | {ref_trxid: task['requestId']}, |
34 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} | 34 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} |
35 | ] | 35 | ] |
36 | }; | 36 | }; |
37 | 37 | ||
38 | console.log(payload); | 38 | console.log(payload); |
39 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); | 39 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); |
40 | } | 40 | } |
41 | 41 | ||
42 | function topupRequest(task, retry) { | 42 | function topupRequest(task, retry) { |
43 | var payload_xml = createPayload(task); | 43 | var payload_xml = createPayload(task); |
44 | //console.log(payload_xml); | 44 | //console.log(payload_xml); |
45 | 45 | ||
46 | var postRequest = { | 46 | var postRequest = { |
47 | host: "202.152.62.2", | 47 | host: "202.152.62.2", |
48 | path: "/RELOAD97.php", | 48 | path: "/RELOAD97.php", |
49 | port: 7713, | 49 | port: 7713, |
50 | method: "POST", | 50 | method: "POST", |
51 | headers: { | 51 | headers: { |
52 | 'Content-Type': 'text/xml', | 52 | 'Content-Type': 'text/xml', |
53 | 'Content-Length': Buffer.byteLength(payload_xml) | 53 | 'Content-Length': Buffer.byteLength(payload_xml) |
54 | } | 54 | } |
55 | }; | 55 | }; |
56 | 56 | ||
57 | var buffer = ""; | 57 | var buffer = ""; |
58 | var req = http.request( postRequest, function( res ) { | 58 | var req = http.request( postRequest, function( res ) { |
59 | 59 | ||
60 | console.log('Status code: ' + res.statusCode ); | 60 | console.log('Status code: ' + res.statusCode ); |
61 | var buffer = ""; | 61 | var buffer = ""; |
62 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 62 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
63 | res.on( "end", function( data ) { | 63 | res.on( "end", function( data ) { |
64 | topupResponseHandler(buffer); | 64 | topupResponseHandler(buffer); |
65 | }); | 65 | }); |
66 | 66 | ||
67 | }); | 67 | }); |
68 | 68 | ||
69 | req.on('error', function(e) { | 69 | req.on('error', function(e) { |
70 | console.log('problem with request: ' + e.message); | 70 | console.log('problem with request: ' + e.message); |
71 | }); | 71 | }); |
72 | 72 | ||
73 | req.write( payload_xml ); | 73 | req.write( payload_xml ); |
74 | req.end(); | 74 | req.end(); |
75 | } | 75 | } |
76 | 76 | ||
77 | function topupResponseHandler(body, request_id) { | 77 | function topupResponseHandler(body, request_id) { |
78 | xml2js(body, function (err, result) { | 78 | xml2js(body, function (err, result) { |
79 | if (err) { | 79 | if (err) { |
80 | console.log(body); | 80 | console.log(body); |
81 | callbackReport(request_id, '40', buffer); | 81 | callbackReport(request_id, '40', buffer); |
82 | return; | 82 | return; |
83 | } | 83 | } |
84 | 84 | ||
85 | console.log(result); | 85 | console.log(result); |
86 | 86 | ||
87 | request_id = result.datacell.ref_trxid[0].trim(); | 87 | request_id = result.datacell.ref_trxid[0].trim(); |
88 | 88 | ||
89 | var response_code = '68'; | 89 | var response_code = '68'; |
90 | 90 | ||
91 | var message = ''; | 91 | var message = ''; |
92 | try { | 92 | try { |
93 | if (result.datacell.message && result.datacell.message.length > 0) { | 93 | if (result.datacell.message && result.datacell.message.length > 0) { |
94 | message = result.datacell.message[0].trim(); | 94 | message = result.datacell.message[0].trim(); |
95 | } else if (result.datacell.msg && result.datacell.msg.length > 0) { | 95 | } else if (result.datacell.msg && result.datacell.msg.length > 0) { |
96 | message = result.datacell.msg[0].trim(); | 96 | message = result.datacell.msg[0].trim(); |
97 | } | 97 | } |
98 | } | 98 | } |
99 | catch(err) { | 99 | catch(err) { |
100 | message = 'exception saat parsing message'; | 100 | message = 'exception saat parsing message'; |
101 | } | 101 | } |
102 | 102 | ||
103 | if (result.datacell.resultcode && result.datacell.resultcode[0] == '999') { | 103 | if (result.datacell.resultcode && result.datacell.resultcode[0] == '999') { |
104 | response_code = '40'; | 104 | response_code = '40'; |
105 | } | 105 | } |
106 | 106 | ||
107 | if (message.indexOf('Nomor tujuan salah') >= 0) { | 107 | if (message.indexOf('Nomor tujuan salah') >= 0) { |
108 | response_code = '14'; | 108 | response_code = '14'; |
109 | } else if (message.indexOf('*GAGAL, transaksi yang sama sudah ada dalam 10 menit') >= 0) { | 109 | } else if (message.indexOf('*GAGAL, transaksi yang sama sudah ada dalam 10 menit') >= 0) { |
110 | response_code = '55'; | 110 | response_code = '55'; |
111 | } else if (message.indexOf('saldo sdh dikembalikan') >= 0) { | 111 | } else if (message.indexOf('saldo sdh dikembalikan') >= 0) { |
112 | response_code = '40' | 112 | response_code = '40' |
113 | } else if (message.indexOf('Trx dpt diulang') >= 0) { | 113 | } else if (message.indexOf('Trx dpt diulang') >= 0) { |
114 | response_code = '40' | 114 | response_code = '40' |
115 | } else if (message.indexOf('SUKSES SN Operator:') >= 0) { | 115 | } else if (message.indexOf('SUKSES SN Operator:') >= 0) { |
116 | response_code = '00'; | 116 | response_code = '00'; |
117 | 117 | ||
118 | var sn = parseSN(message); | 118 | var sn = parseSN(message); |
119 | console.log ('SN Operator: ' + sn); | 119 | console.log ('SN Operator: ' + sn); |
120 | 120 | ||
121 | /* | 121 | /* |
122 | if (!sn) { | 122 | if (!sn) { |
123 | 123 | ||
124 | console.log('Missing real operator SN, using SN from suplier'); | 124 | console.log('Missing real operator SN, using SN from suplier'); |
125 | try { | 125 | try { |
126 | sn = result.datacell.trxid[0].trim(); | 126 | sn = result.datacell.trxid[0].trim(); |
127 | } | 127 | } |
128 | catch(err) { | 128 | catch(err) { |
129 | sn = ''; | 129 | sn = ''; |
130 | } | 130 | } |
131 | } | 131 | } |
132 | */ | 132 | */ |
133 | 133 | ||
134 | if (sn) { | 134 | if (sn) { |
135 | message = 'SN=' + sn + '; ' + message; | 135 | message = 'SN=' + sn + '; ' + message; |
136 | } else { | 136 | } else { |
137 | message = message + ' - ' + ' Unknown original SN'; | 137 | message = message + ' - ' + ' Unknown original SN'; |
138 | } | 138 | } |
139 | } | 139 | } |
140 | 140 | ||
141 | callbackReport(request_id, response_code, message); | 141 | callbackReport(request_id, response_code, message); |
142 | }); | 142 | }); |
143 | } | 143 | } |
144 | 144 | ||
145 | function parseSN(message) { | 145 | function parseSN(message) { |
146 | var results = message.match(/SN Operator: .+ SN Kami/); | 146 | var results = message.match(/SN Operator: .+ SN Kami/); |
147 | if (!results || results.length <= 0) { | 147 | if (!results || results.length <= 0) { |
148 | return ''; | 148 | return ''; |
149 | } | 149 | } |
150 | 150 | ||
151 | var result = results[0]; | 151 | var result = results[0]; |
152 | result = result.replace('SN Operator:', ''); | 152 | result = result.replace('SN Operator:', ''); |
153 | result = result.replace('SN Kami', ''); | 153 | result = result.replace('SN Kami', ''); |
154 | result = result.trim(); | 154 | result = result.trim(); |
155 | 155 | ||
156 | if (result == '00') { | 156 | if (result == '00') { |
157 | result = ''; | 157 | result = ''; |
158 | } | 158 | } |
159 | 159 | ||
160 | return result; | 160 | return result; |
161 | } | 161 | } |
162 | 162 | ||
163 | function createServer() { | 163 | function createServer() { |
164 | 164 | ||
165 | var httpServer = http.createServer(function(req, res) { | 165 | var httpServer = http.createServer(function(req, res) { |
166 | console.log('Got request from partner ("' + req.url + '")'); | 166 | console.log('Got request from partner ("' + req.url + '")'); |
167 | 167 | ||
168 | var body = ""; | 168 | var body = ""; |
169 | req.on('data', function (chunk) { | 169 | req.on('data', function (chunk) { |
170 | body += chunk; | 170 | body += chunk; |
171 | }); | 171 | }); |
172 | 172 | ||
173 | req.on('end', function () { | 173 | req.on('end', function () { |
174 | res.writeHead(200); | 174 | res.writeHead(200); |
175 | res.end('OK'); | 175 | res.end('OK'); |
176 | 176 | ||
177 | console.log(body); | 177 | console.log(body); |
178 | topupResponseHandler(body); | 178 | |
179 | if (req.url == '/sn') { | ||
180 | topupResponseHandler(body); | ||
181 | } else { | ||
182 | console.log('Ignore non /sn report'); | ||
183 | } | ||
179 | }); | 184 | }); |
180 | }); | 185 | }); |
181 | 186 | ||
182 | httpServer.listen(config.h2h_out.listen_port, function() { | 187 | httpServer.listen(config.h2h_out.listen_port, function() { |
183 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 188 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
184 | }); | 189 | }); |
185 | } | 190 | } |
186 | 191 | ||
187 | 192 | ||
188 | function start(_config, _callbackReport) { | 193 | function start(_config, _callbackReport) { |
189 | config = _config; | 194 | config = _config; |
190 | callbackReport = _callbackReport | 195 | callbackReport = _callbackReport |
191 | 196 | ||
192 | createServer(); | 197 | createServer(); |
193 | } | 198 | } |
194 | 199 | ||
195 | exports.start = start; | 200 | exports.start = start; |
196 | exports.topupRequest = topupRequest; | 201 | exports.topupRequest = topupRequest; |
197 | 202 |