随着现在电商等平台如雨后春笋般的发展,在线支付越来越火,各种移动端的支付也是层出不穷,什么微信支付、微博支付等等,其实万变不离其宗,今天大桥就给大家讲解一个Java利用易宝支付在线支付的例子,当然首先要感谢一些传智播客的黎活明老师。
在线支付的第一步,也就是用户在线支付看到的第一个页面,这个页面里主要包含三项:订单号、金额、所选银行,这三个缺一不可。 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>支付第一步,选择支付银行</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <table width="960" border="0" align="center"> <tr> <td width="536" valign="top"> <form action="${pageContext.request.contextPath}/servlet/yeepay/PaymentRequestServlet" method="post" name="paymentform"> <table width="100%" border="0"> <tr> <td height="30" colspan="4"> <table width="100%" height="50" border="0" cellpadding="0" cellspacing="1" bgcolor="#A2E0FF"> <tr> <td align="center" bgcolor="#F7FEFF"> <h3>订单号: <INPUT TYPE="text" NAME="orderid"> 应付金额:¥<INPUT TYPE="text" NAME="amount" size="6">元 </h3> </td> </tr> </table> </td> </tr> <tr> <td colspan="4"></td> </tr> <tr> <td height="30" colspan="4" bgcolor="#F4F8FF"> <span class="STYLE3">请您选择在线支付银行</span> </td> </tr> <tr> <td width="26%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CMBCHINA-NET">招商银行 </td> <td width="25%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="ICBC-NET">工商银行 </td> <td width="25%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="ABC-NET">农业银行 </td> <td width="24%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CCB-NET">建设银行 </td> </tr> <tr> <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CMBC-NET">中国民生银行总行 </td> <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="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="SPDB-NET">上海浦东发展银行 </td> <td><INPUT TYPE="radio" NAME="pd_FrpId" value="ECITIC-NET">中信银行 </td> </tr> <tr> <td colspan="4"></td> </tr> <tr> <td colspan="4" align="center"> <input type="submit" value=" 确认支付 "/> </td> </tr> </table> </form> </td> <td colspan="2" valign="top"> <div class="divts"> <table width="400" border="0" align="center" cellpadding="5" cellspacing="0"> </table> </div> <div id="blankmessage"></div> </td> </tr> <tr> <td></td> <td width="290"></td> <td width="120"></td> </tr> </table> </body> </html> 取得前台提交的三个参数,并封装易宝支付所需的其他参数,并把这些参数放到Request对象中。 package cn.bridgeli.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.bridgeli.utils.ConfigInfo; import cn.bridgeli.utils.PanymentUtil; /** * 发起支付请求 */ public class PaymentRequestServlet 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 orderid = request.getParameter("orderid");//订单号 String amount = request.getParameter("amount");//支付金额 String pd_FrpId = request.getParameter("pd_FrpId");//选择的支付银行 String p1_MerId = ConfigInfo.getValue("p1_MerId"); String keyValue = ConfigInfo.getValue("keyValue"); String merchantCallbackURL = ConfigInfo.getValue("merchantCallbackURL"); String messageType = "Buy"; // 请求命令,在线支付固定为Buy String currency = "CNY"; // 货币单位 String productDesc = ""; // 商品描述 String productCat = ""; // 商品种类 String productId = ""; // 商品ID String addressFlag = "0"; // 需要填写送货信息 0:不需要 1:需要 String sMctProperties = ""; // 商家扩展信息 String pr_NeedResponse = "0"; // 应答机制 String md5hmac = PanymentUtil.buildHmac(messageType, p1_MerId, orderid, amount, currency, productId, productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties, pd_FrpId, pr_NeedResponse, keyValue); request.setAttribute("messageType", messageType); request.setAttribute("merchantID", p1_MerId); request.setAttribute("orderId", orderid); request.setAttribute("amount", amount); request.setAttribute("currency", currency); request.setAttribute("productId", productId); request.setAttribute("productCat", productCat); request.setAttribute("productDesc", productDesc); request.setAttribute("merchantCallbackURL", merchantCallbackURL); request.setAttribute("addressFlag", addressFlag); request.setAttribute("sMctProperties", sMctProperties); request.setAttribute("frpId", pd_FrpId); request.setAttribute("pr_NeedResponse", pr_NeedResponse); request.setAttribute("hmac", md5hmac); request.getRequestDispatcher("/WEB-INF/page/connection.jsp").forward(request, response); } } 因为易宝支付所需的数据都是通过表单提交的,所以取得上一步封装的各个参数,通过一个表单提交到易宝支付提供的接口。所以表单的action应该是易宝支付的接口。 <%@ page language="java" pageEncoding="GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>发起支付请求</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body onload="javascript:document.forms[0].submit()"> <!– http://tech.yeepay.com:8080/robot/debug.action –> <form name="yeepay" action="https://www.yeepay.com/app-merchant-proxy/node" method=’post’> <input type=’hidden’ name=’p0_Cmd’ value="${messageType}"> <!– 请求命令,在线支付固定为Buy –> <input type=’hidden’ name=’p1_MerId’ value="${merchantID}"> <!– 商家ID –> <input type="hidden" name="p2_Order" value="${orderId}"> <!– 商家的交易定单号 –> <input type=’hidden’ name=’p3_Amt’ value="${amount}"> <!– 订单金额 –> <input type=’hidden’ name=’p4_Cur’ value="${currency}"> <!– 货币单位 –> <input type=’hidden’ name=’p5_Pid’ value="${productId}"> <!– 商品ID –> <input type=’hidden’ name=’p6_Pcat’ value="${productCat}"> <!– 商品种类 –> <input type=’hidden’ name=’p7_Pdesc’ value="${productDesc}"> <!– 商品描述 –> <input type=’hidden’ name=’p8_Url’ value="${merchantCallbackURL}"> <!– 交易结果通知地址 –> <input type=’hidden’ name=’p9_SAF’ value="${addressFlag}"> <!– 需要填写送货信息 0:不需要 1:需要 –> <input type=’hidden’ name=’pa_MP’ value="${sMctProperties}"> <!– 商家扩展信息 –> <input type=’hidden’ name=’pd_FrpId’ value="${frpId}"> <!– 银行ID –> <!– 应答机制 为“1”: 需要应答机制;为“0”: 不需要应答机制 –> <input type="hidden" name="pr_NeedResponse" value="0"> <input type=’hidden’ name=’hmac’ value="${hmac}"> <!– MD5-hmac验证码 –> </form> </body> </html> 编写易宝支付处理上一步处理数据完成后所返回的后台处理类(这个处理类必须在公网上,即使是测试程序,即可以访问的),并根据易宝支付所返回的数据,并完成一下几件事(也就是接收易宝支付处理结果的类): (1). 判断支付的结果
...