農業銀行對接會給一個銀企通平台的安裝程序,然后找一台window系統的服務器安裝作為前置機。
附錄:銀企通的配置(本地服務器配置銀行會提供給,默認TCP協議,HTTP協議雖然有選項但是不支持,默認占用前置機的15999端口,ERP模式下可以自己修改)
然后對接支付的主要問題就是給前置機的IP地址的15999端口發送要求格式的報文就行了,端口會根據報文中的交易碼自動判斷當前屬於什么交易。
報文就不貼了,對接的時候跟銀行技術人員多溝通下,問問他們報文的一些注意事項。
//報文拼接可以選擇dom4j Element root = DocumentHelper.createElement("ap"); //根節點 root.addElement("CCTransCode").addText("CFRT02");//交易代碼 root.addElement("ProductID").addText("ICC");//產品標志 root.addElement("ChannelType").addText("ERP");//渠道標志 return root.asXML();
/** * 請求數據:加密標識(1加密,0不加密) + 請求xml數據的長度(默認7位,不夠補空格) + 請求的xml public static String genRequestData(String s) throws Exception { return "0" + String.format("%1$-6s", s.getBytes("gbk").length) + s; } //報文發送時前面會有一個 2-6位的加密標志作為報文頭,這個和銀行確認下是否加密,要以gbk編碼發送給前置機,從網上找的材料意思是前置機是gbk編碼的,utf8加密應該會報錯
/** * TCP報文發送 * @param url * @param port * @param data * @return * @throws Exception */ public static String socketSendAndReceive(String url, int port, String data) throws Exception { System.out.println("請求數據:" + data); Socket socket = new Socket(url, port); OutputStream bw = socket.getOutputStream(); bw.write(data.getBytes("gbk")); bw.flush(); InputStream ips = socket.getInputStream(); StringBuffer sb = new StringBuffer(); int len = 0; byte[] buf = new byte[1024]; while ((len = ips.read(buf)) != -1) { sb.append(new String(buf, 0, len, "gbk")); } bw.close(); ips.close(); socket.close(); return sb.toString(); }
/** * 把XML無腦解析為MAP * * @param msg * @author jieYW * @date 2018/5/29 * @return com.mind.pay.abc.ap.ApXmlBuilder */ public static Map<String,Object> parseXml(String msg)throws Exception { Map<String,Object> resultMap=new HashMap<>(); msg=msg.substring(msg.indexOf("<")); InputStream inputStream = new ByteArrayInputStream(msg.getBytes("UTF-8")); SAXReader reader = new SAXReader(); Document document = reader.read(inputStream); Element root = document.getRootElement(); List<Element> elementList = root.elements(); // 遍歷所有子節點 resultMap=getAllElements(elementList,resultMap); // 釋放資源 inputStream.close(); inputStream = null; return resultMap; } private static Map<String, Object> getAllElements(List<Element> childElements,Map<String,Object> mapEle) { for (Element ele : childElements) { mapEle.put(ele.getName(), ele.getText()); if(ele.elements().size()>0){ mapEle = getAllElements(ele.elements(), mapEle); } } return mapEle; }