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


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

 

准備工作:需要先在項目中引用Senparc.WeiXin.dll和Senparc.WeiXin.MP.dll,開源項目見:https://github.com/JeffreySu/WeiXinMPSDK

 

第一步:綁定域名

先登錄微信公眾平台進入“公眾號設置”的“功能設置”里填寫“JS接口安全域名”。

 

第二步:引入JS文件

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

 

第三步:設置全局變量。

我只列舉了這次用到的幾個公共變量。這里只是測試用的,有需要可以將全局變量寫在配置文件里面更好。

        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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM