Commit 75d31c55e38618cf2d876381d0b2b01620a673cd

Authored by Adhidarma Hadiwinoto
1 parent 3b9df1cc7c
Exists in master

dos2unix

Showing 1 changed file with 175 additions and 175 deletions Side-by-side Diff

1   -"use strict";
2   -
3   -process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
4   -
5   -const http = require('http');
6   -const url = require('url');
7   -const request = require('request');
8   -const resendDelay = require('sate24/resend-delay')
9   -
10   -const komodoRc = {
11   - '00': '00',
12   - '03': '40',
13   - '13': '13',
14   - '14': '14',
15   - '30': '40',
16   - '40': '40',
17   - '68': '68',
18   - '55': '55',
19   - '91': '91',
20   - '92': '40',
21   - '96': '68'
22   -}
23   -
24   -var config;
25   -var aaa;
26   -var logger;
27   -
28   -function start(options) {
29   - if (!options) {
30   - console.log('Undefined options, terminating....');
31   - process.exit(1);
32   - }
33   -
34   - if (options.config) {
35   - config = options.config;
36   - } else {
37   - console.log('Undefined options.config, terminating....')
38   - process.exit(1);
39   - }
40   -
41   - if (options.aaa) {
42   - aaa = options.aaa;
43   - } else {
44   - console.log('Undefined options.aaa, terminating....')
45   - process.exit(1);
46   - }
47   -
48   - if (options && options.logger) {
49   - logger = options.logger;
50   - } else {
51   - console.log('Undefined options.logger, terminating....')
52   - process.exit(1);
53   - }
54   -
55   - resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice});
56   -
57   - createReverseHttpServer();
58   -
59   -}
60   -
61   -function callbackReport(requestId, rc, message, options) {
62   - aaa.callbackReportWithPushToMongoDb(requestId, rc, message);
63   -
64   - if (!options || !options.task) {
65   - return;
66   - }
67   -
68   - if (rc == '68') {
69   - resendDelay.register(options.task);
70   - } else {
71   - resendDelay.cancel(options.task)
72   - }
73   -}
74   -
75   -function topupRequest(task, pendingOnConnectError) {
76   - aaa.insertTaskToMongoDb(task);
77   -
78   - const requestOptions = {
79   - url: config.h2h_out.partner,
80   - qs: {
81   - terminal_name: config.h2h_out.terminal_name,
82   - password: config.h2h_out.password,
83   - product_name: task.remoteProduct,
84   - destination: task.destination,
85   - request_id: task.requestId,
86   - reverse_url: config.h2h_out.reverse_url
87   - }
88   - }
89   -
90   - logger.verbose('Requesting to partner', {request_options: requestOptions});
91   -
92   - request(requestOptions, function(err, response, body) {
93   - if (err) {
94   - logger.warn('Error requesting to partner', {err: err});
95   -
96   - let rc = '68';
97   -
98   - if (err.syscall == 'connect' && !pendingOnConnectError) {
99   - rc = '91';
100   - }
101   - callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task});
102   - return;
103   - }
104   -
105   - if (response.statusCode != 200) {
106   - let rc = '68';
107   -
108   - callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task});
109   - return;
110   - }
111   -
112   - let result = parsePartnerMessage(body);
113   -
114   - if (!result) {
115   - callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task});
116   - return;
117   - }
118   -
119   - processPartnerResponse(result, task);
120   -
121   - })
122   -}
123   -
124   -function topupAdvice(task) {
125   - topupRequest(task, true);
126   -}
127   -
128   -function processPartnerResponse(resObj, task) {
129   - let st24Rc = '68';
130   -
131   - if (komodoRc[resObj.rc]) {
132   - st24Rc = komodoRc[resObj.rc];
133   - }
134   -
135   - let st24Message = resObj.message;
136   - if (resObj.sn) {
137   - st24Message = 'SN=' + resObj.sn + '; ' + st24Message;
138   - }
139   -
140   - callbackReport(resObj.request_id, st24Rc, st24Message, {task: task});
141   -}
142   -
143   -function parsePartnerMessage(partner_message) {
144   - let result;
145   - try {
146   - result = JSON.parse(partner_message);
147   -
148   - }
149   - catch(e) {
150   - logger.verbose('Exception on parsing partner message: ' + partner_message);
151   - result = null;
152   - }
153   -
154   - return result;
155   -}
156   -
157   -function createReverseHttpServer() {
158   - let listenPort = config.h2h_out.listen_port;
159   -
160   - http.createServer(onReverseReport).listen(listenPort, function() {
161   - logger.info('HTTP Reverse/Report server listen on port ' + listenPort);
162   - });
163   -}
164   -
165   -function onReverseReport(request, response) {
166   - response.end('OK');
167   -
168   - const qs = url.parse(request.url, true).query;
169   - logger.verbose('Got reverse report from partner', {qs: qs});
170   - processPartnerResponse(qs);
171   -}
172   -
173   -
174   -exports.start = start;
175   -exports.topupRequest = topupRequest;
  1 +"use strict";
  2 +
  3 +process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  4 +
  5 +const http = require('http');
  6 +const url = require('url');
  7 +const request = require('request');
  8 +const resendDelay = require('sate24/resend-delay')
  9 +
  10 +const komodoRc = {
  11 + '00': '00',
  12 + '03': '40',
  13 + '13': '13',
  14 + '14': '14',
  15 + '30': '40',
  16 + '40': '40',
  17 + '68': '68',
  18 + '55': '55',
  19 + '91': '91',
  20 + '92': '40',
  21 + '96': '68'
  22 +}
  23 +
  24 +var config;
  25 +var aaa;
  26 +var logger;
  27 +
  28 +function start(options) {
  29 + if (!options) {
  30 + console.log('Undefined options, terminating....');
  31 + process.exit(1);
  32 + }
  33 +
  34 + if (options.config) {
  35 + config = options.config;
  36 + } else {
  37 + console.log('Undefined options.config, terminating....')
  38 + process.exit(1);
  39 + }
  40 +
  41 + if (options.aaa) {
  42 + aaa = options.aaa;
  43 + } else {
  44 + console.log('Undefined options.aaa, terminating....')
  45 + process.exit(1);
  46 + }
  47 +
  48 + if (options && options.logger) {
  49 + logger = options.logger;
  50 + } else {
  51 + console.log('Undefined options.logger, terminating....')
  52 + process.exit(1);
  53 + }
  54 +
  55 + resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice});
  56 +
  57 + createReverseHttpServer();
  58 +
  59 +}
  60 +
  61 +function callbackReport(requestId, rc, message, options) {
  62 + aaa.callbackReportWithPushToMongoDb(requestId, rc, message);
  63 +
  64 + if (!options || !options.task) {
  65 + return;
  66 + }
  67 +
  68 + if (rc == '68') {
  69 + resendDelay.register(options.task);
  70 + } else {
  71 + resendDelay.cancel(options.task)
  72 + }
  73 +}
  74 +
  75 +function topupRequest(task, pendingOnConnectError) {
  76 + aaa.insertTaskToMongoDb(task);
  77 +
  78 + const requestOptions = {
  79 + url: config.h2h_out.partner,
  80 + qs: {
  81 + terminal_name: config.h2h_out.terminal_name,
  82 + password: config.h2h_out.password,
  83 + product_name: task.remoteProduct,
  84 + destination: task.destination,
  85 + request_id: task.requestId,
  86 + reverse_url: config.h2h_out.reverse_url
  87 + }
  88 + }
  89 +
  90 + logger.verbose('Requesting to partner', {request_options: requestOptions});
  91 +
  92 + request(requestOptions, function(err, response, body) {
  93 + if (err) {
  94 + logger.warn('Error requesting to partner', {err: err});
  95 +
  96 + let rc = '68';
  97 +
  98 + if (err.syscall == 'connect' && !pendingOnConnectError) {
  99 + rc = '91';
  100 + }
  101 + callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task});
  102 + return;
  103 + }
  104 +
  105 + if (response.statusCode != 200) {
  106 + let rc = '68';
  107 +
  108 + callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task});
  109 + return;
  110 + }
  111 +
  112 + let result = parsePartnerMessage(body);
  113 +
  114 + if (!result) {
  115 + callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task});
  116 + return;
  117 + }
  118 +
  119 + processPartnerResponse(result, task);
  120 +
  121 + })
  122 +}
  123 +
  124 +function topupAdvice(task) {
  125 + topupRequest(task, true);
  126 +}
  127 +
  128 +function processPartnerResponse(resObj, task) {
  129 + let st24Rc = '68';
  130 +
  131 + if (komodoRc[resObj.rc]) {
  132 + st24Rc = komodoRc[resObj.rc];
  133 + }
  134 +
  135 + let st24Message = resObj.message;
  136 + if (resObj.sn) {
  137 + st24Message = 'SN=' + resObj.sn + '; ' + st24Message;
  138 + }
  139 +
  140 + callbackReport(resObj.request_id, st24Rc, st24Message, {task: task});
  141 +}
  142 +
  143 +function parsePartnerMessage(partner_message) {
  144 + let result;
  145 + try {
  146 + result = JSON.parse(partner_message);
  147 +
  148 + }
  149 + catch(e) {
  150 + logger.verbose('Exception on parsing partner message: ' + partner_message);
  151 + result = null;
  152 + }
  153 +
  154 + return result;
  155 +}
  156 +
  157 +function createReverseHttpServer() {
  158 + let listenPort = config.h2h_out.listen_port;
  159 +
  160 + http.createServer(onReverseReport).listen(listenPort, function() {
  161 + logger.info('HTTP Reverse/Report server listen on port ' + listenPort);
  162 + });
  163 +}
  164 +
  165 +function onReverseReport(request, response) {
  166 + response.end('OK');
  167 +
  168 + const qs = url.parse(request.url, true).query;
  169 + logger.verbose('Got reverse report from partner', {qs: qs});
  170 + processPartnerResponse(qs);
  171 +}
  172 +
  173 +
  174 +exports.start = start;
  175 +exports.topupRequest = topupRequest;
176 176 exports.topupAdvice = topupAdvice;
177 177 \ No newline at end of file