Exemplo n.º 1
0
Arquivo: Data.cs Projeto: soon14/vzan
        /**
         *
         * 检测签名是否正确
         * 正确返回true,错误抛异常
         */
        public bool CheckSign()
        {
            //如果没有设置签名,则跳过检测
            if (!IsSet("sign"))
            {
                WxPayException exx = new WxPayException("WxPayData签名存在但不合法!");
                log4net.LogHelper.WriteError(this.GetType(), exx);
                throw exx;
            }
            //如果设置了签名但是签名为空,则抛异常
            else if (GetValue("sign") == null || GetValue("sign").ToString() == "")
            {
                WxPayException exx = new WxPayException("WxPayData签名存在但不合法!");
                log4net.LogHelper.WriteError(this.GetType(), exx);
                throw exx;
            }

            //获取接收到的签名
            string return_sign = GetValue("sign").ToString();
            //第三方支付需要查询KEY的值
            string key = string.Empty;

            if (IsSet("appid"))
            {
                string           appid   = GetValue("appid").ToString();
                PayCenterSetting setting = new PayCenterSettingBLL().GetPayCenterSetting(appid);
                if (setting != null && setting.Id > 0)
                {
                    key = setting.Key;
                }
            }
            //在本地计算新的签名
            string cal_sign = MakeSign(key);

            if (cal_sign == return_sign)
            {
                return(true);
            }

            WxPayException ex = new WxPayException("WxPayData签名验证错误!");

            log4net.LogHelper.WriteError(this.GetType(), ex);
            throw ex;;
        }
Exemplo n.º 2
0
Arquivo: Data.cs Projeto: soon14/vzan
        //public SortedDictionary<string, object> FromXml(string xml, bool checksign)
        //{
        //    if (checksign)
        //    {
        //        return FromXml(xml);
        //    }
        //    if (string.IsNullOrEmpty(xml))
        //    {
        //        WxPayException ex = new WxPayException("将空的xml串转换为WxPayData不合法!");
        //        log4net.LogHelper.WriteError(this.GetType(), ex);
        //        throw ex;
        //    }

        //    XmlDocument xmlDoc = new XmlDocument();
        //    xmlDoc.LoadXml(xml);
        //    XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
        //    XmlNodeList nodes = xmlNode.ChildNodes;
        //    foreach (XmlNode xn in nodes)
        //    {
        //        XmlElement xe = (XmlElement)xn;
        //        m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中
        //    }
        //    return m_values;
        //}

        /**
         * @Dictionary格式转化成url参数格式
         * @ return url格式串, 该串不包含sign字段值
         */
        public string ToUrl()
        {
            string buff = "";

            foreach (KeyValuePair <string, object> pair in m_values)
            {
                if (pair.Value == null)
                {
                    WxPayException ex = new WxPayException("WxPayData内部含有值为null的字段!" + pair.Key);
                    log4net.LogHelper.WriteError(this.GetType(), ex);
                    throw ex;
                }

                if (pair.Key != "sign" && pair.Value.ToString() != "")
                {
                    buff += pair.Key + "=" + pair.Value + "&";
                }
            }
            buff = buff.Trim('&');
            return(buff);
        }
Exemplo n.º 3
0
Arquivo: Data.cs Projeto: soon14/vzan
        /**
         * @将Dictionary转成xml
         * @return 经转换得到的xml串
         * @throws WxPayException
         **/
        public string ToXml()
        {
            //数据为空时不能转化为xml格式
            if (0 == m_values.Count)
            {
                WxPayException ex = new WxPayException("WxPayData数据为空!");
                log4net.LogHelper.WriteError(this.GetType(), ex);
                throw ex;
            }

            string xml = "<xml>";

            foreach (KeyValuePair <string, object> pair in m_values)
            {
                //字段值不能为null,会影响后续流程
                if (pair.Value == null)
                {
                    WxPayException ex = new WxPayException("WxPayData内部含有值为null的字段!");
                    log4net.LogHelper.WriteError(this.GetType(), ex);
                    throw ex;
                }

                if (pair.Value.GetType() == typeof(int))
                {
                    xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
                }
                else if (pair.Value.GetType() == typeof(string))
                {
                    xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
                }
                else//除了string和int类型不能含有其他数据类型
                {
                    WxPayException ex = new WxPayException("WxPayData字段数据类型错误!");
                    log4net.LogHelper.WriteError(this.GetType(), ex);
                    throw ex;
                }
            }
            xml += "</xml>";
            return(xml);
        }
Exemplo n.º 4
0
Arquivo: Data.cs Projeto: soon14/vzan
        /**
         * @将xml转为WxPayData对象并返回对象内部的数据
         * @param string 待转换的xml串
         * @return 经转换得到的Dictionary
         * @throws WxPayException
         */
        public SortedDictionary <string, object> FromXml(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                WxPayException ex = new WxPayException("将空的xml串转换为WxPayData不合法!");
                log4net.LogHelper.WriteError(this.GetType(), ex);
                throw ex;
            }

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml);
            XmlNode     xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
            XmlNodeList nodes   = xmlNode.ChildNodes;

            foreach (XmlNode xn in nodes)
            {
                XmlElement xe = (XmlElement)xn;
                m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中
            }

            try
            {
                //2015-06-29 错误是没有签名
                if ((string)m_values["return_code"] != "SUCCESS")
                {
                    return(m_values);
                }
                CheckSign();//验证签名,不通过会抛异常
            }
            catch (WxPayException ex)
            {
                throw new WxPayException(ex.Message);
            }

            return(m_values);
        }