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