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