1、实现和建行的网关通信需要有建行商户号才可以,使用商户号登陆到建行商户后台可以生产一个商户秘钥,商户秘钥的后30位作为加密时的PUB参数
商户证书下载地址和登陆地址
http://www.ccb.com/fj/20110310_1299746353.html
2、代码实现
这里我写了两个jsp实现
index2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>建行订单表单页</title>
</head>
<body>
<form action="index3.jsp" method="post">
商户编号: <input type="text" name="MERCHANTID" readonly="readonly" value="xxxxxxxxxxx"/><BR>
柜台代码: <input type="text" name="POSID" readonly="readonly" value="xxxxxxx" /><BR>
分行代码: <input type="text" name="BRANCHID" readonly="readonly" value="xxxxxxxxx"/><BR>
商品订单: <input type="text" name="ORDERID" id="orderid" value=""/><BR>
商品金额: <input type="text" name="PAYMENT" value="0.01"/><BR>
支持币种: <input type="text" name="CURCODE" value="01"/><BR>
交易码: <input type="text" name="TXCODE" readonly="readonly" value="xxxxxx"/><BR>
REMARK1: <input type="text" name="REMARK1" value="abc"/><BR>
REMARK2: <input type="text" name="REMARK2" value="123"/><BR>
TYPE: <input type="text" name="TYPE" readonly="readonly" value="1"/><BR>
GATEWAY: <input type="text" name="GATEWAY" value=""/><BR>
CLIENTIP: <input type="text" name="CLIENTIP" value=""/><BR>
REGINFO: <input type="text" name="REGINFO" value="xiaofeixia"/><BR>
PROINFO: <input type="text" name="PROINFO" value="xxxxxx"/><BR>
REFERER: <input type="text" name="REFERER" value=""/><BR>
PUB: <input type="text" name="PUB" value="xxxxxxxxx"/><BR>
<input type="submit"/>
</form>
<script type="text/javascript" defer=true>
Date.prototype.Format = function(fmt)
{ s
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
var time1 = new Date().Format("yyyyMMddhhmmss");
document.getElementById("orderid").value=time1;
</script>
</body>
</html>
index3.jsp
<%@ page language="java" import="java.util.*,java.security.MessageDigest" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String url="https://ibsbjstar.ccb.com.cn/app/ccbMain";
String MERCHANTID=request.getParameter("MERCHANTID");
String POSID=request.getParameter("POSID");
String BRANCHID=request.getParameter("BRANCHID");
String ORDERID=request.getParameter("ORDERID");
String PAYMENT=request.getParameter("PAYMENT");
String CURCODE=request.getParameter("CURCODE");
String TXCODE=request.getParameter("TXCODE");
String REMARK1=request.getParameter("REMARK1");
String REMARK2=request.getParameter("REMARK2");
String TYPE=request.getParameter("TYPE");
String PUB=request.getParameter("PUB");
String GATEWAY=request.getParameter("GATEWAY");
String CLIENTIP=request.getParameter("CLIENTIP");
String REGINFO=request.getParameter("REGINFO");
String PROINFO=request.getParameter("PROINFO");
String REFERER=request.getParameter("REFERER");
//生成MAC
String MAC=getMAC(MERCHANTID,POSID,BRANCHID,ORDERID,PAYMENT,CURCODE,TXCODE,REMARK1,REMARK2,TYPE,PUB,GATEWAY,CLIENTIP,REGINFO,PROINFO,REFERER);
%>
<%!
public static String getMAC(String MERCHANTID,
String POSID,String BRANCHID,
String ORDERID,String PAYMENT ,
String CURCODE,String TXCODE,
String REMARK1,String REMARK2,
String TYPE,String PUB,
String GATEWAY,String CLIENTIP,
String REGINFO,String PROINFO,
String REFERER) throws Exception {
StringBuffer sb=new StringBuffer();
sb.append("MERCHANTID="+MERCHANTID);
sb.append("&POSID="+POSID);
sb.append("&BRANCHID="+BRANCHID);
sb.append("&ORDERID="+ORDERID);
sb.append("&PAYMENT="+PAYMENT);
sb.append("&CURCODE="+CURCODE);
sb.append("&TXCODE="+TXCODE);
sb.append("&REMARK1="+REMARK1);
sb.append("&REMARK2="+REMARK2);
sb.append("&TYPE="+TYPE);
sb.append("&PUB="+PUB);
sb.append("&GATEWAY="+GATEWAY);
sb.append("&CLIENTIP="+CLIENTIP);
sb.append("®INFO="+REGINFO);
sb.append("&PROINFO="+PROINFO);
sb.append("&REFERER="+REFERER);
System.out.println("需要加密的字符串:\n"+sb.toString().trim());
String MAC= bintoascii(encryptMD5(sb.toString().trim().getBytes()));
System.out.println("MAC:\n"+MAC);
return MAC;
}
public static byte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(data);
return md5.digest();
}
public static String bintoascii(byte []bySourceByte){
int len,i;
byte tb;
char high,tmp,low;
String result=new String();
len=bySourceByte.length;
for(i=0;i<len;i++)
{
tb=bySourceByte[i];
tmp=(char)((tb>>>4)&0x000f);
if(tmp>=10)
high=(char)('a'+tmp-10);
else
high=(char)('0'+tmp);
result+=high;
tmp=(char)(tb&0x000f);
if(tmp>=10)
low=(char)('a'+tmp-10);
else
low=(char)('0'+tmp);
result+=low;
}
return result;
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="<%=url%>" method="post" id="form">
商户编号: <input type="text" name="MERCHANTID" value="<%=MERCHANTID%>"/><BR>
柜台代码: <input type="text" name="POSID" value="<%=POSID%>"/><BR>
分行代码: <input type="text" name="BRANCHID" value="<%=BRANCHID%>"/><BR>
商品订单: <input type="text" name="ORDERID" value="<%=ORDERID%>"/><BR>
商品金额: <input type="text" name="PAYMENT" value="<%=PAYMENT%>"/><BR>
支持币种: <input type="text" name="CURCODE" value="<%=CURCODE%>"/><BR>
交易码: <input type="text" name="TXCODE" value="<%=TXCODE%>"/><BR>
REMARK1: <input type="text" name="REMARK1" value="<%=REMARK1%>"/><BR>
REMARK2: <input type="text" name="REMARK2" value="<%=REMARK2%>"/><BR>
TYPE: <input type="text" name="TYPE" value="<%=TYPE%>"/><BR>
GATEWAY: <input type="text" name="GATEWAY" value="<%=GATEWAY%>"/><BR>
CLIENTIP: <input type="text" name="CLIENTIP" value="<%=CLIENTIP%>"/><BR>
REGINFO: <input type="text" name="REGINFO" value="<%=REGINFO%>"/><BR>
PROINFO: <input type="text" name="PROINFO" value="<%=PROINFO%>"/><BR>
REFERER: <input type="text" name="REFERER" value="<%=REFERER%>"/><BR>
MAC: <input type="text" name="MAC" value="<%=MAC%>"/><BR>
<input type="submit"/>
</form>
<script type="text/javascript">
document.getElementById("form").submit();
</script>
</body>
</html>