13.java在线支付所有源码:
创梦综合技术qq交流群:CreDream:251572072
-------------------------------------
a.新建工程payment
--------------
b./payment/src/com/credream/servlet/PaymentRequest.java
package com.credream.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 com.credream.utils.ConfigInfo;
import com.credream.utils.PanymentUtil;
/**
* 发起支付请求
* @author 传智播客
*
*/
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 {
/*
* p1_MerId=10000326625// 商家的id
keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek //密钥
merchantCallbackURL=http\://localhost\:8080/payment/servlet/yeepay/response //
// 这个地址是用来接收易宝支付返回结果的路径.这个路径必须外网可以访问.
* */
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);
//1.注意这里是根据易宝支付的原理来的,商户需要把,一些支付信息,和加密后的支付信息,同时发给易宝支付
//易宝支付收到信息后,利用密钥进行对信息再次加密,拿自己加密后的信息和商户发过来的加密信息
//进行对比,对比结果为一致的时候,那么说明信息没有被串改过.
// 得到加密后的字串.
request.setAttribute("messageType", messageType);//支付命令
request.setAttribute("merchantID", p1_MerId);//商户id
request.setAttribute("orderId", orderid);//订单号
request.setAttribute("amount", amount);//订单金额
request.setAttribute("currency", currency);//货币类型
request.setAttribute("productId", productId);//产品id
request.setAttribute("productCat", productCat);//产品种类
request.setAttribute("productDesc", productDesc);//商品描述
request.setAttribute("merchantCallbackURL", merchantCallbackURL);//返回的URL
request.setAttribute("addressFlag", addressFlag);//收货地址
request.setAttribute("sMctProperties", sMctProperties);//商家扩展信息
request.setAttribute("frpId", pd_FrpId);//银行id
request.setAttribute("pr_NeedResponse", pr_NeedResponse);//应答机制.
request.setAttribute("hmac", md5hmac);//MD5验证码
// 转发.
request.getRequestDispatcher("/WEB-INF/page/connection.jsp").forward(request, response);
}
}
--------------------------------------------------
c./payment/src/com/credream/servlet/PaymentResutlResponse.java
package com.credream.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 com.credream.utils.ConfigInfo;
import com.credream.utils.PanymentUtil;
/**
* 响应银行支付结果请求
* @author 创梦网络
*/
public class PaymentResutlResponse extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 这里调用doPost方法
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String merchantID = ConfigInfo.getValue("p1_MerId"); // 商家ID
String keyValue = ConfigInfo.getValue("keyValue"); // 商家密钥
//取得易宝支付返回的元数据
String sCmd = request.getParameter("r0_Cmd"); //业务类型
String sResultCode = request.getParameter("r1_Code"); //扣款结果,该字段值为1时表示扣款成功.
String sTrxId = request.getParameter("r2_TrxId"); //YeePay易宝交易订单号
String amount = request.getParameter("r3_Amt");//扣款金额,交易结束后,YeePay易宝交易系统将实际扣款金额返回给商户
String currency = request.getParameter("r4_Cur");//交易币种,人民币为CNY
String productId = request.getParameter("r5_Pid");//商品ID
String orderId = request.getParameter("r6_Order");//商户订单号
String userId = request.getParameter("r7_Uid");//YeePay易宝会员ID
String mp = request.getParameter("r8_MP");//商户扩展信息,可以任意填写1K 的字符串,交易返回时将原样返回
String bType = request.getParameter("r9_BType");//交易结果通知类型,1: 交易成功回调(浏览器重定向)2: 交易成功主动通知(服
务器点对点通讯)
String rb_BankId = request.getParameter("rb_BankId");//支付银行
String rp_PayDate = request.getParameter("rp_PayDate");//在银行支付时的时间
//取得md5加密后的字串.
String hmac = request.getParameter("hmac");//MD5交易签名
// 验证返回的数据是否合法
boolean result = PanymentUtil.verifyCallback(hmac, merchantID, sCmd, sResultCode, sTrxId, amount,
currency, productId, orderId, userId, mp, bType, keyValue);
if(result){
if("1".equals(sResultCode)){//如果为1说明,这时候易宝支付扣款成功.
//你们这个地方应该把数据库中订单的支付状态设置成已经支付.
//2.这里一定要注意:如果是游戏点卡的话
// 那么这里只能增加一次,否则用户没刷新一次
// 浏览器点卡就会增加一部分,这样是很危险的.
String message = "订单号为:"+ orderId+ "的订单支付成功了";
message += ",用户支付了"+ amount +"元";
message +=",交易结果通知类型:";
if("1".equals(bType)){// 这里有两种类型,1为引导用户的浏览器重定向到url地址,
//这个是我们提供的.但是如果用户的网络有问题,或者
//浏览器有问题就会中途断掉,这种类型是不可靠的
message += "浏览器重定向";
}else if("2".equals(bType)){//易宝支付主动的通知我们的地址,这两种类型是并行的.
message += "易宝支付网关后台程序通知";
}
message += ",易宝订单系统中的订单号为:"+ sTrxId;
request.setAttribute("message", message);
}else{
request.setAttribute("message", "用户支付失败");
}
}else{
request.setAttribute("message", "数据来源不合法");
}
request.getRequestDispatcher("/WEB-INF/page/paymentResult.jsp").forward(request, response);
}
}
-----------------------------------------------------------------------
d./payment/src/com/credream/utils/ConfigInfo.java
package com.credream.utils;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* 读取配置文件
*
*/
public class ConfigInfo {
// 读取配置文件.
// private static ResourceBundle cacheBundle =null;
private static Properties cache = new Properties();
static{
try {
//cacheBundle = ResourceBundle.getBundle("");
cache.load(ConfigInfo.class.getClassLoader().getResourceAsStream("merchantInfo.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取指定key的值
* @param key
* @return
*/
public static String getValue(String key){
return cache.getProperty(key);
}
}
--------------------------------------------------------------
e./payment/src/com/credream/utils/DigestUtil.java
package com.credream.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class DigestUtil {
private static String encodingCharset = "UTF-8";
/**
* @param aValue
* @param aKey
* @return
*/
public static String hmacSign(String aValue, String aKey) {
byte k_ipad[] = new byte[64];
byte k_opad[] = new byte[64];
byte keyb[];
byte value[];
try {
keyb = aKey.getBytes(encodingCharset);
value = aValue.getBytes(encodingCharset);
} catch (UnsupportedEncodingException e) {
keyb = aKey.getBytes();
value = aValue.getBytes();
}
Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
for (int i = 0; i < keyb.length; i++) {
k_ipad[i] = (byte) (keyb[i] ^ 0x36);
k_opad[i] = (byte) (keyb[i] ^ 0x5c);
}
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
return null;
}
md.update(k_ipad);
md.update(value);
byte dg[] = md.digest();
md.reset();
md.update(k_opad);
md.update(dg, 0, 16);
dg = md.digest();
return toHex(dg);
}
public static String toHex(byte input[]) {
if (input == null)
return null;
StringBuffer output = new StringBuffer(input.length * 2);
for (int i = 0; i < input.length; i++) {
int current = input[i] & 0xff;
if (current < 16)
output.append("0");
output.append(Integer.toString(current, 16));
}
return output.toString();
}
/**
*
* @param args
* @param key
* @return
*/
public static String getHmac(String[] args, String key) {
if (args == null || args.length == 0) {
return (null);
}
StringBuffer str = new StringBuffer();
for (int i = 0; i < args.length; i++) {
str.append(args[i]);
}
return (hmacSign(str.toString(), key));
}
/**
* @param aValue
* @return
*/
public static String digest(String aValue) {
aValue = aValue.trim();
byte value[];
try {
value = aValue.getBytes(encodingCharset);
} catch (UnsupportedEncodingException e) {
value = aValue.getBytes();
}
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
return toHex(md.digest(value));
}
// public static void main(String[] args) {
// System.out.println(hmacSign("AnnulCard1000043252120080620160450.0http://localhost/SZXpro/callback.asp这
4564868265473632445648682654736324511","8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t"));
// }
}
----------------------------------------------------------------
f./payment/src/com/credream/utils/PanymentUtil.java
package com.credream.utils;
public class PanymentUtil {
/**
* 生成hmac方法
*
* @param p0_Cmd 业务类型
* @param p1_MerId 商户编号
* @param p2_Order 商户订单号
* @param p3_Amt 支付金额
* @param p4_Cur 交易币种
* @param p5_Pid 商品名称
* @param p6_Pcat 商品种类
* @param p7_Pdesc 商品描述
* @param p8_Url 商户接收支付成功数据的地址
* @param p9_SAF 送货地址
* @param pa_MP 商户扩展信息
* @param pd_FrpId 银行编码
* @param pr_NeedResponse 应答机制
* @param keyValue 商户密钥
* @return
*/
public static String buildHmac(String p0_Cmd,String p1_MerId,
String p2_Order, String p3_Amt, String p4_Cur,String p5_Pid, String p6_Pcat,
String p7_Pdesc,String p8_Url, String p9_SAF,String pa_MP,String pd_FrpId,
String pr_NeedResponse,String keyValue) {
StringBuffer sValue = new StringBuffer();
// 业务类型
sValue.append(p0_Cmd);
// 商户编号
sValue.append(p1_MerId);
// 商户订单号
sValue.append(p2_Order);
// 支付金额
sValue.append(p3_Amt);
// 交易币种
sValue.append(p4_Cur);
// 商品名称
sValue.append(p5_Pid);
// 商品种类
sValue.append(p6_Pcat);
// 商品描述
sValue.append(p7_Pdesc);
// 商户接收支付成功数据的地址
sValue.append(p8_Url);
// 送货地址
sValue.append(p9_SAF);
// 商户扩展信息
sValue.append(pa_MP);
// 银行编码
sValue.append(pd_FrpId);
// 应答机制
sValue.append(pr_NeedResponse);
String sNewString = DigestUtil.hmacSign(sValue.toString(), keyValue);
return sNewString;
}
/**
* 返回校验hmac方法
*
* @param hmac 支付网关发来的加密验证码
* @param p1_MerId 商户编号
* @param r0_Cmd 业务类型
* @param r1_Code 支付结果
* @param r2_TrxId 易宝支付交易流水号
* @param r3_Amt 支付金额
* @param r4_Cur 交易币种
* @param r5_Pid 商品名称
* @param r6_Order 商户订单号
* @param r7_Uid 易宝支付会员ID
* @param r8_MP 商户扩展信息
* @param r9_BType 交易结果返回类型
* @param keyValue 密钥
* @return
*/
public static boolean verifyCallback(String hmac, String p1_MerId,
String r0_Cmd, String r1_Code, String r2_TrxId, String r3_Amt,
String r4_Cur, String r5_Pid, String r6_Order, String r7_Uid,
String r8_MP, String r9_BType, String keyValue) {
StringBuffer sValue = new StringBuffer();
// 商户编号
sValue.append(p1_MerId);
// 业务类型
sValue.append(r0_Cmd);
// 支付结果
sValue.append(r1_Code);
// 易宝支付交易流水号
sValue.append(r2_TrxId);
// 支付金额
sValue.append(r3_Amt);
// 交易币种
sValue.append(r4_Cur);
// 商品名称
sValue.append(r5_Pid);
// 商户订单号
sValue.append(r6_Order);
// 易宝支付会员ID
sValue.append(r7_Uid);
// 商户扩展信息
sValue.append(r8_MP);
// 交易结果返回类型
sValue.append(r9_BType);
String sNewString = DigestUtil.hmacSign(sValue.toString(), keyValue);
// 这里进行了验证,如果自己加密后的数据,跟易宝支付加密后的数据一样的话,那么验证成功.
if (hmac.equals(sNewString)) {
return true;
}
return false;
}
}
-------------------------------------------------------------------
g./payment/src/merchantInfo.properties
p1_MerId=10000326625
keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek
merchantCallbackURL=http\://172.16.53.1\:8080/payment/servlet/yeepay/response
-----------------------------------------------
h.commons-digester-1.8.jar
YeePay_HTMLcommon_V3.0.jar
commons-logging-1.0.4.jar
javax.servlet.jar
log4j-1.2.9.jar
--------------------------------------
i./payment/WebRoot/WEB-INF/page/connection.jsp
<%@ 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 -->
<!--
http://tech.yeepay.com:8080/robot/debug.action
这个是易宝支付的调试路径.在做好之前最好用调试网关.
https://www.yeepay.com/app-merchant-proxy/node
这个是易宝支付商用的网关.
--><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">
<!--MD5-hmac验证码-->
<input type='hidden' name='hmac' value="${hmac}"><!-- MD5-hmac验证码 -->
</form>
</body>
</html>
----------------------------------------------------
j./payment/WebRoot/WEB-INF/page/paymentResult.jsp
<%@ 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 >
<center><h3><font color="red">
${message }
</font></h3></center>
</body>
</html>
-----------------------------------------------------------------------
k./payment/WebRoot/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>发起支付请求</description>
<display-name>发起支付请求</display-name>
<servlet-name>PaymentRequest</servlet-name>
<servlet-class>com.credream.servlet.PaymentRequest</servlet-class>
</servlet>
<servlet>
<description>响应支付结果请求</description>
<display-name>响应支付结果请求</display-name>
<servlet-name>PaymentResutlResponse</servlet-name>
<servlet-class>cn.itcast.servlet.PaymentResutlResponse</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PaymentRequest</servlet-name>
<url-pattern>/servlet/yeepay/paymentRequest</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PaymentResutlResponse</servlet-name>
<url-pattern>/servlet/yeepay/response</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
----------------------------------------------------------------
l./payment/WebRoot/index.jsp
<%@ 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/paymentRequest" 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">
<tr>
<td bgcolor="#F4F8FF"><span class="STYLE5"> 温馨提示</span></td>
</tr>
<tr>
<td><ul><li> 建行客户需到柜面签约网上银行才能支付</li>
<li>请关闭弹出窗口拦截功能</li>
<li>务必使用IE5.0以上浏览器</li>
<li>支付出错时勿按IE“后退”键</li>
</ul></td>
</tr>
</table>
</div>
<div id="blankmessage"></div> </td>
</tr>
<tr>
<td> </td>
<td width="290"> </td>
<td width="120"> </td>
</tr>
</table>
</body>
</html>
-----------------------------------------------------------------