Commit 4c12d725cc3af9df0d4cb1e7f1ed556bc4835fda
1 parent
fdf5cd24dc
Exists in
master
logging request
Showing 1 changed file with 2 additions and 0 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 | '03': '40', | 11 | '03': '40', |
12 | '13': '13', | 12 | '13': '13', |
13 | '14': '14', | 13 | '14': '14', |
14 | '30': '40', | 14 | '30': '40', |
15 | '68': '68', | 15 | '68': '68', |
16 | '55': '55', | 16 | '55': '55', |
17 | '91': '91', | 17 | '91': '91', |
18 | '92': '40', | 18 | '92': '40', |
19 | '96': '68' | 19 | '96': '68' |
20 | } | 20 | } |
21 | 21 | ||
22 | var config; | 22 | var config; |
23 | var aaa; | 23 | var aaa; |
24 | var logger; | 24 | var logger; |
25 | 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 | /* | 62 | /* |
63 | if (!options.task) { | 63 | if (!options.task) { |
64 | return; | 64 | return; |
65 | } | 65 | } |
66 | 66 | ||
67 | if (rc == '68') { | 67 | if (rc == '68') { |
68 | resendDelay.register(options.task); | 68 | resendDelay.register(options.task); |
69 | } else { | 69 | } else { |
70 | resendDelay.cancel(options.task) | 70 | resendDelay.cancel(options.task) |
71 | } | 71 | } |
72 | */ | 72 | */ |
73 | 73 | ||
74 | } | 74 | } |
75 | 75 | ||
76 | function topupRequest(task, pendingOnConnectError) { | 76 | function topupRequest(task, pendingOnConnectError) { |
77 | aaa.insertTaskToMongoDb(task); | 77 | aaa.insertTaskToMongoDb(task); |
78 | 78 | ||
79 | const requestOptions = { | 79 | const requestOptions = { |
80 | url: config.h2h_out.partner, | 80 | url: config.h2h_out.partner, |
81 | qs: { | 81 | qs: { |
82 | terminal_name: config.h2h_out.username, | 82 | terminal_name: config.h2h_out.username, |
83 | password: config.h2h_out.password, | 83 | password: config.h2h_out.password, |
84 | product_name: task.remoteProduct, | 84 | product_name: task.remoteProduct, |
85 | destination: task.destination, | 85 | destination: task.destination, |
86 | request_id: task.requestId | 86 | request_id: task.requestId |
87 | } | 87 | } |
88 | } | 88 | } |
89 | 89 | ||
90 | logger.verbose('Requesting to partner', {request_options: requestOptions}); | ||
91 | |||
90 | request(requestOptions, function(err, response, body) { | 92 | request(requestOptions, function(err, response, body) { |
91 | if (err) { | 93 | if (err) { |
92 | logger.warn('Error requesting to partner', {err: err}); | 94 | logger.warn('Error requesting to partner', {err: err}); |
93 | 95 | ||
94 | let rc = '68'; | 96 | let rc = '68'; |
95 | 97 | ||
96 | if (err.syscall == 'connect' && !pendingOnConnectError) { | 98 | if (err.syscall == 'connect' && !pendingOnConnectError) { |
97 | rc = '91'; | 99 | rc = '91'; |
98 | } | 100 | } |
99 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); | 101 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); |
100 | return; | 102 | return; |
101 | } | 103 | } |
102 | 104 | ||
103 | if (response.statusCode != 200) { | 105 | if (response.statusCode != 200) { |
104 | let rc = '68'; | 106 | let rc = '68'; |
105 | 107 | ||
106 | callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task}); | 108 | callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task}); |
107 | return; | 109 | return; |
108 | } | 110 | } |
109 | 111 | ||
110 | let result = parsePartnerMessage(body); | 112 | let result = parsePartnerMessage(body); |
111 | processPartnerResponse(result); | 113 | processPartnerResponse(result); |
112 | 114 | ||
113 | }) | 115 | }) |
114 | } | 116 | } |
115 | 117 | ||
116 | function topupAdvice(task) { | 118 | function topupAdvice(task) { |
117 | topupRequest(task, true); | 119 | topupRequest(task, true); |
118 | } | 120 | } |
119 | 121 | ||
120 | function processPartnerResponse(resObj, task) { | 122 | function processPartnerResponse(resObj, task) { |
121 | if (!resObj) { | 123 | if (!resObj) { |
122 | 124 | ||
123 | if (task.requestId) { | 125 | if (task.requestId) { |
124 | callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task}); | 126 | callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task}); |
125 | } | 127 | } |
126 | else { | 128 | else { |
127 | logger.warn('UNDEFINED REQUEST ID'); | 129 | logger.warn('UNDEFINED REQUEST ID'); |
128 | } | 130 | } |
129 | 131 | ||
130 | return; | 132 | return; |
131 | } | 133 | } |
132 | 134 | ||
133 | let st24Rc = '68'; | 135 | let st24Rc = '68'; |
134 | 136 | ||
135 | if (komodoRc[resObj.rc]) { | 137 | if (komodoRc[resObj.rc]) { |
136 | st24Rc = komodoRc[resObj.rc]; | 138 | st24Rc = komodoRc[resObj.rc]; |
137 | } | 139 | } |
138 | 140 | ||
139 | let st24Message = resObj.message; | 141 | let st24Message = resObj.message; |
140 | if (resObj.sn) { | 142 | if (resObj.sn) { |
141 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; | 143 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; |
142 | } | 144 | } |
143 | 145 | ||
144 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); | 146 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); |
145 | } | 147 | } |
146 | 148 | ||
147 | function parsePartnerMessage(partner_message) { | 149 | function parsePartnerMessage(partner_message) { |
148 | let result; | 150 | let result; |
149 | try { | 151 | try { |
150 | result = JSON.parse(partner_message); | 152 | result = JSON.parse(partner_message); |
151 | 153 | ||
152 | } | 154 | } |
153 | catch(e) { | 155 | catch(e) { |
154 | result = null; | 156 | result = null; |
155 | } | 157 | } |
156 | 158 | ||
157 | return result; | 159 | return result; |
158 | } | 160 | } |
159 | 161 | ||
160 | function createReverseHttpServer() { | 162 | function createReverseHttpServer() { |
161 | let listenPort = config.h2h_out.listen_port; | 163 | let listenPort = config.h2h_out.listen_port; |
162 | 164 | ||
163 | http.createServer(onReverseReport).listen(listenPort, function() { | 165 | http.createServer(onReverseReport).listen(listenPort, function() { |
164 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); | 166 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); |
165 | }); | 167 | }); |
166 | } | 168 | } |
167 | 169 | ||
168 | function onReverseReport(request, response) { | 170 | function onReverseReport(request, response) { |
169 | response.end('OK'); | 171 | response.end('OK'); |
170 | 172 | ||
171 | const qs = url.parse(request.url, true).query; | 173 | const qs = url.parse(request.url, true).query; |
172 | logger.verbose('Got reverse report from partner', {qs: qs}); | 174 | logger.verbose('Got reverse report from partner', {qs: qs}); |
173 | processPartnerResponse(qs); | 175 | processPartnerResponse(qs); |
174 | } | 176 | } |
175 | 177 | ||
176 | 178 | ||
177 | exports.start = start; | 179 | exports.start = start; |
178 | exports.topupRequest = topupRequest; | 180 | exports.topupRequest = topupRequest; |
179 | exports.topupAdvice = topupAdvice; | 181 | exports.topupAdvice = topupAdvice; |
180 | 182 |