阅读背景:

微信App支付通知验签

来源:互联网 

微信异步通知:

[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();
        }
[AcceptVerbs("POST")]
        pub



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: