Commit d4f54365cbb53e5614734a7c7236883c83ee6675
1 parent
501ee56e96
Exists in
master
typo
Showing 1 changed file with 1 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 calculateBalanceSignature(userid, password, timestamp) { | 23 | function calculateBalanceSignature(userid, password, timestamp) { |
24 | var a = '0000' + timestamp; | 24 | var a = '0000' + timestamp; |
25 | var b = userid.substr(0, 4) + password; | 25 | var b = userid.substr(0, 4) + password; |
26 | 26 | ||
27 | return xor.encode(a,b); | 27 | return xor.encode(a,b); |
28 | } | 28 | } |
29 | 29 | ||
30 | 30 | ||
31 | function createPayload(task) { | 31 | function createPayload(task) { |
32 | var timestamp = strftime('%H%M%S'); | 32 | var timestamp = strftime('%H%M%S'); |
33 | 33 | ||
34 | var payload = { | 34 | var payload = { |
35 | datacell: [ | 35 | datacell: [ |
36 | { perintah: 'charge'}, | 36 | { perintah: 'charge'}, |
37 | {oprcode: task['remoteProduct']}, | 37 | {oprcode: task['remoteProduct']}, |
38 | {userid: config.h2h_out.userid}, | 38 | {userid: config.h2h_out.userid}, |
39 | {time: timestamp}, | 39 | {time: timestamp}, |
40 | {msisdn: task['destination']}, | 40 | {msisdn: task['destination']}, |
41 | {ref_trxid: task['requestId']}, | 41 | {ref_trxid: task['requestId']}, |
42 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} | 42 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} |
43 | ] | 43 | ] |
44 | }; | 44 | }; |
45 | 45 | ||
46 | console.log(payload); | 46 | console.log(payload); |
47 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); | 47 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); |
48 | } | 48 | } |
49 | 49 | ||
50 | function topupRequest(task, retry) { | 50 | function topupRequest(task, retry) { |
51 | //balanceCheck(); | 51 | //balanceCheck(); |
52 | 52 | ||
53 | var payload_xml = createPayload(task); | 53 | var payload_xml = createPayload(task); |
54 | //console.log(payload_xml); | 54 | //console.log(payload_xml); |
55 | 55 | ||
56 | var postRequest = { | 56 | var postRequest = { |
57 | host: "202.152.62.2", | 57 | host: "202.152.62.2", |
58 | path: "/RELOAD97.php", | 58 | path: "/RELOAD97.php", |
59 | port: 7713, | 59 | port: 7713, |
60 | method: "POST", | 60 | method: "POST", |
61 | headers: { | 61 | headers: { |
62 | 'Content-Type': 'text/xml', | 62 | 'Content-Type': 'text/xml', |
63 | 'Content-Length': Buffer.byteLength(payload_xml) | 63 | 'Content-Length': Buffer.byteLength(payload_xml) |
64 | } | 64 | } |
65 | }; | 65 | }; |
66 | 66 | ||
67 | var buffer = ""; | 67 | var buffer = ""; |
68 | var req = http.request( postRequest, function( res ) { | 68 | var req = http.request( postRequest, function( res ) { |
69 | 69 | ||
70 | console.log('Status code: ' + res.statusCode ); | 70 | console.log('Status code: ' + res.statusCode ); |
71 | var buffer = ""; | 71 | var buffer = ""; |
72 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 72 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
73 | res.on( "end", function( data ) { | 73 | res.on( "end", function( data ) { |
74 | topupResponseHandler(buffer); | 74 | topupResponseHandler(buffer); |
75 | }); | 75 | }); |
76 | 76 | ||
77 | }); | 77 | }); |
78 | 78 | ||
79 | req.on('error', function(e) { | 79 | req.on('error', function(e) { |
80 | console.log('problem with request: ' + e.message); | 80 | console.log('problem with request: ' + e.message); |
81 | }); | 81 | }); |
82 | 82 | ||
83 | req.write( payload_xml ); | 83 | req.write( payload_xml ); |
84 | req.end(); | 84 | req.end(); |
85 | } | 85 | } |
86 | 86 | ||
87 | function topupResponseHandler(body, request_id) { | 87 | function topupResponseHandler(body, request_id) { |
88 | xml2js(body, function (err, result) { | 88 | xml2js(body, function (err, result) { |
89 | if (err) { | 89 | if (err) { |
90 | console.log(body); | 90 | console.log(body); |
91 | callbackReport(request_id, '40', buffer); | 91 | callbackReport(request_id, '40', buffer); |
92 | return; | 92 | return; |
93 | } | 93 | } |
94 | 94 | ||
95 | console.log(result); | 95 | console.log(result); |
96 | 96 | ||
97 | request_id = result.datacell.ref_trxid[0].trim(); | 97 | request_id = result.datacell.ref_trxid[0].trim(); |
98 | 98 | ||
99 | var response_code = '68'; | 99 | var response_code = '68'; |
100 | 100 | ||
101 | var message = ''; | 101 | var message = ''; |
102 | try { | 102 | try { |
103 | if (result.datacell.message && result.datacell.message.length > 0) { | 103 | if (result.datacell.message && result.datacell.message.length > 0) { |
104 | message = result.datacell.message[0].trim(); | 104 | message = result.datacell.message[0].trim(); |
105 | } else if (result.datacell.msg && result.datacell.msg.length > 0) { | 105 | } else if (result.datacell.msg && result.datacell.msg.length > 0) { |
106 | message = result.datacell.msg[0].trim(); | 106 | message = result.datacell.msg[0].trim(); |
107 | } | 107 | } |
108 | } | 108 | } |
109 | catch(err) { | 109 | catch(err) { |
110 | message = 'exception saat parsing message'; | 110 | message = 'exception saat parsing message'; |
111 | } | 111 | } |
112 | 112 | ||
113 | if (result.datacell.resultcode && result.datacell.resultcode[0] == '999') { | 113 | if (result.datacell.resultcode && result.datacell.resultcode[0] == '999') { |
114 | response_code = '40'; | 114 | response_code = '40'; |
115 | } | 115 | } |
116 | 116 | ||
117 | if (message.indexOf('Nomor tujuan salah') >= 0) { | 117 | if (message.indexOf('Nomor tujuan salah') >= 0) { |
118 | response_code = '14'; | 118 | response_code = '14'; |
119 | } else if (message.indexOf('*GAGAL, transaksi yang sama sudah ada dalam 10 menit') >= 0) { | 119 | } else if (message.indexOf('*GAGAL, transaksi yang sama sudah ada dalam 10 menit') >= 0) { |
120 | response_code = '55'; | 120 | response_code = '55'; |
121 | } else if (message.indexOf('saldo sdh dikembalikan') >= 0) { | 121 | } else if (message.indexOf('saldo sdh dikembalikan') >= 0) { |
122 | response_code = '40' | 122 | response_code = '40' |
123 | } else if (message.indexOf('Trx dpt diulang') >= 0) { | 123 | } else if (message.indexOf('Trx dpt diulang') >= 0) { |
124 | response_code = '40' | 124 | response_code = '40' |
125 | } else if (message.indexOf('SUKSES SN Operator:') >= 0) { | 125 | } else if (message.indexOf('SUKSES SN Operator:') >= 0) { |
126 | response_code = '00'; | 126 | response_code = '00'; |
127 | 127 | ||
128 | var sn = parseSN(message); | 128 | var sn = parseSN(message); |
129 | console.log ('SN Operator: ' + sn); | 129 | console.log ('SN Operator: ' + sn); |
130 | 130 | ||
131 | /* | 131 | /* |
132 | if (!sn) { | 132 | if (!sn) { |
133 | 133 | ||
134 | console.log('Missing real operator SN, using SN from suplier'); | 134 | console.log('Missing real operator SN, using SN from suplier'); |
135 | try { | 135 | try { |
136 | sn = result.datacell.trxid[0].trim(); | 136 | sn = result.datacell.trxid[0].trim(); |
137 | } | 137 | } |
138 | catch(err) { | 138 | catch(err) { |
139 | sn = ''; | 139 | sn = ''; |
140 | } | 140 | } |
141 | } | 141 | } |
142 | */ | 142 | */ |
143 | 143 | ||
144 | if (sn) { | 144 | if (sn) { |
145 | message = 'SN=' + sn + '; ' + message; | 145 | message = 'SN=' + sn + '; ' + message; |
146 | } else { | 146 | } else { |
147 | message = 'SN belum didapat. ' + message; | 147 | message = 'SN belum didapat. ' + message; |
148 | response_code = '68'; | 148 | response_code = '68'; |
149 | } | 149 | } |
150 | } | 150 | } |
151 | 151 | ||
152 | callbackReport(request_id, response_code, message); | 152 | callbackReport(request_id, response_code, message); |
153 | }); | 153 | }); |
154 | } | 154 | } |
155 | 155 | ||
156 | function parseSN(message) { | 156 | function parseSN(message) { |
157 | var results = message.match(/SN Operator: .+ SN Kami/); | 157 | var results = message.match(/SN Operator: .+ SN Kami/); |
158 | if (!results || results.length <= 0) { | 158 | if (!results || results.length <= 0) { |
159 | return ''; | 159 | return ''; |
160 | } | 160 | } |
161 | 161 | ||
162 | var result = results[0]; | 162 | var result = results[0]; |
163 | result = result.replace('SN Operator:', ''); | 163 | result = result.replace('SN Operator:', ''); |
164 | result = result.replace('SN Kami', ''); | 164 | result = result.replace('SN Kami', ''); |
165 | result = result.trim(); | 165 | result = result.trim(); |
166 | 166 | ||
167 | if (result == '00') { | 167 | if (result == '00') { |
168 | result = ''; | 168 | result = ''; |
169 | } | 169 | } |
170 | 170 | ||
171 | return result; | 171 | return result; |
172 | } | 172 | } |
173 | 173 | ||
174 | function createServer() { | 174 | function createServer() { |
175 | 175 | ||
176 | var httpServer = http.createServer(function(req, res) { | 176 | var httpServer = http.createServer(function(req, res) { |
177 | var parsed_url = url.parse(req.url, true, true); | 177 | var parsed_url = url.parse(req.url, true, true); |
178 | 178 | ||
179 | console.log('Got request from partner ("' + req.url + '")'); | 179 | console.log('Got request from partner ("' + req.url + '")'); |
180 | 180 | ||
181 | var body = ""; | 181 | var body = ""; |
182 | req.on('data', function (chunk) { | 182 | req.on('data', function (chunk) { |
183 | body += chunk; | 183 | body += chunk; |
184 | }); | 184 | }); |
185 | 185 | ||
186 | req.on('end', function () { | 186 | req.on('end', function () { |
187 | res.writeHead(200); | 187 | res.writeHead(200); |
188 | res.end('OK'); | 188 | res.end('OK'); |
189 | 189 | ||
190 | console.log(body); | 190 | console.log(body); |
191 | 191 | ||
192 | if (parsed_url.pathname == '/sn') { | 192 | if (parsed_url.pathname == '/sn') { |
193 | console.log('Reverse report -- SN'); | 193 | console.log('Reverse report -- SN'); |
194 | topupResponseHandler(body); | 194 | topupResponseHandler(body); |
195 | 195 | ||
196 | } else if (parsed_url.pathname = '/refund') { | 196 | } else if (parsed_url.pathname = '/refund') { |
197 | console.log('Reverse report -- REFUND'); | 197 | console.log('Reverse report -- REFUND'); |
198 | callbackReport(parsed_url.qs.ref_trxid, '40', parsed_url.qs.message); | 198 | callbackReport(parsed_url.query.ref_trxid, '40', parsed_url.qs.message); |
199 | 199 | ||
200 | } else { | 200 | } else { |
201 | console.log('Reverse report -- UNKNOWN'); | 201 | console.log('Reverse report -- UNKNOWN'); |
202 | console.log('Ignore unknown request on reverse url'); | 202 | console.log('Ignore unknown request on reverse url'); |
203 | } | 203 | } |
204 | }); | 204 | }); |
205 | }); | 205 | }); |
206 | 206 | ||
207 | httpServer.listen(config.h2h_out.listen_port, function() { | 207 | httpServer.listen(config.h2h_out.listen_port, function() { |
208 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 208 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
209 | }); | 209 | }); |
210 | } | 210 | } |
211 | 211 | ||
212 | function balanceCheck() { | 212 | function balanceCheck() { |
213 | var timestamp = strftime('%H%M%S'); | 213 | var timestamp = strftime('%H%M%S'); |
214 | 214 | ||
215 | var payload = { | 215 | var payload = { |
216 | datacell: [ | 216 | datacell: [ |
217 | {perintah: 'saldo'}, | 217 | {perintah: 'saldo'}, |
218 | {userid: config.h2h_out.userid}, | 218 | {userid: config.h2h_out.userid}, |
219 | {time: timestamp}, | 219 | {time: timestamp}, |
220 | {sgn: calculateBalanceSignature(config.h2h_out.userid, config.h2h_out.password, timestamp)} | 220 | {sgn: calculateBalanceSignature(config.h2h_out.userid, config.h2h_out.password, timestamp)} |
221 | ] | 221 | ] |
222 | }; | 222 | }; |
223 | 223 | ||
224 | var postRequest = { | 224 | var postRequest = { |
225 | host: "202.152.62.2", | 225 | host: "202.152.62.2", |
226 | path: "/RELOAD97.php", | 226 | path: "/RELOAD97.php", |
227 | port: 7713, | 227 | port: 7713, |
228 | method: "POST", | 228 | method: "POST", |
229 | headers: { | 229 | headers: { |
230 | 'Content-Type': 'text/xml', | 230 | 'Content-Type': 'text/xml', |
231 | 'Content-Length': Buffer.byteLength(payload_xml) | 231 | 'Content-Length': Buffer.byteLength(payload_xml) |
232 | } | 232 | } |
233 | }; | 233 | }; |
234 | 234 | ||
235 | var buffer = ""; | 235 | var buffer = ""; |
236 | var req = http.request( postRequest, function( res ) { | 236 | var req = http.request( postRequest, function( res ) { |
237 | 237 | ||
238 | console.log('Status code: ' + res.statusCode ); | 238 | console.log('Status code: ' + res.statusCode ); |
239 | var buffer = ""; | 239 | var buffer = ""; |
240 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 240 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
241 | res.on( "end", function( data ) { | 241 | res.on( "end", function( data ) { |
242 | console.log('CHECK BALANCE RESULT:'); | 242 | console.log('CHECK BALANCE RESULT:'); |
243 | console.log(buffer); | 243 | console.log(buffer); |
244 | }); | 244 | }); |
245 | 245 | ||
246 | }); | 246 | }); |
247 | 247 | ||
248 | req.on('error', function(e) { | 248 | req.on('error', function(e) { |
249 | console.log('problem with request: ' + e.message); | 249 | console.log('problem with request: ' + e.message); |
250 | }); | 250 | }); |
251 | 251 | ||
252 | req.write( payload_xml ); | 252 | req.write( payload_xml ); |
253 | req.end(); | 253 | req.end(); |
254 | 254 | ||
255 | } | 255 | } |
256 | 256 | ||
257 | 257 | ||
258 | function start(_config, _callbackReport) { | 258 | function start(_config, _callbackReport) { |
259 | config = _config; | 259 | config = _config; |
260 | callbackReport = _callbackReport | 260 | callbackReport = _callbackReport |
261 | 261 | ||
262 | createServer(); | 262 | createServer(); |
263 | } | 263 | } |
264 | 264 | ||
265 | exports.start = start; | 265 | exports.start = start; |
266 | exports.topupRequest = topupRequest; | 266 | exports.topupRequest = topupRequest; |
267 | 267 |