Commit 043c7829876cf0befb8b947e1b4a7fe7838ac4f6
1 parent
dbacc12f55
Exists in
master
strftime
Showing 2 changed files with 14 additions and 1 deletions Inline Diff
package.json
1 | { | 1 | { |
2 | "name": "sate24-to-datacell", | 2 | "name": "sate24-to-datacell", |
3 | "version": "0.0.1", | 3 | "version": "0.0.1", |
4 | "description": "ST24 to Datacell", | 4 | "description": "ST24 to Datacell", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "mocha" | 7 | "test": "mocha" |
8 | }, | 8 | }, |
9 | "repository": { | 9 | "repository": { |
10 | "type": "git", | 10 | "type": "git", |
11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-to-datacell.git" | 11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-to-datacell.git" |
12 | }, | 12 | }, |
13 | "keywords": [ | 13 | "keywords": [ |
14 | "datacell", | 14 | "datacell", |
15 | "ppob", | 15 | "ppob", |
16 | "st24" | 16 | "st24" |
17 | ], | 17 | ], |
18 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", | 18 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", |
19 | "license": "BSD", | 19 | "license": "BSD", |
20 | "dependencies": { | 20 | "dependencies": { |
21 | "sate24": "git+http://git@gitlab.kodesumber.com/reload97/node-sate24.git", | 21 | "sate24": "git+http://git@gitlab.kodesumber.com/reload97/node-sate24.git", |
22 | "iniparser": "~1.0.5", | 22 | "iniparser": "~1.0.5", |
23 | "base64-xor": "~0.10.0", | 23 | "base64-xor": "~0.10.0", |
24 | "request": "~2.60.0", | 24 | "request": "~2.60.0", |
25 | "mathjs": "~1.7.1", | 25 | "mathjs": "~1.7.1", |
26 | "xml": "~1.0.0", | 26 | "xml": "~1.0.0", |
27 | "xml2js": "~0.4.9" | 27 | "xml2js": "~0.4.9", |
28 | "strftime": "~0.9.2" | ||
28 | } | 29 | } |
29 | } | 30 | } |
30 | 31 |
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 | callbackReport(task['requestId'], '40', e.message); | 81 | callbackReport(task['requestId'], '40', e.message); |
82 | }); | 82 | }); |
83 | 83 | ||
84 | req.write( payload_xml ); | 84 | req.write( payload_xml ); |
85 | req.end(); | 85 | req.end(); |
86 | } | 86 | } |
87 | 87 | ||
88 | function topupResponseHandler(body, request_id) { | 88 | function topupResponseHandler(body, request_id) { |
89 | xml2js(body, function (err, result) { | 89 | xml2js(body, function (err, result) { |
90 | if (err) { | 90 | if (err) { |
91 | console.log(body); | 91 | console.log(body); |
92 | callbackReport(request_id, '40', buffer); | 92 | callbackReport(request_id, '40', buffer); |
93 | return; | 93 | return; |
94 | } | 94 | } |
95 | 95 | ||
96 | console.log(result); | 96 | console.log(result); |
97 | 97 | ||
98 | request_id = result.datacell.ref_trxid[0].trim(); | 98 | request_id = result.datacell.ref_trxid[0].trim(); |
99 | 99 | ||
100 | var response_code = '68'; | 100 | var response_code = '68'; |
101 | 101 | ||
102 | var message = ''; | 102 | var message = ''; |
103 | try { | 103 | try { |
104 | if (result.datacell.message && result.datacell.message.length > 0) { | 104 | if (result.datacell.message && result.datacell.message.length > 0) { |
105 | message = result.datacell.message[0].trim(); | 105 | message = result.datacell.message[0].trim(); |
106 | } else if (result.datacell.msg && result.datacell.msg.length > 0) { | 106 | } else if (result.datacell.msg && result.datacell.msg.length > 0) { |
107 | message = result.datacell.msg[0].trim(); | 107 | message = result.datacell.msg[0].trim(); |
108 | } | 108 | } |
109 | } | 109 | } |
110 | catch(err) { | 110 | catch(err) { |
111 | message = 'exception saat parsing message'; | 111 | message = 'exception saat parsing message'; |
112 | } | 112 | } |
113 | 113 | ||
114 | if (result.datacell.resultcode && result.datacell.resultcode[0] == '999') { | 114 | if (result.datacell.resultcode && result.datacell.resultcode[0] == '999') { |
115 | response_code = '40'; | 115 | response_code = '40'; |
116 | } | 116 | } |
117 | 117 | ||
118 | if (message.indexOf('Nomor tujuan salah') >= 0) { | 118 | if (message.indexOf('Nomor tujuan salah') >= 0) { |
119 | response_code = '14'; | 119 | response_code = '14'; |
120 | } else if (message.indexOf('*GAGAL, transaksi yang sama sudah ada dalam 10 menit') >= 0) { | 120 | } else if (message.indexOf('*GAGAL, transaksi yang sama sudah ada dalam 10 menit') >= 0) { |
121 | response_code = '55'; | 121 | response_code = '55'; |
122 | } else if (message.indexOf('saldo sdh dikembalikan') >= 0) { | 122 | } else if (message.indexOf('saldo sdh dikembalikan') >= 0) { |
123 | response_code = '40' | 123 | response_code = '40' |
124 | } else if (message.indexOf('Trx dpt diulang') >= 0) { | 124 | } else if (message.indexOf('Trx dpt diulang') >= 0) { |
125 | response_code = '40' | 125 | response_code = '40' |
126 | } else if (message.indexOf('SUKSES SN Operator:') >= 0) { | 126 | } else if (message.indexOf('SUKSES SN Operator:') >= 0) { |
127 | response_code = '00'; | 127 | response_code = '00'; |
128 | 128 | ||
129 | var sn = parseSN(message); | 129 | var sn = parseSN(message); |
130 | console.log ('SN Operator: ' + sn); | 130 | console.log ('SN Operator: ' + sn); |
131 | 131 | ||
132 | /* | 132 | /* |
133 | if (!sn) { | 133 | if (!sn) { |
134 | 134 | ||
135 | console.log('Missing real operator SN, using SN from suplier'); | 135 | console.log('Missing real operator SN, using SN from suplier'); |
136 | try { | 136 | try { |
137 | sn = result.datacell.trxid[0].trim(); | 137 | sn = result.datacell.trxid[0].trim(); |
138 | } | 138 | } |
139 | catch(err) { | 139 | catch(err) { |
140 | sn = ''; | 140 | sn = ''; |
141 | } | 141 | } |
142 | } | 142 | } |
143 | */ | 143 | */ |
144 | 144 | ||
145 | if (sn) { | 145 | if (sn) { |
146 | message = 'SN=' + sn + '; ' + message; | 146 | message = 'SN=' + sn + '; ' + message; |
147 | } else { | 147 | } else { |
148 | message = 'SN belum didapat. ' + message; | 148 | message = 'SN belum didapat. ' + message; |
149 | response_code = '68'; | 149 | response_code = '68'; |
150 | } | 150 | } |
151 | } | 151 | } |
152 | 152 | ||
153 | callbackReport(request_id, response_code, message); | 153 | callbackReport(request_id, response_code, message); |
154 | }); | 154 | }); |
155 | } | 155 | } |
156 | 156 | ||
157 | function parseSN(message) { | 157 | function parseSN(message) { |
158 | var results = message.match(/SN Operator: .+ SN Kami/); | 158 | var results = message.match(/SN Operator: .+ SN Kami/); |
159 | if (!results || results.length <= 0) { | 159 | if (!results || results.length <= 0) { |
160 | return ''; | 160 | return ''; |
161 | } | 161 | } |
162 | 162 | ||
163 | var result = results[0]; | 163 | var result = results[0]; |
164 | result = result.replace('SN Operator:', ''); | 164 | result = result.replace('SN Operator:', ''); |
165 | result = result.replace('SN Kami', ''); | 165 | result = result.replace('SN Kami', ''); |
166 | result = result.trim(); | 166 | result = result.trim(); |
167 | 167 | ||
168 | if (result == '00') { | 168 | if (result == '00') { |
169 | result = ''; | 169 | result = ''; |
170 | } | 170 | } |
171 | 171 | ||
172 | return result; | 172 | return result; |
173 | } | 173 | } |
174 | 174 | ||
175 | function createServer() { | 175 | function createServer() { |
176 | 176 | ||
177 | var httpServer = http.createServer(function(req, res) { | 177 | var httpServer = http.createServer(function(req, res) { |
178 | var parsed_url = url.parse(req.url, true, true); | 178 | var parsed_url = url.parse(req.url, true, true); |
179 | 179 | ||
180 | console.log('Got request from partner ("' + req.url + '")'); | 180 | console.log('Got request from partner ("' + req.url + '")'); |
181 | 181 | ||
182 | var body = ""; | 182 | var body = ""; |
183 | req.on('data', function (chunk) { | 183 | req.on('data', function (chunk) { |
184 | body += chunk; | 184 | body += chunk; |
185 | }); | 185 | }); |
186 | 186 | ||
187 | req.on('end', function () { | 187 | req.on('end', function () { |
188 | res.writeHead(200); | 188 | res.writeHead(200); |
189 | res.end('OK'); | 189 | res.end('OK'); |
190 | 190 | ||
191 | //console.log(body); | 191 | //console.log(body); |
192 | 192 | ||
193 | if (parsed_url.pathname == '/sn') { | 193 | if (parsed_url.pathname == '/sn') { |
194 | console.log('Reverse report -- SN'); | 194 | console.log('Reverse report -- SN'); |
195 | topupResponseHandler(body); | 195 | topupResponseHandler(body); |
196 | 196 | ||
197 | } else if (parsed_url.pathname = '/refund') { | 197 | } else if (parsed_url.pathname = '/refund') { |
198 | console.log('Reverse report -- REFUND'); | 198 | console.log('Reverse report -- REFUND'); |
199 | callbackReport(parsed_url.query.ref_trxid, '40', parsed_url.query.message); | 199 | callbackReport(parsed_url.query.ref_trxid, '40', parsed_url.query.message); |
200 | 200 | ||
201 | } else { | 201 | } else { |
202 | console.log('Reverse report -- UNKNOWN'); | 202 | console.log('Reverse report -- UNKNOWN'); |
203 | console.log('Ignore unknown request on reverse url'); | 203 | console.log('Ignore unknown request on reverse url'); |
204 | } | 204 | } |
205 | }); | 205 | }); |
206 | }); | 206 | }); |
207 | 207 | ||
208 | httpServer.listen(config.h2h_out.listen_port, function() { | 208 | httpServer.listen(config.h2h_out.listen_port, function() { |
209 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 209 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
210 | }); | 210 | }); |
211 | } | 211 | } |
212 | 212 | ||
213 | function balanceCheck() { | 213 | function balanceCheck() { |
214 | var timestamp = strftime('%H%M%S'); | 214 | var timestamp = strftime('%H%M%S'); |
215 | 215 | ||
216 | var payload = { | 216 | var payload = { |
217 | datacell: [ | 217 | datacell: [ |
218 | {perintah: 'saldo'}, | 218 | {perintah: 'saldo'}, |
219 | {userid: config.h2h_out.userid}, | 219 | {userid: config.h2h_out.userid}, |
220 | {time: timestamp}, | 220 | {time: timestamp}, |
221 | {sgn: calculateBalanceSignature(config.h2h_out.userid, config.h2h_out.password, timestamp)} | 221 | {sgn: calculateBalanceSignature(config.h2h_out.userid, config.h2h_out.password, timestamp)} |
222 | ] | 222 | ] |
223 | }; | 223 | }; |
224 | 224 | ||
225 | var postRequest = { | 225 | var postRequest = { |
226 | host: "202.152.62.2", | 226 | host: "202.152.62.2", |
227 | path: "/RELOAD97.php", | 227 | path: "/RELOAD97.php", |
228 | port: 7713, | 228 | port: 7713, |
229 | method: "POST", | 229 | method: "POST", |
230 | headers: { | 230 | headers: { |
231 | 'Content-Type': 'text/xml', | 231 | 'Content-Type': 'text/xml', |
232 | 'Content-Length': Buffer.byteLength(payload_xml) | 232 | 'Content-Length': Buffer.byteLength(payload_xml) |
233 | } | 233 | } |
234 | }; | 234 | }; |
235 | 235 | ||
236 | var buffer = ""; | 236 | var buffer = ""; |
237 | var req = http.request( postRequest, function( res ) { | 237 | var req = http.request( postRequest, function( res ) { |
238 | 238 | ||
239 | console.log('Status code: ' + res.statusCode ); | 239 | console.log('Status code: ' + res.statusCode ); |
240 | var buffer = ""; | 240 | var buffer = ""; |
241 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 241 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
242 | res.on( "end", function( data ) { | 242 | res.on( "end", function( data ) { |
243 | console.log('CHECK BALANCE RESULT:'); | 243 | console.log('CHECK BALANCE RESULT:'); |
244 | console.log(buffer); | 244 | console.log(buffer); |
245 | }); | 245 | }); |
246 | 246 | ||
247 | }); | 247 | }); |
248 | 248 | ||
249 | req.on('error', function(e) { | 249 | req.on('error', function(e) { |
250 | console.log('problem with request: ' + e.message); | 250 | console.log('problem with request: ' + e.message); |
251 | }); | 251 | }); |
252 | 252 | ||
253 | req.write( payload_xml ); | 253 | req.write( payload_xml ); |
254 | req.end(); | 254 | req.end(); |
255 | 255 | ||
256 | } | 256 | } |
257 | 257 | ||
258 | function balanceFromMessage(message) { | ||
259 | matches = message.match(/Saldo: Rp (\d+)/); | ||
260 | if (!matches) { | ||
261 | return null; | ||
262 | } | ||
263 | |||
264 | if (matches.length <= 1) { | ||
265 | return null; | ||
266 | } | ||
267 | return matches[0]; | ||
268 | } | ||
258 | 269 | ||
259 | function start(_config, _callbackReport) { | 270 | function start(_config, _callbackReport) { |
260 | config = _config; | 271 | config = _config; |
261 | callbackReport = _callbackReport | 272 | callbackReport = _callbackReport |
262 | 273 | ||
263 | createServer(); | 274 | createServer(); |
264 | } | 275 | } |
265 | 276 | ||
266 | exports.start = start; | 277 | exports.start = start; |
267 | exports.topupRequest = topupRequest; | 278 | exports.topupRequest = topupRequest; |
279 | exports.balanceFromMessage = balanceFromMessage; | ||
268 | 280 |