Blame view

partner-datacell.js 4.29 KB
d2933ca99   Adhidarma Hadiwinoto   require http
1
  var http = require('http');
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
2
3
4
  var url = require('url');
  var math = require('mathjs');
  var xml = require('xml');
f53f2def0   Adhidarma Hadiwinoto   mulai tangani dir...
5
  var xml2js = require('xml2js').parseString;
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
6
7
8
9
10
11
12
13
14
15
16
17
  var strftime = require('strftime');
  var xor = require('base64-xor');
  var request = require('request');
  
  var config;
  var callbackReport;
  
  var max_retry = 2;
  var sleep_before_retry = 2000;
  
  function calculateSignature(userid, password, msisdn, timestamp) {
      var a = msisdn.substr(msisdn.length - 4) + timestamp;
e7725f72e   Adhidarma Hadiwinoto   debug sig
18
      console.log(a);
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
19
      var b = userid.substr(0, 4) + password;
e7725f72e   Adhidarma Hadiwinoto   debug sig
20
      console.log(b);
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
21
22
23
24
25
26
27
28
      
      return xor.encode(a,b);
  }
  
  function createPayload(task) {
      var timestamp = strftime('%H%M%S');
      
      var payload = {
9de9fde1f   Adhidarma Hadiwinoto   curly
29
30
31
32
33
34
35
36
37
          datacell: [
              { perintah: 'charge'},
              {oprcode: task['remoteProduct']},
              {userid: config.h2h_out.userid},
              {time: timestamp},
              {msisdn: task['destination']},
              {ref_trxid: task['requestId']},
              {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)}
          ]
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
38
39
40
      };
      
      console.log(payload);
c66f76c6e   Adhidarma Hadiwinoto   top level element
41
42
      return "<?xml version=\"1.0\" ?>
  " + xml(payload);
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  }
  
  function topupRequest(task, retry) {
      if (config.globals.requests_count == undefined) {
          config.globals.requests_count = 1;
      } else {
          config.globals.requests_count++;
      }
      
      if (config.globals.active_requests_count == undefined) {
          config.globals.active_requests_count = 1;
      } else {
          config.globals.active_requests_count++;
      }
      
      if (config.globals.max_active_requests_count == undefined) {
          config.globals.max_active_requests_count = config.globals.active_requests_count;
      } else {
          config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count);
      }
      
      
      if (retry === undefined) {
          retry = max_retry;
      }
      
      var payload_xml = createPayload(task);
40eead5f7   Adhidarma Hadiwinoto   debug payload_xml
70
      console.log(payload_xml);
88b4e0d50   Adhidarma Hadiwinoto   gunakan req.end
71
      
7e26aeff4   Adhidarma Hadiwinoto   coba plain http req
72
73
74
75
      var postRequest = {
          host: "202.152.62.2",
          path: "/RELOAD97.php",
          port: 7713,
a1b7fb567   Adhidarma Hadiwinoto   coba pakai header
76
77
78
79
80
          method: "POST",
          headers: {
              'Content-Type': 'text/xml',
              'Content-Length': Buffer.byteLength(payload_xml)
          }
7e26aeff4   Adhidarma Hadiwinoto   coba plain http req
81
82
83
84
      };
      
      var buffer = "";
      var req = http.request( postRequest, function( res )    {
f53f2def0   Adhidarma Hadiwinoto   mulai tangani dir...
85
86
87
          console.log('Status code: ' + res.statusCode );
          var buffer = "";
          res.on( "data", function( data ) { buffer = buffer + data; } );
a12a4d745   Adhidarma Hadiwinoto   merge direct dan ...
88
          res.on( "end", function( data ) {             
a07de0104   Adhidarma Hadiwinoto   data dan buffer lagi
89
                  topupResponseHandler(buffer);
f53f2def0   Adhidarma Hadiwinoto   mulai tangani dir...
90
          });
7e26aeff4   Adhidarma Hadiwinoto   coba plain http req
91
92
93
94
95
96
97
98
99
  
      });
  
      req.on('error', function(e) {
          console.log('problem with request: ' + e.message);
      });
  
      req.write( payload_xml );
      req.end();
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
100
  }
a12a4d745   Adhidarma Hadiwinoto   merge direct dan ...
101
102
103
104
105
106
107
108
109
110
  function topupResponseHandler(body, request_id) {
      xml2js(body, function (err, result) {
          if (err) {
              console.log(body);
              callbackReport(request_id, '40', buffer);
              return;
          }
          
          console.log(result);
          
0af71fbe8   Adhidarma Hadiwinoto   typo fixed
111
          request_id = result.datacell.ref_trxid[0].trim();
a12a4d745   Adhidarma Hadiwinoto   merge direct dan ...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
          
          var response_code = '68';
          var message = result.datacell.message[0].trim();
          
          if (result.datacell.resultcode[0] == '999') {
              response_code = '40';
              
              if (message.indexOf('Nomor tujuan salah') >= 0) {
                  response_code = '14';
              }
          }
          
          callbackReport(request_id, response_code, message);
      });
  }
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
127
  function createServer() {
e303a718e   Adhidarma Hadiwinoto   penanganan sn
128

e556ce560   Adhidarma Hadiwinoto   refactor nama var...
129
      var httpServer = http.createServer(function(req, res) {
a12a4d745   Adhidarma Hadiwinoto   merge direct dan ...
130
          console.log('Got request from partner ("' + req.url + '")');
e303a718e   Adhidarma Hadiwinoto   penanganan sn
131
132
133
134
135
136
137
          
          var body = "";
          req.on('data', function (chunk) {
              body += chunk;
          });
          
          req.on('end', function () {
e303a718e   Adhidarma Hadiwinoto   penanganan sn
138
139
              res.writeHead(200);
              res.end('OK');
a12a4d745   Adhidarma Hadiwinoto   merge direct dan ...
140
141
142
              
              console.log(body);
              topupResponseHandler(body);
e303a718e   Adhidarma Hadiwinoto   penanganan sn
143
          });
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
      });
      
      httpServer.listen(config.h2h_out.listen_port, function() {
          console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port);
      });
  }
  
  
  function start(_config, _callbackReport) {
      config = _config;
      callbackReport = _callbackReport
  
      createServer();
  }
  
  exports.start = start;
  exports.topupRequest = topupRequest;