網上支付通常有兩種支付方式,一種是直接與銀行對接的支付方式,另外一種與第三方公司對接的方式i,好比易寶等等。第一種通常是現金流量較大的大公司使用。這裏採用的是第二種。node
第一步實現訂單界面算法
<form action="servlet/PaymentSendServlet" method="POST"> <table align="center" width="600" border="6" cellspacing="0" cellpadding="2"> <tr> <td align="center" colspan="4" > <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" >請選擇在線支付銀行</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 colspan="4"> </td> </tr> <tr> <td colspan="4" align="center" ><input type="submit" value=" 確認支付 "/></td> </tr> </table> </form>
第二步將信息轉化爲易寶公司接口所要求的數據格式。app
這裏須要一個商家號和相對應的密鑰。(這裏的商戶號是傳智播客的一位老師的,支付時請注意)jsp
主要代碼:ui
request.setCharacterEncoding("GBK"); //商戶編號 String accountID = "10001126856"; //密鑰 String keyValue = "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl"; //商戶接收支付成功後返回地址 String accountCallbackURL = "http://127.0.0.1:8080/zhifu/PaymentResultServlet"; String orderID = request.getParameter("orderID"); //獲取訂單號 String amount = request.getParameter("amount"); //獲取支付金額 String accountBankID = request.getParameter("pd_FrpId"); //獲取用戶所選擇的銀行 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 = zhifuUtil.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("needResponse", pr_NeedResponse); request.setAttribute("md5hmac", md5hmac);
將數據處理後返回前段jsp中,並以數據隱藏的表單形式將信息提交到易寶接口(注:上述須要易寶的一個加密算法MD5+hmac)加密
<form action="https://www.yeepay.com/app-merchant-proxy/node" method="POST" name="yeepay"> <!-- 如下hidden中的name值爲易寶支付規範的固定命名和順序 --> <input type='hidden' name='p0_Cmd' value="${businessType}"> <input type='hidden' name='p1_MerId' value="${accountID}"> <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}"> <input type='hidden' name='p6_Pcat' value="${productCategory}"> <input type='hidden' name='p7_Pdesc' value="${productDesc}"> <input type='hidden' name='p8_Url' value="${accountCallbackURL}"> <input type='hidden' name='p9_SAF' value="${addressFlag}"> <input type='hidden' name='pa_MP' value="${accountMoreInfo}"> <input type='hidden' name='pd_FrpId' value="${accountBankID}"> <input type="hidden" name='pr_NeedResponse' value="${needResponse}"> <input type='hidden' name='hmac' value="${md5hmac}"> <input type="submit" value="確認支付"> </form>
點擊肯定按鈕提交表單。進入易寶的支付接口界面:spa
最後完成支付後,易寶會返回一組數據,最重要的是扣款結果若返回爲1:扣款成功,不然扣款失敗。咱們對獲得的數據進行判斷便可。code