Commit d28a39698bf99f026f62f8c4846b7fafdc940f1e

Authored by Adhidarma Hadiwinoto
1 parent 77ce67db6e
Exists in master

buffer jadi data

Showing 1 changed file with 1 additions and 24 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 /*
71 request({uri: config.h2h_out.partner, method: 'POST'}, function(error, response, body) {
72 if (error) {
73 var error_mesasge = 'Error requesting to partner: ' + error;
74 console.log(error_message);
75 callbackReport(task['requestId'], '40', error_message);
76 return;
77 }
78
79 if (response.statusCode != 200) {
80 var error_mesasge = 'HTTP status code = ' + response.statusCode;
81 console.log(error_message);
82 callbackReport(task['requestId'], '40', error_message);
83 return;
84 }
85
86 console.log('Direct response from partner:');
87 console.log(body);
88 callbackReport(task['requestId'], '68', 'cek');
89
90 }).end(payload_xml);
91 */
92
93 var postRequest = { 70 var postRequest = {
94 host: "202.152.62.2", 71 host: "202.152.62.2",
95 path: "/RELOAD97.php", 72 path: "/RELOAD97.php",
96 port: 7713, 73 port: 7713,
97 method: "POST", 74 method: "POST",
98 headers: { 75 headers: {
99 'Content-Type': 'text/xml', 76 'Content-Type': 'text/xml',
100 'Content-Length': Buffer.byteLength(payload_xml) 77 'Content-Length': Buffer.byteLength(payload_xml)
101 } 78 }
102 }; 79 };
103 80
104 var buffer = ""; 81 var buffer = "";
105 var req = http.request( postRequest, function( res ) { 82 var req = http.request( postRequest, function( res ) {
106 83
107 console.log('Status code: ' + res.statusCode ); 84 console.log('Status code: ' + res.statusCode );
108 var buffer = ""; 85 var buffer = "";
109 res.on( "data", function( data ) { buffer = buffer + data; } ); 86 res.on( "data", function( data ) { buffer = buffer + data; } );
110 res.on( "end", function( data ) { console.log( buffer ); } ); 87 res.on( "end", function( data ) { console.log( data ); } );
111 88
112 }); 89 });
113 90
114 req.on('error', function(e) { 91 req.on('error', function(e) {
115 console.log('problem with request: ' + e.message); 92 console.log('problem with request: ' + e.message);
116 }); 93 });
117 94
118 req.write( payload_xml ); 95 req.write( payload_xml );
119 req.end(); 96 req.end();
120 } 97 }
121 98
122 function createServer() { 99 function createServer() {
123 var httpServer = http.createServer(function(req, res) { 100 var httpServer = http.createServer(function(req, res) {
124 console.log('Got request from partner, url: ' + req.url); 101 console.log('Got request from partner, url: ' + req.url);
125 console.log('REQ:'); 102 console.log('REQ:');
126 console.log(req); 103 console.log(req);
127 res.end('OK'); 104 res.end('OK');
128 }); 105 });
129 106
130 httpServer.listen(config.h2h_out.listen_port, function() { 107 httpServer.listen(config.h2h_out.listen_port, function() {
131 console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); 108 console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port);
132 }); 109 });
133 } 110 }
134 111
135 112
136 function start(_config, _callbackReport) { 113 function start(_config, _callbackReport) {
137 config = _config; 114 config = _config;
138 callbackReport = _callbackReport 115 callbackReport = _callbackReport
139 116
140 createServer(); 117 createServer();
141 } 118 }
142 119
143 exports.start = start; 120 exports.start = start;
144 exports.topupRequest = topupRequest; 121 exports.topupRequest = topupRequest;
145 122