背景
由於項目是采用Java編寫的,微信包括微信支付大都是PHP相關,於是微信支付官方文檔對java的支持就不是很友好,在網上找了很多文章,基本上沒有一篇是真正跑的通的,經過一番整理,先將java接入微信支付詳細流程總結出來以便后續使用。
步驟一
准備階段:已認證微信號,且通過微信支付認證,這個可以看微信文檔,很詳細,這里就不再重復。
步驟二
配置授權目錄,官方推薦使用https類型的url,不知道http能不能行,個人也推薦使用https的保證不會錯。
配置授權域名
步驟三
微信支付二次開發所需要的參數:
APP_ID,APP_KEY,PARTNER,PARTNER_KEY(AppSecret)
APP_ID和PARTNER_KEY(AppSecret)
PARTNER
APP_KEY(自行設置32位字符)
步驟四
4.1通過頁面跳轉到確認支付頁面,其中的redirect_uri必須是配置授權目錄下的
- <html>
- <head>
- <title>支付測試</title>
- </head>
- <body>
- <a href="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>
- </body>
- </html>
4.2 獲取到openid,再經服務器向微信請求獲取prepay_id,封裝字段並進行簽名后通過jsapi調起微信支付
網頁端
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="format-detection" content="telephone=no" /> <title>測試支付</title> <link href="../css/css.css?v=1.0" rel="stylesheet" type="text/css"> </head> <body> <div class="index_box"> <div class="apply_name">微信js支付測試</div> <div class="branch_con"> <ul> <li><span class="name">測試支付信息[]</span></li> </ul> <p class="cz_btn"><a href="javascript:pay();" class="btn_1">立即支付</a></p> </div> </div> <% String code = request.getParameter("code"); %> <input type="text" id="code" value="<%out.print(code); %>"/> <input type="text" id="path" value="${pageContext.request.contextPath}"/> <div><hr/> code:<%out.print(code); %> </div> <script type="text/javascript"> var appId,timeStamp,nonceStr,pg,signType,paySign; function onBridgeReady(){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId" : appId, //公眾號名稱,由商戶傳入 "timeStamp": timeStamp, //時間戳,自1970年以來的秒數 "nonceStr" : nonceStr, //隨機串 "package" : "prepay_id=" + pg, "signType" : signType, //微信簽名方式: "paySign" : paySign //微信簽名 }, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert("支付成功"); } } ); } function pay(){ var code = document.getElementById("code").value; var path = document.getElementById("path").value; send_request(function(value){ var json = eval("(" + value + ")"); if(json.length > 0){ appId = json[0].appId; timeStamp = json[0].timeStamp; nonceStr = json[0].nonceStr; pg = json[0].pg; signType = json[0].signType; paySign = json[0].paySign; if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } }else{ onBridgeReady(); } } },path+"/pay/payparm.htm?openId="+code, true); } function send_request(callback, urladdress,isReturnData){ var xmlhttp = getXMLHttpRequest(); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4) { try{ if(xmlhttp.status == 200){ if(isReturnData && isReturnData==true){ callback(xmlhttp.responseText); } }else{ callback("頁面找不到!"+ urladdress +""); } } catch(e){ callback("請求發送失敗,請重試!" + e); } } } xmlhttp.open("POST", urladdress, true); xmlhttp.send(null); } function getXMLHttpRequest() { var xmlhttp; if (window.XMLHttpRequest) { try { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType("text/html;charset=UTF-8"); } catch (e) {} } else if (window.ActiveXObject) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHttp"); } catch (e) { try { xmlhttp = new ActiveXObject("Msxml3.XMLHttp"); } catch (e) {} } } } return xmlhttp; } </script> </body> </html>
服務器端
@RequestMapping("/pay/payparm") public void payparm(HttpServletRequest request, HttpServletResponse response){ try { // 獲取openid String openId = (String) request.getSession().getAttribute("openId"); if (openId == null) { openId = getUserOpenId(request); } String appid = WXConfig.APP_ID; String paternerKey = WXConfig.PERTNER_KEY; String out_trade_no = getTradeNo(); Map<String, String> paraMap = new HashMap<String, String>(); paraMap.put("appid", appid); paraMap.put("attach", "測試"); paraMap.put("body", "測試購買支付"); paraMap.put("mch_id", WXConfig.PARTNER); paraMap.put("nonce_str", create_nonce_str()); paraMap.put("openid", openId); paraMap.put("out_trade_no", out_trade_no); paraMap.put("spbill_create_ip", getAddrIp(request)); paraMap.put("total_fee", "1"); paraMap.put("trade_type", "JSAPI"); paraMap.put("notify_url", "http://m.ebiaotong.com/WXPay/notify");// 此路徑是微信服務器調用支付結果通知路徑 String sign = getSign(paraMap, paternerKey); paraMap.put("sign", sign); // 統一下單 https://api.mch.weixin.qq.com/pay/unifiedorder String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; String xml = ArrayToXml(paraMap); String xmlStr = HttpKit.post(url, xml); // 預付商品id String prepay_id = ""; if (xmlStr.indexOf("SUCCESS") != -1) { Map<String, String> map = doXMLParse(xmlStr); prepay_id = (String) map.get("prepay_id"); } String timeStamp = create_timestamp(); String nonceStr = create_nonce_str(); Map<String, String> payMap = new HashMap<String, String>(); payMap.put("appId", appid); payMap.put("timeStamp", timeStamp); payMap.put("nonceStr", nonceStr); payMap.put("signType", "MD5"); payMap.put("package", "prepay_id=" + prepay_id); String paySign = getSign(payMap, paternerKey); payMap.put("pg", prepay_id); payMap.put("paySign", paySign); // 拼接並返回json StringBuilder sBuilder = new StringBuilder("[{"); sBuilder.append("appId:'").append(appid).append("',") .append("timeStamp:'").append(timeStamp).append("',") .append("nonceStr:'").append(nonceStr).append("',") .append("pg:'").append(prepay_id).append("',") .append("signType:'MD5',") .append("paySign:'").append(paySign).append("'"); sBuilder.append("}]"); response.getWriter().print(sBuilder.toString()); response.getWriter().close(); } catch (Exception e) { e.printStackTrace(); } }
測試結果