1. 程式人生 > >java實現網上線上支付

java實現網上線上支付

.Java線上支付所有原始碼

------------------------------------------------------------------------------------------------

a.新建工程payment

------------------------------------------------------------------------------------------------

b.payment/src/com/credream/servlet/PaymentRequest.java

  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. publicclass PaymentRequest extends HttpServlet {    
  14.     publicvoid doGet(HttpServletRequest request, HttpServletResponse response)    
  15.             throws ServletException, IOException {    
  16.         this.doPost(request, response);    
  17.     }    
  18.     publicvoid 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.         request.setCharacterEncoding("GBK");    
  27.         String merchantID=ConfigInfo.getValue("merchantID");    
  28.         String keyValue=ConfigInfo.getValue("keyValue");    
  29.         String merchantCallbackURL=ConfigInfo.getValue("merchantCallbackURL");    
  30.         String orderid=request.getParameter("orderid");//訂單號  
  31.         String amount=request.getParameter("amount");//支付金額  
  32.         String pd_FrpId=request.getParameter("pd_FrpId");//銀行支付方式  
  33.         String messageType="Buy";//請求命令,線上支付固定為buy  
  34.         String currency="CNY";//貨幣單位  
  35.         String productDesc="";//商品描述  
  36.         String productCat="";//商品種類  
  37.         String productId="";//商品ID  
  38.         String addressFlag="0";//需要填寫送貨資訊0:不需要,1:需要  
  39.         String sMctProperties="";//商品擴充套件資訊  
  40.         String pr_NeedResponse="0";//應答機制  
  41.         //下面這個方法用來進行加密,通過呼叫加密類  
  42.         String md5hmac=PaymentUtil.buildHmac(messageType, merchantID, orderid, amount, currency, productId,    
  43.                 productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties, pd_FrpId,    
  44.                 "0", keyValue);    
  45.         request.setAttribute("messageType", messageType);    
  46.         request.setAttribute("merchantID", merchantID);    
  47.         request.setAttribute("orderid", orderid);    
  48.         request.setAttribute("amount", amount);    
  49.         request.setAttribute("currency", currency);    
  50.         request.setAttribute("productId", productId);    
  51.         request.setAttribute("productCat", productCat);    
  52.         request.setAttribute("productDesc", productDesc);    
  53.         request.setAttribute("merchantCallbackURL", merchantCallbackURL);    
  54.         request.setAttribute("addressFlag", addressFlag);    
  55.         request.setAttribute("sMctProperties", sMctProperties);    
  56.         request.setAttribute("pd_FrpId", pd_FrpId);    
  57.         request.setAttribute("pr_NeedResponse", pr_NeedResponse);    
  58.         request.setAttribute("hmac", md5hmac);    
  59.         request.getRequestDispatcher("/WEB-INF/page/connection.jsp").forward(request, response);    
  60.     }    
  61. }    
c.payment/src/com/credream/servlet/PaymentResutlResponse.java
  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. publicclass PaymentResutlResponse extends HttpServlet {  
  15.     publicvoid doGet(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.         this.doPost(request, response);  
  18.     }  
  19.     publicvoid doPost(HttpServletRequest request, HttpServletResponse response)  
  20.             throws ServletException, IOException {  
  21.         request.setCharacterEncoding("GBK");  
  22.         String merchantID=ConfigInfo.getValue("p1_MerId");//商家ID
  23.         String keyValue=ConfigInfo.getValue("keyValue");//商家金鑰
  24.         //取得易寶支付返回的元資料
  25.         String sCmd=request.getParameter("r0_Cmd");//業務型別
  26.         String sResultCode=request.getParameter("r1_Code");<