Commit bf5dde446ee4a98145305494f09fe88383744c0e
1 parent
6ed4dffbff
Exists in
master
Replace space on SN with dash
Showing 1 changed file with 5 additions and 1 deletions Inline Diff
partner-komodo.js
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 | if (resObj.sn) { | 137 | if (resObj.sn) { |
138 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; | 138 | let sn = resObj.sn; |
139 | if (sn.indexOf('/') >= 0) { | ||
140 | sn = sn.trim().replace(/[\s-]+/g, '-'); | ||
141 | } | ||
142 | st24Message = 'SN=' + sn + '; ' + st24Message; | ||
139 | } | 143 | } |
140 | 144 | ||
141 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); | 145 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); |
142 | } | 146 | } |
143 | 147 | ||
144 | function parsePartnerMessage(partner_message) { | 148 | function parsePartnerMessage(partner_message) { |
145 | let result; | 149 | let result; |
146 | try { | 150 | try { |
147 | result = JSON.parse(partner_message); | 151 | result = JSON.parse(partner_message); |
148 | 152 | ||
149 | } | 153 | } |
150 | catch(e) { | 154 | catch(e) { |
151 | logger.verbose('Exception on parsing partner message: ' + partner_message); | 155 | logger.verbose('Exception on parsing partner message: ' + partner_message); |
152 | result = null; | 156 | result = null; |
153 | } | 157 | } |
154 | 158 | ||
155 | return result; | 159 | return result; |
156 | } | 160 | } |
157 | 161 | ||
158 | function createReverseHttpServer() { | 162 | function createReverseHttpServer() { |
159 | let listenPort = config.h2h_out.listen_port; | 163 | let listenPort = config.h2h_out.listen_port; |
160 | 164 | ||
161 | http.createServer(onReverseReport).listen(listenPort, function() { | 165 | http.createServer(onReverseReport).listen(listenPort, function() { |
162 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); | 166 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); |
163 | }); | 167 | }); |
164 | } | 168 | } |
165 | 169 | ||
166 | function onReverseReport(request, response) { | 170 | function onReverseReport(request, response) { |
167 | response.end('OK'); | 171 | response.end('OK'); |
168 | 172 | ||
169 | const qs = url.parse(request.url, true).query; | 173 | const qs = url.parse(request.url, true).query; |
170 | qs.message = 'REVERSE REPORT: ' + qs.message; | 174 | qs.message = 'REVERSE REPORT: ' + qs.message; |
171 | logger.verbose('Got reverse report from partner', {qs: qs}); | 175 | logger.verbose('Got reverse report from partner', {qs: qs}); |
172 | processPartnerResponse(qs); | 176 | processPartnerResponse(qs); |
173 | } | 177 | } |
174 | 178 | ||
175 | 179 | ||
176 | exports.start = start; | 180 | exports.start = start; |
177 | exports.topupRequest = topupRequest; | 181 | exports.topupRequest = topupRequest; |
178 | exports.topupAdvice = topupAdvice; | 182 | exports.topupAdvice = topupAdvice; |
179 | 183 |