即 微信內H5調起支付 ,使用的是微信的內置瀏覽器的API,也是JSSDK,但和微信網頁開發的 JS-SDK是不同的。javascript
微信網頁開發須要引入 http://res.wx.qq.com/open/js/jweixin-1.2.0.js 文件,而後使用配置的方式來進行相關接口的調用;php
而H5調起支付不須要引入文件,調用方式也不一樣,且 注入權限驗證配置的參數也多一些。html
SDK與DEMO下載:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1java
WeixinJSBridge API (微信內置瀏覽器私有接口)ajax
1.首先開通微信支付功能,經過以後能夠拿到商戶號和商戶帳號及密碼,去登陸商戶平臺作開發配置算法
2.登陸商戶平臺配置支付受權目錄json
添加以前須要生成本機的數據證書,不然無權限操做api
支付受權目錄應該爲當前頁面連接的上一級目錄.瀏覽器
接着設置密鑰微信
後面調用須要用到這些信息
調用的話官方的demo例子仍是很全的:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
public class JsApiPay { /// <summary> /// openid用於調用統一下單接口 /// </summary> public string openid { get; set; } /// <summary> /// access_token用於獲取收貨地址js函數入口參數 /// </summary> public string access_token { get; set; } /// <summary> /// 商品金額,用於統一下單 /// </summary> public int total_fee { get; set; } /// <summary> /// 統一下單接口返回結果 /// </summary> public WxPayData unifiedOrderResult { get; set; } public JsApiPay() { } /** * * 經過code換取網頁受權access_token和openid的返回數據,正確時返回的JSON數據包以下: * { * "access_token":"ACCESS_TOKEN", * "expires_in":7200, * "refresh_token":"REFRESH_TOKEN", * "openid":"OPENID", * "scope":"SCOPE", * "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" * } * 其中access_token可用於獲取共享收貨地址 * openid是微信支付jsapi支付接口統一下單時必須的參數 * 更詳細的說明請參考網頁受權獲取用戶基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html * @失敗時拋異常WxPayException */ public void GetOpenidAndAccessTokenFromCode(string code) { try { //構造獲取openid及access_token的url WxPayData data = new WxPayData(); data.SetValue("appid", WxPayConfig.APPID); data.SetValue("secret", WxPayConfig.APPSECRET); data.SetValue("code", code); data.SetValue("grant_type", "authorization_code"); string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl(); //請求url以獲取數據 string result = HttpService.Get(url); Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result); //保存access_token,用於收貨地址獲取 JsonData jd = JsonMapper.ToObject(result); access_token = (string)jd["access_token"]; //獲取用戶openid openid = (string)jd["openid"]; Log.Debug(this.GetType().ToString(), "Get openid : " + openid); Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token); } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxPayException(ex.ToString()); } } /** * 調用統一下單,得到下單結果 * @return 統一下單結果 * @失敗時拋異常WxPayException */ public WxPayData GetUnifiedOrderResult() { //統一下單 WxPayData data = new WxPayData(); data.SetValue("body", "test"); data.SetValue("attach", "test"); data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); data.SetValue("total_fee", total_fee); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); data.SetValue("goods_tag", "test"); data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", openid); WxPayData result = WxPayApi.UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxPayException("UnifiedOrder response error!"); } unifiedOrderResult = result; return result; } /** * * 從統一下單成功返回的數據中獲取微信瀏覽器調起jsapi支付所需的參數, * 微信瀏覽器調起JSAPI時的輸入參數格式以下: * { * "appId" : "wx2421b1c4370ec43b", //公衆號名稱,由商戶傳入 * "timeStamp":" 1395712654", //時間戳,自1970年以來的秒數 * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機串 * "package" : "prepay_id=u802345jgfjsdfgsdg888", * "signType" : "MD5", //微信簽名方式: * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名 * } * @return string 微信瀏覽器調起JSAPI時的輸入參數,json格式能夠直接作參數用 * 更詳細的說明請參考網頁端調起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 * */ public string GetJsApiParameters() { Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing..."); WxPayData jsApiParam = new WxPayData(); jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); string parameters = jsApiParam.ToJson(); Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters); return parameters; } /** * * 獲取收貨地址js函數入口參數,詳情請參考收貨地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9 * @return string 共享收貨地址js函數須要的參數,json格式能夠直接作參數使用 */ public string GetEditAddressParameters() { string parameter = ""; try { //這個地方要注意,參與簽名的是網頁受權獲取用戶信息時微信後臺回傳的完整url string url = string.Format("http://{0}/Business/TongMall/TongPay/Index/?moduleId=5000", ConfigurationManager.AppSettings["DoMain"]); //構造須要用SHA1算法加密的數據 WxPayData signData = new WxPayData(); signData.SetValue("appid", WxPayConfig.APPID); signData.SetValue("url", url); signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp()); signData.SetValue("noncestr", WxPayApi.GenerateNonceStr()); signData.SetValue("accesstoken", access_token); string param = signData.ToUrl(); Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param); //SHA1加密 string addrSign = GetSwcSH1(param); Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign); //獲取收貨地址js函數入口參數 WxPayData afterData = new WxPayData(); afterData.SetValue("appId", WxPayConfig.APPID); afterData.SetValue("scope", "jsapi_address"); afterData.SetValue("signType", "sha1"); afterData.SetValue("addrSign", addrSign); afterData.SetValue("timeStamp", signData.GetValue("timestamp")); afterData.SetValue("nonceStr", signData.GetValue("noncestr")); //轉爲json格式 parameter = afterData.ToJson(); Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter); } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxPayException(ex.ToString()); } return parameter; } public static string GetSwcSH1(string value) { SHA1 algorithm = SHA1.Create(); byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value)); string sh1 = ""; for (int i = 0; i < data.Length; i++) { sh1 += data[i].ToString("x2").ToUpperInvariant(); } return sh1; } }
public class TongPay_Mod { private decimal price; public decimal Price { get { return price; } set { price = value; } } public string wxEditAddrParam { get; set; } public string openid { get; set; } } public class TongJsApiPay_Mod { public string wxJsApiParam { get; set; } public string Info { get; set; } } public ViewResult TongJsApiPay(TongPay_Mod model) { TongJsApiPay_Mod model1 = new TongJsApiPay_Mod(); //檢測是否給當前頁面傳遞了相關參數 if (string.IsNullOrEmpty(model.openid) || string.IsNullOrEmpty(model.Price.ToString())) { Response.Write("<span style='color:#FF0000;font-size:20px'>" + "頁面傳參出錯,請返回重試" + "</span>"); Log.Error(this.GetType().ToString(), "This page have not get params, cannot be inited, exit..."); } //若傳遞了相關參數,則調統一下單接口,得到後續相關接口的入口參數 JsApiPay jsApiPay = new JsApiPay(); jsApiPay.openid = model.openid; jsApiPay.total_fee = Convert.ToInt32((Convert.ToDecimal(model.Price) * 100)); //JSAPI支付預處理 try { WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(); model1.wxJsApiParam = jsApiPay.GetJsApiParameters();//獲取H5調起JS API參數 Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + model1.wxJsApiParam); //在頁面上顯示訂單信息 model1.Info = unifiedOrderResult.ToPrintStr(); } catch (Exception ex) { model1.Info = ex.Message; //model1.Info= "下單失敗,請返回重試"; } return View(model1); }
頁面顯示獲取的參數
@{ Layout = "~/Views/Shared/_Layout_WeUI.cshtml"; } @model WY.WxcModel.TongJsApiPay_Mod <script type="text/javascript"> //調用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', @Html.Raw(Model.wxJsApiParam),//josn串 function (res) { WeixinJSBridge.log(res.err_msg); alert(res.err_code + res.err_desc + res.err_msg); } ); } function callpay() { if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } } else { jsApiCall(); } } </script> <table class="my_header"> <tr> <td> <button class="headerBtn aHeaderLeft"> </button> </td> <td style="text-align: center">訂單確認及支付</td> <td> </td> </tr> </table> <div style="width: 100%; height: 300px; padding-left: 8px;"> 這裏顯示一些確認信息,優惠信息之類的 <br /> <span style='color: #00CD00; font-size: 16px;'> @(new MvcHtmlString(Model.Info)) </span> </div> <a id="btnSave" class="weui-btn weui-btn_primary" data-ajax="false" style="color: #fff; text-shadow: none; margin: 10px" onclick="callpay()">當即支付</a>
支付