Commit dd7844a895e6b08b0dbe6dff08701a762040139d
1 parent
11b9c8c18b
Exists in
master
penanganan error
Showing 1 changed file with 6 additions and 12 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.terminal_name, | 82 | terminal_name: config.h2h_out.terminal_name, |
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}); | 90 | logger.verbose('Requesting to partner', {request_options: requestOptions}); |
91 | 91 | ||
92 | request(requestOptions, function(err, response, body) { | 92 | request(requestOptions, function(err, response, body) { |
93 | if (err) { | 93 | if (err) { |
94 | logger.warn('Error requesting to partner', {err: err}); | 94 | logger.warn('Error requesting to partner', {err: err}); |
95 | 95 | ||
96 | let rc = '68'; | 96 | let rc = '68'; |
97 | 97 | ||
98 | if (err.syscall == 'connect' && !pendingOnConnectError) { | 98 | if (err.syscall == 'connect' && !pendingOnConnectError) { |
99 | rc = '91'; | 99 | rc = '91'; |
100 | } | 100 | } |
101 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); | 101 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); |
102 | return; | 102 | return; |
103 | } | 103 | } |
104 | 104 | ||
105 | if (response.statusCode != 200) { | 105 | if (response.statusCode != 200) { |
106 | let rc = '68'; | 106 | let rc = '68'; |
107 | 107 | ||
108 | 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}); |
109 | return; | 109 | return; |
110 | } | 110 | } |
111 | 111 | ||
112 | let result = parsePartnerMessage(body); | 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 | |||
113 | processPartnerResponse(result, task); | 119 | processPartnerResponse(result, task); |
114 | 120 | ||
115 | }) | 121 | }) |
116 | } | 122 | } |
117 | 123 | ||
118 | function topupAdvice(task) { | 124 | function topupAdvice(task) { |
119 | topupRequest(task, true); | 125 | topupRequest(task, true); |
120 | } | 126 | } |
121 | 127 | ||
122 | function processPartnerResponse(resObj, task) { | 128 | function processPartnerResponse(resObj, task) { |
123 | if (!resObj) { | ||
124 | |||
125 | if (task && task.requestId) { | ||
126 | callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task}); | ||
127 | } | ||
128 | else { | ||
129 | logger.warn('UNDEFINED REQUEST ID'); | ||
130 | } | ||
131 | |||
132 | return; | ||
133 | } | ||
134 | |||
135 | let st24Rc = '68'; | 129 | let st24Rc = '68'; |
136 | 130 | ||
137 | if (komodoRc[resObj.rc]) { | 131 | if (komodoRc[resObj.rc]) { |
138 | st24Rc = komodoRc[resObj.rc]; | 132 | st24Rc = komodoRc[resObj.rc]; |
139 | } | 133 | } |
140 | 134 | ||
141 | let st24Message = resObj.message; | 135 | let st24Message = resObj.message; |
142 | if (resObj.sn) { | 136 | if (resObj.sn) { |
143 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; | 137 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; |
144 | } | 138 | } |
145 | 139 | ||
146 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); | 140 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); |
147 | } | 141 | } |
148 | 142 | ||
149 | function parsePartnerMessage(partner_message) { | 143 | function parsePartnerMessage(partner_message) { |
150 | let result; | 144 | let result; |
151 | try { | 145 | try { |
152 | result = JSON.parse(partner_message); | 146 | result = JSON.parse(partner_message); |
153 | 147 | ||
154 | } | 148 | } |
155 | catch(e) { | 149 | catch(e) { |
156 | logger.verbose('Exception on parsing partner message: ' + partner_message); | 150 | logger.verbose('Exception on parsing partner message: ' + partner_message); |
157 | result = null; | 151 | result = null; |
158 | } | 152 | } |
159 | 153 | ||
160 | return result; | 154 | return result; |
161 | } | 155 | } |
162 | 156 | ||
163 | function createReverseHttpServer() { | 157 | function createReverseHttpServer() { |
164 | let listenPort = config.h2h_out.listen_port; | 158 | let listenPort = config.h2h_out.listen_port; |
165 | 159 | ||
166 | http.createServer(onReverseReport).listen(listenPort, function() { | 160 | http.createServer(onReverseReport).listen(listenPort, function() { |
167 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); | 161 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); |
168 | }); | 162 | }); |
169 | } | 163 | } |
170 | 164 | ||
171 | function onReverseReport(request, response) { | 165 | function onReverseReport(request, response) { |
172 | response.end('OK'); | 166 | response.end('OK'); |
173 | 167 | ||
174 | const qs = url.parse(request.url, true).query; | 168 | const qs = url.parse(request.url, true).query; |
175 | logger.verbose('Got reverse report from partner', {qs: qs}); | 169 | logger.verbose('Got reverse report from partner', {qs: qs}); |
176 | processPartnerResponse(qs); | 170 | processPartnerResponse(qs); |
177 | } | 171 | } |