天天看點

微信支付(.NET版)

前段時間做了網頁版微信支付,遇到很多問題,不過最終還是解決了,現在在這裡記錄下開發流程以及說明,給其他人一些參考。

一、準備工作

    首先肯定得先要開通微信支付功能,之前開通微信支付需要三萬的押金的,現在不需要了,是以就做了這個功能。

    要進行微信支付開發,需要在公衆号背景和微信商戶背景進行相關的設定。

    1、開發目錄配置

         微信支付需要在公衆号背景(微信支付=》開發配置)進行配置支付授權目錄。這裡授權目錄需要是線上位址,也就是可以通過網際網路通路到的位址,微信支付系統需要能夠通過網際網路通路到你的位址。

微信授權目錄需要精确到二級或三級目錄,事例:假如發起支付的連結是 http://www.hxfspace.net/weixin/WeXinPay/WeXinPayChoose  那麼配置的目錄應該         是http://www. hxfspace.net/weixin/WeXinPay/ 其中 http://www. hxfspace.net是域名 weixin是虛拟目錄 WeXinPay也就是Controller 相關的支付請求都在WeXinPay中的action裡面。                

微信支付(.NET版)

     2、OAuth2.0網頁授權域名設定

        微信支付的時候會對支付請求進行回調來擷取授權代碼(code),是以需要在這裡設定授權域名。當然這裡域名是要和支付授權目錄中的域名是同一個。這個不要忘記設定了我當時就是忘記設定然後找半天原因,哭死。

微信支付(.NET版)

    3、相關參數準備

       調用微信支付需要通過腳本向微信支付系統發起支付請求,參數說明見微信jsapihttps://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 

微信支付(.NET版)

        其中package和paySign的生成需要開發者密鑰AppSecret(應用密鑰)、微信商戶号、微信支付密鑰,這些參數的擷取和設定可以看這篇文章http://mp.weixin.qq.com/s?__biz=MjM5MDM3NTY5Nw==&mid=213267860&idx=1&sn=576c80aa0549a8fd75d37d1ac22dbe8c#rd。

二、開發流程

   廢話不多說直接說整理之後的流程:

   1、通過微信授權回調來擷取授權code

   2、通過授權code來換取網頁授權access_token 和openid

   3、調用統一下單接口擷取預支付prepayId

    4、組建jsapi微信支付請求參數,發起支付

    5、接收微信支付回調進行後續操作

三、具體開發(上代碼)

       微信支付隻能線上上環境中進行,調式很不友善,所在在剛開始開發的時候最好在每個關鍵位置記錄好日志。

       1、通過微信授權回調來擷取授權code

            首先把發起支付位址以及相關參數傳給微信支付接口,微信支付接收驗證成功之後,會重新請求你的支付位址并帶上授權code。

           比如我這裡

//判斷是否網頁授權,擷取授權code,沒有代表沒有授權,構造網頁授權擷取code,并重新請求
            if (string.IsNullOrEmpty(Request.QueryString["code"]))
            {
                string redirectUrl = _weChatPaySerivce.GetAuthorizeUrl(account.AppId, account.RedquestUrl,
                    "STATE" + "#wechat_redirect", "snsapi_base");
                return Redirect(redirectUrl);
            }      

        拼接微信網頁授權Url方法

public string GetAuthorizeUrl(string appId, string redirectUrl, string state, string scope)
        {
            string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state={3}",
                    appId, HttpUtility.UrlEncode(redirectUrl), scope, state);
            /* 這一步發送之後,客戶會得到授權頁面,無論同意或拒絕,都會傳回redirectUrl頁面。
             * 如果使用者同意授權,頁面将跳轉至 redirect_uri/?code=CODE&state=STATE。這裡的code用于換取access_token(和通用接口的access_token不通用)
             * 若使用者禁止授權,則重定向後不會帶上code參數,僅會帶上state參數redirect_uri?state=STATE
             */
            AppLog.Write("擷取到授權url:", AppLog.LogMessageType.Debug); 
            return url;
        }      

      2、通過授權code來換取網頁授權access_token 和openid

            從第一步中擷取到授權code之後,組合網頁授權請求url,來擷取access_token 和openid

public Tuple<string, string> GetOpenidAndAccessTokenFromCode(string appId, string code, string appSecret)
        {
            Tuple<string, string> tuple = null;
            try
            {
                string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appId, appSecret, code);
                string result = WeChatPayHelper.Get(url);
                AppLog.Write("微信支付-擷取openid和access_token 請求Url:" + url + "result:" + result, AppLog.LogMessageType.Debug);
                if (!string.IsNullOrEmpty(result))
                {
                    var jd=Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
                    tuple = new Tuple<string, string>(jd["openid"],jd["access_token"]);
                    AppLog.Write("微信支付-擷取openid和access_token成功", AppLog.LogMessageType.Debug);
                }
            }
            catch (Exception ex)
            {
                AppLog.Write("微信支付:擷取openid和access_tokenu異常", AppLog.LogMessageType.Debug,ex);
            }
            return tuple;
        }      

        3、調用統一下單接口擷取預支付prepayId

        這裡RequestHandler是用的網上别人封裝好的dll,幫你封裝好了簽名的生成以及一些驗證請求。dll可以在這他們官網下載下傳http://weixin.senparc.com/

