ASP.Net項目中實現微信APP支付功能

最近挺忙的,沒時間寫東西。然後在弄微信APP支付,網上的搜索一趟,都比較凌亂,我也遇到一些坑,不過也算弄好瞭,記錄分享一下。

1、準備各種調用接口需要的參數,配置app.config。

<!--AppID-->
<add key="AppID" value="" />
<!--AppSecret-->
<add key="AppSecret" value="" />
<!--商戶號-->
<add key="MchID" value="" />
<!--APIKey-->
<add key="APIKey" value="" />

2、調用統一下單API,直接上代碼瞭。

	/// <summary>
    /// Builds the request.
    /// </summary>
    /// <returns></returns>
    public static string BuildRequest()
    {
        var dicParam = CreateParam();

        var signString = CreateURLParamString(dicParam);
        var preString = signString + "&key=" + ConfigHelper.APIKey;
        var sign = Sign(preString, "utf-8").ToUpper();
        dicParam.Add("sign", sign);

        return BuildForm(dicParam);
    }    	

	/// <summary>
    /// Generates the out trade no.
    /// </summary>
    /// <returns></returns>
    private static string GenerateOutTradeNo()
    {
        var ran = new Random();
        return $"{ConfigHelper.MchID}{DateTime.Now:yyyyMMddHHmmss}{ran.Next(999)}";
    }

	/// <summary>
    /// Signs the specified prestr.
    /// </summary>
    /// <param name="prestr">The prestr.</param>
    /// <param name="_input_charset">The input charset.</param>
    /// <returns></returns>
    private static string Sign(string prestr, string _input_charset)
    {
        var sb = new StringBuilder(32);
        MD5 md5 = new MD5CryptoServiceProvider();
        var t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
        foreach (var t1 in t)
        {
            sb.Append(t1.ToString("x").PadLeft(2, '0'));
        }

        return sb.ToString();
    }

	/// <summary>
    /// Creates the parameter.
    /// </summary>
    /// <returns></returns>
    private static SortedDictionary<string, string> CreateParam()
    {
        const string amount = "1";
        double dubamount;
        double.TryParse(amount, out dubamount);
        var notify_url = ConfigHelper.WebSiteUrl + "/api/v1/testWeiXin"; //支付完成後的回調處理頁面
        const string detail = "xxxx";

        var dic = new SortedDictionary<string, string>
        {
            {"appid", ConfigHelper.AppID},//賬號ID
            {"mch_id", ConfigHelper.MchID},//商戶號
            {"nonce_str", Guid.NewGuid().ToString().Replace("-", "")},//隨機字符串
            {"body", detail}, //商品描述
            {"out_trade_no", GenerateOutTradeNo()},//商戶訂單號
            {"total_fee", (dubamount * 100).ToString(CultureInfo.InvariantCulture)},//總金額
            {"spbill_create_ip", GeneralHelper.GetIP()},//終端IP
            {"notify_url", notify_url},//通知地址
            {"trade_type", "APP"}//交易類型
        };
                      
        return dic;
    }

	/// <summary>
    /// Creates the URL parameter string.
    /// </summary>
    /// <param name="dicArray">The dic array.</param>
    /// <returns></returns>
    private static string CreateURLParamString(SortedDictionary<string, string> dicArray)
    {
        var prestr = new StringBuilder();
        foreach (var temp in dicArray.OrderBy(o => o.Key))
        {
            prestr.Append(temp.Key + "=" + temp.Value + "&");
        }

        var nLen = prestr.Length;
        prestr.Remove(nLen - 1, 1);
        return prestr.ToString();
    }

	/// <summary>
    /// Builds the form.
    /// </summary>
    /// <param name="dicParam">The dic parameter.</param>
    /// <returns></returns>
    private static string BuildForm(SortedDictionary<string, string> dicParam)
    {
        var sbXML = new StringBuilder();
        sbXML.Append("<xml>");
        foreach (var temp in dicParam)
        {
            sbXML.Append("<" + temp.Key + ">" + temp.Value + "</" + temp.Key + ">");
        }

        sbXML.Append("</xml>");
        return sbXML.ToString();
    }

	/// <summary>
    /// Froms the XML.
    /// </summary>
    /// <param name="xml">The XML.</param>
    /// <returns></returns>
    /// <exception cref="Exception">將空的xml串轉換為WxPayData不合法!</exception>
    public static SortedDictionary<string, string> FromXml(string xml)
    {
        var sortDic = new SortedDictionary<string, string>();
        if (string.IsNullOrEmpty(xml))
        {
            throw new Exception("將空的xml串轉換為WxPayData不合法!");
        }

        var xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        var xmlNode = xmlDoc.FirstChild;//獲取到根節點<xml>
        var nodes = xmlNode.ChildNodes;
        foreach (XmlNode xn in nodes)
        {
            var xe = (XmlElement)xn;

            if (!sortDic.ContainsKey(xe.Name))
                sortDic.Add(xe.Name, xe.InnerText);
        }
        return sortDic;
    }

	/// <summary>
    /// Posts the specified URL.
    /// </summary>
    /// <param name="url">The URL.</param>
    /// <param name="content">The content.</param>
    /// <param name="contentType">Type of the content.</param>
    /// <returns></returns>
    /// <exception cref="Exception">POST請求錯誤" + e</exception>
    public static string Post(string url, string content, string contentType = "application/x-www-form-urlencoded")
    {
        string result;
        try
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
                var stringContent = new StringContent(content, Encoding.UTF8);
                var response = client.PostAsync(url, stringContent).Result;
                result = response.Content.ReadAsStringAsync().Result;
            }
        }
        catch (Exception e)
        {
            throw new Exception("POST請求錯誤" + e);
        }
        return result;
    }

