From 2a576aa1b0eed7ed97297eda7ae95526cd080f25 Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <gua@adhisimon.org> Date: Wed, 16 May 2018 15:37:00 +0700 Subject: [PATCH] st24.extractBalanceFromMsg --- lib/partner.js | 3 ++- lib/reverse-report.js | 1 + lib/st24.js | 13 +++++++++++++ test.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/partner.js b/lib/partner.js index f53442f..2a0ad0d 100644 --- a/lib/partner.js +++ b/lib/partner.js @@ -149,6 +149,7 @@ function _topUpInquiry(task) { message: stringify(value), sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE, config.sn_pattern), amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE, config.amount_pattern), + balance: st24.extractBalanceFromMsg(value.MESSAGE, config.balance_pattern), raw: value, misc: { task: task @@ -205,7 +206,7 @@ function report(data) { return; } - const task = data.misc.task; + const task = data.misc.task; logger.verbose('Registering resend delay', {trx_id: task.trx_id, destination: task.destination, product: task.product}) resendDelay.register(task, advice); diff --git a/lib/reverse-report.js b/lib/reverse-report.js index e03b084..dc62855 100644 --- a/lib/reverse-report.js +++ b/lib/reverse-report.js @@ -40,6 +40,7 @@ function create() { message: value.MESSAGE, sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), + balance: st24.extractBalanceFromMsg(value.MESSAGE, config.balance_pattern), raw: value, misc: { } diff --git a/lib/st24.js b/lib/st24.js index 287cd97..230b4e4 100644 --- a/lib/st24.js +++ b/lib/st24.js @@ -44,6 +44,19 @@ function extractPriceFromMsg(msg, custom_rule) { return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); } +function extractBalanceFromMsg(msg, custom_rule) { + const default_pattern = "SAL=([\\d\\.]+)"; + const default_match_idx = 1; + + let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); + if (!balance || typeof balance !== 'string') { + return; + } + + return Number(balance.replace(/[^\d]/g, '')); +} + exports.extractSnFromMessage = extractSnFromMessage; exports.extractPriceFromMsg = extractPriceFromMsg; +exports.extractBalanceFromMsg = extractBalanceFromMsg; diff --git a/test.js b/test.js index 2bd9624..14a7cc2 100644 --- a/test.js +++ b/test.js @@ -35,4 +35,32 @@ describe('#st24', function() { }) }) + describe('#extractBalanceFromMsg', function() { + describe('using native ST24 response', function() { + it('should return correct balance', function() { + st24.extractBalanceFromMsg('SN=0516150344145563101; 16/05/18 15:03 ISI TR5 KE 0895350249796, SUKSES.SAL=426.078,HRG=5.250,ID=47285513,SN=0516150344145563101; ..trx lancar').should.equal(426078); + st24.extractBalanceFromMsg('15/05/18 17:19 ISI SAN10 KE 08535686667, NOMOR YANG ANDA MASUKKAN SALAH, MOHON TELITI KEMBALI..SAL=1.144.578,ID=47250459, ..trx lancar').should.equal(1144578) + }) + + it('should return null if there is no balance info', function() { + should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL')); + }) + }) + + describe('using custom rule', function() { + const custom_rule = { + pattern: "SALDO=(\\d+)", + match_idx: 1 + } + + it('should return correct balance', function() { + st24.extractBalanceFromMsg('ISI Telkomsel 10 ke 082139822309 BERHASIL.SN=0041002442595407.HRG=10400.SALDO=104911920', custom_rule).should.equal(104911920); + }) + + it('should return null if there is no balance info', function() { + should.not.exists(st24.extractBalanceFromMsg('ISI Ke 08523548915 GAGAL.TRXID=20180516123010017371', custom_rule)) + }) + }) + }) + }) -- 1.9.0