1. 程式人生 > >java微信支付接入詳細流程

java微信支付接入詳細流程

背景

由於專案是採用Java編寫的,微信包括微信支付大都是PHP相關,於是微信支付官方文件對java的支援就不是很友好,在網上找了很多文章,基本上沒有一篇是真正跑的通的,經過一番整理,先將java接入微信支付詳細流程總結出來以便後續使用。

步驟一

準備階段:已認證微訊號,且通過微信支付認證,這個可以看微信文件,很詳細,這裡就不再重複。

步驟二

配置授權目錄,官方推薦使用https型別的url,不知道http能不能行,個人也推薦使用https的保證不會錯。

 

配置授權域名

 


 


步驟三

微信支付二次開發所需要的引數:

APP_ID,APP_KEY,PARTNER,PARTNER_KEY(AppSecret)

 

APP_IDPARTNER_KEY(AppSecret)

 

PARTNER

 

APP_KEY(自行設定32位字元)

步驟四

4.1通過頁面跳轉到確認支付頁面,其中的redirect_uri必須是配置授權目錄下的

  1. <html>
  2.   <head>
  3.     <title>支付測試</title>
  4.   </head>
  5.   <body>
  6.     <ahref="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx619890d997071358&redirect_uri=https%3A%2F%2Fm.yangji.com%2Fpay%2FpayTest.jsp&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
    >支付測試</a>
  7.   </body>
  8. </html>

4.2 獲取到openid,再經伺服器向微信請求獲取prepay_id,封裝欄位並進行簽名後通過jsapi調起微信支付

網頁端

  1. <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <metacharset="utf-8"/>
  6. <metaname="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
  7. <metaname="apple-mobile-web-app-capable"content="yes"/>
  8. <metaname="apple-mobile-web-app-status-bar-style"content="black"/>
  9. <metaname="format-detection"content="telephone=no"/>
  10. <title>測試支付</title>
  11. <linkhref="../css/css.css?v=1.0"rel="stylesheet"type="text/css">
  12. </head>
  13. <body>
  14.     <divclass="index_box">
  15.         <divclass="apply_name">微信js支付測試</div>
  16.         <divclass="branch_con">
  17.             <ul>
  18.                 <li><spanclass="name">測試支付資訊[]</span></li>
  19.             </ul>
  20.             <pclass="cz_btn"><ahref="javascript:pay();"class="btn_1">立即支付</a></p>
  21.         </div>
  22.     </div>
  23. <%  
  24. String code = request.getParameter("code");  
  25.  %>
  26. <inputtype="text"id="code"value="<%out.print(code); %>"/>
  27. <inputtype="text"id="path"value="${pageContext.request.contextPath}"/>
  28. <div><hr/>
  29.     code:<%out.print(code); %>
  30. </div>
  31.     <scripttype="text/javascript">
  32.         var appId,timeStamp,nonceStr,pg,signType,paySign;  
  33.       function onBridgeReady(){  
  34.            WeixinJSBridge.invoke(  
  35.                'getBrandWCPayRequest', {  
  36.                    "appId" : appId,     //公眾號名稱,由商戶傳入       
  37.                    "timeStamp": timeStamp,         //時間戳,自1970年以來的秒數       
  38.                    "nonceStr" : nonceStr, //隨機串       
  39.                    "package" : "prepay_id=" + pg,       
  40.                    "signType" : signType,         //微信簽名方式:       
  41.                    "paySign" : paySign    //微信簽名   
  42.                },  
  43.                function(res){       
  44.                    if(res.err_msg == "get_brand_wcpay_request:ok" ) {  
  45.                        alert("支付成功");  
  46.                    }   
  47.                }  
  48.            );   
  49.         }  
  50.         function pay(){  
  51.             var code = document.getElementById("code").value;  
  52.             var path = document.getElementById("path").value;  
  53.             send_request(function(value){  
  54.                 var json = eval("(" + value + ")");  
  55.                 if(json.length > 0){  
  56.                     appId = json[0].appId;  
  57.                     timeStamp = json[0].timeStamp;  
  58.                     nonceStr = json[0].nonceStr;  
  59.                     pg = json[0].pg;  
  60.                     signType = json[0].signType;  
  61.                     paySign = json[0].paySign;  
  62.                     if (typeof WeixinJSBridge == "undefined"){  
  63.                        if( document.addEventListener ){  
  64.                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);  
  65.                        }else if (document.attachEvent){  
  66.                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);   
  67.                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);  
  68.                        }  
  69.                     }else{  
  70.                        onBridgeReady();  
  71.                     }   
  72.                 }  
  73.             },path+"/pay/payparm.htm?openId="+code, true);  
  74.         }  
  75. function send_request(callback, urladdress,isReturnData){        
  76.     var xmlhttp = getXMLHttpRequest();  
  77.     xmlhttp.onreadystatechange = function(){  
  78.             if (xmlhttp.readyState == 4) {  
  79.                     try{  
  80.                     if(xmlhttp.status == 200){  
  81.                         if(isReturnData && isReturnData==true){  
  82.                             callback(xmlhttp.responseText);  
  83.                         }  
  84.                     }else{  
  85.                         callback("頁面找不到!"+ urladdress +"");  
  86.                     }  
  87.                 } catch(e){  
  88.                     callback("請求傳送失敗,請重試!" + e);  
  89.                 }  
  90.            }  
  91.     }  
  92.     xmlhttp.open("POST", urladdress, true);  
  93.     xmlhttp.send(null);  
  94. }  
  95. function getXMLHttpRequest() {  
  96.     var xmlhttp;  
  97.     if (window.XMLHttpRequest) {  
  98.         try {  
  99.             xmlhttp = new XMLHttpRequest();  
  100.             xmlhttp.overrideMimeType("text/html;charset=UTF-8");  
  101.         } catch (e) {}  
  102.     } else if (window.ActiveXObject) {  
  103.         try {  
  104.             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
  105.         } catch (e) {  
  106.             try {  
  107.                 xmlhttp = new ActiveXObject("Msxml2.XMLHttp");  
  108.             } catch (e) {  
  109.                 try {  
  110.                     xmlhttp = new ActiveXObject("Msxml3.XMLHttp");  
  111.                 } catch (e) {}  
  112.             }  
  113.         }  
  114.     }  
  115.     return xmlhttp;  
  116. }  
  117.     </script>
  118. </body>
  119. </html>

伺服器端

  1. @RequestMapping("/pay/payparm")  
  2.     publicvoid payparm(HttpServletRequest request, HttpServletResponse response){  
  3.         try {  
  4.             // 獲取openid
  5.             String openId = (String) request.getSession().getAttribute("openId");  
  6.             if (openId == null) {  
  7.                 openId = getUserOpenId(request);  
  8.             }  
  9.             String appid = WXConfig.APP_ID;  
  10.             String paternerKey = WXConfig.PERTNER_KEY;  
  11.             String out_trade_no = getTradeNo();  
  12.             Map<String, String> paraMap = new HashMap<String, String>();  
  13.             paraMap.put("appid", appid);  
  14.             paraMap.put("attach""測試");  
  15.             paraMap.put("body""測試購買支付");  
  16.             paraMap.put("mch_id", WXConfig.PARTNER);  
  17.             paraMap.put("nonce_str", create_nonce_str());  
  18.             paraMap.put("openid", openId);  
  19.             paraMap.put("out_trade_no", out_trade_no);  
  20.             paraMap.put("spbill_create_ip", getAddrIp(request));  
  21.             paraMap.put("total_fee""1");  
  22.             paraMap.put("trade_type""JSAPI");  
  23.             paraMap.put("notify_url""http://m.ebiaotong.com/WXPay/notify");// 此路徑是微信伺服器呼叫支付結果通知路徑  
  24.             String sign = getSign(paraMap, paternerKey);  
  25.             paraMap.put("sign", sign);  
  26.             // 統一下單 https://api.mch.weixin.qq.com/pay/unifiedorder
  27.             String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";  
  28.             String xml = ArrayToXml(paraMap);  
  29.             String xmlStr = HttpKit.post(url, xml);  
  30.             // 預付商品id
  31.             String prepay_id = "";  
  32.             if (xmlStr.indexOf("SUCCESS") != -1) {  
  33.                 Map<String, String> map = doXMLParse(xmlStr);  
  34.                 prepay_id = (String) map.get("prepay_id");  
  35.             }  
  36.             String timeStamp = create_timestamp();  
  37.             String nonceStr = create_nonce_str();  
  38.             Map<String, String> payMap = new HashMap<String, String>();  
  39.             payMap.put("appId", appid);  
  40.             payMap.put("timeStamp", timeStamp);  
  41.             payMap.put("nonceStr", nonceStr);  
  42.             payMap.put("signType""MD5");  
  43.             payMap.put("package""prepay_id=" + prepay_id);  
  44.             String paySign = getSign(payMap, paternerKey);  
  45.             payMap.put("pg", prepay_id);  
  46.             payMap.put("paySign", paySign);  
  47.             // 拼接並返回json
  48.             StringBuilder sBuilder = new StringBuilder("[{");  
  49.             sBuilder.append("appId:'").append(appid).append("',")  
  50.                         .append("timeStamp:'").append(timeStamp).append("',")  
  51.                         .append("nonceStr:'").append(nonceStr).append("',")  
  52.                         .append("pg:'").append(prepay_id).append("',")  
  53.                         .append("signType:'MD5',")  
  54.                         .append("paySign:'").append(paySign).append("'");  
  55.             sBuilder.append("}]");  
  56.             response.getWriter().print(sBuilder.toString());  
  57.             response.getWriter().close();  
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  


測試結果