首頁  >  文章  >  微信小程式  >  使用.NET解析微信支付的實作方法

使用.NET解析微信支付的實作方法

高洛峰
高洛峰原創
2017-03-17 15:21:341549瀏覽

由於微信的廣泛利用,基於微信開發的一系列也應運而生,這篇文章主要介紹了解析微信支付的實現方法(.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、相關參數準備

#呼叫微信支付需要透過腳本向微信支付系統發起支付請求,參數說明請見微信官網支付平台https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 

使用.NET解析微信支付的實作方法





##其中package與paySign的產生需要開發者金鑰AppSecret(應用程式金鑰)、微信商家號、微信支付金鑰

二、開發流程


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

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=&#39;text/javascript&#39;>
                callpay();
                function jsApiCall(){
                 WeixinJSBridge.invoke(
                  &#39;getBrandWCPayRequest&#39;, {
                    requestParam
                  },
                  function (res) {
                    if(res.err_msg == &#39;get_brand_wcpay_request:ok&#39; ){
                        window.location.href = &#39;successUrl&#39;;
                    }else{
                        window.location.href = &#39;failUrl&#39;;
                    }
                  }
                 ); 
                }
               function callpay()
                {
                  if (typeof WeixinJSBridge == &#39;undefined&#39;){
                    if( document.addEventListener ){
                      document.addEventListener(&#39;WeixinJSBridgeReady&#39;, jsApiCall, false);
                    }else if (document.attachEvent){
                      document.attachEvent(&#39;WeixinJSBridgeReady&#39;, jsApiCall); 
                      document.attachEvent(&#39;onWeixinJSBridgeReady&#39;, jsApiCall);
                    }
                  }else{
                    jsApiCall();
                  }
                }
            </script>";
      string requestParam = string.Format(@"&#39;appId&#39;: &#39;{0}&#39;,&#39;timeStamp&#39;: &#39;{1}&#39;,&#39;nonceStr&#39;: &#39;{2}&#39;,&#39;package&#39;: &#39;{3}&#39;,&#39;signType&#39;: &#39;{4}&#39;,&#39;paySign&#39;: &#39;{5}&#39;",
        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();


这里有一点需要注意,就是微信支付回调的时候微信会通知八次,好像是这个数吧,所以你需要在第一次收到通知之后,把收到请求这个状态以xml的格式响应给微信支付接口。当然你不进行这个操作也是可以的,再回调的时候 每次去判断该订单是否已经回调成功,回调成功则不进行处理就可以了。


以上是使用.NET解析微信支付的實作方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn