Commit e90b0bcd08e5b616e1f1a70f5ede72a1a261c00a

Authored by Adhidarma Hadiwinoto
1 parent f1638d5b25
Exists in master

balance check before topup

Showing 1 changed file with 2 additions and 0 deletions Inline Diff

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