Commit 77ce67db6e9fecb8788e73df1fd2781dedca3190

Authored by Adhidarma Hadiwinoto
1 parent e7725f72ec
Exists in master

verbose on req

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