using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using Library; namespace MySystem { public class HaoDaHelper { public readonly static HaoDaHelper Instance = new HaoDaHelper(); private HaoDaHelper() { } #region 盒易付 //测试环境 // string BoxRequestUrl = "https://openapi-test.iboxpay.com/api"; // string BoxAppId = "8840021411446784"; // string BoxAppSecret = "xgwGnmYGjVy0GOWOWkQtRk8Hk8ur5tCV"; // string BoxPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjMQxp24mjxDTr13uPW0y+tiO1yXFGw7P/pPQ0oZKK7F6KstEaus7pLEywBZ5XRXE5jgkhR2TS7Ne7djJfbpn5yFc6pPlz3ZsOfBVeB88NEwhx6xzCGX2eqSSkO33n8w2G0xc2ss5HpYBarT00NBZWhrwOXpdRPYLOYHKVU3Rl+FA9xDw/wYfoWvrr+JSfHRGn/ENMmQFEdckAbPauKaQMrZD2kz+PRrhq56eWnCuVQPcaz/jroVT8qQEgkg2IsNy+DwfLOIqm8IySEpxnQ5wN/KvsQJc2wXDQNf9F5kvWwjoqSSP0qJS+oPRXET+zJb+WTk2y5M6AYoC9NodwsC4NwIDAQAB"; // string BrhCode = "039001"; //生产环境 string BoxRequestUrl = "https://openapi.iboxpay.com/api"; string BoxAppId = "AP5879017839106039808"; string BoxAppSecret = "DGtftIyl6R3nzwPr5YuV5fnmkfwufXRj"; string BoxPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjmjoQirIYZBD9Qon2HkF4j/NAINXtJ7Lzq/WXxTF7t7mg7LNARt0+ZZaeWx8caq2fv5zdsGyyoInL23cBtDI5KmFfK69iA0ygQMK0WbiKqsUB1OpPbT3+9zLuadIJAznjA223lY6CIjTpdLZhaRjImNVqc60bdkx6YsQcA+xW+3r1JH4PPHb7yBEbkKIX8OhyX7U4p0TkbDkAobbjHr5YB9gmYLoSFJMOPfTtSExkv7/Y7IVR9poZAHcr3teFoAiXW3RzxelRtnXxIkl/6AUOKoL5fhr/UTUN+Q18uzNljYWr6SwnTI3EmtzgykaewWtZvV85Xdhe/BjiQ5Xor7YbwIDAQAB"; string BrhCode = "039034"; public string BoxServiceFee(string SnList, string ServiceFee) { function.WriteLog(DateTime.Now.ToString(), "设置盒易付机具服务费"); function.WriteLog(SnList, "设置盒易付机具服务费"); Dictionary reqdic = new Dictionary(); string batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); reqdic.Add("snList", SnList.Split(',').ToList());//终端列表 if(ServiceFee != "0") { ServiceFee += "00"; } reqdic.Add("depositGear", ServiceFee);//押金档位 reqdic.Add("modelId", "MHN10916");//费率,固定0.6 reqdic.Add("brhCode", BrhCode);//机构号 reqdic.Add("batchNo", batchNo);//批次号 string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic); Dictionary headdic = GetHeader(reqdic); string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic); function.WriteLog("请求头\n" + head, "设置盒易付机具服务费"); function.WriteLog("请求参数\n" + req, "设置盒易付机具服务费"); function.WriteLog("请求地址:" + BoxRequestUrl + "/inst/register/terms", "设置盒易付机具服务费"); string result = PostWebRequest(BoxRequestUrl + "/inst/register/terms", req, headdic); function.WriteLog("返回\n" + result + "\n\n", "设置盒易付机具服务费"); return result; } public Dictionary GetHeader(Dictionary reqdic) { Dictionary headdic = new Dictionary(); string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); headdic.Add("appid", BoxAppId);//开发者id headdic.Add("appsecret", BoxAppSecret);//开发者密码 headdic.Add("X-Up-AppId", BoxAppId); headdic.Add("X-Timestamp", timestamp); headdic.Add("X-Sign-Type", "SHA-256"); string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic); headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req)); return headdic; } public string SHA256Sign(string toSignStr) { byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr); SHA256 sha256 = SHA256.Create(); byte[] signByte = sha256.ComputeHash(toSignByte); string sign = Convert.ToBase64String(signByte); return sign; } public bool VerifySign(string toSignStr, string signStr) { byte[] toSignByte = Encoding.Default.GetBytes(toSignStr); byte[] signByte = Convert.FromBase64String(signStr); var toKey = Convert.FromBase64String(BoxPublicKey); var rsaroot = RSA.Create(); rsaroot.ImportSubjectPublicKeyInfo(toKey, out _); var publicKeyParameters = rsaroot.ExportParameters(false); using (var rsa = RSA.Create()) { rsa.ImportParameters(publicKeyParameters); var sha256 = SHA256.Create(); var hash = sha256.ComputeHash(toSignByte); return rsa.VerifyHash(hash, signByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); } } public string SignData(string toSignStr) { byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr); var toKey = Convert.FromBase64String(BoxPublicKey); var rsaroot = RSA.Create(); rsaroot.ImportSubjectPublicKeyInfo(toKey, out _); var publicKeyParameters = rsaroot.ExportParameters(false); using (var rsa = RSA.Create()) { rsa.ImportParameters(publicKeyParameters); var sha256 = SHA256.Create(); var hash = sha256.ComputeHash(toSignByte); byte[] endByte = rsa.SignData(toSignByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); return Convert.ToBase64String(endByte); } } private string PostWebRequest(string postUrl, string paramData, Dictionary headers) { string ret = string.Empty; try { function.WriteLog(DateTime.Now.ToString(), "请求开店宝API日志"); function.WriteLog(postUrl, "请求开店宝API日志"); function.WriteLog(paramData, "请求开店宝API日志"); byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData); // 设置提交的相关参数 System.Net.HttpWebRequest request = System.Net.WebRequest.Create(postUrl) as System.Net.HttpWebRequest; System.Text.Encoding myEncoding = System.Text.Encoding.UTF8; request.Method = "POST"; request.KeepAlive = false; request.AllowAutoRedirect = true; request.ContentType = "application/json"; foreach (string key in headers.Keys) { request.Headers.Add(key, headers[key]); } request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; request.ContentLength = postData.Length; // 提交请求数据 System.IO.Stream outputStream = request.GetRequestStream(); outputStream.Write(postData, 0, postData.Length); outputStream.Close(); System.Net.HttpWebResponse response; System.IO.Stream responseStream; System.IO.StreamReader reader; string srcString; response = request.GetResponse() as System.Net.HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8); srcString = reader.ReadToEnd(); ret = srcString; //返回值赋值 reader.Close(); function.WriteLog(srcString, "请求开店宝API日志"); } catch (System.Net.WebException ex) { System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)ex.Response; System.IO.Stream myResponseStream = response.GetResponseStream(); //获取响应内容 System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream); ret = myStreamReader.ReadToEnd(); myResponseStream.Close(); } catch (Exception ex) { ret = "fail"; function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求开店宝API异常"); } return ret; } #endregion } }