Commit 93f2552cc8afaead97236c9476d4bc430b3c80c6

Authored by Adhidarma Hadiwinoto
1 parent 76325823ce
Exists in master

error code

Showing 1 changed file with 8 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 console.log(a); 18 console.log(a);
19 var b = userid.substr(0, 4) + password; 19 var b = userid.substr(0, 4) + password;
20 console.log(b); 20 console.log(b);
21 21
22 return xor.encode(a,b); 22 return xor.encode(a,b);
23 } 23 }
24 24
25 function createPayload(task) { 25 function createPayload(task) {
26 var timestamp = strftime('%H%M%S'); 26 var timestamp = strftime('%H%M%S');
27 27
28 var payload = { 28 var payload = {
29 datacell: [ 29 datacell: [
30 { perintah: 'charge'}, 30 { perintah: 'charge'},
31 {oprcode: task['remoteProduct']}, 31 {oprcode: task['remoteProduct']},
32 {userid: config.h2h_out.userid}, 32 {userid: config.h2h_out.userid},
33 {time: timestamp}, 33 {time: timestamp},
34 {msisdn: task['destination']}, 34 {msisdn: task['destination']},
35 {ref_trxid: task['requestId']}, 35 {ref_trxid: task['requestId']},
36 {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} 36 {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)}
37 ] 37 ]
38 }; 38 };
39 39
40 console.log(payload); 40 console.log(payload);
41 return "<?xml version=\"1.0\" ?>\n" + xml(payload); 41 return "<?xml version=\"1.0\" ?>\n" + xml(payload);
42 } 42 }
43 43
44 function topupRequest(task, retry) { 44 function topupRequest(task, retry) {
45 if (config.globals.requests_count == undefined) { 45 if (config.globals.requests_count == undefined) {
46 config.globals.requests_count = 1; 46 config.globals.requests_count = 1;
47 } else { 47 } else {
48 config.globals.requests_count++; 48 config.globals.requests_count++;
49 } 49 }
50 50
51 if (config.globals.active_requests_count == undefined) { 51 if (config.globals.active_requests_count == undefined) {
52 config.globals.active_requests_count = 1; 52 config.globals.active_requests_count = 1;
53 } else { 53 } else {
54 config.globals.active_requests_count++; 54 config.globals.active_requests_count++;
55 } 55 }
56 56
57 if (config.globals.max_active_requests_count == undefined) { 57 if (config.globals.max_active_requests_count == undefined) {
58 config.globals.max_active_requests_count = config.globals.active_requests_count; 58 config.globals.max_active_requests_count = config.globals.active_requests_count;
59 } else { 59 } else {
60 config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count); 60 config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count);
61 } 61 }
62 62
63 63
64 if (retry === undefined) { 64 if (retry === undefined) {
65 retry = max_retry; 65 retry = max_retry;
66 } 66 }
67 67
68 var payload_xml = createPayload(task); 68 var payload_xml = createPayload(task);
69 console.log(payload_xml); 69 console.log(payload_xml);
70 70
71 var postRequest = { 71 var postRequest = {
72 host: "202.152.62.2", 72 host: "202.152.62.2",
73 path: "/RELOAD97.php", 73 path: "/RELOAD97.php",
74 port: 7713, 74 port: 7713,
75 method: "POST", 75 method: "POST",
76 headers: { 76 headers: {
77 'Content-Type': 'text/xml', 77 'Content-Type': 'text/xml',
78 'Content-Length': Buffer.byteLength(payload_xml) 78 'Content-Length': Buffer.byteLength(payload_xml)
79 } 79 }
80 }; 80 };
81 81
82 var buffer = ""; 82 var buffer = "";
83 var req = http.request( postRequest, function( res ) { 83 var req = http.request( postRequest, function( res ) {
84 84
85 console.log('Status code: ' + res.statusCode ); 85 console.log('Status code: ' + res.statusCode );
86 var buffer = ""; 86 var buffer = "";
87 res.on( "data", function( data ) { buffer = buffer + data; } ); 87 res.on( "data", function( data ) { buffer = buffer + data; } );
88 res.on( "end", function( data ) { 88 res.on( "end", function( data ) {
89 89
90 xml2js(buffer, function (err, result) { 90 xml2js(buffer, function (err, result) {
91 if (err) { 91 if (err) {
92 console.log(buffer); 92 console.log(buffer);
93 callbackReport(task['requestId'], '40', buffer); 93 callbackReport(task['requestId'], '40', buffer);
94 return; 94 return;
95 } 95 }
96 96
97 console.log(result); 97 console.log(result);
98
99 var response_code = '68';
100 var message = result.datacell.resultcode.message[0].trim();
101 if (result.datacell.resultcode[0] == '999') {
102 response_code = '40';
103 }
104
105 callbackReport(task['requestId'], response_code, message);
98 }); 106 });
99 107
100 }); 108 });
101 109
102 }); 110 });
103 111
104 req.on('error', function(e) { 112 req.on('error', function(e) {
105 console.log('problem with request: ' + e.message); 113 console.log('problem with request: ' + e.message);
106 }); 114 });
107 115
108 req.write( payload_xml ); 116 req.write( payload_xml );
109 req.end(); 117 req.end();
110 } 118 }
111 119
112 function createServer() { 120 function createServer() {
113 var httpServer = http.createServer(function(req, res) { 121 var httpServer = http.createServer(function(req, res) {
114 console.log('Got request from partner, url: ' + req.url); 122 console.log('Got request from partner, url: ' + req.url);
115 console.log('REQ:'); 123 console.log('REQ:');
116 console.log(req); 124 console.log(req);
117 res.end('OK'); 125 res.end('OK');
118 }); 126 });
119 127
120 httpServer.listen(config.h2h_out.listen_port, function() { 128 httpServer.listen(config.h2h_out.listen_port, function() {
121 console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); 129 console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port);
122 }); 130 });
123 } 131 }
124 132
125 133
126 function start(_config, _callbackReport) { 134 function start(_config, _callbackReport) {
127 config = _config; 135 config = _config;
128 callbackReport = _callbackReport 136 callbackReport = _callbackReport
129 137
130 createServer(); 138 createServer();
131 } 139 }
132 140
133 exports.start = start; 141 exports.start = start;
134 exports.topupRequest = topupRequest; 142 exports.topupRequest = topupRequest;
135 143