Skip to content

Commit

Permalink
ISO8583 versions flexibility #7
Browse files Browse the repository at this point in the history
  • Loading branch information
juks committed Nov 7, 2019
1 parent 3f502cb commit dd83a83
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
66 changes: 35 additions & 31 deletions lib/iso8583-packet/lib/iso8583Packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -128,24 +132,24 @@ 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() {
var rc = this.getField(39);

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
Expand All @@ -171,7 +175,7 @@ Iso8583Packet.prototype.getAnnotation = function() {
if (this.isExpired) result += ' <expired>';

return result;
}
};

// Pad the packet values that has to be padded
Iso8583Packet.prototype.pad = function() {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -244,7 +248,7 @@ Iso8583Packet.prototype.unsetFields = function(values, error) {

this.updateBitMask();
this.validate();
}
};

// Generate string message
Iso8583Packet.prototype.getMessage = function(options) {
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
46 changes: 23 additions & 23 deletions lib/testSuite/lib/socketBank.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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));
Expand Down Expand Up @@ -91,7 +91,7 @@ testBank.prototype.reply200 = function(sourcePacket, responseCode) {
}

p.setFields({
0: '0210',
0: sourcePacket.messageTypeVersion + '210',
7: now.format("MMDDHHmmss"),
39: responseCode
});
Expand All @@ -102,7 +102,7 @@ testBank.prototype.reply200 = function(sourcePacket, responseCode) {
});

return p;
}
};


// Reversal packet reply
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -191,7 +191,7 @@ testBank.prototype.getPan = function(data) {
} else {
return null;
}
}
};

// Accounts stuff
function bankAccount() {
Expand All @@ -203,21 +203,21 @@ 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) {
var acct = this.getAccount(pan);
if (!acct) return false;

return acct.amount;
}
};

// Purchase operation
testBank.prototype.purchase = function(pan, amount) {
Expand All @@ -227,21 +227,21 @@ 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) {
var acct = this.getAccount(pan);
if (!acct) return false;

this.setBalance(pan, acct['balance'] + amount);
}
};

module.exports = testBank;

0 comments on commit dd83a83

Please sign in to comment.