3、生成預付訂單,獲取prepay_id。

	/// <summary>
    /// Gets the value from dic.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="dic">The dic.</param>
    /// <param name="key">The key.</param>
    /// <returns></returns>
    public static T GetValueFromDic<T>(IDictionary<string, string> dic, string key)
    {
        string val;
        dic.TryGetValue(key, out val);

        var returnVal = default(T);
        if (val != null)
            returnVal = (T)Convert.ChangeType(val, typeof(T));

        return returnVal;
    }

	/// <summary>
    /// Builds the application pay.
    /// </summary>
    /// <param name="prepayid">The prepayid.</param>
    /// <returns></returns>
    public static string BuildAppPay(string prepayid)
    {
        var dicParam = CreateWapAndAppPayParam(prepayid);
        var signString = CreateURLParamString(dicParam);
        var preString = signString + "&key=" + ConfigHelper.APIKey;

        var sign = Sign(preString, "utf-8").ToUpper();
        dicParam.Add("sign", sign);

        return JsonConvert.SerializeObject(
            new
            {
                appid = dicParam["appid"],
                partnerid = dicParam["partnerid"],
                prepayid = dicParam["prepayid"],
                package = dicParam["package"],
                noncestr = dicParam["noncestr"],
                timestamp = dicParam["timestamp"],
                sign = dicParam["sign"]
            });
    }

	/// <summary>
    /// Creates the wap and application pay parameter.
    /// </summary>
    /// <param name="prepayId">The prepay identifier.</param>
    /// <returns></returns>
    private static SortedDictionary<string, string> CreateWapAndAppPayParam(string prepayId)
    {
        var dic = new SortedDictionary<string, string>
        {
            {"appid", ConfigHelper.AppID},//公眾賬號ID
            {"partnerid", ConfigHelper.MchID},//商戶號
            {"prepayid", prepayId},//預支付交易會話ID
            {"package", "Sign=WXPay"},//擴展字段
            {"noncestr", Guid.NewGuid().ToString().Replace("-", "")},//隨機字符串
            {
                "timestamp",
                (Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds)).ToString()
            }//時間戳
        };
        
        return dic;
    }

	/// <summary>
    /// Validatons the query result.
    /// </summary>
    /// <param name="dic">The dic.</param>
    /// <returns></returns>
    public static bool ValidatonQueryResult(SortedDictionary<string, string> dic)
    {
        var result = false;

        if (dic.ContainsKey("return_code") && dic.ContainsKey("return_code"))
        {
            if (dic["return_code"] == "SUCCESS" && dic["result_code"] == "SUCCESS")
                result = true;
        }

        if (result) return true;

        var sb = new StringBuilder();
        foreach (var item in dic.Keys)
        {
            sb.Append(item + ":" + dic[item] + "|");
        }

        return false;
    }

