Commit 1a8b8868c56cbaf43012261a55b456e0376b1e09
1 parent
f71680fb1a
Exists in
master
saldo tidak cukup
Showing 1 changed file with 50 additions and 49 deletions Side-by-side Diff
partner-cjk.js
... | ... | @@ -19,10 +19,10 @@ function initMongoClient() { |
19 | 19 | if (!config.mongodb || !config.mongodb.url) { |
20 | 20 | return; |
21 | 21 | } |
22 | - | |
22 | + | |
23 | 23 | try { |
24 | 24 | var url = config.mongodb.url; |
25 | - | |
25 | + | |
26 | 26 | mongoClient.connect(url, function(err, db) { |
27 | 27 | if (err) { |
28 | 28 | logger.warn('Failed to connect to mongodb', {err: err}); |
... | ... | @@ -34,7 +34,7 @@ function initMongoClient() { |
34 | 34 | } |
35 | 35 | catch(err) { |
36 | 36 | logger.warn('Exception when connecting to mongodb', {err: err, url: url}); |
37 | - } | |
37 | + } | |
38 | 38 | } |
39 | 39 | |
40 | 40 | var maxRetry = 10; |
... | ... | @@ -43,11 +43,11 @@ var sleepBeforeRetry = 30; |
43 | 43 | function start(_config, _callbackReport, options) { |
44 | 44 | config = _config; |
45 | 45 | callbackReport = _callbackReport |
46 | - | |
46 | + | |
47 | 47 | if (options && options.aaa) { |
48 | 48 | aaa = options.aaa; |
49 | 49 | } |
50 | - | |
50 | + | |
51 | 51 | if (options && options.logger) { |
52 | 52 | logger = options.logger; |
53 | 53 | } else { |
... | ... | @@ -57,7 +57,7 @@ function start(_config, _callbackReport, options) { |
57 | 57 | ] |
58 | 58 | }); |
59 | 59 | } |
60 | - | |
60 | + | |
61 | 61 | initMongoClient(); |
62 | 62 | } |
63 | 63 | |
... | ... | @@ -65,12 +65,12 @@ function calculateSignature(params) { |
65 | 65 | var passwordHash = crypto.createHash('sha1').update(params.password).digest().toString('hex'); |
66 | 66 | var plain = params.trxtype + params.prdcode + params.value + params.msisdn + params.trxid + params.uid + passwordHash; |
67 | 67 | var signature = crypto.createHash('sha256').update(plain).digest().toString('hex'); |
68 | - | |
68 | + | |
69 | 69 | try { |
70 | 70 | logger.verbose('Signature calculated', {plain: plain, signature: signature}); |
71 | 71 | } |
72 | 72 | catch(err) {} |
73 | - | |
73 | + | |
74 | 74 | return signature; |
75 | 75 | } |
76 | 76 | |
... | ... | @@ -86,18 +86,18 @@ function createXmlPayload(params) { |
86 | 86 | {hash: calculateSignature(params)} |
87 | 87 | ] |
88 | 88 | }); |
89 | - | |
89 | + | |
90 | 90 | try { logger.verbose("Payload: " + payload); } |
91 | 91 | catch(errLog) {} |
92 | - | |
92 | + | |
93 | 93 | return payload; |
94 | 94 | } |
95 | 95 | |
96 | 96 | function insertTaskToMongoDb(task) { |
97 | 97 | if (!isMongoReady()) { return; } |
98 | - | |
98 | + | |
99 | 99 | task.supplier = config.globals.gateway_name; |
100 | - | |
100 | + | |
101 | 101 | try { |
102 | 102 | mongodb.collection(config.mongodb.collection).insertOne(task); |
103 | 103 | } |
... | ... | @@ -108,7 +108,7 @@ function insertTaskToMongoDb(task) { |
108 | 108 | |
109 | 109 | function pushResponseToMongoDb(task, response) { |
110 | 110 | if (!isMongoReady()) { return; } |
111 | - | |
111 | + | |
112 | 112 | try { |
113 | 113 | mongodb.collection(config.mongodb.collection).updateOne( |
114 | 114 | {requestId: task.requestId}, |
... | ... | @@ -138,7 +138,7 @@ function isMongoReady() { |
138 | 138 | if (!config.mongodb) { return; } |
139 | 139 | if (!config.mongodb.collection) { return; } |
140 | 140 | if (!mongodb) { return; } |
141 | - | |
141 | + | |
142 | 142 | return true; |
143 | 143 | } |
144 | 144 | |
... | ... | @@ -157,7 +157,7 @@ function getSNFromMessage(message) { |
157 | 157 | |
158 | 158 | function hasSuccessKeywords(message) { |
159 | 159 | var keywords = ['SUKSES', 'Finish']; |
160 | - | |
160 | + | |
161 | 161 | var count = keywords.length; |
162 | 162 | for (var i=0; i < count; i++) { |
163 | 163 | if (message.indexOf(keywords[i]) >= 0) { |
... | ... | @@ -171,9 +171,10 @@ function supplierRcToST24Rc(rc) { |
171 | 171 | var rcs = { |
172 | 172 | '0001': '40', |
173 | 173 | '0012': '40', // hash data tidak sesuai |
174 | + '0015': '40', // saldo tidak cukup | |
174 | 175 | '0019': '13', // produk tidak tersedia |
175 | 176 | } |
176 | - | |
177 | + | |
177 | 178 | if (rcs[rc]) { |
178 | 179 | return rcs[rc]; |
179 | 180 | } else { |
... | ... | @@ -191,57 +192,57 @@ function getSNFromResponseObject(respObj) { |
191 | 192 | } |
192 | 193 | |
193 | 194 | function topupResponseHandler(body, task) { |
194 | - | |
195 | + | |
195 | 196 | //logger.info('Got reply from partner', {body: body}); |
196 | - | |
197 | + | |
197 | 198 | xml2js(body, function(err, result) { |
198 | 199 | var ts = strftime('%Y-%m-%d %H:%M:%S', new Date()); |
199 | - | |
200 | + | |
200 | 201 | if (err) { |
201 | 202 | logger.warn('Got invalid XML from partner', {err: err, body: body, task: task}); |
202 | 203 | callbackReport(task.requestId, '68', body); |
203 | - | |
204 | + | |
204 | 205 | pushResponseToMongoDb(task, {ts: ts, supplier: config.globals.gateway_name, raw: body}); |
205 | 206 | return; |
206 | 207 | } |
207 | 208 | logger.info('XML message from partner', {result: result}); |
208 | 209 | pushResponseToMongoDb(task, {ts: ts, supplier: config.globals.gateway_name, raw: body, parsed: result}); |
209 | - | |
210 | + | |
210 | 211 | var rc = '68'; |
211 | 212 | var message = result.ciwaru.msg[0]; |
212 | - | |
213 | + | |
213 | 214 | if (message.toUpperCase().indexOf('PENDING') >= 0) { |
214 | 215 | rc = '68'; |
215 | 216 | } |
216 | 217 | else if (hasSuccessKeywords(message)) { |
217 | - | |
218 | + | |
218 | 219 | var sn = getSNFromResponseObject(result); |
219 | - | |
220 | + | |
220 | 221 | if (!sn) { |
221 | 222 | sn = getSNFromMessage(message); |
222 | 223 | } |
223 | - | |
224 | + | |
224 | 225 | message = 'SN=' + sn + '; ' + message; |
225 | 226 | rc = '00'; |
226 | - | |
227 | + | |
227 | 228 | } |
228 | 229 | else if (message.indexOf('Nomor Pelanggan Salah') >= 0) { |
229 | - | |
230 | + | |
230 | 231 | rc = '14'; |
231 | - | |
232 | + | |
232 | 233 | } else { |
233 | 234 | rc = supplierRcToST24Rc(result.ciwaru.rc[0]); |
234 | 235 | if (!rc) { |
235 | 236 | rc = '68'; |
236 | 237 | } |
237 | 238 | } |
238 | - | |
239 | + | |
239 | 240 | if ((task.retry == maxRetry) || (rc != '68')) { |
240 | 241 | callbackReport(task.requestId, rc, message); |
241 | 242 | } else { |
242 | 243 | logger.info('Not reporting to AAA for duplicate 68', {task: task}); |
243 | 244 | } |
244 | - | |
245 | + | |
245 | 246 | if (rc == '68') { |
246 | 247 | topupRequestRetry(task); |
247 | 248 | } |
... | ... | @@ -250,7 +251,7 @@ function topupResponseHandler(body, task) { |
250 | 251 | |
251 | 252 | function topupRequestRetry(task) { |
252 | 253 | task.retry--; |
253 | - | |
254 | + | |
254 | 255 | if (task.retry > 0) { |
255 | 256 | logger.info('Retrying in ' + sleepBeforeRetry + 's'); |
256 | 257 | setTimeout(topupRequest, sleepBeforeRetry * 1000, task, task.retry); |
... | ... | @@ -261,23 +262,23 @@ function topupRequestRetry(task) { |
261 | 262 | } |
262 | 263 | |
263 | 264 | function topupRequest(task, retry) { |
264 | - | |
265 | + | |
265 | 266 | if (retry === undefined) { |
266 | - | |
267 | + | |
267 | 268 | task.ts = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss'); |
268 | 269 | task.ts_date = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD'); |
269 | - | |
270 | + | |
270 | 271 | insertTaskToMongoDb(task); |
271 | - | |
272 | + | |
272 | 273 | retry = maxRetry; |
273 | 274 | } |
274 | - | |
275 | + | |
275 | 276 | if (!task.retry) { |
276 | 277 | task.retry = retry; |
277 | 278 | } |
278 | 279 | |
279 | 280 | var remoteProduct = task.remoteProduct.split(','); |
280 | - | |
281 | + | |
281 | 282 | var params = { |
282 | 283 | trxtype: '01', |
283 | 284 | prdcode: remoteProduct[0], |
... | ... | @@ -287,9 +288,9 @@ function topupRequest(task, retry) { |
287 | 288 | uid: config.h2h_out.userid, |
288 | 289 | password: config.h2h_out.password, |
289 | 290 | }; |
290 | - | |
291 | + | |
291 | 292 | var postBody = createXmlPayload(params); |
292 | - | |
293 | + | |
293 | 294 | var partnerUrl = url.parse(config.h2h_out.partner); |
294 | 295 | var postRequest = { |
295 | 296 | host: partnerUrl.hostname, |
... | ... | @@ -301,27 +302,27 @@ function topupRequest(task, retry) { |
301 | 302 | 'Content-Length': Buffer.byteLength(postBody) |
302 | 303 | } |
303 | 304 | }; |
304 | - | |
305 | + | |
305 | 306 | logger.info('POST to partner', {postRequest: postRequest}); |
306 | 307 | var req = http.request(postRequest, function( res ) { |
307 | - | |
308 | + | |
308 | 309 | logger.verbose('Status code: ' + res.statusCode ); |
309 | 310 | var buffer = ""; |
310 | - | |
311 | - res.on( "data", function( data ) { | |
312 | - buffer = buffer + data; | |
311 | + | |
312 | + res.on( "data", function( data ) { | |
313 | + buffer = buffer + data; | |
313 | 314 | }); |
314 | - | |
315 | - res.on( "end", function( data ) { | |
315 | + | |
316 | + res.on( "end", function( data ) { | |
316 | 317 | topupResponseHandler(buffer, task); |
317 | 318 | }); |
318 | - | |
319 | + | |
319 | 320 | }); |
320 | - | |
321 | + | |
321 | 322 | req.on('error', function(e) { |
322 | 323 | logger.warn('problem with request: ' + e.message); |
323 | 324 | callbackReport(task['requestId'], '68', e.message); |
324 | - | |
325 | + | |
325 | 326 | topupRequestRetry(task); |
326 | 327 | }); |
327 | 328 |