ASP.Net项目中实现微信APP支付功能

最近挺忙的,没时间写东西。然后在弄微信APP支付,网上的搜索一趟,都比较凌乱,我也遇到一些坑,不过也算弄好了,记录分享一下。

1、准备各种调用接口需要的参数,配置app.config。








2、调用统一下单API,直接上代码了。

	/// 
    /// Builds the request.
    /// 
    /// 
    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);
    }    	

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

	/// 
    /// Signs the specified prestr.
    /// 
    /// The prestr.
    /// The input charset.
    /// 
    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();
    }

	/// 
    /// Creates the parameter.
    /// 
    /// 
    private static SortedDictionary 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
        {
            {"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;
    }

	/// 
    /// Creates the URL parameter string.
    /// 
    /// The dic array.
    /// 
    private static string CreateURLParamString(SortedDictionary 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();
    }

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

        sbXML.Append("");
        return sbXML.ToString();
    }

	/// 
    /// Froms the XML.
    /// 
    /// The XML.
    /// 
    /// 将空的xml串转换为WxPayData不合法!
    public static SortedDictionary FromXml(string xml)
    {
        var sortDic = new SortedDictionary();
        if (string.IsNullOrEmpty(xml))
        {
            throw new Exception("将空的xml串转换为WxPayData不合法!");
        }

        var xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        var xmlNode = xmlDoc.FirstChild;//获取到根节点
        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;
    }

	/// 
    /// Posts the specified URL.
    /// 
    /// The URL.
    /// The content.
    /// Type of the content.
    /// 
    /// POST请求错误" + e
    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。

	/// 
    /// Gets the value from dic.
    /// 
    /// 
    /// The dic.
    /// The key.
    /// 
    public static T GetValueFromDic(IDictionary 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;
    }

	/// 
    /// Builds the application pay.
    /// 
    /// The prepayid.
    /// 
    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"]
            });
    }

	/// 
    /// Creates the wap and application pay parameter.
    /// 
    /// The prepay identifier.
    /// 
    private static SortedDictionary CreateWapAndAppPayParam(string prepayId)
    {
        var dic = new SortedDictionary
        {
            {"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;
    }

	/// 
    /// Validatons the query result.
    /// 
    /// The dic.
    /// 
    public static bool ValidatonQueryResult(SortedDictionary 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(dic, "prepay_id");
            if (!string.IsNullOrEmpty(prepay_id))
            {
                var payInfo = JsonConvert.DeserializeObject(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(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(dic, "return_code");

        if (!string.IsNullOrEmpty(returnCode) && returnCode == "SUCCESS")//通讯成功
        {
            var result = WeiXinUtil.WePayNotifyValidation(dic);
            if (result)
            {
                var transactionid = WeiXinUtil.GetValueFromDic(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(dic, "result_code");

                        if (!string.IsNullOrEmpty(status) && status == "SUCCESS")
                        {
                            var order = new Order()
                            {
                                OrderNumber = WeiXinUtil.GetValueFromDic(dic, "out_trade_no"),
                                TransactionId = transactionid,
                                ProductPrice = WeiXinUtil.GetValueFromDic(dic, "total_fee") / 100,
                                TradeType = WeiXinUtil.GetValueFromDic(dic, "trade_type"),
                                BankType = WeiXinUtil.GetValueFromDic(dic, "bank_type"),
                                PayDate = DateTime.Parse(WeiXinUtil.GetValueFromDic(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;

	/// 
    /// Gets the request XML data.
    /// 
    /// The request.
    /// 
    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();
    }

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

        var tradeType = GetValueFromDic(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;
    }

	/// 
    /// Builds the query request.
    /// 
    /// The transaction identifier.
    /// The dic.
    /// 
    public static string BuildQueryRequest(string transactionId, SortedDictionary 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);
    }

    /// 
    /// Creates the query parameter.
    /// 
    /// The transaction identifier.
    /// 
    private static SortedDictionary CreateQueryParam(string transactionId)
    {
        var dic = new SortedDictionary
        {
            {"appid", ConfigHelper.AppID},//公众账号ID
            {"mch_id", ConfigHelper.MchID},//商户号
            {"nonce_str", Guid.NewGuid().ToString().Replace("-", "")},//随机字符串
            {"transaction_id", transactionId}//微信订单号
        };
        return dic;
    }

	/// 
    /// Builds the return XML.
    /// 
    /// The code.
    /// The return MSG.
    /// 
    public static string BuildReturnXml(string code, string returnMsg)
    {
        return
            $"";
    }

6、总结:这个可以直接拿来用了,反反复复测试了很多遍,遇到的问题有关于钱,还有签名的问题,调试都解决了。继续解决问题,积累经验。

到此这篇关于ASP.Net项目中实现微信APP支付功能的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(ASP.Net项目中实现微信APP支付功能)