java實現網上線上支付
阿新 • • 發佈:2019-01-25
.Java線上支付所有原始碼
------------------------------------------------------------------------------------------------
a.新建工程payment
------------------------------------------------------------------------------------------------
b.payment/src/com/credream/servlet/PaymentRequest.java
-
package
- 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.PaymentUtil;
- /**
- * 發起支付請求
- *
- */
- publicclass PaymentRequest extends HttpServlet {
- publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doPost(request, response);
- }
-
publicvoid 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 merchantID=ConfigInfo.getValue("merchantID");
- String keyValue=ConfigInfo.getValue("keyValue");
- String merchantCallbackURL=ConfigInfo.getValue("merchantCallbackURL");
- String orderid=request.getParameter("orderid");//訂單號
- String amount=request.getParameter("amount");//支付金額
- String pd_FrpId=request.getParameter("pd_FrpId");//銀行支付方式
- 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=PaymentUtil.buildHmac(messageType, merchantID, orderid, amount, currency, productId,
- productCat, productDesc, merchantCallbackURL, addressFlag, sMctProperties, pd_FrpId,
- "0", keyValue);
- request.setAttribute("messageType", messageType);
- request.setAttribute("merchantID", merchantID);
- 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("pd_FrpId", pd_FrpId);
- request.setAttribute("pr_NeedResponse", pr_NeedResponse);
- request.setAttribute("hmac", md5hmac);
- request.getRequestDispatcher("/WEB-INF/page/connection.jsp").forward(request, response);
- }
- }
- 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 cn.itcast.utils.ConfigInfo;
- import cn.itcast.utils.PaymentUtil;
- /**
- *
- * 響應銀行支付結果請求
- *
- * **/
- publicclass PaymentResutlResponse extends HttpServlet {
- publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doPost(request, response);
- }
- publicvoid 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");<