java微信支付接入流程詳解
背景
由于項目是采用java編寫的,微信包括微信支付大都是php相關(guān),于是微信支付官方文檔對java的支持就不是很友好,在網(wǎng)上找了很多文章,基本上沒有一篇是真正跑的通的,經(jīng)過一番整理,先將java接入微信支付詳細流程總結(jié)出來以便后續(xù)使用。
步驟一
準(zhǔn)備階段:已認證微信號,且通過微信支付認證,這個可以看微信文檔,很詳細,這里就不再重復(fù)。
步驟二
配置授權(quán)目錄,官方推薦使用https類型的url,不知道http能不能行,個人也推薦使用https的保證不會錯。
配置授權(quán)域名
步驟三
微信支付二次開發(fā)所需要的參數(shù):
APP_ID,APP_KEY,PARTNER,PARTNER_KEY(AppSecret)
APP_ID和PARTNER_KEY(AppSecret)
PARTNER
APP_KEY(自行設(shè)置32位字符)
步驟四
4.1通過頁面跳轉(zhuǎn)到確認支付頁面,其中的redirect_uri必須是配置授權(quán)目錄下的
<html> <head> <title>支付測試</title> </head> <body> <a >支付測試</a> </body> </html>
4.2 獲取到openid,再經(jīng)服務(wù)器向微信請求獲取prepay_id,封裝字段并進行簽名后通過jsapi調(diào)起微信支付
網(wǎng)頁端
<%@ 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="external nofollow" 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();" rel="external nofollow" 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年以來的秒數(shù) "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("請求發(fā)送失敗,請重試!" + 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>
服務(wù)器端
@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");// 此路徑是微信服務(wù)器調(diào)用支付結(jié)果通知路徑 String sign = getSign(paraMap, paternerKey); paraMap.put("sign", sign); // 統(tǒng)一下單 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); // 預(yù)付商品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(); } }
測試結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringSecurity的TokenStore四種實現(xiàn)方式小結(jié)
本文主要介紹了SpringSecurity的TokenStore四種實現(xiàn)方式小結(jié),分別是InMemoryTokenStore,JdbcTokenStore,JwkTokenStore,RedisTokenStore,具有一定的參考價值,感興趣的可以了解一下2024-01-01SpringBoot使用flyway初始化數(shù)據(jù)庫
這篇文章主要介紹了SpringBoot如何使用flyway初始化數(shù)據(jù)庫,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-03-03Java使用JSONPath解析JSON完整內(nèi)容詳解
這篇文章主要介紹了Java使用JSONPath解析JSON完整內(nèi)容詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03RestTemplate的DELETE及PUT等請求方法使用精講
這篇文章主要為大家介紹了RestTemplate的DELETE及PUT等請求方法的使用精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03