//建立支付應答對象
            RequestHandler packageReqHandler = new RequestHandler(null);
            //初始化
            packageReqHandler.Init();
            //時間戳
            string timeStamp = TenPayUtil.GetTimestamp();
            //随機字元串
            string nonceStr = TenPayUtil.GetNoncestr();
            //設定package訂單參數 生成prepayId預支付Id
            packageReqHandler.SetParameter("appid", account.AppId);          //公衆賬号ID
            packageReqHandler.SetParameter("mch_id", account.PartnertId);          //商戶号
            packageReqHandler.SetParameter("nonce_str", nonceStr);                    //随機字元串
            packageReqHandler.SetParameter("body", account.Body);
            packageReqHandler.SetParameter("out_trade_no", account.OrderSerialId);        //商家訂單号
            packageReqHandler.SetParameter("total_fee", account.TotalAmount);                    //商品金額,以分為機關(money * 100).ToString()
            packageReqHandler.SetParameter("spbill_create_ip", account.RequestIp);   //使用者的公網ip,不是商戶伺服器IP
            packageReqHandler.SetParameter("notify_url", account.NotifyUrl);            //接收财付通通知的URL
            packageReqHandler.SetParameter("trade_type", "JSAPI");                        //交易類型
            packageReqHandler.SetParameter("openid", account.OpenId);                        //使用者的openId
            string sign = packageReqHandler.CreateMd5Sign("key", account.PaySignKey);
            packageReqHandler.SetParameter("sign", sign);                        //簽名
            string prepayId = string.Empty;
            try
            {
                string data = packageReqHandler.ParseXML();
                var result = TenPayV3.Unifiedorder(data);
                MailHelp.SendMail("調用統一下單接口,下單結果:--"+result+"請求參數:"+data);
                var res = XDocument.Parse(result);
                prepayId = res.Element("xml").Element("prepay_id").Value;
                AppLog.Write("調用統一下單接口擷取預支付prepayId成功", AppLog.LogMessageType.Debug);
            }
            catch (Exception ex)
            {
                AppLog.Write("擷取到openid和access_tokenu異常", AppLog.LogMessageType.Debug, ex);
                MailHelp.SendMail("調用統一下單接口擷取預支付prepayid異常:", ex);
                return null;
            }      

       4、組建jsapi微信支付請求參數,發起支付

            我這裡是首先組裝好微信支付所需要的參數,然後再建立調用js腳本     

//生成JsAPI支付參數
            RequestHandler paySignReqHandler = new RequestHandler(null);
            paySignReqHandler.SetParameter("appId", account.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", account.PaySignKey);
            WeChatJsPayRequestModel resultModel = new WeChatJsPayRequestModel
            {
                AppId = account.AppId,
                NonceStr = nonceStr,
                TimeStamp = timeStamp,
                Package = string.Format("prepay_id={0}", prepayId),
                PaySign = paySign,
                SignType = "MD5"
            };      

         建立調用腳本

private string CreateWeixinJs(WeChatJsPayRequestModel model)
        {
            string js = @"<script type='text/javascript'>
                                callpay();
                                function jsApiCall(){
                                  WeixinJSBridge.invoke(
                                    'getBrandWCPayRequest', {
                                        requestParam
                                    },
                                    function (res) {
                                        if(res.err_msg == 'get_brand_wcpay_request:ok' ){
                                                window.location.href = 'successUrl';
                                        }else{
                                                window.location.href = 'failUrl';
                                        }
                                    }
                                  ); 
                                }
                              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>";
            string requestParam = string.Format(@"'appId': '{0}','timeStamp': '{1}','nonceStr': '{2}','package': '{3}','signType': '{4}','paySign': '{5}'",
                model.AppId, model.TimeStamp, model.NonceStr, model.Package, model.SignType, model.PaySign);
            js = js.Replace("requestParam", requestParam)
                .Replace("successUrl", model.JumpUrl + "&result=1")
                .Replace("failUrl", model.JumpUrl + "&result=0");
            AppLog.Write("生成可執行腳本成功", AppLog.LogMessageType.Debug);
            return js;
        }      

        5、接收微信支付回調進行後續操作

             回調的時候首先需要驗證簽名是否正确,保證安全性,簽名驗證通過之後再進行後續的操作,訂單狀态、通知啥的。 

ResponseHandler resHandler = new ResponseHandler(System.Web.HttpContext.Current);
            bool isSuccess = _weChatPaySerivce.ProcessNotify(resHandler);
            if (isSuccess)
            {
                string result = @"<xml>
                                    <return_code><![CDATA[SUCCESS]]></return_code>
                                    <return_msg><![CDATA[支付成功]]></return_msg>
                                 </xml>";
                HttpContext.Response.Write(result);
                HttpContext.Response.End();
            }
            return new EmptyResult();      

            這裡有兩點需要注意

  1、微信支付回調的時候微信會通知八次,好像是這個數吧,是以你需要在第一次收到通知之後,把收到請求這個狀态以xml的格式響應給微信支付接口。當然你不進行這個操作也是可以的,再回調的時候 每次去判斷該訂單是否已經回調成功,回調成功則不進行處理就可以了。

  2、微信回調方式為post請求方式,是以回調的接口需要支援POST

四、參考文檔

       1、官方文檔 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6

       2、部落格園  http://www.cnblogs.com/txw1958/p/wxpayv3-jsapi.html

       3、第三方元件http://weixin.senparc.com/

       4、度娘    https://www.baidu.com/

最後感謝大家的閱讀,這裡推薦一個公衆号 猿大俠的客棧,會不定時的推送分享IT相關技術文章。

繼續閱讀