How do I pack an ISO 8583 message?

The code snippet below show you how to pack an ISO 8583 message.

package org.kodejava.jpos;

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

import java.io.InputStream;

public class PackISOMessage {
    public static void main(String[] args) {
        PackISOMessage iso = new PackISOMessage();
        try {
            String message = iso.buildISOMessage();
            System.out.printf("Message = %s", message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String buildISOMessage() throws Exception {
        try {
            // Load package from resources directory.
            InputStream is = getClass().getResourceAsStream("/fields.xml");
            GenericPackager packager = new GenericPackager(is);

            ISOMsg isoMsg = new ISOMsg();
            isoMsg.setPackager(packager);
            isoMsg.setMTI("0200");

            isoMsg.set(3, "000010");
            isoMsg.set(4, "1500");
            isoMsg.set(7, "1206041200");
            isoMsg.set(11, "000001");
            isoMsg.set(41, "12340001");
            isoMsg.set(49, "840");
            printISOMessage(isoMsg);

            byte[] result = isoMsg.pack();
            return new String(result);
        } catch (ISOException e) {
            throw new Exception(e);
        }
    }

    private void printISOMessage(ISOMsg isoMsg) {
        try {
            System.out.printf("MTI = %s%n", isoMsg.getMTI());
            for (int i = 1; i <= isoMsg.getMaxField(); i++) {
                if (isoMsg.hasField(i)) {
                    System.out.printf("Field (%s) = %s%n", i, isoMsg.getString(i));
                }
            }
        } catch (ISOException e) {
            e.printStackTrace();
        }
    }
}

When you run the program you’ll get the following output:

MTI = 0200
Field (3) = 000010
Field (4) = 1500
Field (7) = 1206041200
Field (11) = 000001
Field (41) = 12340001
Field (49) = 840
Message = 02003220000000808000000010000000001500120604120000000112340001840

The xml packager (fields.xml) can be downloaded from the following link: fields.xml.

Maven Dependency

<dependency>
    <groupId>org.jpos</groupId>
    <artifactId>jpos</artifactId>
    <version>2.1.8</version>
</dependency>

Maven Central

Wayan

26 Comments

  1. I get error:

    at com.mycompany.mavenproject2.PackISOMessage.buildISOMessage(PackISOMessage.java:46)
        at com.mycompany.mavenproject2.PackISOMessage.main(PackISOMessage.java:19)
    
    Reply
    • java.lang.Exception: org.jpos.iso.ISOException: java.net.MalformedURLException (java.net.MalformedURLException)
          at com.mycompany.mavenproject2.PackISOMessage.buildISOMessage(PackISOMessage.java:46)
          at com.mycompany.mavenproject2.PackISOMessage.main(PackISOMessage.java:19)
      Caused by: org.jpos.iso.ISOException: java.net.MalformedURLException (java.net.MalformedURLException)
      
      Reply
    • Hi Tony,

      I think the error because it cannot find the fields.xml file. I am using Maven, in this case I place the fields.xml file inside the resources directory (src/main/resources/fields.xml).

      Instead of loading the packager file as a resource you can also do something like this:

      String filename = "D:/jpos/fields.xml";
      GenericPackager packager = new GenericPackager(filename);
      
      Reply
  2. I created a project named mavenproject2 and placed the fields.xml in cfg/packager but the error still there.

    InputStream is = getClass().getResourceAsStream("mavenproject2/cfg/packager/fields.xml");
    GenericPackager packager = new GenericPackager(is);
    
    Reply
  3. Error while compiling sample ISO Message Program:

    Exception in thread "main" java.lang.UnsupportedClassVersionError: org/jpos/iso/ISOException : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
        at java.lang.Class.getMethod0(Class.java:2856)
        at java.lang.Class.getMethod(Class.java:1668)
        at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
    
    Reply
  4. Hi,

    We are getting response but that response we pass to the parseISoData using “iso87ascii.xml” so we are are not getting proper field data 62 and 63 can please help us.

    Reply
      • public void parseISoData(String data) throws ISOException, IOException {
            CustomXmlReader packager;
            InputStream inputstream = getApplicationContext().getAssets().open("iso87ascii.xml");
        
            packager = new CustomXmlReader(inputstream);
        
            ISOMsg isoMsg = new ISOMsg();
            isoMsg.setPackager(packager);
            isoMsg.unpack(data.getBytes());
        
            // print the DE list
            logISOMsg(isoMsg);
        }
        
      • 00b2600000004108102038010002c0000692000014927514512207220041303038303030303036382020202020202020202020202020200085894f96d55d7421d9000000000000000054455354494e47205445414d205448414e4520202020205448414e452020202020202020204d48202020202020202020202020202020202020202020202020202020202020003600344b50894f96d55d7421d9ef349aa6cc0fe6173f03e4ce8dcaa3a9d62e12b21ff3c32d

    • Hi Sonali,

      I found that your message doesn’t contain the MTI information and also are you correctly calculate the message bitmap and supply the content for data element 62 and 63 in your message?

      Reply
  5. MTI 0810, if we are parsing the data in parseIsoData method using (“iso87ascii.xml”); but we are not getting the proper data element 62 and 63.

    Reply
      • 00b2 length
        6000000041 header
        0810 MTI
        2038010002c00006 Bitmap
        920000 processing
        683279 stan
        1129030722 date and time
        0041 nii
        3030 respose code
        3830303030303638 TID
        202020202020202020202020202020 MID
        0085
        9bcb28c2e08d11ee000000000000000054455354494e47205445414d205448414e4520202020205448414e452020202020202020204d48202020202020202020202020202020202020202020202020202020202020
        003600344b509bcb28c2e08d11eebbe04de6eb78a79c48bbb4f43f0f759171846d85cd4d584d00000000000000000000000000000000000000000000000000000000000000000000000000000000

    • Hi Sonali,

      I am processing the following message:

      08102038010002c00006920000683279112903072200413030383030303030363820202020202020202020202020202000859bcb28c2e08d11ee000000000000000054455354494e47205445414d205448414e4520202020205448414e452020202020202020204d48202020202020202020202020202020202020202020202020202020202020003600344b509bcb28c2e08d11eebbe04de6eb78a79c48bbb4f43f0f759171846d85cd4d584d00000000000000000000000000000000000000000000000000000000000000000000000000000000
      

      I remove the length (00b2) and the header (6000000041). And here is the result I got:

      MTI = 0810
      Field (3) = 920000
      Field (11) = 683279
      Field (12) = 112903
      Field (13) = 0722
      Field (24) = 004
      Field (39) = 13
      Field (41) = 03038303
      Field (42) = 030303036382020
      Field (62) = 0202020202020202020202000859bcb28c2e08d11ee000000000000000054455354494e47205445414d205448414e4520202020205448414e452020202020202020204d4820202020202020202020202020202020202020202020202020202020202000360
      Field (63) = 4b509bcb28c2e08d11eebbe04de6eb78a7
      
      Reply
    • Hi, Sonali, based on the last example of message break down you have modified the iso87ascii.xml right? And for the data element 62 and 63 make sure you add the correct length of the data element contents. Field 62 and 63 type is IFA_LLLCHAR it means that the message if prefixed with three digits length. For example 00859bcb28c, 008 is the length and 59bcb28c is the data.

      Reply
  6. Hi , I am using this parser for long. It works fine when the bitmap is readable. But when the bitmap comes in non-readable format in ASCII, I am unable to get the bitmap bytes and unpack the iso msg . can u help

    Reply
  7. Hi Wayan,

    Can you please help on how to add the SSL certificated if any host requires, how can we implement this in above code?

    Reply
  8. Hi Wayan .. Can you pls help me resolve below error. I am using the same code and fields.xml file
    java.lang.Exception: org.jpos.iso.ISOException: org.xml.sax.SAXParseException; systemId: file:///c:/Users/apochir/Desktop/Logs/fields.xml; lineNumber: 3; columnNumber: 13; Document is invalid: no grammar found. (org.xml.sax.SAXParseException; systemId: file:///c:/Users/apochir/Desktop/Logs/fields.xml; lineNumber: 3; columnNumber: 13; Document is invalid: no grammar found.)
    at com.test.PackISOMessage.buildISOMessage(PackISOMessage.java:58)
    at com.test.PackISOMessage.main(PackISOMessage.java:26)

    Reply

Leave a Reply to sonaliCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.