Commit 422b88762a1a3dce4032e3dd9b4e029e946eb133
1 parent
6ec424b07d
Exists in
master
topupCheck
Showing 1 changed file with 63 additions and 5 deletions Inline Diff
partner-bayarkilat.js
1 | var request = require('request'); | 1 | var request = require('request'); |
2 | var url = require('url'); | 2 | var url = require('url'); |
3 | var winston = require('winston'); | 3 | var winston = require('winston'); |
4 | var xml2jsParser = require('xml2js').parseString; | 4 | var xml2jsParser = require('xml2js').parseString; |
5 | 5 | ||
6 | var config; | 6 | var config; |
7 | var aaa; | 7 | var aaa; |
8 | var callbackReport; | 8 | var callbackReport; |
9 | var logger; | 9 | var logger; |
10 | 10 | ||
11 | var maxCheckRetry = 20; | ||
12 | var delayBeforeCheckRetry = 30 * 1000; | ||
13 | |||
11 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | 14 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
12 | 15 | ||
13 | function start(_config, _callbackReport, options) { | 16 | function start(_config, _callbackReport, options) { |
14 | config = _config; | 17 | config = _config; |
15 | callbackReport = _callbackReport; | 18 | callbackReport = _callbackReport; |
16 | 19 | ||
17 | if (options && options.aaa) { | 20 | if (options && options.aaa) { |
18 | aaa = options.aaa; | 21 | aaa = options.aaa; |
19 | } | 22 | } |
20 | 23 | ||
21 | if (options && options.logger) { | 24 | if (options && options.logger) { |
22 | logger = options.logger; | 25 | logger = options.logger; |
23 | } else { | 26 | } else { |
24 | logger = new winston.Logger({ | 27 | logger = new winston.Logger({ |
25 | transports: [ | 28 | transports: [ |
26 | new (winston.transports.Console)() | 29 | new (winston.transports.Console)() |
27 | ] | 30 | ] |
28 | }); | 31 | }); |
29 | } | 32 | } |
30 | } | 33 | } |
31 | 34 | ||
32 | 35 | function createRequestOptions(methodName, task) { | |
33 | function topupRequest(task, retry) { | 36 | if (('PURCHASE', 'CHECKING').indexOf(methodName) < 0) { |
34 | aaa.insertTaskToMongoDb(task); | 37 | return; |
38 | } | ||
35 | 39 | ||
36 | var partnerUrl = url.parse(config.h2h_out.partner); | 40 | var partnerUrl = url.parse(config.h2h_out.partner); |
37 | var product = prepareRemoteProductCode(task.remoteProduct); | 41 | var product = prepareRemoteProductCode(task.remoteProduct); |
38 | 42 | ||
43 | var destination = task.destination; | ||
44 | if (methodName == 'CHECKING') { | ||
45 | destination = paddingDestination(destination); | ||
46 | } | ||
47 | |||
39 | var options = { | 48 | var options = { |
40 | url: config.h2h_out.partner, | 49 | url: config.h2h_out.partner, |
41 | qs: { | 50 | qs: { |
42 | request: 'PURCHASE*' | 51 | request: methodName + '*' |
43 | + task.requestId + '*' | 52 | + task.requestId + '*' |
44 | + product.product + '*' | 53 | + product.product + '*' |
45 | + product.productDetail + '*' | 54 | + product.productDetail + '*' |
46 | + task.destination + '*' | 55 | + destination + '*' |
47 | + product.nominal + '*' | 56 | + product.nominal + '*' |
48 | + '0*' | 57 | + '0*' |
49 | + config.h2h_out.noid + '*' | 58 | + config.h2h_out.noid + '*' |
50 | + config.h2h_out.userid + '*' | 59 | + config.h2h_out.userid + '*' |
51 | + config.h2h_out.password | 60 | + config.h2h_out.password |
52 | } | 61 | } |
53 | }; | 62 | }; |
54 | 63 | ||
64 | return options; | ||
65 | } | ||
66 | |||
67 | function topupCheck(task, retry) { | ||
68 | if (retry === null || retry === undefined) { | ||
69 | retry = maxCheckRetry + 1; | ||
70 | } | ||
71 | |||
72 | retry = retry - 1; | ||
73 | |||
74 | if (retry < 0) { | ||
75 | var message = 'Max retry check transaction retry exceeded'; | ||
76 | var response = { | ||
77 | raw: message, | ||
78 | parsed: { | ||
79 | MESSAGE: message | ||
80 | } | ||
81 | } | ||
82 | aaa.pushResponseToMongoDb(task, response, '68'); | ||
83 | callbackReport(task.requestId, '68', message); | ||
84 | return; | ||
85 | } | ||
86 | |||
87 | requestToPartner('CHECKING', task, retry); | ||
88 | } | ||
89 | |||
90 | function topupRequest(task) { | ||
91 | requestToPartner('PURCHASE', task); | ||
92 | } | ||
93 | |||
94 | function requestToPartner(methodName, task, retry) { | ||
95 | aaa.insertTaskToMongoDb(task); | ||
96 | |||
97 | var options = createRequestOptions(methodName, task); | ||
98 | |||
55 | logger.info('Requesting to partner', {requestOption: options}); | 99 | logger.info('Requesting to partner', {requestOption: options}); |
56 | request(options, function(error, response, body) { | 100 | request(options, function(error, response, body) { |
57 | if (error) { | 101 | if (error) { |
58 | logger.warn('Error requesting to partner', {error: error}); | 102 | logger.warn('Error requesting to partner', {error: error}); |
59 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); | 103 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); |
60 | 104 | ||
61 | var _response = { | 105 | var _response = { |
62 | raw: 'Error requesting to partner. ' + error, | 106 | raw: 'Error requesting to partner. ' + error, |
63 | parsed: { | 107 | parsed: { |
64 | MESSAGE: 'Error requesting to partner. ' + error, | 108 | MESSAGE: 'Error requesting to partner. ' + error, |
65 | error: error | 109 | error: error |
66 | } | 110 | } |
67 | } | 111 | } |
68 | aaa.pushResponseToMongoDb(task, _response, '68'); | 112 | aaa.pushResponseToMongoDb(task, _response, '68'); |
69 | 113 | ||
114 | setTimeout( | ||
115 | topupCheck, | ||
116 | delayBeforeCheckRetry, | ||
117 | task, | ||
118 | retry | ||
119 | ); | ||
120 | |||
70 | return; | 121 | return; |
71 | } | 122 | } |
72 | 123 | ||
73 | if (response.statusCode != 200) { | 124 | if (response.statusCode != 200) { |
74 | var message = 'Partner response with http status code other that 200 (' + response.statusCode + ')'; | 125 | var message = 'Partner response with http status code other that 200 (' + response.statusCode + ')'; |
75 | 126 | ||
76 | logger.warn(message); | 127 | logger.warn(message); |
77 | callbackReport(task.requestId, '68', message); | 128 | callbackReport(task.requestId, '68', message); |
78 | 129 | ||
79 | var _response = { | 130 | var _response = { |
80 | raw: 'Partner response with http status code other that 200 (' + response.statusCode + ')', | 131 | raw: 'Partner response with http status code other that 200 (' + response.statusCode + ')', |
81 | parsed: { | 132 | parsed: { |
82 | MESSAGE: 'Partner response with http status code other that 200 (' + response.statusCode + ')', | 133 | MESSAGE: 'Partner response with http status code other that 200 (' + response.statusCode + ')', |
83 | responseHttpStatus: response.statusCode, | 134 | responseHttpStatus: response.statusCode, |
84 | responseBody: body, | 135 | responseBody: body, |
85 | } | 136 | } |
86 | } | 137 | } |
87 | aaa.pushResponseToMongoDb(task, _response, '68'); | 138 | aaa.pushResponseToMongoDb(task, _response, '68'); |
88 | 139 | ||
140 | setTimeout( | ||
141 | topupCheck, | ||
142 | delayBeforeCheckRetry, | ||
143 | task, | ||
144 | retry | ||
145 | ); | ||
146 | |||
89 | return; | 147 | return; |
90 | } | 148 | } |
91 | 149 | ||
92 | logger.verbose('Got respose', {rawBody: body}); | 150 | logger.verbose('Got respose', {rawBody: body}); |
93 | parseResponse(body, task); | 151 | parseResponse(body, task); |
94 | }); | 152 | }); |
95 | } | 153 | } |
96 | 154 | ||
97 | function getSn(response) { | 155 | function getSn(response) { |
98 | try { | 156 | try { |
99 | var sn = response.xml.ket1[0]; | 157 | var sn = response.xml.ket1[0]; |
100 | return sn; | 158 | return sn; |
101 | } | 159 | } |
102 | catch (err) { | 160 | catch (err) { |
103 | return; | 161 | return; |
104 | } | 162 | } |
105 | } | 163 | } |
106 | 164 | ||
107 | function getHarga(response) { | 165 | function getHarga(response) { |
108 | try { | 166 | try { |
109 | var harga = response.xml.amount[0]; | 167 | var harga = response.xml.amount[0]; |
110 | return harga; | 168 | return harga; |
111 | } | 169 | } |
112 | catch (err) { | 170 | catch (err) { |
113 | return; | 171 | return; |
114 | } | 172 | } |
115 | } | 173 | } |
116 | 174 | ||
117 | function getSaldo(response) { | 175 | function getSaldo(response) { |
118 | try { | 176 | try { |
119 | var saldo = response.xml.saldo[0]; | 177 | var saldo = response.xml.saldo[0]; |
120 | return saldo; | 178 | return saldo; |
121 | } | 179 | } |
122 | catch (err) { | 180 | catch (err) { |
123 | return; | 181 | return; |
124 | } | 182 | } |
125 | } | 183 | } |
126 | 184 | ||
127 | function parseResponse(body, task) { | 185 | function parseResponse(body, task) { |
128 | xml2jsParser(body, function(err, response) { | 186 | xml2jsParser(body, function(err, response) { |
129 | if (err) { | 187 | if (err) { |
130 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); | 188 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); |
131 | 189 | ||
132 | var message = 'Error parsing XML. ' + err + '. ' + body; | 190 | var message = 'Error parsing XML. ' + err + '. ' + body; |
133 | 191 | ||
134 | var _response = { | 192 | var _response = { |
135 | raw: body, | 193 | raw: body, |
136 | parsed: { | 194 | parsed: { |
137 | MESSAGE: message | 195 | MESSAGE: message |
138 | } | 196 | } |
139 | } | 197 | } |
140 | aaa.pushResponseToMongoDb(task, _response, '68'); | 198 | aaa.pushResponseToMongoDb(task, _response, '68'); |
141 | 199 | ||
142 | callbackReport(task.requestId, '68', message); | 200 | callbackReport(task.requestId, '68', message); |
143 | return; | 201 | return; |
144 | } | 202 | } |
145 | 203 | ||
146 | logger.info('Got response', {response: response}); | 204 | logger.info('Got response', {response: response}); |
147 | 205 | ||
148 | var responseCode; | 206 | var responseCode; |
149 | var message; | 207 | var message; |
150 | 208 | ||
151 | try { | 209 | try { |
152 | responseCode = response.xml.response_code[0]; | 210 | responseCode = response.xml.response_code[0]; |
153 | message = response.xml.response_message[0]; | 211 | message = response.xml.response_message[0]; |
154 | } | 212 | } |
155 | catch(errGetParam) { | 213 | catch(errGetParam) { |
156 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); | 214 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); |
157 | 215 | ||
158 | var _response = { | 216 | var _response = { |
159 | raw: body, | 217 | raw: body, |
160 | parsed: { | 218 | parsed: { |
161 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, | 219 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, |
162 | xml: response.xml | 220 | xml: response.xml |
163 | } | 221 | } |
164 | } | 222 | } |
165 | aaa.pushResponseToMongoDb(task, _response, '68'); | 223 | aaa.pushResponseToMongoDb(task, _response, '68'); |
166 | 224 | ||
167 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); | 225 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); |
168 | return; | 226 | return; |
169 | } | 227 | } |
170 | 228 | ||
171 | var st24rc; | 229 | var st24rc; |
172 | if (parseInt(responseCode) == 0) { | 230 | if (parseInt(responseCode) == 0) { |
173 | st24rc = '00'; | 231 | st24rc = '00'; |
174 | } | 232 | } |
175 | else if (parseInt(responseCode) == '99') { | 233 | else if (parseInt(responseCode) == '99') { |
176 | st24rc = '68' | 234 | st24rc = '68' |
177 | } | 235 | } |
178 | else { | 236 | else { |
179 | st24rc = '40'; | 237 | st24rc = '40'; |
180 | } | 238 | } |
181 | 239 | ||
182 | var st24message = message; | 240 | var st24message = message; |
183 | if (responseCode) { | 241 | if (responseCode) { |
184 | st24message = responseCode + ' ' + st24message; | 242 | st24message = responseCode + ' ' + st24message; |
185 | } | 243 | } |
186 | 244 | ||
187 | if (st24rc == '00') { | 245 | if (st24rc == '00') { |
188 | var sn = getSn(response); | 246 | var sn = getSn(response); |
189 | 247 | ||
190 | if (sn) { | 248 | if (sn) { |
191 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; | 249 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; |
192 | } | 250 | } |
193 | } | 251 | } |
194 | 252 | ||
195 | var harga = getHarga(response); | 253 | var harga = getHarga(response); |
196 | if (harga) { | 254 | if (harga) { |
197 | st24message = st24message + '. Harga ' + harga; | 255 | st24message = st24message + '. Harga ' + harga; |
198 | } | 256 | } |
199 | 257 | ||
200 | var saldo = getSaldo(response); | 258 | var saldo = getSaldo(response); |
201 | if (saldo) { | 259 | if (saldo) { |
202 | st24message = st24message + '. Saldo ' + saldo; | 260 | st24message = st24message + '. Saldo ' + saldo; |
203 | aaa.updateBalance(saldo); | 261 | aaa.updateBalance(saldo); |
204 | } | 262 | } |
205 | 263 | ||
206 | var _response = { | 264 | var _response = { |
207 | raw: body, | 265 | raw: body, |
208 | parsed: { | 266 | parsed: { |
209 | MESSAGE: st24message, | 267 | MESSAGE: st24message, |
210 | xml: response.xml | 268 | xml: response.xml |
211 | } | 269 | } |
212 | } | 270 | } |
213 | 271 | ||
214 | aaa.pushResponseToMongoDb(task, _response, st24rc); | 272 | aaa.pushResponseToMongoDb(task, _response, st24rc); |
215 | 273 | ||
216 | callbackReport(task.requestId, st24rc, st24message); | 274 | callbackReport(task.requestId, st24rc, st24message); |
217 | }); | 275 | }); |
218 | } | 276 | } |
219 | 277 | ||
220 | function prepareRemoteProductCode(remoteProduct) { | 278 | function prepareRemoteProductCode(remoteProduct) { |
221 | var product = remoteProduct.split(','); | 279 | var product = remoteProduct.split(','); |
222 | 280 | ||
223 | if (product.length != 3) { | 281 | if (product.length != 3) { |
224 | return; | 282 | return; |
225 | } | 283 | } |
226 | 284 | ||
227 | return { | 285 | return { |
228 | product: product[0], | 286 | product: product[0], |
229 | productDetail: product[1], | 287 | productDetail: product[1], |
230 | nominal: product[2] | 288 | nominal: product[2] |
231 | } | 289 | } |
232 | } | 290 | } |
233 | 291 | ||
234 | function paddingDestination(destination, width) { | 292 | function paddingDestination(destination, width) { |
235 | if (!width) { | 293 | if (!width) { |
236 | width = 13; | 294 | width = 13; |
237 | } | 295 | } |
238 | 296 | ||
239 | if (destination.length > width) { | 297 | if (destination.length > width) { |
240 | return destination; | 298 | return destination; |
241 | } | 299 | } |
242 | 300 | ||
243 | var padder = "000000000000000000000000000"; | 301 | var padder = "000000000000000000000000000"; |
244 | 302 | ||
245 | return (padder + destination).slice(-1 * width); | 303 | return (padder + destination).slice(-1 * width); |
246 | } | 304 | } |
247 | 305 | ||
248 | exports.start = start; | 306 | exports.start = start; |
249 | exports.topupRequest = topupRequest; | 307 | exports.topupRequest = topupRequest; |
250 | exports.prepareRemoteProductCode = prepareRemoteProductCode; | 308 | exports.prepareRemoteProductCode = prepareRemoteProductCode; |
251 | exports.paddingDestination = paddingDestination; | 309 | exports.paddingDestination = paddingDestination; |
252 | 310 |