4、調用獲取支付信息,給到APP發起支付操作。

		var requestXml = WeiXinUtil.BuildRequest();
        var resultXml = WeiXinUtil.Post("https://api.mch.weixin.qq.com/pay/unifiedorder", requestXml);

        var dic = WeiXinUtil.FromXml(resultXml);

        string returnCode;
        dic.TryGetValue("return_code", out returnCode);

        if (returnCode == "SUCCESS")
        {
            var prepay_id = WeiXinUtil.GetValueFromDic<string>(dic, "prepay_id");
            if (!string.IsNullOrEmpty(prepay_id))
            {
                var payInfo = JsonConvert.DeserializeObject<WeiXinUtil.WxPayModel>(WeiXinUtil.BuildAppPay(prepay_id));

                json.Add(new JProperty("appid", payInfo.appid));
                json.Add(new JProperty("partnerid", payInfo.partnerid));
                json.Add(new JProperty("prepayid", payInfo.prepayid));
                json.Add(new JProperty("package", payInfo.package));
                json.Add(new JProperty("noncestr", payInfo.noncestr));
                json.Add(new JProperty("timestamp", payInfo.timestamp));
                json.Add(new JProperty("sign", payInfo.sign));
                json.Add(new JProperty("code", 0));
                json.Add(new JProperty("msg", "成功"));
                return this.Jsonp(json.ToString());
            }
            else
            {
                json.Add(new JProperty("code", 40028));
                json.Add(new JProperty("msg", "支付錯誤:" + WeiXinUtil.GetValueFromDic<string>(dic, "err_code_des")));
                return this.Jsonp(json.ToString());
            }
        }
        else
        {
            return this.Jsonp(ApiException.OrderFailed());
        }

5、APP支付完成,獲得回調信息,就OK瞭。

		var context = this.HttpContext;
        var request = context.Request;
        var verifyResult = false;
        var requestXml = WeiXinUtil.GetRequestXmlData(request);
        var dic = WeiXinUtil.FromXml(requestXml);
        var returnCode = WeiXinUtil.GetValueFromDic<string>(dic, "return_code");

        if (!string.IsNullOrEmpty(returnCode) && returnCode == "SUCCESS")//通訊成功
        {
            var result = WeiXinUtil.WePayNotifyValidation(dic);
            if (result)
            {
                var transactionid = WeiXinUtil.GetValueFromDic<string>(dic, "transaction_id");

                if (!string.IsNullOrEmpty(transactionid))
                {
                    var queryXml = WeiXinUtil.BuildQueryRequest(transactionid, dic);
                    var queryResult = WeiXinUtil.Post("https://api.mch.weixin.qq.com/pay/orderquery", queryXml);
                    var queryReturnDic = WeiXinUtil.FromXml(queryResult);

                    if (WeiXinUtil.ValidatonQueryResult(queryReturnDic))//查詢成功
                    {
                        verifyResult = true;
                        var status = WeiXinUtil.GetValueFromDic<string>(dic, "result_code");

                        if (!string.IsNullOrEmpty(status) && status == "SUCCESS")
                        {
                            var order = new Order()
                            {
                                OrderNumber = WeiXinUtil.GetValueFromDic<string>(dic, "out_trade_no"),
                                TransactionId = transactionid,
                                ProductPrice = WeiXinUtil.GetValueFromDic<decimal>(dic, "total_fee") / 100,
                                TradeType = WeiXinUtil.GetValueFromDic<string>(dic, "trade_type"),
                                BankType = WeiXinUtil.GetValueFromDic<string>(dic, "bank_type"),
                                PayDate = DateTime.Parse(WeiXinUtil.GetValueFromDic<string>(dic, "time_end")),
                                StatusId = 1,
                                IsPresent = false,
                                AddDate = DateTime.Now,
                                IsDelete = false
                            };

                            CURD.Add(order, ConfigHelper.WriteDB);

                            WeiXinUtil.BuildReturnXml("OK", "成功");
                        }
                    }
                    else
                        WeiXinUtil.BuildReturnXml("FAIL", "訂單查詢失敗");
                }
                else
                    WeiXinUtil.BuildReturnXml("FAIL", "支付結果中微信訂單號不存在");
            }
            else
                WeiXinUtil.BuildReturnXml("FAIL", "簽名失敗");
        }
        else
        {
            string returnmsg;
            dic.TryGetValue("return_msg", out returnmsg);
            throw new Exception("異步通知錯誤:" + returnmsg);
        }

        return verifyResult;

	/// <summary>
    /// Gets the request XML data.
    /// </summary>
    /// <param name="request">The request.</param>
    /// <returns></returns>
    public static string GetRequestXmlData(HttpRequestBase request)
    {
        var stream = request.InputStream;
        int count;
        var buffer = new byte[1024];
        var builder = new StringBuilder();
        while ((count = stream.Read(buffer, 0, 1024)) > 0)
        {
            builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
        }
        stream.Flush();
        stream.Close();

        return builder.ToString();
    }

    /// <summary>
    /// Wes the pay notify validation.
    /// </summary>
    /// <param name="dic">The dic.</param>
    /// <returns></returns>
    public static bool WePayNotifyValidation(SortedDictionary<string, string> dic)
    {
        var sign = GetValueFromDic<string>(dic, "sign");
        if (dic.ContainsKey("sign"))
        {
            dic.Remove("sign");
        }

        var tradeType = GetValueFromDic<string>(dic, "trade_type");
        var preString = CreateURLParamString(dic);

        if (string.IsNullOrEmpty(tradeType))
        {
            var preSignString = preString + "&key=" + ConfigHelper.APIKey;
            var signString = Sign(preSignString, "utf-8").ToUpper();
            return signString == sign;
        }
        else
            return false;
    }

	/// <summary>
    /// Builds the query request.
    /// </summary>
    /// <param name="transactionId">The transaction identifier.</param>
    /// <param name="dic">The dic.</param>
    /// <returns></returns>
    public static string BuildQueryRequest(string transactionId, SortedDictionary<string, string> dic)
    {
        var dicParam = CreateQueryParam(transactionId);
        var signString = CreateURLParamString(dicParam);
        var key = ConfigHelper.APIKey;
        var preString = signString + "&key=" + key;
        var sign = Sign(preString, "utf-8").ToUpper();
        dicParam.Add("sign", sign);

        return BuildForm(dicParam);
    }

    /// <summary>
    /// Creates the query parameter.
    /// </summary>
    /// <param name="transactionId">The transaction identifier.</param>
    /// <returns></returns>
    private static SortedDictionary<string, string> CreateQueryParam(string transactionId)
    {
        var dic = new SortedDictionary<string, string>
        {
            {"appid", ConfigHelper.AppID},//公眾賬號ID
            {"mch_id", ConfigHelper.MchID},//商戶號
            {"nonce_str", Guid.NewGuid().ToString().Replace("-", "")},//隨機字符串
            {"transaction_id", transactionId}//微信訂單號
        };
        return dic;
    }

	/// <summary>
    /// Builds the return XML.
    /// </summary>
    /// <param name="code">The code.</param>
    /// <param name="returnMsg">The return MSG.</param>
    /// <returns></returns>
    public static string BuildReturnXml(string code, string returnMsg)
    {
        return
            $"<xml><return_code><![CDATA[[code]]]></return_code><return_msg><![CDATA[{returnMsg}]]></return_msg></xml>";
    }

6、總結:這個可以直接拿來用瞭,反反復復測試瞭很多遍,遇到的問題有關於錢,還有簽名的問題,調試都解決瞭。繼續解決問題,積累經驗。

到此這篇關於ASP.Net項目中實現微信APP支付功能的文章就介紹到這瞭。希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: