.Java在线支付所有源码
------------------------------------------------------------------------------------------------
a.新建工程payment
------------------------------------------------------------------------------------------------
b.payment/src/com/credream/servlet/PaymentRequest.java
[java] view plain copy
1. package com.credream.servlet;
2. import java.io.IOException;
3. import javax.servlet.ServletException;
4. import javax.servlet.http.HttpServlet;
5. import javax.servlet.http.HttpServletRequest;
6. import javax.servlet.http.HttpServletResponse;
7. import cn.itcast.utils.ConfigInfo;
8. import cn.itcast.utils.PaymentUtil;
9. /**
10. * 发起支付请求
11. *
12. */
13. public class PaymentRequest extends HttpServlet {
14. public void doGet(HttpServletRequest request, HttpServletResponse response)
15. throws ServletException, IOException {
16. this.doPost(request, response);
17. }
18. public void doPost(HttpServletRequest request, HttpServletResponse response)
19. throws ServletException, IOException {
20. /*
21. * p1_MerId=10000326625// 商家的id
22. keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek //密钥
23. merchantCallbackURL=http\://localhost\:8080/payment/servlet/yeepay/response //
24. 这个地址是用来接收易宝支付返回结果的路径.这个路径必须外网可以访问.
25. */
26. "GBK");
27. "merchantID");
28. "keyValue");
29. "merchantCallbackURL");
30.
31. "orderid");//订单号
32. "amount");//支付金额
33. "pd_FrpId");//银行支付方式
34.
35. "Buy";//请求命令,在线支付固定为buy
36. "CNY";//货币单位
37. "";//商品描述
38. "";//商品种类
39. "";//商品ID
40. "0";//需要填写送货信息0:不需要,1:需要
41. "";//商品扩展信息
42. "0";//应答机制
43.
44. //下面这个方法用来进行加密,通过调用加密类
45. String md5hmac=PaymentUtil.buildHmac(messageType, merchantID, orderid, amount, currency, productId,
46. productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties, pd_FrpId,
47. "0", keyValue);
48. "messageType", messageType);
49. "merchantID", merchantID);
50. "orderid", orderid);
51. "amount", amount);
52. "currency", currency);
53. "productId", productId);
54. "productCat", productCat);
55. "productDesc", productDesc);
56. "merchantCallbackURL", merchantCallbackURL);
57. "addressFlag", addressFlag);
58. "sMctProperties", sMctProperties);
59. "pd_FrpId", pd_FrpId);
60. "pr_NeedResponse", pr_NeedResponse);
61. "hmac", md5hmac);
62.
63. "/WEB-INF/page/connection.jsp").forward(request, response);
64. }
65. }
c.payment/src/com/credream/servlet/PaymentResutlResponse.java
[java] view plain copy
1. package com.credream.servlet;
2. import java.io.IOException;
3. import javax.servlet.ServletException;
4. import javax.servlet.http.HttpServlet;
5. import javax.servlet.http.HttpServletRequest;
6. import javax.servlet.http.HttpServletResponse;
7. import cn.itcast.utils.ConfigInfo;
8. import cn.itcast.utils.PaymentUtil;
9. /**
10. *
11. * 响应银行支付结果请求
12. *
13. * **/
14. public class PaymentResutlResponse extends HttpServlet {
15.
16. public void doGet(HttpServletRequest request, HttpServletResponse response)
17. throws ServletException, IOException {
18. this.doPost(request, response);
19. }
20.
21. public void doPost(HttpServletRequest request, HttpServletResponse response)
22. throws ServletException, IOException {
23. "GBK");
24. "p1_MerId");//商家ID
25. "keyValue");//商家密钥
26. //取得易宝支付返回的元数据
27. "r0_Cmd");//业务类型
28. "r1_Code");//扣款结果,该字段值为1时,表示扣款成功。
29. "r2_TrxId");//yeepay易宝交易订单号
30. "r3_Amt");//扣款金额,交易结束后,yeepay易宝交易系统将实际金额返回给客户
31. "r4_Cur");//交易币种,人民币为CNY
32. "r5_Pid");//商品ID
33. "r6_Order");//商户订单号
34. "r7_Uid");//yeepay易宝会员ID
35. "r8_Mp");//商户扩展信息
36. "r9_BType");//交易结果通知类型,1: 交易成功回调(浏览器重定向)2: 交易成功主动通知(服务器点对点通讯)
37.
38. "rb_BankId");//支付银行
39. "rp_PayDate");//在线支付的时间
40. //取得md5加密后的字串.
41. "hmac");//MD5交易签名
42. // 验证返回的数据是否合法
43. boolean result=PaymentUtil.verifyCallback(hmac, merchantID, sCmd, sResultCode, sTrxId, amount,
44. currency, productId,orderId, userId, mp, bType, keyValue);
45.
46. if(result){
47. if("1".equals(sResultCode)) {//如果为1说明,这时候易宝支付扣款成功.
48. //你们这个地方应该把数据库中订单的支付状态设置成已经支付.
49. //2.这里一定要注意:如果是游戏点卡的话
50. // 那么这里只能增加一次,否则用户每刷新一次
51. // 浏览器点卡就会增加一部分,这样是很危险的
52.
53. "订单号为"+orderId+"的订单支付成功了";
54. ",用户支付了"+amount+"元";
55. ",交易结果通知类型";
56. if("1".equals(bType)){
57. "浏览器重定向";
58. else if("2".equals(bType)){
59. "易宝支付网关后台程序通知";
60. }
61. ",易宝订单系统中的订单号为:"+ sTrxId;
62. "message", message);
63. else{
64. "message", "用户支付失败");
65. }
66. else{
67. "message", "数据来源不合法");
68. }
69. "/WEB-INF/page/paymentResult.jsp").forward(request, response);
70. }
71. }
d.payment/src/com/credream/utils/ConfigInfo.java
[java] view plain copy
1. package com.credream.utils;
2. import java.util.ResourceBundle;
3. //读取配置文件
4. public class ConfigInfo {
5. private static ResourceBundle cache=null;
6. static{
7. "merchantInfo");
8. }
9. //获取指定key的值
10. public static String getValue(String key){
11. return cache.getString(key);
12. }
13. }
e.payment/src/com/credream/utils/DigestUtil.java
[java] view plain copy
1. package com.credream.utils;
2. import java.io.UnsupportedEncodingException;
3. import java.security.MessageDigest;
4. import java.security.NoSuchAlgorithmException;
5. import java.util.Arrays;
6.
7. public class DigestUtil {
8.
9. private static String encodingCharset = "UTF-8";
10.
11. /**
12. * @param aValue
13. * @param aKey
14. * @return
15. */
16. public static String hmacSign(String aValue, String aKey) {
17. byte k_ipad[] = new byte[64];
18. byte k_opad[] = new byte[64];
19. byte keyb[];
20. byte value[];
21. try {
22. keyb = aKey.getBytes(encodingCharset);
23. value = aValue.getBytes(encodingCharset);
24. catch (UnsupportedEncodingException e) {
25. keyb = aKey.getBytes();
26. value = aValue.getBytes();
27. }
28. 64, (byte) 54);
29. 64, (byte) 92);
30. for (int i = 0; i < keyb.length; i++) {
31. byte) (keyb[i] ^ 0x36);
32. byte) (keyb[i] ^ 0x5c);
33. }
34. null;
35. try {
36. "MD5");
37. catch (NoSuchAlgorithmException e) {
38.
39. return null;
40. }
41. md.update(k_ipad);
42. md.update(value);
43. byte dg[] = md.digest();
44. md.reset();
45. md.update(k_opad);
46. 0, 16);
47. dg = md.digest();
48. return toHex(dg);
49. }
50. public static String toHex(byte input[]) {
51. if (input == null)
52. return null;
53. new StringBuffer(input.length * 2);
54. for (int i = 0; i < input.length; i++) {
55. int current = input[i] & 0xff;
56. if (current < 16)
57. "0");
58. 16));
59. }
60. return output.toString();
61. }
62. /**
63. *
64. * @param args
65. * @param key
66. * @return
67. */
68. public static String getHmac(String[] args, String key) {
69. if (args == null || args.length == 0) {
70. return (null);
71. }
72. new StringBuffer();
73. for (int i = 0; i < args.length; i++) {
74. str.append(args[i]);
75. }
76. return (hmacSign(str.toString(), key));
77. }
78. /**
79. * @param aValue
80. * @return
81. */
82. public static String digest(String aValue) {
83. aValue = aValue.trim();
84. byte value[];
85. try {
86. value = aValue.getBytes(encodingCharset);
87. catch (UnsupportedEncodingException e) {
88. value = aValue.getBytes();
89. }
90. null;
91. try {
92. "SHA");
93. catch (NoSuchAlgorithmException e) {
94. e.printStackTrace();
95. return null;
96. }
97. return toHex(md.digest(value));
98. }
99. // public static void main(String[] args) {
100. // System.out.println(hmacSign("AnnulCard1000043252120080620160450.0http://localhost/SZXpro/callback.asp这
101. //4564868265473632445648682654736324511","8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t"));
102. // }
103. }
f、payment/src/com/credream/utils/PanymentUtil.java
1. package com.credream.utils;
2.
3. public class PanymentUtil {
4. /**
5. * 生成hmac方法
6. *
7. * @param p0_Cmd 业务类型
8. * @param p1_MerId 商户编号
9. * @param p2_Order 商户订单号
10. * @param p3_Amt 支付金额
11. * @param p4_Cur 交易币种
12. * @param p5_Pid 商品名称
13. * @param p6_Pcat 商品种类
14. * @param p7_Pdesc 商品描述
15. * @param p8_Url 商户接收支付成功数据的地址
16. * @param p9_SAF 送货地址
17. * @param pa_MP 商户扩展信息
18. * @param pd_FrpId 银行编码
19. * @param pr_NeedResponse 应答机制
20. * @param keyValue 商户密钥
21. * @return
22. */
23. public static String buildHmac(String p0_Cmd,String p1_MerId,
24. String p2_Order, String p3_Amt, String p4_Cur,String p5_Pid, String p6_Pcat,
25. String p7_Pdesc,String p8_Url, String p9_SAF,String pa_MP,String pd_FrpId,
26. String pr_NeedResponse,String keyValue) {
27. new StringBuffer();
28. // 业务类型
29. sValue.append(p0_Cmd);
30. // 商户编号
31. sValue.append(p1_MerId);
32. // 商户订单号
33. sValue.append(p2_Order);
34. // 支付金额
35. sValue.append(p3_Amt);
36. // 交易币种
37. sValue.append(p4_Cur);
38. // 商品名称
39. sValue.append(p5_Pid);
40. // 商品种类
41. sValue.append(p6_Pcat);
42. // 商品描述
43. sValue.append(p7_Pdesc);
44. // 商户接收支付成功数据的地址
45. sValue.append(p8_Url);
46. // 送货地址
47. sValue.append(p9_SAF);
48. // 商户扩展信息
49. sValue.append(pa_MP);
50. // 银行编码
51. sValue.append(pd_FrpId);
52. // 应答机制
53. sValue.append(pr_NeedResponse);
54.
55. String sNewString = DigestUtil.hmacSign(sValue.toString(), keyValue);
56. return sNewString;
57. }
58.
59. /**
60. * 返回校验hmac方法
61. *
62. * @param hmac 支付网关发来的加密验证码
63. * @param p1_MerId 商户编号
64. * @param r0_Cmd 业务类型
65. * @param r1_Code 支付结果
66. * @param r2_TrxId 易宝支付交易流水号
67. * @param r3_Amt 支付金额
68. * @param r4_Cur 交易币种
69. * @param r5_Pid 商品名称
70. * @param r6_Order 商户订单号
71. * @param r7_Uid 易宝支付会员ID
72. * @param r8_MP 商户扩展信息
73. * @param r9_BType 交易结果返回类型
74. * @param keyValue 密钥
75. * @return
76. */
77. public static boolean verifyCallback(String hmac, String p1_MerId,
78. String r0_Cmd, String r1_Code, String r2_TrxId, String r3_Amt,
79. String r4_Cur, String r5_Pid, String r6_Order, String r7_Uid,
80. String r8_MP, String r9_BType, String keyValue) {
81. new StringBuffer();
82. // 商户编号
83. sValue.append(p1_MerId);
84. // 业务类型
85. sValue.append(r0_Cmd);
86. // 支付结果
87. sValue.append(r1_Code);
88. // 易宝支付交易流水号
89. sValue.append(r2_TrxId);
90. // 支付金额
91. sValue.append(r3_Amt);
92. // 交易币种
93. sValue.append(r4_Cur);
94. // 商品名称
95. sValue.append(r5_Pid);
96. // 商户订单号
97. sValue.append(r6_Order);
98. // 易宝支付会员ID
99. sValue.append(r7_Uid);
100. // 商户扩展信息
101. sValue.append(r8_MP);
102. // 交易结果返回类型
103. sValue.append(r9_BType);
104. String sNewString = DigestUtil.hmacSign(sValue.toString(), keyValue);
105.
106. if (hmac.equals(sNewString)) {
107. return true;
108. }
109. return false;
110. }
111. }
g.
payment/src/merchantInfo.properties
[html] view plain copy
- p1_MerId=10000326625
- keyValue=0acqgug6x57m0wrsiod6clpn1ezh47r2ot5h1zkq5dztiic8y5xkm5g0p0ek
- merchantCallbackURL=http\://localhost\:8080/payment/servlet/yeepay/response
- 第一个是商户的id,第二个是易宝给商户的密钥,第三个是接收易宝支付返回的支付结果的地址.
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
1. <%@ page language="java" pageEncoding="GBK"%>
2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3. <html>
4. <head>
5. <title>发送支付请求</title>
6. "pragma" content="no-cache">
7. "cache-control" content="no-cache">
8. "expires" content="0">
9. "keywords" content="keyword1,keyword2,keyword3">
10. "description" content="This is my page">
11. </head>
12.
13. "JavaScript:document.forms[0].submit()">
14. //tech.yeepay.com:8080/robot/debug.action
15. 这个是易宝支付的调试路径,在做好之前最好用调试网关 -->
16. "yeepay" action=" https://www.yeepay.com/app-merchant-proxy/node" method="post">
17.
18. "hidden" name="p0_Cmd" value="${messageType}"> <!-- 请求命令,在线支付固定为Buy -->
19. "hidden" name="p1_Merid" value="${merchantID}"> <!-- 商家ID -->
20. "hidden" name="p2_Order" value="${orderId}"> <!-- 商家交易订单号 -->
21. "hidden" name="p3_Amt" value="${amount}"> <!-- 订单金额 -->
22. "hidden" name="p4_Cur" value="${currency}"> <!-- 货币单位 -->
23. "hidden" name="p5_Pid" value="${productId}"> <!-- 商品ID -->
24. "hidden" name="p6_Pcat" value="${productCat}"> <!-- 商品种类 -->
25. "hidden" name="p7_Pdesc" value="${productDesc}"> <!-- 商品描述 -->
26. "hidden" name="p8-Url" value="${merchantCallbackURL}"> <!-- 交易结果通知地址 -->
27. "hidden" name="p9_SAF" value="${addressFlag}"> <!-- 需要填写送货地址0:不需要 1:需要 -->
28. "hidden" name="pa_MP" value="${sMctProperties}"> <!-- 商家扩展信息 -->
29. "hidden" name="pd_Frpid" value="${frpid}"> <!-- 银行ID -->
30. "hidden" name="pr_NeedResponse" value="0"> <!-- 应答机制为"1":需要应答机制 "0":不需要应答机制 -->
31. <!-- MD5-hmac验证码 -->
32. "hidden" name="hmac" value="${hmac}">
33. "submit" value="发 送"> -->
34. </form>
35. </body>
36. </html>
-------------------------------------------------------------------------------------------------------
j.payment/WebRoot/WEB-INF/page/paymentResult.jsp
1. <%@ page language="java" pageEncoding="GBK"%>
2.
3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4. <html>
5. <head>
6. <title>支付结果</title>
7. "pragma" content="no-cache">
8. "cache-control" content="no-cache">
9. "expires" content="0">
10. "keywords" content="keyword1,keyword2,keyword3">
11. "description" content="This is my page">
12. </head>
13. <body>
14. <center>
15. <h3>
16. "red">
17. ${message }
18. </font>
19. </h3>
20. </center>
21. </body>
22. </html>
k./payment/WebRoot/WEB-INF/web.xml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.4"
3. xmlns="http://java.sun.com/xml/ns/j2ee"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6. >
7. <servlet>
8. <description>发起支付请求</description>
9. <display-name>发起支付请求</display-name>
10. <servlet-name>PaymentRequest</servlet-name>
11. <servlet-class>com.credream.servlet.PaymentRequest</servlet-class>
12. </servlet>
13. <servlet>
14. <description>响应支付结果请求</description>
15. <display-name>响应支付结果请求</display-name>
16. <servlet-name>PaymentResutlResponse</servlet-name>
17. <servlet-class>cn.itcast.servlet.PaymentResutlResponse</servlet-class>
18. </servlet>
19. <servlet-mapping>
20. <servlet-name>PaymentRequest</servlet-name>
21. <url-pattern>/servlet/yeepay/paymentRequest</url-pattern>
22. </servlet-mapping>
23. <servlet-mapping>
24. <servlet-name>PaymentResutlResponse</servlet-name>
25. <url-pattern>/servlet/yeepay/response</url-pattern>
26. </servlet-mapping>
27. <welcome-file-list>
28. <welcome-file>index.jsp</welcome-file>
29. </welcome-file-list>
30. </web-app>
l./payment/WebRoot/index.jsp
1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3. <html>
4. <head>
5. <title>巴巴运动网_支付第一步,选择支付银行</title>
6. <meta http-equiv="pragma" content="no-cache">
7. <meta http-equiv="cache-control" content="no-cache">
8. <meta http-equiv="expires" content="0">
9. </head>
10. <body>
11. <table width="960" border="0" align="center">
12. <tr>
13. <td width="536" valign="top">
14. <form action="${pageContext.request.contextPath}/servlet/yeepay/paymentRequest" method="post" name="paymentform">
15. <table width="100%" border="0">
16. <tr>
17. <td height="30" colspan="4"><table width="100%" height="50" border="0" cellpadding="0" cellspacing="1" bgcolor="#A2E0FF">
18. <tr>
19. <td align="center" bgcolor="#F7FEFF"><h3>订单号:<INPUT TYPE="text" NAME="orderid"> 应付金额:¥<INPUT TYPE="text"
20. NAME="amount" size="6">元</h3></td>
21. </tr>
22. </table></td>
23. </tr>
24. <tr>
25. <td colspan="4"> </td>
26. </tr>
27. <tr>
28. <td height="30" colspan="4" bgcolor="#F4F8FF"><span class="STYLE3">请您选择在线支付银行</span> </td>
29. </tr>
30. <tr>
31. <td width="26%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CMBCHINA-NET">招商银行 </td>
32. <td width="25%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="ICBC-NET">工商银行</td>
33. <td width="25%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="ABC-NET">农业银行</td>
34. <td width="24%" height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CCB-NET">建设银行 </td>
35. </tr>
36. <tr>
37. <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CMBC-NET">中国民生银行总行</td>
38. <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CEB-NET" >光大银行 </td>
39. <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="BOCO-NET">交通银行</td>
40. <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="SDB-NET">深圳发展银行</td>
41. </tr>
42. <tr>
43. <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="BCCB-NET">北京银行</td>
44. <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="CIB-NET">兴业银行 </td>
45. <td height="25"><INPUT TYPE="radio" NAME="pd_FrpId" value="SPDB-NET">上海浦东发展银行 </td>
46. <td ><INPUT TYPE="radio" NAME="pd_FrpId" value="ECITIC-NET">中信银行</td>
47. </tr>
48. <tr>
49. <td colspan="4"> </td>
50. </tr>
51. <tr>
52. <td colspan="4" align="center"><input type="submit" value=" 确认支付 " /></td>
53. </tr>
54. </table>
55. </form> </td>
56. <td colspan="2" valign="top"><div class="divts"><table width="400" border="0" align="center" cellpadding="5" cellspacing="0">
57. <tr>
58. <td bgcolor="#F4F8FF"><span class="STYLE5"> 温馨提示</span></td>
59. </tr>
60. <tr>
61. <td><ul><li> 建行客户需到柜面签约网上银行才能支付</li>
62. <li>请关闭弹出窗口拦截功能</li>
63. <li>务必使用IE5.0以上浏览器</li>
64. <li>支付出错时勿按IE“后退”键</li>
65. </ul></td>
66. </tr>
67. </table>
68. </div>
69. <div id="blankmessage"></div> </td>
70. </tr>
71. <tr>
72. <td> </td>
73. <td width="290"> </td>
74. <td width="120"> </td>
75. </tr>
76. </table>
77. </body>
78. </html>