In this article, I will show you how to build (pack) and parse (unpack) ISO 8583 Message in Java. We need jPos library to pack ISO 8583 message. Also, we need Common CLI and JDOM library for jPos to unpack ISO 8583. Before we begin, don’t forget to add all mentioned library to your classpath. In this tutorial, I’m using Eclipse. Off course you can use another IDE :)

First, we create class Pack.java under package com.wordpress.bayurimba.iso8583. The purpose of this class is to Pack ISO 8583 Message.

package com.wordpress.bayurimba.iso8583;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.packager.GenericPackager;

public class Pack {

	public static void main(String[] args) throws ISOException {
		// Initialize packager. in this example, I'm using
		// XML packager. We also can use Java Code Packager
		// This code throws ISOException
		GenericPackager packager = new GenericPackager("packager/iso87ascii.xml");

		// Setting packager
		ISOMsg isoMsg = new ISOMsg();
		isoMsg.setPackager(packager);

		// Setting MTI
		isoMsg.set(0, "0100");

		// Setting processing code
		isoMsg.set(3, "020000");

		// Setting transaction amount
		isoMsg.set(4, "5000");

		// Setting transmission date and time
		isoMsg.set(7, new SimpleDateFormat("MMddHHmmss").format(new Date()));

		// Setting system trace audit number
		isoMsg.set(11, "123456");

		// Setting data element #48
		isoMsg.set(48, "Example Value");

		// pack the ISO 8583 Message
		byte[] bIsoMsg = isoMsg.pack();

		// output ISO 8583 Message String
		String isoMessage = "";
		for (int i = 0; i < bIsoMsg.length; i++) {
			isoMessage += (char) bIsoMsg[i];
		}
		System.out.println("Packed ISO8385 Message = '"+isoMessage+"'");
	}

}

Wait, what is the meaning of xml file we load in line 16? File iso87ascii.xml in line 16, is xml packager that we need to pack/unpack ISO 8583 message. This file define type and length for each data element. This xml file, you can download it from here. Put it on folder packager.

After that, run this class. We’ll get output like this. Continue reading