Created
June 26, 2012 08:13
-
-
Save jamalsa/2994316 to your computer and use it in GitHub Desktop.
ISO8583 String Message Builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package id.co.vsi.common.ISO8583; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.concurrent.atomic.AtomicLong; | |
/** | |
* | |
* @author Jamal | |
*/ | |
public class MessageBuilder | |
{ | |
public static final String GW_POSTPAID_INQUIRY_REQUEST = "2100403000410001000005??501??????????????????????????6014070280001019VI105V3????????????"; | |
public static final String GW_POSTPAID_PAYMENT_REQUEST_HEAD = "22005030004100010000"; | |
public static final String GW_PREPAID_INQUIRY_REQUEST = "210040300041000100000553502??????????????????????????6012070022295031VI105V3000000000000000000000000"; | |
public static final String GW_PREPAID_PAYMENT_REQUEST_HEAD = "2200503000410001000405535023600000000050000"; | |
public static final String GW_NONTAGLIS_INQUIRY_REQUEST = "2100403000410001000005??504??????????????????????????6012070022295023VI105V3?????????????000"; | |
public static final String GW_NONTAGLIS_PAYMENT_REQUEST_HEAD = "22005030004100010004"; | |
protected static AtomicLong cStan = new AtomicLong(100000000000L); | |
protected static AtomicLong cSwitcherRefnum = new AtomicLong(1892362392400230049L); | |
protected static final String cSwitcherRefnumPadding = "0000000000000"; | |
protected static final ThreadLocal<SimpleDateFormat> cDateFormatter = new ThreadLocal<SimpleDateFormat>(){ | |
@Override | |
protected SimpleDateFormat initialValue() | |
{ | |
return new SimpleDateFormat("yyyyMMddHHmmss"); | |
} | |
}; | |
private static ThreadLocal<StringBuilder> cGWPostpaidInqReqBuilder = new ThreadLocal<StringBuilder>() | |
{ | |
@Override | |
protected StringBuilder initialValue() { | |
StringBuilder tGWPostpaidInqReqTemplate = new StringBuilder(89); | |
tGWPostpaidInqReqTemplate.append(GW_POSTPAID_INQUIRY_REQUEST); | |
tGWPostpaidInqReqTemplate.replace(39, 53, cDateFormatter.get().format(new Date())); | |
return tGWPostpaidInqReqTemplate; | |
} | |
@Override | |
public StringBuilder get() { | |
StringBuilder b = super.get(); | |
return b; | |
} | |
}; | |
private static ThreadLocal<StringBuilder> cGWPrepaidInqReqBuilder = new ThreadLocal<StringBuilder>() | |
{ | |
@Override | |
protected StringBuilder initialValue() { | |
StringBuilder tGWPostpaidInqReqTemplate = new StringBuilder(89); | |
tGWPostpaidInqReqTemplate.append(GW_PREPAID_INQUIRY_REQUEST); | |
tGWPostpaidInqReqTemplate.replace(39, 53, cDateFormatter.get().format(new Date())); | |
return tGWPostpaidInqReqTemplate; | |
} | |
@Override | |
public StringBuilder get() { | |
StringBuilder b = super.get(); | |
return b; | |
} | |
}; | |
private static ThreadLocal<StringBuilder> cGWNontaglisInqReqBuilder = new ThreadLocal<StringBuilder>() | |
{ | |
@Override | |
protected StringBuilder initialValue() { | |
StringBuilder tGWPostpaidInqReqTemplate = new StringBuilder(89); | |
tGWPostpaidInqReqTemplate.append(GW_NONTAGLIS_INQUIRY_REQUEST); | |
tGWPostpaidInqReqTemplate.replace(39, 53, cDateFormatter.get().format(new Date())); | |
return tGWPostpaidInqReqTemplate; | |
} | |
@Override | |
public StringBuilder get() { | |
StringBuilder b = super.get(); | |
return b; | |
} | |
}; | |
private static ThreadLocal<StringBuilder> cEmptyMessageBuilder = new ThreadLocal<StringBuilder>() | |
{ | |
@Override | |
protected StringBuilder initialValue() { | |
return new StringBuilder(); | |
} | |
@Override | |
public StringBuilder get() { | |
StringBuilder b = super.get(); | |
b.setLength(0); | |
return b; | |
} | |
}; | |
public static String buildGWPostpaidInquryRequest(String pSubscriberId) | |
{ | |
StringBuilder tResult = cGWPostpaidInqReqBuilder.get(); | |
tResult.replace(22, 24, pSubscriberId.substring(0, 2)); | |
tResult.replace(27, 39, Long.toString(cStan.getAndIncrement())); | |
tResult.replace(76, 88, pSubscriberId); | |
return tResult.toString(); | |
} | |
public static String buildGWPostpaidPaymentRequest(String pInquiryResponse) | |
{ | |
StringBuilder tResult = cEmptyMessageBuilder.get(); | |
tResult.append(GW_POSTPAID_PAYMENT_REQUEST_HEAD); | |
tResult.append(pInquiryResponse.substring(20, 82)); | |
int tPayReqBit48Length = pInquiryResponse.substring(89).length() + 33; | |
tResult.append(tPayReqBit48Length); | |
tResult.append(pInquiryResponse.substring(89, 109)); | |
int tBillStatus = Character.getNumericValue(pInquiryResponse.charAt(108)); | |
int tPaymentStatus = tBillStatus > 4? 4: tBillStatus; | |
tResult.append(tPaymentStatus); | |
tResult.append(pInquiryResponse.substring(109, 141)); | |
tResult.append(cSwitcherRefnum.getAndIncrement()); | |
tResult.append(cSwitcherRefnumPadding); | |
tResult.append(pInquiryResponse.substring(141)); | |
return tResult.toString(); | |
} | |
public static String buildGWPrepaidInquryRequest(String pSubscriberIdOrMSN) | |
{ | |
StringBuilder tResult = cGWPrepaidInqReqBuilder.get(); | |
tResult.replace(27, 39, Long.toString(cStan.getAndIncrement())); | |
if(pSubscriberIdOrMSN.length() == 11) | |
{ | |
tResult.replace(76, 87, pSubscriberIdOrMSN); | |
tResult.replace(99, 100, "0"); | |
} | |
else | |
{ | |
tResult.replace(87, 99, pSubscriberIdOrMSN); | |
tResult.replace(99, 100, "1"); | |
} | |
return tResult.toString(); | |
} | |
public static String buildGWPrepaidPaymentRequest(String pInquiryResponse) | |
{ | |
StringBuilder tResult = cEmptyMessageBuilder.get(); | |
tResult.append(GW_PREPAID_PAYMENT_REQUEST_HEAD); | |
tResult.append(pInquiryResponse.substring(27, 66)); | |
int tPayReqBit48Length = Integer.parseInt(pInquiryResponse.substring(70, 73)) + 33; | |
tResult.append(tPayReqBit48Length); | |
tResult.append(pInquiryResponse.substring(73, 136)); | |
tResult.append(cSwitcherRefnum.getAndIncrement()); | |
tResult.append(cSwitcherRefnumPadding); | |
tResult.append(pInquiryResponse.substring(136, 174)); | |
tResult.append('0'); | |
tResult.append(pInquiryResponse.substring(174)); | |
return tResult.toString(); | |
} | |
public static String buildGWNontaglisInquryRequest(String pNoReg) | |
{ | |
StringBuilder tResult = cGWNontaglisInqReqBuilder.get(); | |
tResult.replace(22, 24, pNoReg.substring(0, 2)); | |
tResult.replace(27, 39, Long.toString(cStan.getAndIncrement())); | |
tResult.replace(76, 89, pNoReg); | |
return tResult.toString(); | |
} | |
public static String buildGWNontaglisPaymentRequest(String pInquiryResponse) | |
{ | |
StringBuilder tResult = cEmptyMessageBuilder.get(); | |
tResult.append(GW_NONTAGLIS_PAYMENT_REQUEST_HEAD); | |
tResult.append(pInquiryResponse.substring(20, 82)); | |
int tPayReqBit48Length = Integer.parseInt(pInquiryResponse.substring(86, 89)) + 32; | |
tResult.append(tPayReqBit48Length); | |
tResult.append(pInquiryResponse.substring(89, 190)); | |
tResult.append(cSwitcherRefnum.getAndIncrement()); | |
tResult.append(cSwitcherRefnumPadding); | |
tResult.append(pInquiryResponse.substring(190)); | |
return tResult.toString(); | |
} | |
private static String padStart(String string, int minLength, char padChar) | |
{ | |
if (string.length() >= minLength) | |
{ | |
return string; | |
} | |
StringBuilder sb = new StringBuilder(minLength); | |
for (int i = string.length(); i < minLength; i++) | |
{ | |
sb.append(padChar); | |
} | |
sb.append(string); | |
return sb.toString(); | |
} | |
private static String padEnd(String string, int minLength, char padChar) | |
{ | |
if (string.length() >= minLength) | |
{ | |
return string; | |
} | |
StringBuilder sb = new StringBuilder(minLength); | |
sb.append(string); | |
for (int i = string.length(); i < minLength; i++) | |
{ | |
sb.append(padChar); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment