生活随笔
收集整理的這篇文章主要介紹了
网上支付心案例payment
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?案例的作用及用法參考該類的說明注釋??工具類分別有:??package?cn.itcast.utils;????import?java.util.Properties;???????public?class?ConfigInfo?{??????private?static?Properties?cache?=?new?Properties();??????static{??????????try?{??????????????cache.load(ConfigInfo.class.getClassLoader().getResourceAsStream("merchantInfo.properties"));??????????}?catch?(Exception?e)?{??????????????e.printStackTrace();??????????}??????}????????????????public?static?String?getValue(String?key){??????????return?cache.getProperty(key);??????}??}????package?cn.itcast.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";????????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();??????}???????????????????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));??????}?????????????????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));????????}????????}??????package?cn.itcast.utils;????public?class?PanymentUtil?{??????????????????????????????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;??????}???????????????????????????????????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);????????????????????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;??????}??}??????Servlet相關的類??package?cn.itcast.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.itcast.utils.ConfigInfo;??import?cn.itcast.utils.PanymentUtil;????????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?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";???????????String?currency?=?"CNY";???????????String?productDesc?=?"";???????????String?productCat?=?"";???????????String?productId?=?"";???????????String?addressFlag?=?"0";???????????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);??????}????}??????package?cn.itcast.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.itcast.utils.ConfigInfo;??import?cn.itcast.utils.PanymentUtil;????????public?class?PaymentResutlResponse?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?merchantID?=?ConfigInfo.getValue("p1_MerId");???????????String?keyValue?=?ConfigInfo.getValue("keyValue");?????????????????????String?sCmd?=?request.getParameter("r0_Cmd");???????????String?sResultCode?=?request.getParameter("r1_Code");???????????String?sTrxId?=?request.getParameter("r2_TrxId");???????????String?amount?=?request.getParameter("r3_Amt");??????????String?currency?=?request.getParameter("r4_Cur");??????????String?productId?=?request.getParameter("r5_Pid");??????????String?orderId?=?request.getParameter("r6_Order");??????????String?userId?=?request.getParameter("r7_Uid");??????????String?mp??=?request.getParameter("r8_MP");??????????String?bType?=?request.getParameter("r9_BType");??????????String?rb_BankId??=?request.getParameter("rb_BankId");??????????String?rp_PayDate?=?request.getParameter("rp_PayDate");??????????String?hmac?=?request.getParameter("hmac");????????????????????boolean?result?=?PanymentUtil.verifyCallback(hmac,?merchantID,?sCmd,?sResultCode,?sTrxId,?amount,??????????????????currency,?productId,?orderId,?userId,?mp,?bType,?keyValue);??????????if(result){??????????????if("1".equals(sResultCode)){????????????????????????????????????String?message?=?"訂單號為:"+?orderId+?"的訂單支付成功了";??????????????????message?+=?",用戶支付了"+?amount?+"元";??????????????????message?+=",交易結果通知類型:";??????????????????if("1".equals(bType)){???????????????????????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);??????}????}????顯示界面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>????發起支付請求??<%@?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?οnlοad="javascript:document.forms[0].submit()">??????<!--?http:??????<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>????支付結果??<%@?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>?
轉載于:https://www.cnblogs.com/chenchangyan/p/3539667.html
總結
以上是生活随笔為你收集整理的网上支付心案例payment的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。