TestHaoDaService.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using MySystem.Models.Main;
  6. using Library;
  7. using LitJson;
  8. using System.Threading;
  9. using System.Net;
  10. using System.IO;
  11. namespace MySystem
  12. {
  13. /// <summary>
  14. /// 好哒测试服务
  15. /// </summary>
  16. public class TestHaoDaService
  17. {
  18. public readonly static TestHaoDaService Instance = new TestHaoDaService();
  19. private TestHaoDaService()
  20. { }
  21. public void Start()
  22. {
  23. Thread th = new Thread(StartListen);
  24. th.IsBackground = true;
  25. th.Start();
  26. }
  27. public void StartListen()
  28. {
  29. // GetFTPListInfo(); // 获取好哒FTPInfo
  30. GetFTPDataInfo(); //获取好哒FTPDataInfo
  31. //开户意愿申请
  32. // RedisDbconn.Instance.AddList("AlipayForHaoDaQueue",824);
  33. // RedisDbconn.Instance.AddList("WeChatForHaoDaQueue",824);
  34. // AlipayAddInfo(826);//提交支付宝开户意愿申请单
  35. // AlipayAddInfo(828,"202309011037906510");//查询支付宝开户意愿申请状态
  36. // WeChatAddInfo(826);//提交微信开户意愿申请单
  37. // WeChatAddInfo(825,"2000002424428809");//查询微信开户意愿申请状态
  38. // ConfiemMer(1833);//新建商户
  39. // QueryMerchantStatus(1833);//新建商户查询状态
  40. // GetTencentAddressInfo(112); //获取商户经纬坐标
  41. // ChangeMerchantCard(1);//商户修改银行卡
  42. // QueryChangeCardStatus(1874);//商户修改银行卡状态
  43. // AddOpenDivideAccounts(832); //添加分账方
  44. // QueryAuthStatus(1845);//查询实名认证状态
  45. // WeChatFunctionForHD.Instance.QueryAuthMerchant("600955391");
  46. // AlipayFunctionForHD.Instance.AuthOrderAuthQuery("2088820718243322");
  47. // StartProfitShareListen();
  48. // HaoDaHelper.Instance.BindWeChatAppId("1490230", "21622558", AppConfig.Haoda.SubAppId);
  49. // HaoDaHelper.Instance.OrderDivideAccountsQuery("FZ2023110315225748140470708", "1492500"); //查询分账结果
  50. // WebCMSEntities db = new WebCMSEntities();
  51. // List<int> ids = new List<int>();
  52. // foreach(int id in ids)
  53. // {
  54. // ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id) ?? new ConsumerOrders();
  55. // MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  56. // string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  57. // string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  58. // string orderNo = order.SeoTitle; //交易订单号(好哒平台订单号,G开头)
  59. // string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  60. // string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  61. // string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
  62. // // decimal hdfee = 0.0038M * order.PayMoney;
  63. // // hdfee = decimal.Parse(hdfee.ToString("f2"));
  64. // // decimal servicefee = 0.01M * order.PayMoney;
  65. // // servicefee = decimal.Parse(servicefee.ToString("f2"));
  66. // // decimal amount = order.PayMoney - hdfee - servicefee;
  67. // // amount = amount * 100; //金额(分)
  68. // // servicefee = servicefee * 100;
  69. // // string seviceAmount = servicefee.ToString("f0"); //服务费
  70. // string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, "1", "0"));
  71. // }
  72. // string content = function.ReadInstance("content.txt");
  73. // string[] list = content.Split('\n');
  74. // foreach(string sub in list)
  75. // {
  76. // JsonData jsonObj = JsonMapper.ToObject(sub);
  77. // string orderNo = jsonObj["orderNo"].ToString();
  78. // JsonData detail = jsonObj["detail"];
  79. // string me = detail[0]["amount"].ToString();
  80. // string mer = detail[1]["amount"].ToString();
  81. // ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == orderNo) ?? new ConsumerOrders();
  82. // if(order.SettleAmount > 0)
  83. // {
  84. // MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  85. // MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  86. // string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  87. // string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  88. // string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  89. // string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  90. // decimal settleAmount = (decimal)order.SettleAmount / 100M;
  91. // decimal divideAmt = (1 - 0.0038M) * order.PayMoney;
  92. // divideAmt = decimal.Parse(divideAmt.ToString("f2"));
  93. // decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent / 100 - 0.01M);
  94. // hdfee = decimal.Parse(hdfee.ToString("f2"));
  95. // decimal servicefee = 0.01M * order.PayMoney - hdfee;
  96. // servicefee = decimal.Parse(servicefee.ToString("f2"));
  97. // decimal amount = order.PayMoney*(1-0.0038M) - servicefee;
  98. // amount = decimal.Parse(amount.ToString("f2"));
  99. // decimal otherAmt = settleAmount - servicefee - amount;
  100. // if(otherAmt != 0)
  101. // {
  102. // amount += otherAmt;
  103. // }
  104. // amount -= decimal.Parse(mer) / 100M;
  105. // servicefee -= decimal.Parse(me) / 100M;
  106. // if(amount > 0)
  107. // {
  108. // if(servicefee < 0) servicefee = 0;
  109. // amount = amount * 100 - 1; //金额(分)
  110. // servicefee = servicefee * 100;
  111. // string seviceAmount = servicefee.ToString("f0"); //服务费
  112. // string amountAmount = amount.ToString("f0");
  113. // string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, "0", amountAmount, seviceAmount));
  114. // }
  115. // }
  116. // }
  117. // List<string> list = new List<string>();
  118. // list.Add("{\"orderNo\":\"G2231124C02180937731\",\"settle\":\"29687\",\"part\":\"29575\"}");
  119. // foreach(string sub in list)
  120. // {
  121. // JsonData jsonObj = JsonMapper.ToObject(sub);
  122. // string orderNo = jsonObj["orderNo"].ToString();
  123. // string settle = jsonObj["settle"].ToString();
  124. // string part = jsonObj["part"].ToString();
  125. // ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == orderNo) ?? new ConsumerOrders();
  126. // if(order.SettleAmount > 0)
  127. // {
  128. // MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  129. // MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  130. // string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  131. // string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  132. // string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  133. // string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  134. // decimal settleamount = decimal.Parse(settle);
  135. // decimal partamount = decimal.Parse(part);
  136. // decimal amount = settleamount - partamount;
  137. // if(amount > 0)
  138. // {
  139. // decimal servicefee = amount / 100M;
  140. // servicefee = decimal.Parse(servicefee.ToString("f0"));
  141. // amount -= servicefee;
  142. // string seviceAmount = servicefee.ToString("f0"); //服务费
  143. // string amountAmount = amount.ToString("f0");
  144. // string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, "0", amountAmount, seviceAmount));
  145. // }
  146. // }
  147. // }
  148. // db.Dispose();
  149. }
  150. /// <summary>
  151. /// 从ftp服务器上获得文件列表
  152. /// </summary>
  153. /// <param name="RequedstPath">服务器下的相对路径</param>
  154. /// <returns></returns>
  155. public static List<string> GetFTPListInfo(string RequedstPath = "/haoda-trade")
  156. {
  157. // 47.108.253.46
  158. // 用户名:hdftp
  159. // 密:haodatradeftp2024
  160. // 目录:/haoda-trade
  161. List<string> strs = new List<string>();
  162. try
  163. {
  164. string uri = "ftp://47.108.253.46" + RequedstPath; //目标路径 path为服务器地址        ②
  165. FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  166. // ftp用户名和密码
  167. reqFTP.Credentials = new NetworkCredential("hdftp", "haodatradeftp2024");
  168. reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  169. WebResponse response = reqFTP.GetResponse();
  170. StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
  171. string line = reader.ReadLine();
  172. while (line != null)
  173. {
  174. if (!line.Contains("<DIR>"))
  175. {
  176. string msg = line.Substring(0).Trim();
  177. msg = msg.Substring(msg.Length - 24, 24);
  178. strs.Add(msg);
  179. }
  180. line = reader.ReadLine();
  181. }
  182. reader.Close();
  183. response.Close();
  184. return strs;
  185. }
  186. catch (Exception ex)
  187. {
  188. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  189. }
  190. return strs;
  191. }
  192. public static void GetFTPDataInfo()
  193. {
  194. // 47.108.253.46
  195. // 用户名:hdftp
  196. // 密:haodatradeftp2024
  197. // 目录:/haoda-trade
  198. // FTP服务器的地址
  199. string ftpServerAddress = "ftp://47.108.253.46";
  200. // FTP登录凭证
  201. string ftpUser = "hdftp";
  202. string ftpPassword = "haodatradeftp2024";
  203. // 要下载的文件路径
  204. string filePath = "/haoda-trade/20240513.csv";
  205. try
  206. {
  207. // 创建FtpWebRequest对象
  208. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  209. request.Method = WebRequestMethods.Ftp.DownloadFile;
  210. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  211. // 使用WebResponse获取响应
  212. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  213. // 打开数据流
  214. Stream responseStream = response.GetResponseStream();
  215. using (StreamReader reader = new StreamReader(responseStream))
  216. {
  217. WebCMSEntities db = new WebCMSEntities();
  218. // 读取数据
  219. string fileContents = reader.ReadToEnd();
  220. if (!string.IsNullOrEmpty(fileContents))
  221. {
  222. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  223. var DataList = DataInfo[1].Split('\n');
  224. foreach (var DataListItem in DataList)
  225. {
  226. var DataListInfo = DataListItem.Split(',');
  227. var MerchantNo = ""; // 商户号
  228. var BaseNo = ""; // 设备号
  229. var OrderNo = ""; // 订单号
  230. var PayWay = ""; // 支付方式(微信 支付宝)
  231. var TradeType = ""; // 交易类型
  232. var TradeAmount = ""; // 交易金额
  233. var TradeFee = ""; // 交易手续费
  234. var TradeDate = ""; // 交易时间
  235. var TradeCycle = ""; // 结算周期
  236. MerchantNo = DataListInfo[0];
  237. BaseNo = DataListInfo[1];
  238. OrderNo = DataListInfo[2];
  239. PayWay = DataListInfo[3];
  240. TradeType = DataListInfo[4];
  241. TradeAmount = DataListInfo[5];
  242. TradeFee = DataListInfo[6];
  243. TradeDate = DataListInfo[7];
  244. TradeCycle = DataListInfo[8];
  245. TradeDate = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2) + " " + TradeDate.Substring(8, 2) + ":" + TradeDate.Substring(10, 2) + ":" + TradeDate.Substring(12, 2);
  246. var PayMode = 0;
  247. if (PayWay.Contains("支付宝")) PayMode = 1;
  248. if (PayWay.Contains("微信")) PayMode = 2;
  249. var merchantAddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == MerchantNo) ?? new MerchantAddInfo();
  250. if (merchantAddInfo.Id > 0)
  251. {
  252. var merchantInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == merchantAddInfo.Id) ?? new MerchantInfo();
  253. var orders = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == OrderNo && OrderNo == OrderNo) ?? new ConsumerOrders();
  254. if (orders.Id == 0)
  255. {
  256. var query = db.ConsumerOrders.Add(new ConsumerOrders()
  257. {
  258. Status = 1,
  259. CreateDate = DateTime.Parse(TradeDate),
  260. UpdateDate = DateTime.Parse(TradeDate),
  261. SnNo = BaseNo,
  262. PayMoney = decimal.Parse(TradeAmount),
  263. PayMode = PayMode,
  264. SeoTitle = OrderNo,
  265. OrderNo = OrderNo,
  266. MerchantId = merchantAddInfo.Id,
  267. UserId = merchantInfo.UserId
  268. }).Entity;
  269. }
  270. }
  271. }
  272. db.SaveChanges();
  273. db.Dispose();
  274. }
  275. // 关闭响应
  276. response.Close();
  277. }
  278. // 关闭响应
  279. response.Close();
  280. }
  281. catch (WebException ex)
  282. {
  283. // 如果抛出WebException,则可能是文件不存在
  284. // 根据响应状态码判断是否文件不存在
  285. if (((FtpWebResponse)ex.Response).StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
  286. {
  287. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString() + "文件" + filePath + "不存在", "获取好哒FTP文件数据异常"); // 文件不存在,返回false
  288. }
  289. else
  290. {
  291. // 其他错误
  292. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  293. }
  294. }
  295. }
  296. public void StartProfitShareListen()
  297. {
  298. bool op = true;
  299. while (op)
  300. {
  301. try
  302. {
  303. WebCMSEntities db = new WebCMSEntities();
  304. var list = db.ConsumerOrders.Where(m => m.Status == 1 && m.IsAct == 0).OrderByDescending(m => m.Id).ToList();
  305. foreach (var sub in list)
  306. {
  307. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  308. if (order != null)
  309. {
  310. int Status = 2;
  311. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  312. MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
  313. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  314. //发起分账
  315. string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  316. string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  317. string orderNo = order.SeoTitle; //交易订单号(好哒平台订单号,G开头)
  318. string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  319. string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  320. string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
  321. decimal amount = order.PayMoney * (1M - 0.0038M) * 100;
  322. string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, amount.ToString().Split('.')[0], "0"));
  323. if (result.Contains("\"resultCode\":\"0\"") && result.Contains("分账金额不足"))
  324. {
  325. Status = 1;
  326. }
  327. order.Status = Status;
  328. }
  329. }
  330. db.SaveChanges();
  331. db.Dispose();
  332. }
  333. catch (Exception ex)
  334. {
  335. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒分账队列异常");
  336. }
  337. Thread.Sleep(2000);
  338. }
  339. }
  340. //新建商户查询状态
  341. public void QueryMerchantStatus(int MerchantId)
  342. {
  343. WebCMSEntities db = new WebCMSEntities();
  344. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  345. var BrhCode = HaodaUnionParam.BrhCode;
  346. if (AddInfo.BrandId == 1) BrhCode = HaodaParam.BrhCode;
  347. var returnInfo = HaoDaHelper.Instance.QueryMerchantStatus(BrhCode, AddInfo.MchtNo);
  348. // string result = HaoDaHelper.Instance.AddNewMerchant(AddNewMerchantSetUtil.SetValue(AddInfo));
  349. db.Dispose();
  350. }
  351. //提交支付宝开户意愿申请单
  352. public void AlipayAddInfo(int Id)
  353. {
  354. try
  355. {
  356. WebCMSEntities db = new WebCMSEntities();
  357. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  358. string result = AlipayFunctionForHD.Instance.AuthOrderCreate(info, info.AlipayAuthToken);
  359. JsonData jsonObj = JsonMapper.ToObject(result);
  360. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  361. reqdic.Add("Id", Id); // 商户Id
  362. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  363. LogHelper.Instance.WriteLog(req, "提交支付宝商户意愿申请单");
  364. Dictionary<string, string> headdic = HaoDaHelper.Instance.GetHeader(req);
  365. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  366. LogHelper.Instance.WriteLog("请求参数\n" + req, "提交支付宝商户意愿申请单");
  367. LogHelper.Instance.WriteLog(result, "提交支付宝商户意愿申请单");
  368. if (jsonObj["alipay_merchant_indirect_authorder_create_response"]["code"].ToString() == "10000")
  369. {
  370. LogHelper.Instance.WriteLog("success", "提交支付宝商户意愿申请单");
  371. string order_no = jsonObj["alipay_merchant_indirect_authorder_create_response"]["order_no"].ToString();
  372. // RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + order_no + "\"}");
  373. LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
  374. }
  375. else
  376. {
  377. LogHelper.Instance.WriteLog("false", "提交支付宝商户意愿申请单");
  378. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  379. info.QueryCount = -1;
  380. merchant.QueryCount = -1;
  381. info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_create_response"]["msg"].ToString();
  382. LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
  383. db.SaveChanges();
  384. }
  385. db.Dispose();
  386. }
  387. catch (Exception ex)
  388. {
  389. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
  390. }
  391. }
  392. //查询支付宝开户意愿申请状态
  393. public void AlipayAddInfo(int Id, string OrderNo)
  394. {
  395. try
  396. {
  397. LogHelper.Instance.WriteLog("start", "查询支付宝商户意愿申请状态");
  398. WebCMSEntities db = new WebCMSEntities();
  399. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  400. string result = AlipayFunctionForHD.Instance.AuthOrderQuery(OrderNo);
  401. JsonData jsonObj = JsonMapper.ToObject(result);
  402. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  403. reqdic.Add("Id", Id); // 商户Id
  404. reqdic.Add("OrderNo", OrderNo); // 商户Id
  405. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  406. LogHelper.Instance.WriteLog(req, "查询支付宝商户意愿申请状态");
  407. Dictionary<string, string> headdic = HaoDaHelper.Instance.GetHeader(req);
  408. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  409. LogHelper.Instance.WriteLog("请求参数\n" + req, "查询支付宝商户意愿申请状态");
  410. LogHelper.Instance.WriteLog(result, "查询支付宝商户意愿申请状态");
  411. if (jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["code"].ToString() == "10000")
  412. {
  413. LogHelper.Instance.WriteLog("start1", "查询支付宝商户意愿申请状态");
  414. string order_status = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["order_status"].ToString();
  415. if (order_status == "CONTACT_CONFIRM")
  416. {
  417. LogHelper.Instance.WriteLog("start1", "查询支付宝商户意愿申请状态");
  418. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  419. info.QueryCount = 1;
  420. merchant.QueryCount = 1;
  421. info.AlipayRemark = "";
  422. info.AlipaySignUrl = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["qr_code"].ToString();
  423. db.SaveChanges();
  424. // RedisDbconn.Instance.AddList("HaoDaAuthQueryHelper", "{\"MerchantId\":\"" + Id + "\",\"MchtNo\":\"" + info.MchtNo + "\",\"StoreNo\":\"" + info.StoreNo + "\"}");
  425. LogHelper.Instance.WriteLog("start1end", "查询支付宝商户意愿申请状态");
  426. }
  427. else if (order_status == "AUDIT_REJECT" || order_status == "AUDIT_FREEZE")
  428. {
  429. LogHelper.Instance.WriteLog("start2", "查询支付宝商户意愿申请状态");
  430. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  431. info.QueryCount = -1;
  432. merchant.QueryCount = -1;
  433. info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["msg"].ToString();
  434. LogHelper.Instance.WriteLog("start2end", "查询支付宝商户意愿申请状态");
  435. db.SaveChanges();
  436. }
  437. else if (order_status == "AUDIT_PASS")
  438. {
  439. LogHelper.Instance.WriteLog("start2", "查询支付宝商户意愿申请状态");
  440. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  441. info.QueryCount = 2;
  442. merchant.QueryCount = 2;
  443. info.AlipayRemark = "";
  444. LogHelper.Instance.WriteLog("start2end", "查询支付宝商户意愿申请状态");
  445. if (merchant.Latitude == 0)
  446. {
  447. //签约成功调用腾讯地图接口获取地址
  448. RedisDbconn.Instance.AddList("GetTencentAddressInfoQueueHd", "{\"MerChantId\":\"" + merchant.Id + "\",\"Address\":\"" + merchant.Areas + merchant.Address + "\"}");
  449. }
  450. db.SaveChanges();
  451. }
  452. // else
  453. // {
  454. // RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + OrderNo + "\"}");
  455. // }
  456. }
  457. else
  458. {
  459. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  460. info.QueryCount = -1;
  461. merchant.QueryCount = -1;
  462. info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["msg"].ToString();
  463. db.SaveChanges();
  464. }
  465. db.Dispose();
  466. }
  467. catch (Exception ex)
  468. {
  469. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
  470. }
  471. }
  472. //提交微信开户意愿申请单
  473. public void WeChatAddInfo(int Id)
  474. {
  475. try
  476. {
  477. WebCMSEntities db = new WebCMSEntities();
  478. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  479. string result = WeChatFunctionForHD.Instance.MerchantApply(info);
  480. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  481. reqdic.Add("Id", Id); // 商户Id
  482. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  483. LogHelper.Instance.WriteLog(req, "提交微信商户意愿申请单");
  484. Dictionary<string, string> headdic = HaoDaHelper.Instance.GetHeader(req);
  485. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  486. LogHelper.Instance.WriteLog("请求参数\n" + req, "提交微信商户意愿申请单");
  487. LogHelper.Instance.WriteLog(result, "提交微信商户意愿申请单");
  488. if (result.Contains("\"applyment_id\":"))
  489. {
  490. LogHelper.Instance.WriteLog("success", "提交微信商户意愿申请单");
  491. JsonData json = JsonMapper.ToObject(result);
  492. string applyment_id = json["applyment_id"].ToString();
  493. // RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + applyment_id + "\"}");
  494. LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
  495. }
  496. else if (result.Contains("\"message\":"))
  497. {
  498. LogHelper.Instance.WriteLog("false", "提交微信商户意愿申请单");
  499. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  500. JsonData jsonObj = JsonMapper.ToObject(result);
  501. info.Status = -1;
  502. merchant.Status = -1;
  503. info.WeChatRemark = jsonObj["message"].ToString();
  504. LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
  505. db.SaveChanges();
  506. }
  507. db.Dispose();
  508. }
  509. catch (Exception ex)
  510. {
  511. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "微信开户意愿申请异常");
  512. }
  513. }
  514. //查询微信开户意愿申请状态
  515. public void WeChatAddInfo(int Id, string ApplymentId)
  516. {
  517. try
  518. {
  519. WebCMSEntities db = new WebCMSEntities();
  520. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  521. string result = WeChatFunctionForHD.Instance.QueryMerchant(ApplymentId);
  522. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  523. reqdic.Add("Id", Id); // 商户Id
  524. reqdic.Add("ApplymentId", ApplymentId); // 商户Id
  525. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  526. LogHelper.Instance.WriteLog(req, "提交微信商户意愿申请单");
  527. Dictionary<string, string> headdic = HaoDaHelper.Instance.GetHeader(req);
  528. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  529. LogHelper.Instance.WriteLog("请求参数\n" + req, "提交微信商户意愿申请单");
  530. JsonData jsonObj = JsonMapper.ToObject(result);
  531. if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT")
  532. {
  533. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  534. info.Status = 1;
  535. merchant.Status = 1;
  536. info.WeChatRemark = "";
  537. info.WeChatSignUrl = jsonObj["qrcode_data"].ToString();
  538. db.SaveChanges();
  539. // RedisDbconn.Instance.AddList("HaoDaAuthQueryHelper", "{\"MerchantId\":\"" + Id + "\",\"MchtNo\":\"" + info.MchtNo + "\",\"StoreNo\":\"" + info.StoreNo + "\"}");
  540. }
  541. else if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
  542. {
  543. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  544. info.Status = -1;
  545. merchant.Status = -1;
  546. info.WeChatRemark = jsonObj["reject_reason"].ToString();
  547. db.SaveChanges();
  548. }
  549. // else
  550. // {
  551. // RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + ApplymentId + "\"}");
  552. // }
  553. db.Dispose();
  554. }
  555. catch (Exception ex)
  556. {
  557. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "微信开户意愿申请异常");
  558. }
  559. }
  560. //添加分账接收方账户
  561. public void AddOpenDivideAccounts(int MerchantId)
  562. {
  563. WebCMSEntities db = new WebCMSEntities();
  564. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  565. string result = HaoDaHelper.Instance.AddOpenDivideAccounts(AddOpenDivideAccountsUtil.AddValue(AddInfo));
  566. db.Dispose();
  567. }
  568. public void ConfiemMer(int MerchantId)
  569. {
  570. WebCMSEntities db = new WebCMSEntities();
  571. Models.Main1.WebCMSEntities db1 = new Models.Main1.WebCMSEntities();
  572. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  573. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  574. if (AddInfo != null && merchant != null)
  575. {
  576. string BusinessCode = AddInfo.BusinessCode;
  577. if (string.IsNullOrEmpty(BusinessCode))
  578. {
  579. BusinessCode = "LKB" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(5);
  580. AddInfo.BusinessCode = BusinessCode;
  581. }
  582. string pwd = AddInfo.MobilePhone.Substring(5);
  583. merchant.LoginPwd = function.MD532(pwd);
  584. MerchantParamSet query = db.MerchantParamSet.FirstOrDefault(m => m.Id == MerchantId);
  585. if (query == null)
  586. {
  587. query = new MerchantParamSet();
  588. query.IsAll = 1;
  589. db.MerchantParamSet.Add(query);
  590. db.SaveChanges();
  591. }
  592. MerchantForMobile merchantForMobile = db.MerchantForMobile.FirstOrDefault(m => m.Mobile == AddInfo.MobilePhone);
  593. if (merchantForMobile == null)
  594. {
  595. merchantForMobile = db.MerchantForMobile.Add(new MerchantForMobile()
  596. {
  597. Mobile = AddInfo.MobilePhone,
  598. }).Entity;
  599. db.SaveChanges();
  600. }
  601. merchantForMobile.MerchantId = MerchantId;
  602. db.SaveChanges();
  603. Models.Main1.MerchantLoginInfo info = db1.MerchantLoginInfo.FirstOrDefault(m => m.LoginMobile == AddInfo.MobilePhone);
  604. if (info == null)
  605. {
  606. info = db1.MerchantLoginInfo.Add(new Models.Main1.MerchantLoginInfo()
  607. {
  608. LoginMobile = AddInfo.MobilePhone,
  609. }).Entity;
  610. db1.SaveChanges();
  611. }
  612. info.LoginPwd = function.MD532(pwd);
  613. db1.SaveChanges();
  614. Models.Main1.MerchantAccountLinkInfo infolink = db1.MerchantAccountLinkInfo.FirstOrDefault(m => m.LoginId == info.Id && m.MerchantId == MerchantId && m.Kind == 2);
  615. if (infolink == null)
  616. {
  617. infolink = db1.MerchantAccountLinkInfo.Add(new Models.Main1.MerchantAccountLinkInfo()
  618. {
  619. LoginId = info.Id,
  620. MerchantId = MerchantId,
  621. Kind = 2,
  622. }).Entity;
  623. db1.SaveChanges();
  624. }
  625. if (AddInfo.Status <= 0)
  626. {
  627. //好哒新建商户
  628. string result = HaoDaHelper.Instance.AddNewMerchant(AddNewMerchantSetUtil.SetValue(AddInfo));
  629. JsonData jsonObj = JsonMapper.ToObject(result);
  630. //创建成功
  631. if (jsonObj["resultCode"].ToString() == "1")
  632. {
  633. AddInfo.WeChatRemark = "";
  634. if (string.IsNullOrEmpty(AddInfo.MchtNo))
  635. {
  636. AddInfo.MchtNo = jsonObj["data"]["mchtNo"].ToString();
  637. }
  638. AddInfo.Status = 0;
  639. merchant.Status = 0;
  640. db.SaveChanges();
  641. QueryMerchantStatus queryMerchantStatus = new QueryMerchantStatus();
  642. queryMerchantStatus.MerchantId = MerchantId.ToString();
  643. queryMerchantStatus.MerchantNo = AddInfo.MchtNo;
  644. // RedisDbconn.Instance.AddList("WeChatSignHdQueue", queryMerchantStatus);
  645. }
  646. //创建失败
  647. else
  648. {
  649. AddInfo.Status = -1;
  650. merchant.Status = -1;
  651. AddInfo.WeChatRemark = jsonObj["errorDesc"].ToString();
  652. // if (string.IsNullOrEmpty(AddInfo.MchtNo))
  653. // {
  654. // if (jsonObj.ToJson().Contains("mchtNo"))
  655. // {
  656. // AddInfo.MchtNo = jsonObj["data"]["mchtNo"].ToString();
  657. // }
  658. // }
  659. db.SaveChanges();
  660. }
  661. }
  662. }
  663. db.Dispose();
  664. db1.Dispose();
  665. }
  666. //商户开通分账
  667. public void OpenDivideAccounts(int MerchantId)
  668. {
  669. WebCMSEntities db = new WebCMSEntities();
  670. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  671. string result = HaoDaHelper.Instance.OpenDivideAccounts(AddInfo.OutMchtNo, AddInfo.StoreNo);
  672. db.Dispose();
  673. }
  674. //分账接收方签约
  675. public void DivideAccountsSign(int MerchantId)
  676. {
  677. WebCMSEntities db = new WebCMSEntities();
  678. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  679. string result = HaoDaHelper.Instance.DivideAccountsSign("230817000000000278", AddInfo.OutMchtNo, AddInfo.StoreNo);
  680. db.Dispose();
  681. }
  682. //查询实名认证状态
  683. public void QueryAuthStatus(int MerchantId)
  684. {
  685. WebCMSEntities db = new WebCMSEntities();
  686. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  687. string result = HaoDaHelper.Instance.QueryAuthStatus(AddInfo.OutMchtNo, AddInfo.StoreNo);
  688. db.Dispose();
  689. }
  690. //商户修改银行卡
  691. public void ChangeMerchantCard(int MerchantId)
  692. {
  693. WebCMSEntities db = new WebCMSEntities();
  694. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  695. var request = TestChangeMerchantCardSetUtil.SetValue(AddInfo);
  696. JsonData Infos = JsonMapper.ToObject(HaoDaHelper.Instance.ChangeMerchantCard(request));
  697. JsonData results = JsonMapper.ToObject(HaoDaHelper.Instance.QueryChangeCardStatus(AddInfo.MchtNo, Infos["data"]["taskId"].ToString()));
  698. //TODO:修改结算卡变更商户相关信息
  699. // AccountNumber
  700. // BankName
  701. // BankBranchId
  702. // BankAddressCode
  703. // BankAccountType
  704. // OpenUnionNo
  705. // OpenCollectBankCode
  706. // OpenBankRegionCode
  707. // OpenBankAccout
  708. // OpenBankAccName
  709. // ZbankRegionCode
  710. // ZBankNo
  711. db.Dispose();
  712. }
  713. //获取商户经纬坐标
  714. public void GetTencentAddressInfo(int Id)
  715. {
  716. WebCMSEntities db = new WebCMSEntities();
  717. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id);
  718. string Address = merchant.Areas + merchant.Address;
  719. string Key = Library.ConfigurationManager.AppSettings["TencentKey"].ToString();
  720. LogHelper.Instance.WriteLog("商户Id\n" + Id + "\n\n", "获取商户经纬坐标");
  721. LogHelper.Instance.WriteLog("请求参数\n" + Address + "\n\n", "获取商户经纬坐标");
  722. var info = function.GetWebRequest("https://apis.map.qq.com/ws/geocoder/v1/?address=" + Address + "&key=" + Key);
  723. JsonData result = JsonMapper.ToObject(info);
  724. //创建成功
  725. if (result["status"].ToString() == "0")
  726. {
  727. var merInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  728. if (merInfo.Id > 0)
  729. {
  730. merInfo.Longitude = decimal.Parse(result["result"]["location"]["lng"].ToString());
  731. merInfo.Latitude = decimal.Parse(result["result"]["location"]["lat"].ToString());
  732. db.SaveChanges();
  733. db.Dispose();
  734. }
  735. }
  736. LogHelper.Instance.WriteLog("返回\n" + info + "\n\n", "获取商户经纬坐标");
  737. db.Dispose();
  738. }
  739. public void QueryChangeCardStatus(int MerchantId)
  740. {
  741. WebCMSEntities db = new WebCMSEntities();
  742. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  743. // JsonData results = JsonMapper.ToObject(HaoDaHelper.Instance.QueryChangeCardStatus(AddInfo.MchtNo, Infos["data"]["taskId"].ToString()));
  744. JsonData results = JsonMapper.ToObject(HaoDaHelper.Instance.QueryChangeCardStatus(AddInfo.MchtNo, "16592654"));
  745. }
  746. public void refund()
  747. {
  748. Thread th = new Thread(refunddo);
  749. th.IsBackground = true;
  750. th.Start();
  751. }
  752. public void refunddo()
  753. {
  754. for (int month = 10; month <= 11; month++)
  755. {
  756. for (int day = 1; day <= 31; day++)
  757. {
  758. string content = function.ReadInstance("/log/好哒聚合支付退款/2023/" + month + "/" + day + "/content.log");
  759. if (!string.IsNullOrEmpty(content))
  760. {
  761. content = content.Replace("\n\n\n", "####");
  762. content = content.Replace("\n", "");
  763. content = content.Replace("####", "\n");
  764. MatchCollection mc = Regex.Matches(content, "请求参数.*?退款次数超出限制");
  765. foreach (Match m in mc)
  766. {
  767. string mchtNo = Regex.Match(m.Value, "\"mchtNo\":\".*?\"").Value.Replace("\"mchtNo\":\"", "").Replace("\"", "");
  768. string storeNo = Regex.Match(m.Value, "\"storeNo\":\".*?\"").Value.Replace("\"storeNo\":\"", "").Replace("\"", "");
  769. string outOrderNo = Regex.Match(m.Value, "\"outOrderNo\":\".*?\"").Value.Replace("\"outOrderNo\":\"", "").Replace("\"", "");
  770. string refundAmount = Regex.Match(m.Value, "\"refundAmount\":\".*?\"").Value.Replace("\"refundAmount\":\"", "").Replace("\"", "");
  771. function.WriteLog("mchtNo:" + mchtNo + ";storeNo:" + storeNo + ";outOrderNo:" + outOrderNo + ";refundAmount:" + refundAmount + ";", "补充退款日志");
  772. var info = HaoDaHelper.Instance.AggregatedPayRefund(mchtNo, storeNo, "HDTK" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), outOrderNo, refundAmount);
  773. function.WriteLog(info + "\n\n", "补充退款日志");
  774. Thread.Sleep(1000);
  775. }
  776. }
  777. }
  778. }
  779. }
  780. }
  781. }