Exemplo n.º 1
0
        public static string NotifyDataFromContext(HttpContext context, ref bool IsOk, ref WxPayData WPD)
        {
            string Result = "";

            //接收从微信后台POST过来的数据
            System.IO.Stream s = context.Request.InputStream;
            int count          = 0;

            byte[]        buffer  = new byte[1024];
            StringBuilder builder = new StringBuilder();

            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();

            Business.WeiXinPay.Log("通知内容:" + builder.ToString());

            //转换数据格式并验证签名
            WxPayData data = new WxPayData();

            try
            {
                data.FromXml(builder.ToString());
                WPD    = data;
                IsOk   = true;
                Result = data.MakeSign();
                Business.WeiXinPay.Log("签名码之前数据:" + data.ToString());
                Business.WeiXinPay.Log("签名码:" + Result.ToString());
                if (data.GetValue("sign").ToString() == Result)
                {
                    Result = "签名验证成功";
                    IsOk   = true;
                }
                else
                {
                    Result = "签名验证失败";
                    IsOk   = false;
                }
            }
            catch (WxPayException ex)
            {
                //若签名错误,则立即返回结果给微信支付后台
                Result = ex.Message.ToString();
            }

            return(Result);
        }