package com.tenwa.sdk.utils; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Vector; import com.amarsoft.awe.Configure; import com.tenwa.sdk.VO.SDKCheckCallBackVO; import com.tenwa.sdk.VO.SDKCheckCallDetail; import com.tenwa.sdk.VO.SDKCheckCallInfoVO; import com.tenwa.sdk.VO.SDKPayCallBackVO; import com.tenwa.sdk.VO.SDKPayCallDetail; import com.tenwa.sdk.VO.SDKPayCallInfoVO; public class HttpRequest { private String ipAddr ; private int timeout ; public String getIpAddr() { return ipAddr; } public void setIpAddr(String ipAddr) { this.ipAddr = ipAddr; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public HttpRequest(String ipAddr) { this.ipAddr = ipAddr; } public HttpRequest() throws Exception { Configure config = Configure.getInstance(); this.ipAddr = config.getConfigure("SDK_CMB_URL") ; this.timeout = Integer.parseInt(config.getConfigure("SDK_CMB_TIME_OUT") ); } public SDKPayCallBackVO sendPayRequest(String data) { SDKPayCallBackVO callBack = new SDKPayCallBackVO(); SDKPayCallInfoVO info = new SDKPayCallInfoVO(); String result = ""; try { URL url = new URL(this.ipAddr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(this.timeout); connection.setReadTimeout(this.timeout); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept", "application/xml"); connection.setRequestProperty("Content-Type", "application/xml"); connection.connect(); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "GBK"); out.append(data); out.flush(); out.close(); int code = connection.getResponseCode(); InputStream is = null; if (code == 200) { is = connection.getInputStream(); } else { is = connection.getErrorStream(); } int length = (int) connection.getContentLength(); if (length != -1) { byte[] datas = new byte[length]; byte[] temp = new byte[512]; int readLen = 0; int destPos = 0; while ((readLen = is.read(temp)) > 0) { System.arraycopy(temp, 0, datas, destPos, readLen); destPos += readLen; } result = new String(datas, "GBK"); } is.close(); // System.out.println("----"); // System.out.println(result); // System.out.println("----"); } catch (MalformedURLException e) { info.setSTATUS(-1); info.setERRMSG("URL协议、格式或者路径错误"); } catch (UnsupportedEncodingException e) { info.setSTATUS(-1); info.setERRMSG("字符编码有问题"); } catch (ProtocolException e) { info.setSTATUS(-1); info.setERRMSG("协议异常"); } catch (IOException e) { info.setSTATUS(-1); info.setERRMSG("响应超时"); } if (info.getSTATUS() != -1) { info.setSTATUS(1); info.setXML(result); } callBack.setInfo(info); return toPayObj(callBack); } @SuppressWarnings({ "rawtypes", "unchecked" }) private SDKPayCallBackVO toPayObj(SDKPayCallBackVO vo) { if (vo.getInfo().getSTATUS() != -1) { SDKPayCallInfoVO info = vo.getInfo(); String result = vo.getInfo().getXML(); if (result != null && result.length() > 0) { XmlPacket pktRsp = XmlPacket.valueOf(result); if (pktRsp != null) { String sRetCod = pktRsp.getRETCOD(); String sFunnam = pktRsp.getFUNNAM(); String sDattpy = pktRsp.getDATTYP(); String sErrmsg = pktRsp.getERRMSG(); info.setRETCOD(sRetCod); info.setFUNNAM(sFunnam); info.setDATTYP(sDattpy); Vector vec = null ; if (sRetCod.equals("0")) { vec = pktRsp.getProperty("NTQPAYRQZ"); if(vec!=null && vec.size()>0) { List details = new ArrayList(); for (Map propPayResult : vec) { SDKPayCallDetail detail_one = new SDKPayCallDetail(); String REQSTS = (String) propPayResult.get("REQSTS"); String RTNFLG = (String) propPayResult.get("RTNFLG"); String SQRNBR = (String) propPayResult.get("SQRNBR"); String YURREF = (String) propPayResult.get("YURREF"); String REQNBR = (String) propPayResult.get("REQNBR"); String OPRSQN = (String) propPayResult.get("OPRSQN"); String OPRALS = (String) propPayResult.get("OPRALS"); String ERRCOD = (String) propPayResult.get("ERRCOD"); String ERRTXT = (String) propPayResult.get("ERRTXT"); detail_one.setERRCOD(ERRCOD); detail_one.setERRTXT(ERRTXT); detail_one.setOPRALS(OPRALS); detail_one.setOPRSQN(OPRSQN); detail_one.setREQNBR(REQNBR); detail_one.setREQSTS(REQSTS); detail_one.setRTNFLG(RTNFLG); detail_one.setSQRNBR(SQRNBR); detail_one.setYURREF(YURREF); if (REQSTS.equals("FIN") && RTNFLG.equals("F")) { detail_one.setERRTXT("支付失败:" + ERRTXT); } else { detail_one.setERRTXT("支付已被银行受理(支付状态:" + REQSTS + ")"); } details.add(detail_one); } vo.setDetails(details); } } else if (sRetCod.equals("-9")) { info.setERRMSG("支付未知异常,请查询支付结果确认支付状态,错误信息:" + sErrmsg); } else { info.setERRMSG("支付失败:" + sErrmsg); } } else { info.setERRMSG("响应报文解析失败"); } } vo.setInfo(info); } return vo; } public SDKCheckCallBackVO sendCheckRequest(String data) { SDKCheckCallBackVO callBack = new SDKCheckCallBackVO(); SDKCheckCallInfoVO info = new SDKCheckCallInfoVO(); String result = ""; try { URL url = new URL(this.ipAddr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(this.timeout); connection.setReadTimeout(this.timeout); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept", "application/xml"); connection.setRequestProperty("Content-Type", "application/xml"); connection.connect(); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "GBK"); out.append(data); out.flush(); out.close(); int code = connection.getResponseCode(); InputStream is = null; if (code == 200) { is = connection.getInputStream(); } else { is = connection.getErrorStream(); } int length = (int) connection.getContentLength(); if (length != -1) { byte[] datas = new byte[length]; byte[] temp = new byte[512]; int readLen = 0; int destPos = 0; while ((readLen = is.read(temp)) > 0) { System.arraycopy(temp, 0, datas, destPos, readLen); destPos += readLen; } result = new String(datas, "GBK"); } is.close(); System.out.println("----"); System.out.println(result); System.out.println("----"); } catch (MalformedURLException e) { info.setSTATUS(-1); info.setERRMSG("URL协议、格式或者路径错误"); } catch (UnsupportedEncodingException e) { info.setSTATUS(-1); info.setERRMSG("字符编码有问题"); } catch (ProtocolException e) { info.setSTATUS(-1); info.setERRMSG("协议异常"); } catch (IOException e) { info.setSTATUS(-1); info.setERRMSG("响应超时"); } if (info.getSTATUS() != -1) { info.setSTATUS(1); info.setXML(result); } callBack.setInfo(info); return toCheckObj(callBack); } @SuppressWarnings({ "rawtypes", "unchecked" }) private SDKCheckCallBackVO toCheckObj(SDKCheckCallBackVO vo) { if (vo.getInfo().getSTATUS() != -1) { SDKCheckCallInfoVO info = vo.getInfo(); String result = vo.getInfo().getXML(); if (result != null && result.length() > 0) { XmlPacket pktRsp = XmlPacket.valueOf(result); if (pktRsp != null) { String sRetCod = pktRsp.getRETCOD(); String sFunnam = pktRsp.getFUNNAM(); String sDattpy = pktRsp.getDATTYP(); String sErrmsg = pktRsp.getERRMSG(); info.setRETCOD(sRetCod); info.setFUNNAM(sFunnam); info.setDATTYP(sDattpy); Vector vec = null ; if (sRetCod.equals("0")) { vec = pktRsp.getProperty("NTQPAYQYZ"); if(vec!=null && vec.size()>0) { List details = new ArrayList(); for (Map propPayResult : vec) { SDKCheckCallDetail detail_one = new SDKCheckCallDetail(); String BUSCOD = (String) propPayResult.get("BUSCOD"); String BUSMOD = (String) propPayResult.get("BUSMOD"); String DBTBBK = (String) propPayResult.get("DBTBBK"); String DBTACC = (String) propPayResult.get("DBTACC"); String DBTNAM = (String) propPayResult.get("DBTNAM"); String DBTBNK = (String) propPayResult.get("DBTBNK"); String CRTACC = (String) propPayResult.get("CRTACC"); String CRTNAM = (String) propPayResult.get("CRTNAM"); String CRTBNK = (String) propPayResult.get("CRTBNK"); String CCYNBR = (String) propPayResult.get("CCYNBR"); String TRSAMT = (String) propPayResult.get("TRSAMT"); String YURREF = (String) propPayResult.get("YURREF"); String REQNBR = (String) propPayResult.get("REQNBR"); String REQSTS = (String) propPayResult.get("REQSTS"); String RTNFLG = (String) propPayResult.get("RTNFLG"); String RTNNAR = (String) propPayResult.get("RTNNAR"); detail_one.setBUSCOD(BUSCOD); detail_one.setBUSMOD(BUSMOD); detail_one.setDBTBBK(DBTBBK); detail_one.setDBTACC(DBTACC); detail_one.setDBTNAM(DBTNAM); detail_one.setDBTBNK(DBTBNK); detail_one.setCRTACC(CRTACC); detail_one.setCRTNAM(CRTNAM); detail_one.setCRTBNK(CRTBNK); detail_one.setCCYNBR(CCYNBR); detail_one.setTRSAMT(TRSAMT); detail_one.setYURREF(YURREF); detail_one.setREQNBR(REQNBR); detail_one.setYURREF(YURREF); detail_one.setREQSTS(REQSTS); detail_one.setRTNFLG(RTNFLG); detail_one.setRTNNAR(RTNNAR); details.add(detail_one); } vo.setDetails(details); } } else if (sRetCod.equals("-9")) { info.setERRMSG("支付未知异常,请查询支付结果确认支付状态,错误信息:" + sErrmsg); } else { info.setERRMSG("支付失败:" + sErrmsg); } } else { info.setERRMSG("响应报文解析失败"); } } vo.setInfo(info); } return vo; } }