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