Blame view

partner-datacell.js 4.17 KB
d2933ca99   Adhidarma Hadiwinoto   require http
1
  var http = require('http');
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  var url = require('url');
  var math = require('mathjs');
  var xml = require('xml');
  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;
      var b = userid.substr(0, 4) + password;
      
      return xor.encode(a,b);
  }
  
  function createPayload(task) {
      var timestamp = strftime('%H%M%S');
      
      var payload = {
9de9fde1f   Adhidarma Hadiwinoto   curly
26
27
28
29
30
31
32
33
34
          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...
35
36
37
      };
      
      console.log(payload);
c66f76c6e   Adhidarma Hadiwinoto   top level element
38
39
      return "<?xml version=\"1.0\" ?>
  " + xml(payload);
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  }
  
  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
67
      console.log(payload_xml);
274f90989   Adhidarma Hadiwinoto   coba multipart
68
      /*
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
      request.post(config.h2h_out.partner, {message: payload_xml}, function(error, response, body) {
          if (error) {
              var error_mesasge = 'Error requesting to partner: ' + error;
              console.log(error_message);
              callbackReport(task['requestId'], '40', error_message);
              return;
          }
          
          if (response.statusCode != 200) {
              var error_mesasge = 'HTTP status code =  ' + response.statusCode;
              console.log(error_message);
              callbackReport(task['requestId'], '40', error_message);
              return;
          }
          
          console.log('Direct response from partner:');
          console.log(body);
          callbackReport(task['requestId'], '68', 'cek');
          
      });;
274f90989   Adhidarma Hadiwinoto   coba multipart
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      */
      
      request(
          {
              method: 'PUT',
              uri: 'config.h2h_out.partner',
              multipart: [
                  'content-type': 'text/xml',
                  body: payload_xml
              ]
          },
          
          function(error, response, body) {
              if (error) {
                  var error_mesasge = 'Error requesting to partner: ' + error;
                  console.log(error_message);
                  callbackReport(task['requestId'], '40', error_message);
                  return;
              }
              
              if (response.statusCode != 200) {
                  var error_mesasge = 'HTTP status code =  ' + response.statusCode;
                  console.log(error_message);
                  callbackReport(task['requestId'], '40', error_message);
                  return;
              }
              
              console.log('Direct response from partner:');
              console.log(body);
              callbackReport(task['requestId'], '68', 'cek');
          }
      );
      
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
122
123
124
  }
  
  function createServer() {
e556ce560   Adhidarma Hadiwinoto   refactor nama var...
125
126
127
      var httpServer = http.createServer(function(req, res) {
          console.log('Got request from partner: ' + req.url);
          res.end('OK');
ff32317e9   Adhidarma Hadiwinoto   prototype partner...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      });
      
      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;