Monday, April 23, 2012

Dealing with ISOMsg and JPOS in Java

Dear reader,
This article is solely for my reference. If it is not useful too, please don't comment.

//Complete method for Credit using JPos connector
public TransactionResult credit() {
    TransactionResult tr = null;
    QMUX qmx = null;
    ISOMsg reqMsg = null;
    reqMsg = createCreditMsg(SystemInfo, PaymentRequest, "NPCI");
    String muxName = "mux.npci-mux";
    if (muxName == null)
        throw new ProcessingException("Unable to obtain QMUX name");
    qmx = (QMUX) NameRegistrar.get(muxName);
    int timeOut = 30000;
    
    respMsg = (OboISOMsg) qmx.request(reqMsg, timeOut);  //OboISOMsg extends IsoMsg, see below

    //Checking for Timeout
    if (respMsg == null || "91".equalsIgnoreCase(respMsg.getString(39))) {   //"00" means successful response
        System.out.println("Request Timed Out! Sending the verification message");
        ISOMsg verfMsg = (ISOMsg) reqMsg.clone();
        NPCIUserData srcUserData = (NPCIUserData) paymReq.getTransUnit().getUserData();
        NPCIUserData tgtUserData = (NPCIUserData) paymReq.getTransUnit().getMemberData();
        try {
            verfMsg.set(120,"Deepak8801001"); //name+nbin+mas; Take data from srcUserData and tgtUserData
            int retryCount = count++;
            for (int i = 0; i < 3; i++) {  //3 times re-sending.
                verfMsg.set(11, "111222");  //STAN: 6 digits
                int verifyReqTimeOut = 2000;
                respMsg = (OboISOMsg) qmx.request(verfMsg, verifyReqTimeOut);
                if (respMsg != null    && !"91".equalsIgnoreCase(respMsg.getString(39)))
                    break;
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    
    //Checking for proper response
    if(respMsg != null && respMsg.msgProcessingOK) {  //Means response is proper
        tr = processCreditResponse(respMsg, paymReq.getTransUnit().getUserData().getDeviceNumber(), reqMsg); 
    }
    //Means a failed response
    else {
        tr = new TransactionResult();
        tr.setStatus("FAILED");
        ProcessorResult pResult = new ProcessorResult();
        if (reqMsg != null) {
            pResult.set("DE11", reqMsg.getString(11));
            pResult.set("DE37", reqMsg.getString(37));
        }
        tr.setProcessorrResult(pResult);
    }
 return tr;
}

public TransactionResult processCreditResponse(ISOMsg respMsg,
            String devNbr, ISOMsg reqMsg) throws InvalidParameterException,
            MethodNotSupportedException, ProcessingException {
        TransactionResult tr = (TransactionResult) processDefaultResponse(
                new TransactionResult(), respMsg, devNbr, reqMsg);
        return tr;
}

protected ProcessorResponse processDefaultResponse(
            ProcessorResponse pr, ISOMsg respMsg, String dn, ISOMsg reqMsg) {
        pr.setDeviceNumber(dn);
        pr.setMappedAccountNumber(respMsg.getString(2));
        pr.setReturnCode(respMsg.getString(39));
        pr.setStatus(("00".equals(respMsg.getString(39)) ? "SUCCESS": "FAILED"));
        if (respMsg.hasField(38))
            pr.setExternalTransactionRef(respMsg.getString(38));

        ProcessorResult procRes = new ProcessorResult();
        procRes.set("DE3", respMsg.getString(3)); // processing cd
        procRes.set("DE7", respMsg.getString(7)); // transm dt
        procRes.set("DE11", respMsg.getString(11)); // trc nbr
        procRes.set("DE12", respMsg.getString(12)); // tx time
        procRes.set("DE13", respMsg.getString(13)); // tx date
        procRes.set("DE37", respMsg.getString(37)); // retrvl ref nbr
        procRes.set("DE39", respMsg.getString(39)); // response cd
        if (respMsg.hasField(120)) {
            String beneName = getBeneficiaryName(respMsg);
            if (beneName != null)
                procRes.set("BENEFICIARY", beneName); // beneficiary Name
        }
        pr.setProcessorrResult(procRes);
        return pr;
}


public class OboISOMsg extends ISOMsg {
    private byte[] rawMsg = null;
    public boolean msgProcessingOK = true;
    private Exception processingErr = null;
    private Long dbID = null;
    public boolean msgIsUnhandled = false;
    private long processingTime = 0L;
}

public class ProcessorPaymentRequest extends ProcessorRequest {    
    private Amount amount;
    private String memoText;
    private String transactionType;
    private String firstName    =    null;
    private String lastName        =    null;
    private String middleName    =    null;
    private String cardType        =    null;
    //setter and getter
}

public class PaymentRequest  extends ProcessorPaymentRequest {    
    private TransactionUnit transUnit;
    private String memo;
    //setter and getter
}
public class TransactionUnit extends EWPBusinessObject{
    public static final String kNullStr    =    "{NULL}";
    private UserData userData;
    private UserData memberData;
    private String status;
    private Amount fee;
    private String externalRefId;    
    private String processrCode;
    //setter and getter
}    

public class SystemInfo implements Serializable{
    private static final String kUnknown    =    "unknown";    
    private String systemCode    = kUnknown;
    private String serverIP        = kUnknown;    
    private String callerIP        = kUnknown;
    //setter and getter
}    
    
<void method="put"> 
     <string>jPOS_MUX_NAME</string> 
     <string>mux.npci-mux</string> 
</void> 


public ISOMsg createCreditMsg(SystemInfo sysInf, PaymentRequest paymReq, String fi) throws Exception {
        ISOMsg msg = new ISOMsg("0200");
        NPCIUserData srcUserData = (NPCIUserData) paymReq.getTransUnit().getUserData();
        NPCIUserData tgtUserData = (NPCIUserData) paymReq.getTransUnit().getMemberData();
        try {
            msg.set(2, tgtUserData.getNBin() + "00" + "1" + "00"+ tgtUserData.getDeviceNumber());
            msg.set(3, NPCIConstants.DE3_PROCESSING_CODE);  //900000
            msg.set(4, ISOUtil.padleft(amount, 12, '0'));   //amount can be replaced by 
                                                            //ISOCurrency.convertToIsoMsg("12500")
            msg.set(7, ISODate.getDateTime(new Date()));
            msg.set(11, "111222");  //STAN: 6 digits
            msg.set(12, ISODate.getTime(new Date()));
            msg.set(13, ISODate.getDate(new Date()));
            msg.set(18, NPCIConstants.DE18_MCC); //4814
            msg.set(22, NPCIConstants.DE22_POS_ENTRY_MODE); //019
            msg.set(25, NPCIConstants.DE25_POS_CONDITION_CODE); //05
            msg.set(32, NPCIConstants.DE32_AcquirerId); //100035
            msg.set(37, getISO8583P037RtrRefNbr(msg));  //Reference number, check method 
                                                        //getISO8583P037RtrRefNbr() given below.            
            msg.set(41, NPCIConstants.DE41_Tid    + srcUserData.getDeviceNumber().substring(5, 10););    
            
            msg.set(42, ISOUtil.strpad(NPCIConstants.DE42_Mid) + srcUserData.getDeviceNumber(), 15);
            msg.set(49, NPCIConstants.DE49_CurrencyCode); //356: for Indian rupees
            msg.set(120, "Deepak8801001"); //name+nbin+mas
        } catch (ISOException e) {
            e.printStackTrace();
        }
        return msg;
}

protected String getISO8583P037RtrRefNbr(ISOMsg req) {
        String rrn = null;
        Date d = new Date();
        String jd = ISODate.getJulianDate(d); // YDDD
        SimpleDateFormat sd = new SimpleDateFormat("HH");
        String hh = sd.format(d); // HH
        rrn = jd + hh + req.getString(11);
        return rrn;
}
    
public class UserData {        
    private String deviceNumber;
    private String accountNumber;
    private Long accountId;
    private String mappedAccountNumber;
    private String mappedRoutingNumber;
    private String validationPIN;
    private String mappedAccountExpiry;
    private String firstName;
    private String lastName;
    private String middleName;
    private String prepaidCardStatus;
    private AddressTO userAddress;
    private String bank_ICA; ///For Master Card Money Send
    private String cardType;
    private String mappedACType;
    private UserLocale userLocale;
    //setter and getter
}    
public class NPCIUserData extends UserData {    
    private String mas;
    private String NBin;
    //setter and getter
}
----------------------------------END---------------------------------------

No comments:

Post a Comment