Commit a1b7fb5673d4a476c18aab43dabda21864f01c9f

Authored by Adhidarma Hadiwinoto
1 parent 7e26aeff47
Exists in master

coba pakai header

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