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