using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Web; using System.Web.Script.Serialization; namespace ApplyTest { public class Common { public enum RetCode { 成功 = 100, 订单待支付 = 101, 请求参数错误 = 201, 安全验证不通过 = 202, 会员已经注销 = 203, 登录失败, 用户名或密码错误 = 204, 用户不存在 = 205, 系统异常 = 999 } #region 交易配置相关属性 //客户号 //private const string custom_no = "linkea-test"; ////校验码秘钥 //private const string shaCode = "DAD7640691B9FAD3665217887F384B6F"; //AppCode private const string app_key = "100083"; //app_secret 秘钥 private const string app_secret = "457fbfa0-0076-44c2-b45b-2b3aa84d6750"; //工作秘钥 private const string work_key = "D905EEAD570840AC96A34DEB42A29B67"; ////Ksn //private const string ksn = "66660000000001"; ////渠道号 //private const string channel_no = "ALIPAY"; ////微信 WECHAT_PAY ////交易类型 //private const string paysEnum = "ALIPAY"; //请求Url private const string postUrl = "http://gateway.ringou.cn/opengw/router/rest.htm"; //http://101.68.65.38:7002/opengw/router/rest.htm"; #endregion public static String Get_Http(String a_strUrl, int timeout) { string strResult; try { HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl); myReq.Timeout = timeout; HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); Stream myStream = HttpWResp.GetResponseStream(); StreamReader sr = new StreamReader(myStream, Encoding.UTF8); StringBuilder strBuilder = new StringBuilder(); while (-1 != sr.Peek()) { strBuilder.Append(sr.ReadLine()); } strResult = strBuilder.ToString(); } catch (Exception exp) { strResult = "错误:" + exp.Message; } return strResult; } public static string GetSha256(string str) { SHA256 sha256 = new SHA256CryptoServiceProvider();//建立一个SHA256 byte[] source = Encoding.UTF8.GetBytes(str);//将字符串转为Byte[] byte[] crypto = sha256.ComputeHash(source);//进行SHA256加密 string byteStr = string.Empty; if (crypto != null || crypto.Length > 0) { foreach (var item in crypto) { byteStr += string.Format("{0:X2}", item); } } return byteStr; } public static string GetMD5(string s) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(s)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } #region BubbleSort public static string[] BubbleSort(string[] r) { int i, j; string temp; bool exchange; for (i = 0; i < r.Length; i++) { exchange = false; for (j = r.Length - 2; j >= i; j--) { if (String.CompareOrdinal(r[j + 1], r[j]) < 0) { temp = r[j + 1]; r[j + 1] = r[j]; r[j] = temp; exchange = true; } } if (!exchange) { break; } } return r; } #endregion public static string CreatUrl(string Amount, string dynamic_id, string out_trade_no, string payType, string subject = "牵软网络") { int i; string custom_no = "", shaCode = "", ksn = "", channel_no = "", paysEnum = ""; switch (payType) { case "ALIPAY": custom_no = "linkea-qianruan-alipay"; //校验码秘钥 shaCode = "9DACF5FBDCF7EE8D5CFFC32E80ADFF51"; //Ksn ksn = "678804280000001"; //渠道号 channel_no = "ALIPAY"; //交易类型 paysEnum = "ALIPAY"; break; case "WECHAT_PAY": custom_no = "linkea-qianruan-wechat"; //校验码秘钥 shaCode = "3C9D77299B35012BC02BDAB8B1000C29"; //Ksn ksn = "678804280000002"; //渠道号 channel_no = "WECHAT_PAY"; //交易类型 paysEnum = "WECHAT_PAY"; break; } //构造数组; string[] Oristr ={ "access_token=", "app_key="+ app_key, "channel_no="+payType, "check_code="+ GetSha256(custom_no + payType + Amount + shaCode), "custom_no="+ custom_no, "dynamic_id="+dynamic_id, "dynamic_id_type="+ "bar_code", "format="+ "json", "ksn="+ ksn, "method="+ "linkea.pays.thirdpay.createAndPay", "out_trade_no="+ out_trade_no, "pays_enum="+ payType, "product_code="+ "BARCODE_PAY_OFFLINE", "subject="+ subject, "timestamp="+ System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "total_fee="+ Amount, "v="+"1.1", "work_key="+ work_key, "sign_method=md5" }; //进行排序; string[] Sortedstr = BubbleSort(Oristr); //构造待md5摘要字符串 ; StringBuilder prestr = new StringBuilder(); for (i = 0; i < Sortedstr.Length; i++) { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i]); } else { prestr.Append(Sortedstr[i] + "&"); } } //生成Md5摘要; string jiami = app_secret + prestr.ToString().Replace("=", "").Replace("&", "") + app_secret; string sign = GetMD5(jiami); //构造支付Url; char[] delimiterChars = { '=' }; StringBuilder parameter = new StringBuilder(); parameter.Append(postUrl + "?"); for (i = 0; i < Sortedstr.Length; i++) { parameter.Append(Sortedstr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].Split(delimiterChars)[1]) + "&"); } parameter.Append("&sign=" + sign.ToUpper()); //返回支付Url; return parameter.ToString(); } public static string trade_query(string out_trade_no, string PAYTYPE) { string custom_no = "", shaCode = "", ksn = "", channel_no = "", paysEnum = ""; switch (PAYTYPE) { case "ALIPAY": custom_no = "linkea-qianruan-alipay"; //校验码秘钥 shaCode = "9DACF5FBDCF7EE8D5CFFC32E80ADFF51"; //Ksn ksn = "678804280000001"; //渠道号 channel_no = "ALIPAY"; //交易类型 paysEnum = "ALIPAY"; break; case "WECHAT_PAY": custom_no = "linkea-qianruan-wechat"; //校验码秘钥 shaCode = "3C9D77299B35012BC02BDAB8B1000C29"; //Ksn ksn = "678804280000002"; //渠道号 channel_no = "WECHAT_PAY"; //交易类型 paysEnum = "WECHAT_PAY"; break; } string[] Oristr ={ "channel_no="+ PAYTYPE, "app_key="+ app_key, "check_code="+ GetSha256(custom_no + PAYTYPE + shaCode), "custom_no="+custom_no, "format="+ "json", "work_key=" +work_key, "ksn="+ksn, "method="+ "linkea.pays.thirdpay.query", "out_trade_no="+out_trade_no, "pays_enum="+PAYTYPE, "timestamp="+ System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "sign_method=md5", "v="+"1.1" }; //进行排序; Oristr = BubbleSort(Oristr); StringBuilder prestr = new StringBuilder(); for (int i = 0; i < Oristr.Length; i++) { if (i == Oristr.Length - 1) { prestr.Append(Oristr[i]); } else { prestr.Append(Oristr[i] + "&"); } } string jiami = app_secret + prestr.ToString().Replace("=", "").Replace("&", "") + app_secret; string sign = GetMD5(jiami); //构造 char[] delimiterChars = { '=' }; StringBuilder parameter = new StringBuilder(); parameter.Append(postUrl + "?"); for (int i = 0; i < Oristr.Length; i++) { parameter.Append(Oristr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Oristr[i].Split(delimiterChars)[1]) + "&"); } parameter.Append("&sign=" + sign.ToUpper()); //返回支付Url; return parameter.ToString(); } } public class ResultObject_response { public string result_code { get; set; } public string result_msg { get; set; } public string sign { get; set; } } public class PayCreateObject { /// /// 流水码 /// public string trade_no { get; set; } /// /// 门店内码 /// public string shopcode { get; set; } /// /// 机器内码 /// public string machinecode { get; set; } /// /// 时间戳 /// public string timestamp { get; set; } /// /// 金额 /// public string amount { get; set; } /// /// 支付类型 /// public string paymode { get; set; } public string custom_no { get; set; } public string trade_desc { get; set; } public string signcode { get; set; } } public class PayCreateResult { public string trade_no { get; set; } //public string shopcode //{ // get; // set; //} //public string machinecode //{ // get; // set; //} //public string timestamp //{ // get; // set; //} public string trade_num { get; set; } public string result_code { get; set; } public string result_desc { get; set; } } public class PayQueryObject { public string trade_no { get; set; } public string shopcode { get; set; } public string machinecode { get; set; } public string timestamp { get; set; } public string paymode { get; set; } public string signcode { get; set; } } public class PayQueryResult { public string trade_no { get; set; } public string trade_num { get; set; } //public string shopcode //{ // get; // set; //} //public string machinecode //{ // get; // set; //} //public string timestamp //{ // get; // set; //} public string result_code { get; set; } public string result_desc { get; set; } } public class JsonHelper { public static string ArrayToJson(string[] strs) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.Length; i++) { sb.AppendFormat("'{0}':'{1}',", i + 1, strs[i]); } if (sb.Length > 0) { return ("{" + sb.ToString().TrimEnd(new char[] { ',' }) + "}"); } return ""; } public static string ArrayToJson(List list, string propertyname) { StringBuilder sb = new StringBuilder(); if (list.Count > 0) { sb.Append("[{\""); sb.Append(propertyname); sb.Append("\":["); foreach (T t in list) { sb.Append("\""); sb.Append(t.ToString()); sb.Append("\","); } return (sb.ToString().TrimEnd(new char[] { ',' }) + "]}]"); } return ""; } public static string DataRowToJson(DataRow dr) { StringBuilder sb = new StringBuilder(); sb.Append("{"); foreach (DataColumn dc in dr.Table.Columns) { sb.Append("\""); sb.Append(dc.ColumnName); sb.Append("\":\""); if (((dr[dc] != null) && (dr[dc] != DBNull.Value)) && (dr[dc].ToString() != "")) { sb.Append(dr[dc]); } else { sb.Append(" "); } sb.Append("\","); } sb = sb.Remove(0, sb.Length - 1); sb.Append("},"); return sb.ToString(); } private static List GetObjectProperty(object o) { List propertyslist = new List(); PropertyInfo[] propertys = o.GetType().GetProperties(); foreach (PropertyInfo p in propertys) { propertyslist.Add(string.Concat(new object[] { "\"", p.Name.ToString(), "\":\"", p.GetValue(o, null), "\"" })); } return propertyslist; } public static string HashtableToJson(Hashtable data, string dtName) { StringBuilder sb = new StringBuilder(); sb.Append("{\""); sb.Append(dtName); sb.Append("\":[{"); foreach (object key in data.Keys) { object value = data[key]; sb.Append("\""); sb.Append(key); sb.Append("\":\""); if (!(string.IsNullOrEmpty(value.ToString()) || (value == DBNull.Value))) { sb.Append(value).Replace(@"\", "/"); } else { sb.Append(" "); } sb.Append("\","); } sb = sb.Remove(sb.Length - 1, 1); sb.Append("}]}"); return JsonCharFilter(sb.ToString()); } public static string IListToJson(IList list, bool FlagBool = false) { StringBuilder sb = new StringBuilder(); sb.Append("["); foreach (T t in list) { sb.Append(ObjectToJson(t, FlagBool) + ","); } return (sb.ToString().TrimEnd(new char[] { ',' }) + "]"); } public static string IListToJson(IList list, string ClassName) { StringBuilder sb = new StringBuilder(); sb.Append("{\"" + ClassName + "\":["); foreach (T t in list) { sb.Append(ObjectToJson(t) + ","); } return (sb.ToString().TrimEnd(new char[] { ',' }) + "]}"); } private static string JsonCharFilter(string sourceStr) { return sourceStr; } public static string ListToJson(List objlist, string jsonName, bool ObjectName = true) { string result = string.Empty; if (ObjectName) { result += "{"; if (jsonName.Equals(string.Empty)) { object o = objlist[0]; jsonName = o.GetType().ToString(); result = result + "\"" + jsonName + "\":["; } else { result = result + "\"" + jsonName + "\":["; } } else { result = "["; } bool firstline = true; foreach (object oo in objlist) { if (!firstline) { result = result + "," + ObjectToJson(oo); } else { result = result + ObjectToJson(oo); firstline = false; } } if (ObjectName) { return (result + "]}"); } else { return (result + "]"); } } private static string ObjectToJson(object o) { string result = "{"; List ls_propertys = new List(); ls_propertys = GetObjectProperty(o); foreach (string str_property in ls_propertys) { if (result.Equals("{")) { result = result + str_property; } else { result = result + "," + str_property; } } return (result + "}"); } public static string ObjectToJson(T t, bool FlagBool = false) { FlagBool = false; StringBuilder sb = new StringBuilder(); string json = ""; if (t == null) { return json; } sb.Append("{"); PropertyInfo[] properties = t.GetType().GetProperties(); foreach (PropertyInfo pi in properties) { sb.Append("\"" + pi.Name.ToString() + "\""); sb.Append(":"); if (FlagBool == true && pi.PropertyType.ToString() == "System.Boolean") { sb.Append(pi.GetValue(t, null)); } else { sb.Append("\"" + pi.GetValue(t, null) + "\""); } sb.Append(","); } return (sb.ToString().TrimEnd(new char[] { ',' }) + "}"); } public static string ObjectToJson(T t, string ClassName) { StringBuilder sb = new StringBuilder(); sb.Append("{\"" + ClassName + "\":["); string json = ""; if (t == null) { return json; } sb.Append("{"); PropertyInfo[] properties = t.GetType().GetProperties(); foreach (PropertyInfo pi in properties) { sb.Append("\"" + pi.Name.ToString() + "\""); sb.Append(":"); sb.Append("\"" + pi.GetValue(t, null) + "\""); sb.Append(","); } return (sb.ToString().TrimEnd(new char[] { ',' }) + "}]}"); } public static string ObjectToJson(IList IL, string jsonName) { StringBuilder Json = new StringBuilder(); Json.Append("{\"" + jsonName + "\":["); if (IL.Count > 0) { for (int i = 0; i < IL.Count; i++) { PropertyInfo[] pis = Activator.CreateInstance().GetType().GetProperties(); Json.Append("{"); for (int j = 0; j < pis.Length; j++) { Json.Append(string.Concat(new object[] { "\"", pis[j].Name.ToString(), "\":\"", pis[j].GetValue(IL[i], null), "\"" })); if (j < (pis.Length - 1)) { Json.Append(","); } } Json.Append("}"); if (i < (IL.Count - 1)) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); } /// /// JSON格式数组转化为对应的List /// /// /// JSON格式数组 /// public static List JSONStringToList(string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); //设置转化JSON格式时字段长度 List objs = Serializer.Deserialize>(JsonStr); return objs; } public static Object Json2Obj(String json, Type t) { try { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(t); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { return serializer.ReadObject(ms); } } catch { return null; } } } }