一、支付流程图

二、流程分析
1.页面ajax访问后台(servlet或resetful接口),给后台传支付参数
2.后台根据参数组织明文和暗文后,返回给前端ajax
3.前端ajax获取后台组织的明文和暗文,以此作为参数访问支付网关
4.支付成功后,光大会访问设置的回调地址,在回调地址中验签,通过则处理订单,以组织明文和暗文回复光大,支付完成
三、代码
1.view层
(1)ajax访问后台资源
function pay(restfulUrl, formdata){ $.get( requestUrl+formdata, function(data, status){ if(data.status == -1) { alert(data.msg); return; } else if(data.status == 1) //window.open(payUrl);//open形式qq浏览器打不开,href的形式可以打开 window.location.href = data.msg.payUrl+"?Plain="+data.msg.plain+"&Signature="+data.msg.signature; }, "json" ); });
3.service层(restful)
(1)PayService.java
1 public class PayService { 2 3 /** 4 * 发送光大支付请求 5 */ 6 public String pay(Map<String, String> paramMap) throws Exception { 7 8 StringBuilder sb = new StringBuilder(""); 9 String split = "~|~"; 10 sb.append("MerchantId="+Config.merchantId+split); //商户代码 11 sb.append("IdentityType=2"+split); //用户标识类型 固定2, 用户ID 12 sb.append("MerCifId="+UUID.randomUUID().toString()+split); //客户在本系统的唯一标示(客户ID),光大据此来记录用户的银行卡信息 13 sb.append("PayType=01"+split); //支付类型 01 网上支付(固定值) 14 sb.append("MerchantSeqNo="+order_id+split); //商户订单号 15 sb.append("MerchantDateTime="+new Date()+split); //商户时间 16 sb.append("TransAmount="+payAmount+split); //交易金额 单位:元 17 sb.append("TerminalType=0"+split); //终端标识类型 0 IMEI,1 MAC,2 UUID(针对IOS系统),3 OTHER 18 sb.append("TerminalId=web"+split); //终端号 19 sb.append("MerSecName="+split); //二级商户名,测试必送中文,生产可以空着 20 sb.append("ProductCataLog=57"+split); //57:综合服务(固定值) 21 sb.append("MerProduct="+""+split); //商品名称 22 sb.append("MerchantUrl="callbackUrl+split); //用于后台通知商户,用servlet响应 23 sb.append("MerchantUrl1="+successUrl+split); //用于后台通知商户失败或者默认情况下,引导客户回商户页面,用servlet响应 24 //sb.append("UserIp", "127.0.0.1"); //支付地址,测试时127.0.0.1在微信浏览器中无法打开 25 sb.append("msgExt="+paramMap.get("biztypeName")); //附加信息 26 27 String plain = sb.toString(); 28 String signature = CebMerchantSignVerify.merchantSignData_ABA(plain); 29 30 sb.delete(0, sb.length()); 31 sb.append("{"); 32 sb.append(""payUrl":""+cebPayUrl+"","); 33 sb.append(""plain":""+plain+"","); 34 sb.append(""signature":""+signature+"""); 35 sb.append("}"); 36 37 return new JSONUtil.toJson(CebBean(1, sb.toString(), "test")); 38 } 39 }
4.回调处理
(1)CallBack.java
@Path("/payback")
public class CebbackService {
@Context
HttpServletRequest req;
@Context
HttpServletResponse resp;
@POST
@Path("/cebback")
public String cebBack(@FormParam("Plain") String plain,
@FormParam("Signature") String signature) {
try {
connection = JDBCUtil.getConnection();
connection.setAutoCommit(false);
//response.setCharacterEncoding("GBK"); //移动端 get utf-8,pc端 post GBK;
//验签银行信息
boolean flag = CebMerchantSignVerify.merchantVerifyPayGate_ABA(signature, plain);
if(flag){
//拆分接收到的plain,提取需要的字段响应光大
String[] datas = plain.split("~\|~");
Map<String, String> map = new HashMap<String, String>();
String [] temp = new String[2];
for(int i = 0 ; i < datas.length ; i++){
System.out.println(datas[i]);
temp = datas[i].split("=");
if(temp.length == 1)
map.put(temp[0], "");
else
map.put(temp[0], temp[1]);
}
map.put("signature", signature);
//组织响应光大的信息
StringBuffer sf = new StringBuffer();
sf.append("merchantId="+map.get("merchantId")+"~|~"); //商户代码 Char 12
sf.append("orderId="+map.get("orderId")+"~|~"); //订单号 Char 30
sf.append("transDateTime="+map.get("transDateTime")+"~|~"); //交易时间 Char 14 订单发起的时间
sf.append("procStatus=1~|~"); //处理状态 Char 1 1: 处理成功
sf.append("MerchantUrl2="+Config.successUrl+"?orderId="+map.get("orderId")); //商户URL2 Char 256 商户成功接收支付订单情况下的,商户展示页面
String plain = sf.toString();
String signature = CebMerchantSignVerify.merchantSignData_ABA(plain);
//resp.reset();
PrintWriter out = resp.getWriter();
resp.getWriter().print("Plain=" + plain);
resp.getWriter().print("
");
resp.getWriter().print("Signature=" + signature);
resp.getWriter().flush();
resp.getWriter().close();
return "光大回调成功!";
}else{
return "不是光大回调此方法";
}
} catch (Exception e) {
e.printStackTrace();
try {
if(connection != null)
connection.rollback();
log(e.getMessage());
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally{
JDBCUtil.close(connection);
}
}
/**
* 成功响应光大后的回调处理(形式)
*/
@GET
@Path("/paysuccess")
public String cebPaySuccess(){
try {
resp.sendRedirect("cebPaySuccess.html?orderId="+req.getParameterMap().get("Plain").get("orderId"));
} catch (IOException e) {
e.printStackTrace();
}
return "支付成功!";
}
(2)servlet
callback.java
1 public void service(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 Connection conn = null; 4 try { 5 conn =JDBCUtil.getConn(); 6 conn.setAutoCommit(false); 7 String plain = request.getParameter("Plain"); 8 String signature = request.getParameter("Signature"); 9 10 //验签银行信息 11 boolean flag = CebMerchantSignVerify.merchantVerifyPayGate_ABA(signature, plain); 12 if(flag){ 13 //拆分接收到的plain,提取需要的字段响应光大 14 Map<String, String> map = CebUtil.getParam(plain, "~\|~"); 15 map.put("signature", signature); 16 17 //组织响应信息 18 StringBuffer sf = new StringBuffer(); 19 sf.append("merchantId="+map.get("merchantId")+"~|~"); //商户代码 Char 12 20 sf.append("orderId="+map.get("orderId")+"~|~"); //订单号 Char 30 21 sf.append("transDateTime="+map.get("transDateTime")+"~|~"); //交易时间 Char 14 订单发起的时间 22 sf.append("procStatus=1~|~"); //处理状态 Char 1 1: 处理成功 23 sf.append("MerchantUrl2="+cebSuccessUrl);//商户URL2 Char 256 商户成功接收支付订单情况下的,商户展示页面 用servlet响应 24 //sf.append("MerchantUrl2="+cebSuccessUrl/restful/callback/cebsuccess");//报错:java.lang.IllegalArgumentException: Illegal character in query 25 String plain = sf.toString(); 26 String signature = CebMerchantSignVerify.merchantSignData_ABA(plain); 27 28 //响应光大支付的回调 29 //response.reset(); 30 PrintWriter out = response.getWriter(); 31 out.print("Plain=" + resPlain); 32 out.print(" "); 33 out.print("Signature=" + resSignature); 34 out.flush(); 35 out.close(); 36 //return; //写或不写,光大支付都能正确收到响应 37 }else{ 38 System.out.println("非光大调用此方法"); 39 return; 40 } 41 } catch (Exception e) { 42 try { 43 if(conn != null) 44 conn.rollback(); 45 } catch (SQLException e1) { 46 e1.printStackTrace(); 47 } 48 return; 49 } finally{ 50 JDBCUtil.closeConn(conn); 51 } 52 }
success.java
1 public void service(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 try { 4 response.sendRedirect(cebSuccessUrl+"?orderId="+request.getParameterMap().get("Plain").get("orderId")); 5 } catch (IOException e) { 6 e.printStackTrace(); 7 } 8 } 9
四、运行结果


