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