123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
- using System.Xml;
- using LitJson;
- using WayneCloud.Models.Models;
- namespace Wechat.PayAPI
- {
-
-
-
-
-
-
- public class WxPayData
- {
- public WxPayData()
- {
- }
-
- private SortedDictionary<string, object> m_values = new SortedDictionary<string, object>();
-
- public void SetValue(string key, object value)
- {
- m_values[key] = value;
- }
-
- public object GetValue(string key)
- {
- object o = null;
- m_values.TryGetValue(key, out o);
- return o;
- }
-
- public bool IsSet(string key)
- {
- object o = null;
- m_values.TryGetValue(key, out o);
- if (null != o)
- return true;
- else
- return false;
- }
-
- public string ToXml()
- {
-
- if (0 == m_values.Count)
- {
- Log.Error(this.GetType().ToString(), "WxPayData数据为空!");
- throw new WxPayException("WxPayData数据为空!");
- }
- string xml = "<xml>";
- foreach (KeyValuePair<string, object> pair in m_values)
- {
-
- if (pair.Value == null)
- {
- continue;
- }
- 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
- {
- Log.Error(this.GetType().ToString(), "WxPayData字段数据类型错误!");
- throw new WxPayException("WxPayData字段数据类型错误!");
- }
- }
- xml += "</xml>";
- return xml;
- }
-
- public SortedDictionary<string, object> FromXml(WxPayConfig config, string xml)
- {
- if (string.IsNullOrEmpty(xml))
- {
- Log.Error(this.GetType().ToString(), "将空的xml串转换为WxPayData不合法!");
- throw new WxPayException("将空的xml串转换为WxPayData不合法!");
- }
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(xml);
- XmlNode xmlNode = xmlDoc.FirstChild;
- XmlNodeList nodes = xmlNode.ChildNodes;
- foreach (XmlNode xn in nodes)
- {
- XmlElement xe = (XmlElement)xn;
- m_values[xe.Name] = xe.InnerText;
- }
-
- try
- {
-
- if(m_values["return_code"] != "SUCCESS")
- {
- return m_values;
- }
- CheckSign(config);
- }
- catch(WxPayException ex)
- {
- throw new WxPayException(ex.Message);
- }
- return m_values;
- }
-
- public string ToUrl()
- {
- string buff = "";
- foreach (KeyValuePair<string, object> pair in m_values)
- {
- if (pair.Value != null)
- {
- if (pair.Key != "sign" && pair.Value.ToString() != "")
- {
- buff += pair.Key + "=" + pair.Value + "&";
- }
- }
- }
- buff = buff.Trim('&');
- return buff;
- }
-
- public string ToJson()
- {
- string jsonStr = JsonMapper.ToJson(m_values);
- return jsonStr;
- }
-
- public string ToPrintStr()
- {
- string str = "";
- foreach (KeyValuePair<string, object> pair in m_values)
- {
- if (pair.Value == null)
- {
- Log.Error(this.GetType().ToString(), "WxPayData内部含有值为null的字段!");
- throw new WxPayException("WxPayData内部含有值为null的字段!");
- }
- str += string.Format("{0}={1}<br>", pair.Key, pair.Value.ToString());
- }
- Log.Debug(this.GetType().ToString(), "Print in Web Page : " + str);
- return str;
- }
-
- public string MakeSign(WxPayConfig config)
- {
-
- string str = ToUrl();
-
- str += "&key=" + config.KEY;
-
- var md5 = MD5.Create();
- var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
- var sb = new StringBuilder();
- foreach (byte b in bs)
- {
- sb.Append(b.ToString("x2"));
- }
-
- return sb.ToString().ToUpper();
- }
-
- public bool CheckSign(WxPayConfig config)
- {
-
- if (!IsSet("sign"))
- {
- Log.Error(this.GetType().ToString(), "WxPayData签名存在但不合法!");
- throw new WxPayException("WxPayData签名存在但不合法!");
- }
-
- else if(GetValue("sign") == null || GetValue("sign").ToString() == "")
- {
- Log.Error(this.GetType().ToString(), "WxPayData签名存在但不合法!");
- throw new WxPayException("WxPayData签名存在但不合法!");
- }
-
- string return_sign = GetValue("sign").ToString();
-
- string cal_sign = MakeSign(config);
- if (cal_sign == return_sign)
- {
- return true;
- }
- Log.Error(this.GetType().ToString(), "WxPayData签名验证错误!");
- throw new WxPayException("WxPayData签名验证错误!");
- }
-
- public SortedDictionary<string, object> GetValues()
- {
- return m_values;
- }
- }
- public class WxPayDataWithResult
- {
- public WxPayData PayData {get; set;}
- public int Status { get; set; }
- }
- }
|