PosChannelHelper.cs 15 KB

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