三、支付流程源码
1、创建前端网页index.jsp,提交form表单数据,如:订单号orderID、金额amount和付款银行pd_FrpID。这里注意大小写,网上好多源码这里大小写不一样,有写成orderId。这些变量名定义随意,但是后面的servlet中一定要保持变量名一致,不然getParameter后值为null。当初找这个马大哈的错误找了半天。。。。。
<%@ page language="java" import="java.util.*" pageEncoding="<strong><span style="color:#FF0000;">GBK</span></strong>"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Java在线支付第一步,选择支付银行</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<form action="${pageContext.request.contextPath}/servlet/yeepay/PaymentRequest" method="POST">
<table align="center" width="600" border="6" cellspacing="0" cellpadding="2">
<tr>
<td align="center" colspan="4" bgcolor="#FFD2E9">
<b>订单号:</b><input type="text" name="orderID">
<b>应付金额:¥</b><input type="text" name="amount" size="6"><b>元</b>
</td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td colspan="4" bgcolor="#C0C0C0">请选择在线支付银行</td>
</tr>
<tr>
<td height="25" width="24%"><input type="radio" name="pd_FrpID" value="ICBC-NET">工商银行</td>
<td height="25" width="24%"><input type="radio" name="pd_FrpId" value="CMBCHINA-NET">招商银行</td>
<td height="25" width="24%"><input type="radio" name="pd_FrpID" value="ABC-NET">农业银行</td>
<td height="25" width="28%"><input type="radio" name="pd_FrpID" value="CCB-NET">建设银行</td>
</tr>
<tr>
<td height="25"><input type="radio" name="pd_FrpID" value="CEB-NET" >光大银行</td>
<td height="25"><input type="radio" name="pd_FrpID" value="BOCO-NET">交通银行</td>
<td height="25"><input type="radio" name="pd_FrpID" value="CMBC-NET">民生银行</td>
<td height="25"><input type="radio" name="pd_FrpID" value="SDB-NET">深圳发展银行</td>
</tr>
<tr>
<td height="25"><input type="radio" name="pd_FrpID" value="BCCB-NET">北京银行</td>
<td height="25"><input type="radio" name="pd_FrpID" value="CIB-NET">兴业银行</td>
<td height="25"><input type="radio" name="pd_FrpID" value="ECITIC-NET">中信银行</td>
<td height="25"><input type="radio" name="pd_FrpID" value="SPDB-NET">浦东发展银行</td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td colspan="4" align="center" bgcolor="#FFDAB5"><input type="submit" value=" 确认支付 "/></td>
</tr>
</table>
</form>
</body>
</html>
2、表单信息提交给PaymentRequest,并转发到WEB-INF文件夹下子文件夹page下的connect.jsp
package myservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import myutil.ConfigInfo;
import myutil.PaymentUtil;
//用于发起支付请求
/**
* 发起支付请求
* @see 正式商户:本人亲测,可以直接连到银行页面
* @see p1_MerId=10001126856
* @see keyValue=69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl
* @see ==============================================================================================
* @see accountCallbackURL=http://127.0.0.1:8088/payment/servlet/yeepay/PaymentResultServlet
* @see 此时机器需联网。若使用路由上的网,只要把路由的IP写进去,再在路由里配置一个8088端口的转发规则,即可
<pre name="code" class="java"> * @see 若是网线接入,直接改成http://127.0.0.1:8080/payment/servlet/yeepay/PaymentResultServlet
* @see ==============================================================================================
*/
public class PaymentRequest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
/* //测试商户:商户编号
String accountID = "10000432521";
//测试商户:密钥
String keyValue = "8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t";
//测试商户:商户接收支付成功数据的地址
String accountCallbackURL = "http://127.0.0.1:8088/payment/servlet/yeepay/PaymentResultServlet";
*/
String orderID = request.getParameter("orderID");//订单号
String amount = request.getParameter("amount");//支付金额
String accountBankID = request.getParameter("pd_FrpId");//获取用户选择的支付银行
String accountID = ConfigInfo.getValue("p1_Merid");
String keyValue = ConfigInfo.getValue("keyValue");
String accountCallbackURL = ConfigInfo.getValue("merchantCallbackURL");
System.out.println("accountID="+accountID+" keyValue="+keyValue+" accountCallbackURL="+accountCallbackURL);
String businessType = "Buy"; //业务类型。Buy为在线支付
String currency = "CNY"; //交易币种。CNY为人民币
String productDesc = ""; //商品描述
String productCategory = ""; //商品种类
String productID = ""; //商品ID
String addressFlag = "0"; //送货地址。0为不需要,1为需要
String accountMoreInfo = ""; //商户扩展信息
String pr_NeedResponse = "0"; //应答机制
String md5hmac = PaymentUtil.buildHmac(
businessType, accountID, orderID, amount, currency, productID, productCategory,
productDesc, accountCallbackURL, addressFlag, accountMoreInfo, accountBankID,
pr_NeedResponse, keyValue);
request.setAttribute("businessType", businessType);
request.setAttribute("accountID", accountID);
request.setAttribute("orderID", orderID);
request.setAttribute("amount", amount);
request.setAttribute("currency", currency);
request.setAttribute("productID", productID);
request.setAttribute("productCategory", productCategory);
request.setAttribute("productDesc", productDesc);
request.setAttribute("accountCallbackURL", accountCallbackURL);
request.setAttribute("addressFlag", addressFlag);
request.setAttribute("accountMoreInfo", accountMoreInfo);
request.setAttribute("accountBankID", accountBankID);
request.setAttribute("pr_NeedResponse", pr_NeedResponse);
request.setAttribute("md5hmac", md5hmac);
request.getRequestDispatcher("/WEB-INF/page/connection.jsp").forward(request, response);
}
}
3、 connect.jsp转发到易宝的正式网关正式网关:https://www.yeepay.com/app-merchant-proxy/node,本人亲测,转了四五次一分钱,木得办法,学这个消化慢,所以只能不着急,多学几次就会啦~
测试网关:http://tech.yeepay.com:8080/robot/debug.action(测试网关进不去,出现以下情况,至今不知道是什么原因,有小伙伴知道的麻烦告知一声)
package myservlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import myutil.ConfigInfo;
import myutil.PaymentUtil;
public class PaymentResultResponse extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("<strong><span style="color:#FF0000;">GBK</span></strong>");
String p1_MerId = ConfigInfo.getValue("p1_MerID");
String keyValue = ConfigInfo.getValue("keyValue");
String r0_Cmd = request.getParameter("r0_Cmd"); //业务类型
String r1_Code = request.getParameter("r1_Code"); //扣款结果。1:扣款成功
String r2_TrxId = request.getParameter("r2_TrxId"); //易宝交易订单号
String r3_Amt = request.getParameter("r3_Amt"); //扣款金额。交易结束后,易宝交易系统将实际扣款金额返回给商户
String r4_Cur = request.getParameter("r4_Cur"); //交易币种。人民币为CNY
String r5_Pid = request.getParameter("r5_Pid"); //商品ID
String r6_Order = request.getParameter("r6_Order"); //商户订单号
String r7_Uid = request.getParameter("r7_Uid"); //易宝会员ID
String r8_MP = request.getParameter("r8_MP"); //商户扩展信息。可任意填写1K的字符串,交易返回时将原样返回
String r9_BType = request.getParameter("r9_BType"); //交易结果通知类型。1:交易成功回调(浏览器重定向),2:交易成功主动通知(服务器点对点通讯)
String rb_BankId = request.getParameter("rb_BankId");//支付银行
String rp_PayDate = request.getParameter("rp_PayDate");//在银行支付时的时间
String hmac = request.getParameter("hmac"); //MD5交易签名
boolean result = PaymentUtil.verifyCallback(
hmac, p1_MerId, r0_Cmd, r1_Code, r2_TrxId, r3_Amt, r4_Cur,
r5_Pid, r6_Order, r7_Uid, r8_MP, r9_BType, keyValue);
if (result) {
if ("1".equals(r1_Code)) {
<strong>//支付成功,应该把数据库中订单支付状态改为已支付,否则容易出现"无限刷点卡"的BUG </strong>
StringBuffer message = new StringBuffer();
message.append("订单号为:" + r6_Order + " 的订单支付成功,");
message.append("用户支付了" + r3_Amt + "元。<br/>");
message.append("交易结果通知类型:");
if ("1".equals(r9_BType)) {
message.append("浏览器重定向。<br/>");
} else if ("2".equals(r9_BType)) {
message.append("易宝支付网关后台程序通知。<br/>");
}
message.append("易宝订单系统中的订单号为:" + r2_TrxId);
request.setAttribute("message", message);
} else {
request.setAttribute("message", "用户支付失败");
}
} else {
request.setAttribute("message", "数据来源不合法");
}
request.getRequestDispatcher("/WEB-INF/page/paymentResult.jsp").forward(request, response);
}
}