posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

微信异步通知:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[AcceptVerbs("POST")]
        public void Notify()
        {
            //编码(101-登录无效,102-账号无效,200-成功,201-失败,202~299-其他原因1-99,300-无效提交方式,400-无效参数)
            MessagesDataCodeModel json = new MessagesDataCodeModel(false, "无效参数", 401);
            int notify_id = 0;
            string result = "failed";
            try
            {
                //得到微信推送来的xml数据
                Stream s = System.Web.HttpContext.Current.Request.InputStream;
                byte[] b = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
                string postStr = Encoding.UTF8.GetString(b);
 
                SortedDictionary<string, string> requestXML = Common.TenpayUtil.GetInfoFromXml(postStr);
                if (requestXML != null && requestXML.Count > 0)
                {
                    Models.WeiXinNotifyRecords model = GetNotifyModel(requestXML);//将xml数据转换为Model
                    model.remark = "";//postStr;
                    model.CreateDate = DateTime.Now;
                    notify_id = WeiXinNotifyRecordsBLL.Append(model);//记录微信通知
                    model.ID = notify_id;
 
                    #region 验签
                    //微信返回的签名字符串
                    string sign = requestXML["sign"];
                    requestXML.Remove("sign");
                    //待签名字符串
                    string signStr = AlipaySignature.GetSignContent(requestXML) + "&key=" + Common.ConfigApi.WeiXinPay_API_Key;//借用阿里的方法
                    string newsign = Utils.GetMD5(signStr).ToUpper();//MD5加密,转大写
                    bool ValidateSign = sign == newsign;//验证签名是否一致
                    #endregion
 
                    #region 处理订单
                    if (ValidateSign)
                    {
                        Models.TradeInfo tradeInfo = TradeInfoBLL.GetEntityByTradeNo(model.out_trade_no);
                        if (tradeInfo != null && model.appid == ConfigApi.WeiXinPay_App_app_id && model.mch_id == ConfigApi.WeiXinPay_App_mch_id)
                        {
                            result = "success";//TODO:处理订单逻辑,完成后 result="success"
                        }
                        else
                        {
                            result = "TradeError";
                            logger.Error("WeiXinPayController.Notify【订单数据与通知数据不符】");
                        }
                    }
                    else
                    {
                        result = "CheckSignError";
                        logger.Error("WeiXinPayController.Notify【验签失败】");
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                logger.Error("WeiXinPayController.Notify【程序异常】", ex);
                result = "exception";
            }
 
            string xmlstr = @"<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
            if (result != "success")
            {
                xmlstr = @"<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[FAIL]]></return_msg></xml>";
            }
            HttpContext.Current.Response.Write(xmlstr);
            HttpContext.Current.Response.End();
        }

  

把XML数据转换为SortedDictionary<string, string>集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// <summary>
        /// 把XML数据转换为SortedDictionary<string, string>集合
        /// </summary>
        /// <param name="strxml"></param>
        /// <returns></returns>
        public static SortedDictionary<string, string> GetInfoFromXml(string xmlstring)
        {
            SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlstring);
                XmlElement root = doc.DocumentElement;
                int len = root.ChildNodes.Count;
                for (int i = 0; i < len; i++)
                {
                    string name = root.ChildNodes[i].Name;
                    if (!sParams.ContainsKey(name))
                    {
                        sParams.Add(name.Trim(), root.ChildNodes[i].InnerText.Trim());
                    }
                }
            }
            catch { }
            return sParams;
        }

  

把参数排序后拼接,得到签名字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static string GetSignContent(IDictionary<string, string> parameters)
       {
           // 第一步:把字典按Key的字母顺序排序
           IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
           IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
 
           // 第二步:把所有参数名和参数值串在一起
           StringBuilder query = new StringBuilder("");
           while (dem.MoveNext())
           {
               string key = dem.Current.Key;
               string value = dem.Current.Value;
               if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
               {
                   query.Append(key).Append("=").Append(value).Append("&");
               }
           }
           string content = query.ToString().Substring(0, query.Length - 1);
 
           return content;
       }

  

签名算法文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3

posted on   邢帅杰  阅读(1984)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示