Skip to content

Instantly share code, notes, and snippets.

@inash
Created November 8, 2013 20:32
Show Gist options
  • Save inash/7377162 to your computer and use it in GitHub Desktop.
Save inash/7377162 to your computer and use it in GitHub Desktop.
/** Import the net nodejs package. */
var connect = require('net');
function Izs(host, port) {
/** Field object type to define a field for Izs message fields. */
var Field = function(name, length, value, justify) {
if (name == null) throw new Error('a name has to be specified for a field.');
if (length == null) length = 0;
if (value == null) value = '';
if (justify == null) justify = 'left';
return { name: name, length: length, value: value, justify: justify };
};
function Fields() {
this.fields = [
new Field('length', 5, '00578'), new Field('type', 3, 'ABE'),
new Field('vendor', 10, 'BML'), new Field('product', 5, 'NODEJ'),
new Field('terminal', 15), new Field('batch', 15), new Field('merchant', 15),
new Field('location', 35), new Field('reference', 16),
new Field('feeCode', 2), new Field('feeAmount', 14, '', 'right'),
new Field('accessFee', 14, '', 'right'), new Field('timestamp', 19),
new Field('userId', 4, 'IZS'), new Field('customerId', 10), new Field('requestType', 1, 'E'),
new Field('debitAccount', 13), new Field('debitAccountCurrency', 3),
new Field('debitTransactionCode', 3), new Field('debitAmount', 14, '', 'right'),
new Field('debitNarrative1', 35), new Field('debitNarrative2', 35),
new Field('debitNarrative3', 35), new Field('creditAccount', 13),
new Field('creditAccountCurrency', 3), new Field('creditTransactionCode', 3),
new Field('creditAmount', 14, '', 'right'), new Field('creditNarrative1', 35),
new Field('creditNarrative2', 35), new Field('creditNarrative3', 35),
new Field('exchangeRate', 25, '', 'right'), new Field('startDate', 7),
new Field('endDate', 7), new Field('code1', 3), new Field('code2', 3),
new Field('sequence', 3, '', 'right'), new Field('flag1', 1),
new Field('consumerAccount', 20), new Field('serviceNumber', 20),
new Field('billNumber', 20), new Field('mobileNumber', 10)];
}
/** Set the value of the specified field. If the field name is not valid throw an Error. */
Fields.prototype.setValue = function(name, value) {
var field = null;
this.fields.forEach(function(e) {
if (e.name == name) {
e.value = value;
field = e;
}
});
if (field == null) throw new Error('invalid field name for setValue()');
};
Fields.prototype.serialize = function() {
var fields = this.fields.map(function(e) {
var value = null;
if (e.justify == 'left') {
value = e.value + ' ';
value = value.substring(0, e.length);
} else if (e.justify == 'right') {
value = ' ' + e.value;
value = value.substring(e.value - e.length, e.length);
}
return value;
});
return fields.join('');
};
/** Initialize the list of Fields. */
this.fields = new Fields();
/** Try connecting to Izs. */
this.conn = connect.connect({ host: host, port: port});
this.conn.on('error', function(error) { throw error; });
};
Izs.prototype.setValue = function(name, value) {
this.fields.setValue(name, value);
};
Izs.prototype.request = function(onDataCallback) {
if (typeof(onDataCallback) != 'function')
throw new Error('the onDataCallback to request should be a function.');
this.conn.on('data', onDataCallback);
this.conn.write(this.fields.serialize());
};
Izs.prototype.close = function() {
this.conn.end();
};
module.exports = Izs;
var izs = require('./izs.js');
var client = new izs('172.16.1.11', 5125);
client.setValue('reference', '123');
client.setValue('debitAccount', '7703217955101');
client.setValue('debitAccountCurrency', 'MRF');
client.setValue('debitTransactionCode', '010');
client.setValue('debitAmount', '1400');
client.request(function(data) {
console.log(data.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment