Compare View
Commits (7)
Changes
Showing 3 changed files Side-by-side Diff
package.json
partner-datacell.js
... | ... | @@ -13,6 +13,9 @@ var callbackReport; |
13 | 13 | var max_retry = 2; |
14 | 14 | var sleep_before_retry = 2000; |
15 | 15 | |
16 | +var trx_balances = {}; | |
17 | +var trx_prices = {}; | |
18 | + | |
16 | 19 | function calculateSignature(userid, password, msisdn, timestamp) { |
17 | 20 | var a = msisdn.substr(msisdn.length - 4) + timestamp; |
18 | 21 | var b = userid.substr(0, 4) + password; |
... | ... | @@ -149,11 +152,39 @@ function topupResponseHandler(body, request_id) { |
149 | 152 | response_code = '68'; |
150 | 153 | } |
151 | 154 | } |
155 | + | |
156 | + if (response_code == '00') { | |
157 | + var price = priceFromMessage(message); | |
158 | + if (price != null) { | |
159 | + trx_prices[request_id] = price; | |
160 | + setTimeout(deleteTrxPrice, 3 * 24 * 3600 * 1000, request_id); | |
161 | + } else if (trx_prices[request_id] !== undefined) { | |
162 | + price = trx_prices[request_id]; | |
163 | + message = message + ' -- Harga: ' + price; | |
164 | + } | |
165 | + | |
166 | + var balance = balanceFromMessage(message); | |
167 | + if (balance != null) { | |
168 | + trx_balances[request_id] = balances; | |
169 | + setTimeout(deleteTrxBalance, 3 * 24 * 3600 * 1000, request_id); | |
170 | + } else if (trx_balances[request_id] !== undefined) { | |
171 | + balance = trx_balances[request_id]; | |
172 | + message = message + ' -- Saldo: ' + balance; | |
173 | + } | |
174 | + } | |
152 | 175 | |
153 | 176 | callbackReport(request_id, response_code, message); |
154 | 177 | }); |
155 | 178 | } |
156 | 179 | |
180 | +function deleteTrxPrice(request_id) { | |
181 | + delete trx_prices[request_id]; | |
182 | +} | |
183 | + | |
184 | +function deleteTrxBalance(request_id) { | |
185 | + delete trx_balances[request_id]; | |
186 | +} | |
187 | + | |
157 | 188 | function parseSN(message) { |
158 | 189 | var results = message.match(/SN Operator: .+ SN Kami/); |
159 | 190 | if (!results || results.length <= 0) { |
... | ... | @@ -255,6 +286,31 @@ function balanceCheck() { |
255 | 286 | |
256 | 287 | } |
257 | 288 | |
289 | +function balanceFromMessage(message) { | |
290 | + var matches = message.match(/Saldo: Rp (\d+)/); | |
291 | + | |
292 | + if (!matches) { | |
293 | + return null; | |
294 | + } | |
295 | + if (matches.length < 2) { | |
296 | + return null; | |
297 | + } | |
298 | + | |
299 | + return matches[1]; | |
300 | +} | |
301 | + | |
302 | +function priceFromMessage(message) { | |
303 | + var matches = message.match(/Harga: (\d+)/); | |
304 | + | |
305 | + if (!matches) { | |
306 | + return null; | |
307 | + } | |
308 | + if (matches.length < 2) { | |
309 | + return null; | |
310 | + } | |
311 | + | |
312 | + return matches[1]; | |
313 | +} | |
258 | 314 | |
259 | 315 | function start(_config, _callbackReport) { |
260 | 316 | config = _config; |
... | ... | @@ -265,3 +321,5 @@ function start(_config, _callbackReport) { |
265 | 321 | |
266 | 322 | exports.start = start; |
267 | 323 | exports.topupRequest = topupRequest; |
324 | +exports.balanceFromMessage = balanceFromMessage; | |
325 | +exports.priceFromMessage = priceFromMessage; |
test.js
... | ... | @@ -0,0 +1,49 @@ |
1 | +var assert = require("assert"); | |
2 | + | |
3 | +describe('partner-datacell', function() { | |
4 | + var partner = require('./partner-datacell'); | |
5 | + | |
6 | + describe('#balanceFromMessage', function() { | |
7 | + var message; | |
8 | + | |
9 | + it('should return 8306874', function() { | |
10 | + assert.equal(8306874, partner.balanceFromMessage('IR25 No: 085697273881 sudah diterima dan sdg diproses. SN Kami :243588112. Harga: 24750. Saldo: Rp 8306874.')); | |
11 | + }); | |
12 | + | |
13 | + it('should return 8351574', function() { | |
14 | + assert.equal(8351574, partner.balanceFromMessage('TEL20 No: 085372113774 sudah diterima dan sdg diproses. SN Kami :243586975. Harga: 19950. Saldo: Rp 8351574.')); | |
15 | + }); | |
16 | + | |
17 | + it('should return 0', function() { | |
18 | + assert.equal(0, partner.balanceFromMessage('TEL20 No: 085372113774 sudah diterima dan sdg diproses. SN Kami :243586975. Harga: 19950. Saldo: Rp 0.')); | |
19 | + }); | |
20 | + | |
21 | + it('should return null', function() { | |
22 | + assert.equal(null, partner.balanceFromMessage('XL25 No: 08174945541 SUKSES SN Operator: 970729963933 SN Kami: 243591297.')); | |
23 | + }); | |
24 | + }); | |
25 | + | |
26 | + describe('#priceFromMessage', function() { | |
27 | + var message; | |
28 | + | |
29 | + it('should return 24750', function() { | |
30 | + assert.equal(24750, partner.priceFromMessage('IR25 No: 085697273881 sudah diterima dan sdg diproses. SN Kami :243588112. Harga: 24750. Saldo: Rp 8306874.')); | |
31 | + }); | |
32 | + | |
33 | + it('should return 19950', function() { | |
34 | + assert.equal(19950, partner.priceFromMessage('TEL20 No: 085372113774 sudah diterima dan sdg diproses. SN Kami :243586975. Harga: 19950. Saldo: Rp 8351574.')); | |
35 | + }); | |
36 | + | |
37 | + it('should return 0', function() { | |
38 | + assert.equal(0, partner.priceFromMessage('TEL20 No: 085372113774 sudah diterima dan sdg diproses. SN Kami :243586975. Harga: 0. Saldo: Rp 8351574.')); | |
39 | + }); | |
40 | + | |
41 | + it('should return null', function() { | |
42 | + assert.equal(null, partner.priceFromMessage('XL25 No: 08174945541 SUKSES SN Operator: 970729963933 SN Kami: 243591297.')); | |
43 | + }); | |
44 | + }); | |
45 | + | |
46 | + | |
47 | + | |
48 | + | |
49 | +}); |