微信支付-JSAPI支付V3-發起一次支付請求

JSAPI支付業務流程:圖片來源於:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=7_4php

 

準備工做:須要先在項目中引用Senparc.WeiXin.dll和Senparc.WeiXin.MP.dll,開源項目見:https://github.com/JeffreySu/WeiXinMPSDKgit

 

第一步:綁定域名github

先登陸微信公衆平臺進入「公衆號設置」的「功能設置」裏填寫「JS接口安全域名」。api

 

第二步:引入JS文件安全

在須要調用JS接口的頁面引入以下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.0.0.js服務器

 

第三步:設置全局變量。微信

我只列舉了此次用到的幾個公共變量。這裏只是測試用的,有須要能夠將全局變量寫在配置文件裏面更好。app

        private string AppId = "***";//公衆帳號ID
        private string AppSecret = "***";//公衆帳號密鑰
        private string Key = "***";//微信支付密鑰
        private string MchId = "***";//微信支付分配的商戶號

 

第四步:經過Config接口注入權限驗證配置微信公衆平臺

前臺JS代碼:異步

$http.post("/GetPayConfig", JSON.stringify({no:$location.search().no}), "").success(function (data) {
      wx.config({
           debug: true, // 開啓調試模式,調用的全部api的返回值會在客戶端alert出來,若要查看傳入的參數,能夠在pc端打開,參數信息會經過log打出,僅在pc端時纔會打印。
           appId: data.appId, // 必填,公衆號的惟一標識
           timestamp: data.timeStamp, // 必填,生成簽名的時間戳
           nonceStr: data.nonceStr, // 必填,生成簽名的隨機串
           signature: data.signature,// 必填,簽名,見附錄1
           jsApiList: ['chooseWXPay'] // 必填,須要使用的JS接口列表,全部JS接口列表見附錄2
      });

      wx.ready(function () {
                // config信息驗證後會執行ready方法,全部接口調用都必須在config接口得到結果以後,config是一個客戶端的異步操做,因此若是須要在頁面加載時就調用相關接口,則須把相關接口放在ready函數中調用來確保正確執行。對於用戶觸發時才調用的接口,則能夠直接調用,不須要放在ready函數中。

     });
      wx.error(function(res){
               // config信息驗證失敗會執行error函數,如簽名過時致使驗證失敗,具體錯誤信息能夠打開config的debug模式查看,也能夠在返回的res參數中查看,對於SPA能夠在這裏更新簽名。

     });
});

後臺代碼:

        /// <summary>
        /// 獲取權限驗證配置
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public JsonResult GetPayConfig(string no)
        {
            string url = "http://******pay?no="+no ;//這裏是當前頁面的地址

            string timeStamp = Senparc.Weixin.MP.TenPayLib.TenPayUtil.GetTimestamp();
            string nonceStr = Senparc.Weixin.MP.TenPayLib.TenPayUtil.GetNoncestr();
            string JsTicket = Senparc.Weixin.MP.CommonAPIs.JsApiTicketContainer.TryGetTicket(AppId, AppSecret);
            string signature = "";

            Senparc.Weixin.MP.TenPayLib.RequestHandler paySignReqHandler = new Senparc.Weixin.MP.TenPayLib.RequestHandler(null);

            paySignReqHandler.SetParameter("jsapi_ticket", JsTicket);
            paySignReqHandler.SetParameter("noncestr", nonceStr);
            paySignReqHandler.SetParameter("timestamp", timeStamp);
            paySignReqHandler.SetParameter("url", url);

            signature = paySignReqHandler.CreateSHA1Sign();

            System.Collections.Hashtable hashtable = new System.Collections.Hashtable();

            hashtable.Add("appId",AppId);
            hashtable.Add("timeStamp", timeStamp);
            hashtable.Add("nonceStr", nonceStr);
            hashtable.Add("signature", signature);

            return Json(hashtable);
        }

 

第五步:發起一個微信支付請求

