diff --git a/examples/kopnus/config.json b/examples/kopnus/config.json index 96dd566..d3684d5 100644 --- a/examples/kopnus/config.json +++ b/examples/kopnus/config.json @@ -7,6 +7,10 @@ "pattern": "SN=(.*?)\\.", "match_idx": 1 }, + "amount_pattern": { + "pattern": "HRG=(\\d+)", + "match_idx": 1 + }, "remote_products": { "S10": "3043051", "S20": "3023092" diff --git a/lib/partner.js b/lib/partner.js index d31cf57..2f18c5e 100644 --- a/lib/partner.js +++ b/lib/partner.js @@ -96,7 +96,7 @@ function _topUpRequest(task, isAdvice) { rc: partnerRc[value[RESPONSECODE_TAG]] || '40', message: stringify(value), sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE, config.sn_pattern), - amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), + amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE, config.amount_pattern), raw: value, misc: { task: task @@ -144,7 +144,7 @@ function _topUpInquiry(task) { rc: partnerRc[value.RESPONSECODE] || '40', message: stringify(value), sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE, config.sn_pattern), - amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), + amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE, config.amount_pattern), raw: value, misc: { task: task diff --git a/lib/st24.js b/lib/st24.js index dec96b4..287cd97 100644 --- a/lib/st24.js +++ b/lib/st24.js @@ -1,6 +1,6 @@ "use strict"; -function extractSnFromMessage(msg, custom_rule) { +function extractFromMessage(msg, default_pattern, default_match_idx, custom_rule) { if (!msg || typeof msg !== 'string') { return; } @@ -13,8 +13,8 @@ function extractSnFromMessage(msg, custom_rule) { match_idx = custom_rule.match_idx; } else { - pattern = "^SN=(.*?);"; - match_idx = 1; + pattern = default_pattern; + match_idx = default_match_idx; } const re = new RegExp(pattern); @@ -27,23 +27,21 @@ function extractSnFromMessage(msg, custom_rule) { } else { return; } + } -function extractPriceFromMsg(msg) { - if (!msg || typeof msg !== 'string') { - return; - } +function extractSnFromMessage(msg, custom_rule) { + const default_pattern = "^SN=(.*?);"; + const default_match_idx = 1; - let match = msg.match(/\d,HRG=(.*?),ID=/); - if (!match || match.length < 2) { - return; - } + return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); +} - if (!match[1]) { - return; - } +function extractPriceFromMsg(msg, custom_rule) { + const default_pattern = "\\d,HRG=(.*?),ID="; + const default_match_idx = 1; - return parseInt(match[1].replace(/\./g, '')); + return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); }