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.

Output Iso8583

Output Iso8583

If you see something similar, then your class running successfully.

Now, we will unpack the ISO 8583 Message that we got from output above. Create class Unpack.java under package com.wordpress.bayurimba.iso8583.

package com.wordpress.bayurimba.iso8583;

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

public class Unpack {	

	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);

		// this is ISO8583 Message that we will Unpack
		String isoMessage = "010032200000000100000200000000000050000804170346123456013Example Value";

		// first, we must convert ISO8583 Message String to byte[]
		byte[] bIsoMessage = new byte[isoMessage.length()];
		for (int i = 0; i < bIsoMessage.length; i++) {
			bIsoMessage[i] = (byte) (int) isoMessage.charAt(i);
		}

		// second, we unpack the message
		isoMsg.unpack(bIsoMessage);		

		// last, print the unpacked ISO8583
		System.out.println("MTI='"+isoMsg.getMTI()+"'");
		for(int i=1; i<=isoMsg.getMaxField(); i++){
			if(isoMsg.hasField(i))
				System.out.println(i+"='"+isoMsg.getString(i)+"'");
		}
	}

}

Now, run the class. You’ll see output like this.

Output Unpack Iso8583

Output Unpack Iso8583

In summary, this is my Eclipse package explorer looks like.

Package Explorer

Package Explorer

Well, that all folks:mrgreen: Now you had learned to pack/unpack ISO 8583 Message in Java