前臺JS代碼:

 $http.post("/GetPaySign", JSON.stringify({ trade: $scope.model }), "").success(function (data) {
            wx.chooseWXPay({
                timestamp: data.timeStamp, // 支付簽名時間戳,注意微信jssdk中的全部使用timestamp字段均爲小寫。但最新版的支付後臺生成簽名使用的timeStamp字段名需大寫其中的S字符
                nonceStr: data.nonceStr, // 支付簽名隨機串,不長於 32 位
                package: data.package, // 統一支付接口返回的prepay_id參數值,提交格式如:prepay_id=***)
                signType: "MD5", // 簽名方式,默認爲'SHA1',使用新版支付需傳入'MD5'
                paySign: data.paysign, // 支付簽名
                success: function (res) {
                    //成功以後的處理
                }
            });
        });

後臺代碼:

        /// <summary>
        /// 獲取支付請求參數
        /// </summary>
        /// <param name="trade"></param>
        /// <returns></returns>
        public JsonResult GetPaySign(TRADE_Model trade)
        {
            string timeStamp = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetTimestamp();
            string nonceStr = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr();
            string body = "test";//商品或支付單簡要描述
            string out_trade_no = trade.NO;//商戶系統內部的訂單號,32個字符內,可包含字母,其餘說明見商戶訂單號
            int total_fee = 1;//訂單總金額,只能是整數。
            string spbill_create_ip = Request.UserHostAddress;//APP和網頁支付提交用戶端IP,Native支付填調用微信支付API的機器IP
            string notify_url = "http://***/PayNotifyUrl";//接收微信支付異步通知回調地址
            string trade_type = "JSAPI";//JSAPI,NATIVE,APP,WAP
            string openid = "";//trade_type=JSAPI,此參數必傳,用戶在商戶appid下的惟一標識。必傳,這裏須要將去獲取openid賦值上去

            //建立支付應答對象
            Senparc.Weixin.MP.TenPayLibV3.RequestHandler packageReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null);
            //初始化
            packageReqHandler.Init();

            //設置package訂單參數
            packageReqHandler.SetParameter("appid", AppId);
            packageReqHandler.SetParameter("mch_id", MchId);
            packageReqHandler.SetParameter("nonce_str", nonceStr);
            packageReqHandler.SetParameter("body", body);
            packageReqHandler.SetParameter("out_trade_no", out_trade_no);
            packageReqHandler.SetParameter("total_fee", total_fee.ToString());
            packageReqHandler.SetParameter("spbill_create_ip", spbill_create_ip);
            packageReqHandler.SetParameter("notify_url", notify_url);
            packageReqHandler.SetParameter("trade_type", trade_type);
            packageReqHandler.SetParameter("openid", openid);

            string sign = packageReqHandler.CreateMd5Sign("key",Key);

            packageReqHandler.SetParameter("sign", sign);

            string data = packageReqHandler.ParseXML();

            var result = Senparc.Weixin.MP.AdvancedAPIs.TenPayV3.Unifiedorder(data);
            var res = System.Xml.Linq.XDocument.Parse(result);
            string prepayId = res.Element("xml").Element("prepay_id").Value;

            timeStamp = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetTimestamp();
            nonceStr = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr();

            Senparc.Weixin.MP.TenPayLibV3.RequestHandler paysignReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null);

            paysignReqHandler.Init();

            //設置支付參數
            paysignReqHandler.SetParameter("appId",AppId);
            paysignReqHandler.SetParameter("timeStamp", timeStamp);
            paysignReqHandler.SetParameter("nonceStr", nonceStr);
            paysignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId));
            paysignReqHandler.SetParameter("signType", "MD5");

            string paysign = paysignReqHandler.CreateMd5Sign("key",Key);

            paysignReqHandler.SetParameter("paysign", paysign);
                      
            return Json(paysignReqHandler.GetAllParameters());
        }

至此,就到了須要用戶輸入密碼完成支付。支付成功後微信服務器會發通知到PayNotifyUrl。

相關文章
相關標籤/搜索