Commit cd0b8704701a8898a218e5086df034d9e3184687

Authored by Adhidarma Hadiwinoto
1 parent 08f290bce0
Exists in master

Include original rc on message

Showing 1 changed file with 5 additions and 0 deletions Inline Diff

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