From dd83a83f2e998180dce0f5417fd4db95f3a5c11c Mon Sep 17 00:00:00 2001 From: Igor Askarov Date: Thu, 7 Nov 2019 14:41:47 +0300 Subject: [PATCH] ISO8583 versions flexibility #7 --- lib/iso8583-packet/lib/iso8583Packet.js | 66 +++++++++++++------------ lib/testSuite/lib/socketBank.js | 46 ++++++++--------- 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/lib/iso8583-packet/lib/iso8583Packet.js b/lib/iso8583-packet/lib/iso8583Packet.js index bedbf5c..01f0789 100644 --- a/lib/iso8583-packet/lib/iso8583Packet.js +++ b/lib/iso8583-packet/lib/iso8583Packet.js @@ -4,16 +4,18 @@ var fs = require('fs'); // The constructor function Iso8583Packet(data, options) { - this.messageTypeId = null; - this.messageTypeGroup = null; - this.annotation = null; - this.fields = {}; - this.parseError = null; - this.isFaulty = false; - this.isSystemFaulty = false; - this.isExpired = false; - this.checkErrors = []; - this.rawData = ''; + this.messageTypeId = null; + this.messageTypeIdShort = null; + this.messageTypeGroup = null; + this.messageVersion = null; + this.annotation = null; + this.fields = {}; + this.parseError = null; + this.isFaulty = false; + this.isSystemFaulty = false; + this.isExpired = false; + this.checkErrors = []; + this.rawData = ''; if (!options) options = {}; @@ -75,17 +77,19 @@ function Iso8583Packet(data, options) { this.pad(); this.validate(); -}; +} // Updates packet message type group Iso8583Packet.prototype.setMessageTypeIdGroup = function() { if (this.fields[0]) { this.messageTypeId = this.fields[0]; if (this.messageTypeId && this.messageTypeId.length == 4) { + this.messageTypeVersion = parseInt(this.messageTypeId.substr(0, 1)); + this.messageTypeIdShort = parseInt(this.messageTypeId.substr(1, 3)); this.messageTypeGroup = parseInt(this.messageTypeId.substr(1, 1)); } } -} +}; // Validates packet fields Iso8583Packet.prototype.validate = function(errorMessage) { @@ -128,12 +132,12 @@ Iso8583Packet.prototype.validate = function(errorMessage) { this.annotation = this.getAnnotation(); return null; -} +}; // Expiration check Iso8583Packet.prototype.checkExpired = function() { this.isExpired = this.packager.config.validators.isExpiredPacket(this, this.systemConfig.expirationLimitSeconds); -} +}; // Faulty status ckeck Iso8583Packet.prototype.checkFaulty = function() { @@ -141,11 +145,11 @@ Iso8583Packet.prototype.checkFaulty = function() { this.isFaulty = parseInt(rc) ? true : false; this.isSystemFaulty = this.systemConfig.sysErrors.indexOf(rc) >= 0 ? true : false; -} +}; // Returns packet annotation string Iso8583Packet.prototype.getAnnotation = function() { - var mti = this.messageTypeId; + var mti = this.messageTypeIdShort; var result = ''; // Purchase / Refund @@ -171,7 +175,7 @@ Iso8583Packet.prototype.getAnnotation = function() { if (this.isExpired) result += ' '; return result; -} +}; // Pad the packet values that has to be padded Iso8583Packet.prototype.pad = function() { @@ -186,17 +190,17 @@ Iso8583Packet.prototype.pad = function() { } } } -} +}; // Pads given value with zeroes on the left Iso8583Packet.prototype.zeroPad = function(val, i) { return ('00000000000000000000000000000000' + val).slice(-this.packager.format[i]['length']); -} +}; // Checks if if there are packet consistency errors Iso8583Packet.prototype.hasErrors = function() { return this.checkErrors.length > 0; -} +}; // Returns the packet field value Iso8583Packet.prototype.getField = function(id) { @@ -205,17 +209,17 @@ Iso8583Packet.prototype.getField = function(id) { } else { return null; } -} +}; // Returns all packet fields Iso8583Packet.prototype.getFields = function() { return this.fields; -} +}; // Updates the current packet bitmask Iso8583Packet.prototype.updateBitMask = function() { this.fields[1] = this.packager.hexMask(this.packager.getBinMask(this.fields)); -} +}; // Sets packet field value Iso8583Packet.prototype.setFields = function(values, error) { @@ -232,7 +236,7 @@ Iso8583Packet.prototype.setFields = function(values, error) { if (newFieldAdded) this.updateBitMask(); this.validate(); -} +}; // Clears packet fields Iso8583Packet.prototype.unsetFields = function(values, error) { @@ -244,7 +248,7 @@ Iso8583Packet.prototype.unsetFields = function(values, error) { this.updateBitMask(); this.validate(); -} +}; // Generate string message Iso8583Packet.prototype.getMessage = function(options) { @@ -254,7 +258,7 @@ Iso8583Packet.prototype.getMessage = function(options) { var header = options.header ? this.getHeader(message.length) : Buffer.from(''); return Buffer.concat([header, message]); -} +}; // Return source raw message Iso8583Packet.prototype.getRawMessage = function(options) { @@ -269,12 +273,12 @@ Iso8583Packet.prototype.getRawMessage = function(options) { } else { return header.toString() + this.rawData; } -} +}; // Returns the length header Iso8583Packet.prototype.getHeader = function(lengthVal) { return this.packager.config.generators.getHeader(lengthVal); -} +}; // Return pretty-formatted values Iso8583Packet.prototype.pretty = function() { @@ -327,7 +331,7 @@ Iso8583Packet.prototype.pretty = function() { drop += '\n'; return drop; -} +}; // Returns caption that describes given field value Iso8583Packet.prototype.getFieldCaption = function(id, val) { @@ -354,11 +358,11 @@ Iso8583Packet.prototype.getFieldCaption = function(id, val) { } return result; -} +}; // Checks if the given error code is valid for the current system Iso8583Packet.prototype.isValidError = function(code) { return this.systemConfig.errors.hasOwnProperty(parseInt(code)); -} +}; module.exports = Iso8583Packet; diff --git a/lib/testSuite/lib/socketBank.js b/lib/testSuite/lib/socketBank.js index abecd70..ed3a145 100644 --- a/lib/testSuite/lib/socketBank.js +++ b/lib/testSuite/lib/socketBank.js @@ -13,16 +13,16 @@ function testBank() { testBank.prototype.replyPacket = function(sourcePacket, responseCode) { if (!responseCode) responseCode = 0; - if (sourcePacket.messageTypeId == '0800') { + if (sourcePacket.messageTypeIdShort == '800') { return this.reply800(sourcePacket, responseCode); - } else if (sourcePacket.messageTypeId == '0200') { + } else if (sourcePacket.messageTypeIdShort == '200') { return this.reply200(sourcePacket, responseCode); - } else if (sourcePacket.messageTypeId == '0400') { + } else if (sourcePacket.messageTypeIdShort == '400') { return this.reply400(sourcePacket, responseCode); } else { return null; } -} +}; // Echo packet reply testBank.prototype.reply800 = function(sourcePacket, responseCode) { @@ -32,7 +32,7 @@ testBank.prototype.reply800 = function(sourcePacket, responseCode) { if (!responseCode) responseCode = 0; p.setFields({ - 0: '0810', + 0: sourcePacket.messageTypeVersion + '810', 7: now.format("MMDDHHmmss"), 37: this.genRrn(), 39: responseCode @@ -41,14 +41,14 @@ testBank.prototype.reply800 = function(sourcePacket, responseCode) { p.unsetFields([24, 42]); return p; -} +}; // Purchase packet reply testBank.prototype.reply200 = function(sourcePacket, responseCode) { p = new iso8583Packet(sourcePacket.fields); var now = moment(new Date()); - if (!responseCode) responseCode = 00; + if (!responseCode) responseCode = 0; var tr = 0; var amount = sourcePacket.getField(4); var pan = this.getPan(sourcePacket.getField(35)); @@ -91,7 +91,7 @@ testBank.prototype.reply200 = function(sourcePacket, responseCode) { } p.setFields({ - 0: '0210', + 0: sourcePacket.messageTypeVersion + '210', 7: now.format("MMDDHHmmss"), 39: responseCode }); @@ -102,7 +102,7 @@ testBank.prototype.reply200 = function(sourcePacket, responseCode) { }); return p; -} +}; // Reversal packet reply @@ -128,13 +128,13 @@ testBank.prototype.reply400 = function(sourcePacket, responseCode) { } p.setFields({ - 0: '0410', + 0: sourcePacket.messageTypeVersion + '410', 7: now.format("MMDDHHmmss"), 39: responseCode }); return p; -} +}; // Creates new transaction testBank.prototype.addTransaction = function(p) { @@ -146,12 +146,12 @@ testBank.prototype.addTransaction = function(p) { ac: this.genAc(), pan: p.getField(2), amount: p.getField(4) - } + }; this.transactions[transaction.rrn] = transaction; return transaction; -} +}; // Reverse the transaction testBank.prototype.reverseTransaction = function(p) { @@ -166,17 +166,17 @@ testBank.prototype.reverseTransaction = function(p) { } else { return false; } -} +}; // Generates transaction rrn testBank.prototype.genRrn = function() { return helpers.randomString(12, 1); -} +}; // Generates transaction approval code testBank.prototype.genAc = function() { return helpers.randomString(6); -} +}; // Gets pan from track data testBank.prototype.getPan = function(data) { @@ -191,7 +191,7 @@ testBank.prototype.getPan = function(data) { } else { return null; } -} +}; // Accounts stuff function bankAccount() { @@ -203,13 +203,13 @@ testBank.prototype.getAccount = function(pan) { if (!this.accounts.hasOwnProperty(pan)) return false; return this.accounts[pan]; -} +}; // Creates new account testBank.prototype.addAccount = function(pan, amount) { if (!amount) balance = 0; this.accounts[pan] = {balance: amount, pan: pan}; -} +}; // Checks account balance testBank.prototype.getBalance = function(pan) { @@ -217,7 +217,7 @@ testBank.prototype.getBalance = function(pan) { if (!acct) return false; return acct.amount; -} +}; // Purchase operation testBank.prototype.purchase = function(pan, amount) { @@ -227,14 +227,14 @@ testBank.prototype.purchase = function(pan, amount) { this.setBalance(pan, acct.balance - amount); return true; -} +}; // Sets account balance testBank.prototype.setBalance = function(pan, amount) { if (!this.getAccount(pan)) return false; this.accounts[pan]['balance'] = amount; -} +}; // Adds funds to account balance testBank.prototype.addAmount = function(pan, amount) { @@ -242,6 +242,6 @@ testBank.prototype.addAmount = function(pan, amount) { if (!acct) return false; this.setBalance(pan, acct['balance'] + amount); -} +}; module.exports = testBank;