Friday, May 4, 2012

ISO Message: Packing and Un-Packing

Dear Reader,
Here I am writing about how to pack and unpack ISO messages for banking transactions.
For basic necessary stuffs please read my earlier written blog and then come here, link is below:
http://deepakmodi2006.blogspot.in/2012/04/iso-8583-standard-in-java-for-financial.html

jPOS is a Java platform-based, ISO-8583 based financial transaction library/framework that can be 
customized and extended in order to implement financial interchanges.

So first thing to do is download JPOS libraries from its website.
Following Jars are required for the same. Please see the picture below.

For packing and unpacking an ISO message I have written four programs: basic.xml genericpackager.dtd BuildISOMessage.java ParseISOMessage.java Output of program.. ------------------------------------------- package dmodi; import java.io.IOException; import org.jpos.iso.ISOException; import org.jpos.iso.ISOMsg; import org.jpos.iso.packager.GenericPackager; public class BuildISOMessage { public static void main(String[] args) throws IOException, ISOException { //Create Packager based on XML that contain DE type GenericPackager packager = new GenericPackager("basic.xml"); // Create ISO Message ISOMsg isoMsg = new ISOMsg(); isoMsg.setPackager(packager); isoMsg.setMTI("0200"); isoMsg.set(3, "201234"); isoMsg.set(4, "10000"); isoMsg.set(7, "110722180"); isoMsg.set(11, "123456"); isoMsg.set(32, "100005"); isoMsg.set(44, "A5DFGR"); isoMsg.set(105, "ABCDEFGHIJ 1234567890"); // print the DE list logISOMsg(isoMsg); // Get and print the output result byte[] data = isoMsg.pack(); String dataString=new String(data); System.out.println("Packed ISO Message : " +dataString); ParseISOMessage parse=new ParseISOMessage(); parse.setData(dataString); parse.parseIsoMessage(); } private static void logISOMsg(ISOMsg msg) { System.out.println("----ISO MESSAGE to Pack-----"); try { System.out.println(" MTI : " + msg.getMTI()); for (int i = 1; i <= msg.getMaxField(); i++) { if (msg.hasField(i)) { System.out.println(" Field-" + i + " : " + msg.getString(i)); } } } catch (ISOException e) { e.printStackTrace(); } finally { System.out.println("--------------------"); } } } ------------------------------------------- //ParseISOMessage.java package dmodi; import java.io.IOException; import org.jpos.iso.ISOException; import org.jpos.iso.ISOMsg; import org.jpos.iso.packager.GenericPackager; public class ParseISOMessage { String data=""; public void parseIsoMessage() throws IOException, ISOException { // Create Packager based on XML that contain DE type GenericPackager packager = new GenericPackager("basic.xml"); System.out.println("Data to unpack : " + data); // Create ISO Message ISOMsg isoMsg = new ISOMsg(); isoMsg.setPackager(packager); isoMsg.unpack(data.getBytes()); // print the DE list logISOMsg(isoMsg); } private static void logISOMsg(ISOMsg msg) { System.out.println("----Unpacked ISO MESSAGE-----"); try { System.out.println(" MTI : " + msg.getMTI()); for (int i = 1; i <= msg.getMaxField(); i++) { if (msg.hasField(i)) { System.out.println(" Field-" + i + " : " + msg.getString(i)); } } } catch (ISOException e) { e.printStackTrace(); } finally { System.out.println("--------------------"); } } public void setData(String strData){ data=strData; } } ------------------------------------------- //basic.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE isopackager SYSTEM "genericpackager.dtd"> <isopackager> <isofield id="0" length="4" name="MESSAGE TYPE INDICATOR" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="1" length="64" name="BIT MAP" class="org.jpos.iso.IFA_BITMAP"/> <isofield id="3" length="6" name="PROCESSING CODE" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="4" length="12" name="AMOUNT, TRANSACTION" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="7" length="10" name="TRANSMISSION DATE AND TIME" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="11" length="6" name="SYSTEM TRACE AUDIT NUMBER" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="32" length="6" name="Acquirer Id" class="org.jpos.iso.IFA_NUMERIC"/> <isofield id="44" length="25" name="ADITIONAL RESPONSE DATA" class="org.jpos.iso.IFA_LLCHAR"/> <isofield id="105" length="999" name="RESERVED ISO USE" class="org.jpos.iso.IFA_LLLCHAR"/> </isopackager> ------------------------------------------- //genericpackager.dtd <?xml version="1.0" encoding="UTF-8"?> <!ELEMENT isopackager (isofield+,isofieldpackager*)*> <!ATTLIST isopackager maxValidField CDATA #IMPLIED> <!ATTLIST isopackager bitmapField CDATA #IMPLIED> <!ATTLIST isopackager firstField CDATA #IMPLIED> <!ATTLIST isopackager emitBitmap (true|false) #IMPLIED> <!ATTLIST isopackager headerLength CDATA #IMPLIED> <!-- isofield --> <!ELEMENT isofield (#PCDATA)> <!ATTLIST isofield id CDATA #REQUIRED> <!ATTLIST isofield length CDATA #REQUIRED> <!ATTLIST isofield name CDATA #REQUIRED> <!ATTLIST isofield class NMTOKEN #REQUIRED> <!ATTLIST isofield token CDATA #IMPLIED> <!ATTLIST isofield pad (true|false) #IMPLIED> <!-- isofieldpackager --> <!ELEMENT isofieldpackager (isofield+,isofieldpackager*)*> <!ATTLIST isofieldpackager id CDATA #REQUIRED> <!ATTLIST isofieldpackager name CDATA #REQUIRED> <!ATTLIST isofieldpackager length CDATA #REQUIRED> <!ATTLIST isofieldpackager class NMTOKEN #REQUIRED> <!ATTLIST isofieldpackager token CDATA #IMPLIED> <!ATTLIST isofieldpackager pad (true|false) #IMPLIED> <!ATTLIST isofieldpackager packager NMTOKEN #REQUIRED> <!ATTLIST isofieldpackager emitBitmap (true|false) #IMPLIED> <!ATTLIST isofieldpackager maxValidField CDATA #IMPLIED> <!ATTLIST isofieldpackager bitmapField CDATA #IMPLIED> <!ATTLIST isofieldpackager firstField CDATA #IMPLIED> <!ATTLIST isofieldpackager headerLength CDATA #IMPLIED> ------------------------------------------- Output: ----ISO MESSAGE to Pack----- MTI : 0200 Field-3 : 201234 Field-4 : 10000 Field-7 : 110722180 Field-11 : 123456 Field-32 : 100005 Field-44 : A5DFGR Field-105 : ABCDEFGHIJ 1234567890 -------------------- Packed ISO Message : 0200B2200001001000000000000000800000201234000000010000011072218012345610000506A5DFGR021ABCDEFGHIJ 1234567890 Data to unpack : 0200B2200001001000000000000000800000201234000000010000011072218012345610000506A5DFGR021ABCDEFGHIJ 1234567890 ----Unpacked ISO MESSAGE----- MTI : 0200 Field-3 : 201234 Field-4 : 000000010000 Field-7 : 0110722180 Field-11 : 123456 Field-32 : 100005 Field-44 : A5DFGR Field-105 : ABCDEFGHIJ 1234567890 -------------------- -----------------------------------END--------------------------------

1 comment:

  1. Thank you Deepak for the good explanation of ISOMsg and its working. I have a question related to packing and unpacking of a ISOMsg if the message have sub-fields present. like:

    GenericPackager packager = new GenericPackager("src/main/resources/basic.xml");

    ISOMsg m = new ISOMsg();
    m.setPackager(packager);

    m.set(0,"0200");
    m.set(11,"020011");
    m.set(25,"00");
    m.set(41,"22222222");
    //Sub Field
    ISOMsg msg = new ISOMsg(63);
    msg.set(1, "d");
    msg.set(2, "$");
    msg.set(3, "9");
    msg.set(4, "1");
    m.set(msg);

    m.dump(System.out, "\t");
    byte pack[] = m.pack();
    System.out.println("Packed String >>>>>>> "+new String(pack));

    //unpack
    ISOMsg um = new ISOMsg();
    um.setPackager(packager);
    um.dump(System.out, "\t");
    um.unpack(pack);

    Inside basic.xml
    ==========















    Its giving me error while packing the msg >> LOG:














    Exception in thread "main" org.jpos.iso.ISOException: error packing field 63 (org.jpos.iso.ISOException: org.jpos.iso.IF_CHAR: Problem packing field -1 (java.lang.ClassCastException: java.util.BitSet cannot be cast to java.lang.String))
    at org.jpos.iso.ISOBasePackager.pack(ISOBasePackager.java:128)
    at org.jpos.iso.ISOMsg.pack(ISOMsg.java:409)
    at com.alphapayments.cloudstore.eglobal.processor.EglobalRequestMapperHelper.main(EglobalRequestMapperHelper.java:166)
    Nested:org.jpos.iso.ISOException: org.jpos.iso.IF_CHAR: Problem packing field -1 (java.lang.ClassCastException: java.util.BitSet cannot be cast to java.lang.String)
    at org.jpos.iso.ISOStringFieldPackager.pack(ISOStringFieldPackager.java:155)
    at org.jpos.iso.packager.GenericSubFieldPackager.pack(GenericSubFieldPackager.java:126)
    at org.jpos.iso.ISOMsgFieldPackager.pack(ISOMsgFieldPackager.java:59)
    at org.jpos.iso.ISOBasePackager.pack(ISOBasePackager.java:119)
    at org.jpos.iso.ISOMsg.pack(ISOMsg.java:409)
    at com.alphapayments.cloudstore.eglobal.processor.EglobalRequestMapperHelper.main(EglobalRequestMapperHelper.java:166)
    Nested:java.lang.ClassCastException: java.util.BitSet cannot be cast to java.lang.String
    at org.jpos.iso.ISOStringFieldPackager.pack(ISOStringFieldPackager.java:141)
    at org.jpos.iso.packager.GenericSubFieldPackager.pack(GenericSubFieldPackager.java:126)
    at org.jpos.iso.ISOMsgFieldPackager.pack(ISOMsgFieldPackager.java:59)
    at org.jpos.iso.ISOBasePackager.pack(ISOBasePackager.java:119)
    at org.jpos.iso.ISOMsg.pack(ISOMsg.java:409)
    at com.alphapayments.cloudstore.eglobal.processor.EglobalRequestMapperHelper.main(EglobalRequestMapperHelper.java:166)

    ReplyDelete