PosChannelHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.IO;
  13. using System.Net;
  14. public class PosChannelHelper
  15. {
  16. public readonly static PosChannelHelper Instance = new PosChannelHelper();
  17. private PosChannelHelper()
  18. {
  19. }
  20. public void Start()
  21. {
  22. Thread th = new Thread(DoWorks);
  23. th.IsBackground = true;
  24. th.Start();
  25. }
  26. private void DoWorks()
  27. {
  28. while (true)
  29. {
  30. try
  31. {
  32. WebCMSEntities db = new WebCMSEntities();
  33. DoSomething(db);
  34. db.Dispose();
  35. Thread.Sleep(600000);
  36. }
  37. catch (Exception ex)
  38. {
  39. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "补下单机具券奖励异常");
  40. }
  41. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "补下单机具券奖励日志");
  42. }
  43. }
  44. public void DoSomething(WebCMSEntities db)
  45. {
  46. List<PosChannelSetRecord> list = db.PosChannelSetRecord.Where(m => m.Status == 0).ToList();
  47. foreach(PosChannelSetRecord sub in list)
  48. {
  49. string jsonString = QueryTermAcqInfo(sub.PosSn);
  50. JsonData jsonData = JsonMapper.ToObject(jsonString);
  51. if(jsonData["resultCode"].ToString() == "1")
  52. {
  53. // string acqInstId = jsonData["data"]["acqInstId"].ToString();
  54. // if(sub.ChannelNo == acqInstId)
  55. // {
  56. PosChannelSetRecord edit = db.PosChannelSetRecord.FirstOrDefault(m => m.Id == sub.Id);
  57. if(edit != null)
  58. {
  59. edit.Status = 1;
  60. db.SaveChanges();
  61. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == edit.PosId) ?? new PosMachinesTwo();
  62. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  63. string ChannelName = "";
  64. if(sub.ChannelNo == "48130000") ChannelName = "环讯";
  65. if(sub.ChannelNo == "48310000") ChannelName = "易生";
  66. if(sub.ChannelNo == "48330000") ChannelName = "海科";
  67. if(sub.ChannelNo == "48470000") ChannelName = "中付";
  68. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  69. {
  70. UserId = pos.BuyUserId, //创客
  71. Title = "机具通道变更通知", //标题
  72. Content = "<div class='f16' style='padding:12px 16px;'>尊敬的" + user.RealName + "盟主您好:<br/><br/><div class='f14' style='text-indent:2em;'>您名下的机具" + pos.PosSn + ",通道已更改为" + ChannelName + ",请知悉!</ div> </ div>", //内容
  73. CreateDate = DateTime.Now,
  74. }));
  75. }
  76. // }
  77. }
  78. }
  79. }
  80. #region 盒易付
  81. //测试环境
  82. // string BoxRequestUrl = "https://openapi-test.iboxpay.com/api";
  83. // string BoxAppId = "8840021411446784";
  84. // string BoxAppSecret = "xgwGnmYGjVy0GOWOWkQtRk8Hk8ur5tCV";
  85. // string BoxPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjMQxp24mjxDTr13uPW0y+tiO1yXFGw7P/pPQ0oZKK7F6KstEaus7pLEywBZ5XRXE5jgkhR2TS7Ne7djJfbpn5yFc6pPlz3ZsOfBVeB88NEwhx6xzCGX2eqSSkO33n8w2G0xc2ss5HpYBarT00NBZWhrwOXpdRPYLOYHKVU3Rl+FA9xDw/wYfoWvrr+JSfHRGn/ENMmQFEdckAbPauKaQMrZD2kz+PRrhq56eWnCuVQPcaz/jroVT8qQEgkg2IsNy+DwfLOIqm8IySEpxnQ5wN/KvsQJc2wXDQNf9F5kvWwjoqSSP0qJS+oPRXET+zJb+WTk2y5M6AYoC9NodwsC4NwIDAQAB";
  86. // string BrhCode = "039001";
  87. //生产环境
  88. string BoxRequestUrl = "https://openapi.iboxpay.com/api";
  89. string BoxAppId = "AP5879017839106039808";
  90. string BoxAppSecret = "DGtftIyl6R3nzwPr5YuV5fnmkfwufXRj";
  91. string BoxPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjmjoQirIYZBD9Qon2HkF4j/NAINXtJ7Lzq/WXxTF7t7mg7LNARt0+ZZaeWx8caq2fv5zdsGyyoInL23cBtDI5KmFfK69iA0ygQMK0WbiKqsUB1OpPbT3+9zLuadIJAznjA223lY6CIjTpdLZhaRjImNVqc60bdkx6YsQcA+xW+3r1JH4PPHb7yBEbkKIX8OhyX7U4p0TkbDkAobbjHr5YB9gmYLoSFJMOPfTtSExkv7/Y7IVR9poZAHcr3teFoAiXW3RzxelRtnXxIkl/6AUOKoL5fhr/UTUN+Q18uzNljYWr6SwnTI3EmtzgykaewWtZvV85Xdhe/BjiQ5Xor7YbwIDAQAB";
  92. string BrhCode = "039034";
  93. public string BoxServiceFee(string SnList, string ServiceFee)
  94. {
  95. function.WriteLog(DateTime.Now.ToString(), "设置盒易付机具服务费");
  96. function.WriteLog(SnList, "设置盒易付机具服务费");
  97. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  98. Dictionary<string, string> headdic = new Dictionary<string, string>();
  99. string batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  100. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  101. headdic.Add("appid", BoxAppId);//开发者id
  102. headdic.Add("appsecret", BoxAppSecret);//开发者密码
  103. headdic.Add("X-Up-AppId", BoxAppId);
  104. headdic.Add("X-Timestamp", timestamp);
  105. headdic.Add("X-Sign-Type", "SHA-256");
  106. reqdic.Add("snList", SnList.Split(',').ToList());//终端列表
  107. if(ServiceFee != "0")
  108. {
  109. ServiceFee += "00";
  110. }
  111. reqdic.Add("depositGear", ServiceFee);//押金档位
  112. reqdic.Add("modelId", "MHN10916");//费率,固定0.6
  113. reqdic.Add("brhCode", BrhCode);//机构号
  114. reqdic.Add("batchNo", batchNo);//批次号
  115. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  116. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  117. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  118. function.WriteLog("请求头\n" + head, "设置盒易付机具服务费");
  119. function.WriteLog("请求参数\n" + req, "设置盒易付机具服务费");
  120. function.WriteLog("请求地址:" + BoxRequestUrl + "/inst/register/terms", "设置盒易付机具服务费");
  121. string result = PostWebRequest(BoxRequestUrl + "/inst/register/terms", req, headdic);
  122. function.WriteLog("返回\n" + result + "\n\n", "设置盒易付机具服务费");
  123. return result;
  124. }
  125. public string BoxFee(string MerNo, string Fee)
  126. {
  127. function.WriteLog(DateTime.Now.ToString(), "设置盒易付机具费率");
  128. function.WriteLog(MerNo, "设置盒易付机具费率");
  129. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  130. Dictionary<string, string> headdic = new Dictionary<string, string>();
  131. string batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  132. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  133. headdic.Add("appid", BoxAppId);//开发者id
  134. headdic.Add("appsecret", BoxAppSecret);//开发者密码
  135. headdic.Add("X-Up-AppId", BoxAppId);
  136. headdic.Add("X-Timestamp", timestamp);
  137. headdic.Add("X-Sign-Type", "SHA-256");
  138. reqdic.Add("mchtNo", MerNo);//终端列表
  139. reqdic.Add("brhCode", BrhCode);//机构号
  140. reqdic.Add("tNDiscId", Fee == "0.6" ? "MHN10916" : "MHN10917");//费率模型
  141. reqdic.Add("effectiveFlag", "1");//立即生效
  142. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  143. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  144. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  145. function.WriteLog("请求头\n" + head, "设置盒易付机具费率");
  146. function.WriteLog("请求参数\n" + req, "设置盒易付机具费率");
  147. function.WriteLog("请求地址:" + BoxRequestUrl + "/inst/mcht/update_disc", "设置盒易付机具费率");
  148. string result = PostWebRequest(BoxRequestUrl + "/inst/mcht/update_disc", req, headdic);
  149. function.WriteLog("返回\n" + result + "\n\n", "设置盒易付机具费率");
  150. return result;
  151. }
  152. //查询终端收单机构
  153. public string QueryTermAcqInfo(string SnNo)
  154. {
  155. function.WriteLog(DateTime.Now.ToString(), "查询终端收单机构");
  156. function.WriteLog(SnNo, "查询终端收单机构");
  157. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  158. Dictionary<string, string> headdic = new Dictionary<string, string>();
  159. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  160. headdic.Add("appid", BoxAppId);//开发者id
  161. headdic.Add("appsecret", BoxAppSecret);//开发者密码
  162. headdic.Add("X-Up-AppId", BoxAppId);
  163. headdic.Add("X-Timestamp", timestamp);
  164. headdic.Add("X-Sign-Type", "SHA-256");
  165. reqdic.Add("tusn", SnNo); //机具号
  166. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  167. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  168. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  169. function.WriteLog("请求头\n" + head, "查询终端收单机构");
  170. function.WriteLog("请求参数\n" + req, "查询终端收单机构");
  171. function.WriteLog("请求地址:" + BoxRequestUrl + "/inst/term/queryTermAcqInfo", "查询终端收单机构");
  172. string result = PostWebRequest(BoxRequestUrl + "/inst/term/queryTermAcqInfo", req, headdic);
  173. function.WriteLog("返回\n" + result + "\n\n", "查询终端收单机构");
  174. return result;
  175. }
  176. public string SHA256Sign(string toSignStr)
  177. {
  178. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  179. SHA256 sha256 = SHA256.Create();
  180. byte[] signByte = sha256.ComputeHash(toSignByte);
  181. string sign = Convert.ToBase64String(signByte);
  182. return sign;
  183. }
  184. public bool VerifySign(string toSignStr, string signStr)
  185. {
  186. byte[] toSignByte = Encoding.Default.GetBytes(toSignStr);
  187. byte[] signByte = Convert.FromBase64String(signStr);
  188. var toKey = Convert.FromBase64String(BoxPublicKey);
  189. var rsaroot = RSA.Create();
  190. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  191. var publicKeyParameters = rsaroot.ExportParameters(false);
  192. using (var rsa = RSA.Create())
  193. {
  194. rsa.ImportParameters(publicKeyParameters);
  195. var sha256 = SHA256.Create();
  196. var hash = sha256.ComputeHash(toSignByte);
  197. return rsa.VerifyHash(hash, signByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  198. }
  199. }
  200. public string SignData(string toSignStr)
  201. {
  202. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  203. var toKey = Convert.FromBase64String(BoxPublicKey);
  204. var rsaroot = RSA.Create();
  205. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  206. var publicKeyParameters = rsaroot.ExportParameters(false);
  207. using (var rsa = RSA.Create())
  208. {
  209. rsa.ImportParameters(publicKeyParameters);
  210. var sha256 = SHA256.Create();
  211. var hash = sha256.ComputeHash(toSignByte);
  212. byte[] endByte = rsa.SignData(toSignByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  213. return Convert.ToBase64String(endByte);
  214. }
  215. }
  216. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  217. {
  218. string ret = string.Empty;
  219. try
  220. {
  221. function.WriteLog(DateTime.Now.ToString(), "请求开店宝API日志");
  222. function.WriteLog(postUrl, "请求开店宝API日志");
  223. function.WriteLog(paramData, "请求开店宝API日志");
  224. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  225. // 设置提交的相关参数
  226. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  227. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  228. request.Method = "POST";
  229. request.KeepAlive = false;
  230. request.AllowAutoRedirect = true;
  231. request.ContentType = "application/json";
  232. foreach (string key in headers.Keys)
  233. {
  234. request.Headers.Add(key, headers[key]);
  235. }
  236. 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)";
  237. request.ContentLength = postData.Length;
  238. // 提交请求数据
  239. Stream outputStream = request.GetRequestStream();
  240. outputStream.Write(postData, 0, postData.Length);
  241. outputStream.Close();
  242. HttpWebResponse response;
  243. Stream responseStream;
  244. StreamReader reader;
  245. string srcString;
  246. response = request.GetResponse() as HttpWebResponse;
  247. responseStream = response.GetResponseStream();
  248. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  249. srcString = reader.ReadToEnd();
  250. ret = srcString; //返回值赋值
  251. reader.Close();
  252. function.WriteLog(srcString, "请求开店宝API日志");
  253. }
  254. catch (WebException ex)
  255. {
  256. HttpWebResponse response = (HttpWebResponse)ex.Response;
  257. Stream myResponseStream = response.GetResponseStream();
  258. //获取响应内容
  259. StreamReader myStreamReader = new StreamReader(myResponseStream);
  260. ret = myStreamReader.ReadToEnd();
  261. myResponseStream.Close();
  262. }
  263. catch (Exception ex)
  264. {
  265. ret = "fail";
  266. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求开店宝API异常");
  267. }
  268. return ret;
  269. }
  270. #endregion
  271. }