NetCore8583
NetCore8583 is a library that helps parse/read and generate ISO 8583 messages for .NET Core
Install / Use
/learn @Tochemey/NetCore8583README
NetCore8583
NetCore8583 is feature complete and mature.
📖 Introduction
NetCore8583 is a dotnet core implementation of the ISO 8583 protocol.
NetCore8583 is a library that helps parse/read and generate ISO 8583 messages. It does not handle sending or reading them over a network connection, but it does parse the data you have read and can generate the data you need to write over a network connection.
🏦 ISO 8583 overview
ISO8583 is a message format used for credit card transactions, banking and other commercial interaction between different systems. It has an ASCII variant and a binary one, and it is somewhat convoluted and difficult to implement.
The main format of the ISO8583 is something like this:
| ISO header (optional) | Message Type | primary bitmap | secondary bitmap (optional) | data fields | | --------------------- | ------------ | -------------- | --------------------------- | ----------- |
The ISO header is a string containing some code that can vary according to the message type.
The message type is a number expressed as 4 hex digits (or 2 bytes when using binary format).
The bitmap is 64 bits long and it is encoded as 16 hex characters or as 8 bytes when using binary format. Every bit that is set in the bitmap indicates that the corresponding field is present. If the first bit is set, then field 1 is present, and so on.
The fields in the message are numbered from 1 to 64. Field 1 is the secondary bitmap, if present. The secondary bitmap allows for the message to have fields from 65 to 128.
Wikipedia has a very good article on the whole specification.
📦 Usage
The library is available on nuget package. You can get it via:
dotnet add package NetCore8583
💬 Support
One can use the following channel to report a bug or discuss a feature or an enhancement:
If you find this library very useful in your day job, kindly show some love by starring it.
⚙️ How does NetCore8583 work?
NetCore8583 offers a MessageFactory, which once properly configured, can create different message types with some values predefined, and can also parse a byte array to create an ISO message. Messages are represented by IsoMessage objects, which store IsoValue instances for their data fields. You can work with the IsoValue or use the convenience methods of IsoMessage to work directly with the stored values.
🏗️ MessageFactory and IsoMessage classes
These are the two main classes you need to use to work with ISO8583 messages. An IsoMessage can be encoded into a signed byte array. You can set and get the values for each field in an IsoMessage, and it will adjust itself to use a secondary bitmap if necessary. An IsoMessage has settings to encode itself in binary or ASCII, to use a secondary bitmap even if it's not necessary, and it can have its own ISO header.
However, it can be cumbersome to programmatically create IsoMessage all the time. The MessageFactory is a big aid in creating IsoMessage with predefined values; also, it can set the date and the trace number in each new message.
- There is an extension method that helps switch between signed byte array and unsigned byte array.
🔧 How to configure the MessageFactory
There are five main things you need to configure in a MessageFactory: ISO headers, message templates, parsing templates, TraceNumberGenerator, and custom field encoders.
-
Iso headers: ISO headers are strings that are associated with a message type. Whenever you ask the message factory to create an
IsoMessage, it will pass the corresponding ISO header (if present) to the new message. -
Message Templates: A message template is an
IsoMessageitself; theMessageFactorycan have a template for each message type it needs to create. When it creates a message and it has a template for that message type, it copies the fields from the template to the new message before returning it. -
Parsing Templates: A parsing template is a map containing
FieldParseInfoobjects as values and the field numbers as the keys. AFieldParseInfoobject contains anIsoTypeand an optional length; with this information and the field number, theMessageFactorycan parse incoming messages, first analyzing the message type and then using the parsing template for that type; when parsing a message, theMessageFactoryonly parses the fields that are specified in the message's bitmap. For example if the bitmap specifies field 4, the factory will get theFieldParseInfostored in the map under key 4, and will attempt to parse the field according to the type and length specified by theFieldParseInfo. A message does not need to contain all the fields specified in a parsing template, but a parsing template must contain all the fields specified in the bitmap of a message, or the MessageFactory won't be able to parse it because it has no way of knowing how it should parse that field (and also all subsequent fields). -
ITraceNumberGenerator: When creating new messages, they usually need to have a unique trace number, contained in field 11. Also, they usually need to have the date they were created (or the date the transaction was originated) in field 7. The
MessageFactorycan automatically set the current date on all new messages, you just need to set the assignDate property to true. And it can also assign a new trace number to each message it creates, but for this it needs a TraceNumberGenerator. TheITraceNumberGeneratorinterface defines anextTrace()method, which must return a new trace number between 1 and 999999. It needs to be cyclic, so it returns 1 again after returning 999999. And usually, it needs to be thread-safe. NetCore8583 only defines the interface; in production environments you will usually need to implement your own TraceNumberGenerator, getting the new trace number from a sequence in a database or some similar mechanism. As an example, the library includes theSimpleTraceGenerator, which simply increments an in-memory value. -
Custom fields encoders: Certain implementations of ISO8583 specify fields which contain many subfields. If you only handle strings in those fields, you'll have to encode all those values before storing them in an
IsoMessage, and decode them when you get them from an IsoMessage. In these cases you can implement aCustomField, which is an interface that defines two methods, one for encoding an object into a String and another for decoding an object from a String. You can pass theMessageFactoryaCustomFieldfor every field where you want to store custom values, so that parsed messages will return the objects decoded by theCustomFieldinstead of just strings; and when you set a value in anIsoMessage, you can specify the CustomField to be used to encode the value as a String
🔌 Custom Field encoders
Sometimes there are fields that contain several sub-fields or separate pieces of data. NetCore8583 will only parse the field for you, but you still have to parse those pieces of data from the field when you parse a message, and/or encode several pieces of data into a field when creating a message.
NetCore8583 can help with this process, by means of the custom field encoders. To use this feature, first you need to implement the ICustomField interface. You can see how it is done in the following test classes TestParsing.cs and TestIsoMessage.cs using the CustomField48.cs class.
🛠️ Easy way to configure ISO 8583 messages templates
There are two ways to configure message templates and parsing templates:
- XML configuration -- declare templates, headers, and parse guides in a XML file and load it into the
MessageFactory. - Programmatic configuration (Builder API) -- use the fluent [
MessageFactoryBuilder](./NetCore8583/Builder/MessageFactoryB
