CheckAlipaySignService.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.Models;
  5. using Library;
  6. using LitJson;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. namespace MySystem
  10. {
  11. public class CheckAlipaySignService
  12. {
  13. public readonly static CheckAlipaySignService Instance = new CheckAlipaySignService();
  14. private CheckAlipaySignService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartListen);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void StartListen()
  23. {
  24. while (true)
  25. {
  26. string content = RedisDbconn.Instance.RPop<string>("AlipaySignQueue");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. string result = StartDo(Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantSign>(content));
  30. if(result == "wait")
  31. {
  32. Thread.Sleep(10000);
  33. RedisDbconn.Instance.AddList("AlipaySignQueue", content);
  34. }
  35. }
  36. else
  37. {
  38. Thread.Sleep(2000);
  39. }
  40. }
  41. }
  42. public string StartDo(MerchantSign sign)
  43. {
  44. try
  45. {
  46. WebCMSEntities db = new WebCMSEntities();
  47. string content = AlipayFunction.Instance.QuerySignStatus(sign.BusinessCode);
  48. JsonData dic = JsonMapper.ToObject(content);
  49. if (dic["alipay_open_agent_order_query_response"]["code"].ToString() == "10000")
  50. {
  51. // MERCHANT_INFO_HOLD=暂存,提交事务出现业务校验异常时,会暂存申请单信息,可以调用业务接口修正参数,并重新提交
  52. // MERCHANT_AUDITING=审核中,申请信息正在人工审核中
  53. // MERCHANT_CONFIRM=待商户确认,申请信息审核通过,等待联系人确认签约或授权
  54. // MERCHANT_CONFIRM_SUCCESS=商户确认成功,商户同意签约或授权
  55. // MERCHANT_CONFIRM_TIME_OUT=商户超时未确认,如果商户受到确认信息15天内未确认,则需要重新提交申请信息
  56. // MERCHANT_APPLY_ORDER_CANCELED=审核失败或商户拒绝,申请信息审核被驳回,或者商户选择拒绝签约或授权
  57. if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM")
  58. {
  59. string sign_url = dic["alipay_open_agent_order_query_response"]["confirm_url"].ToString();
  60. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  61. if (merchantadd != null)
  62. {
  63. merchantadd.QueryCount = 1;
  64. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  65. if (merchant != null)
  66. {
  67. merchant.QueryCount = 1;
  68. }
  69. string confirm_url = dic["alipay_open_agent_order_query_response"]["confirm_url"].ToString();
  70. string fileName = function.MD5_16(Guid.NewGuid().ToString());
  71. function.CreateQRCode2(sign_url, fileName, "/" + confirm_url);
  72. merchantadd.AlipaySignUrl = confirm_url;
  73. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  74. db.SaveChanges();
  75. }
  76. return "wait";
  77. }
  78. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM_SUCCESS")
  79. {
  80. //{"alipay_open_agent_order_query_response":{"code":"10000","msg":"Success","order_no":"20230704034000100000030319581766","product_agent_status_infos":[{"product_code":"FACE_TO_FACE_PAYMENT","product_name":"当面付","status":"EFFECTED"}],"order_status":"MERCHANT_CONFIRM_SUCCESS","merchant_pid":"2088641525908640"},"sign":"UyyrAf+Q+2jd5VVsaKUyWTMW8gFadUWWQIoYSTLzYFx0NOetjbN4XE9xq2kYvoJIYgQQOhohDKyAZZ7FOnOz9J849AXaqW1VYfVY9FSDvnSCdcUrAdyXGQEp7TcbnMtP5RTXZO+VCKRpvvUc/LVdXMPMetUbzhyaahQf/T512aQNuYFjxH5QfMdzGDwevt47iNoANITwXqns1A+ETBCuxHlx2Ps7WXpPxzqvFYK9CBD5MQCaq7IrDityzAUcMFiMjE3LlXb1zgg6PlsHnJBZm0Zde1PYzKZcHKnx2bxWl2upwpC3uj6drxzkGqzcZ4D21cTlndm8GIRLo2XHpd79Hw=="}
  81. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  82. if (merchantadd != null)
  83. {
  84. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  85. if(content.Contains("restrict_infos"))
  86. {
  87. if(dic["alipay_open_agent_order_query_response"]["restrict_infos"].Count > 0)
  88. {
  89. merchantadd.QueryCount = -1;
  90. merchantadd.AlipayRemark = dic["alipay_open_agent_order_query_response"]["restrict_infos"][0]["restrict_reason"].ToString();
  91. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  92. if (merchant != null)
  93. {
  94. merchant.QueryCount = -1;
  95. }
  96. }
  97. else
  98. {
  99. merchantadd.QueryCount = 2;
  100. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  101. if (merchant != null)
  102. {
  103. merchant.QueryCount = 2;
  104. }
  105. }
  106. }
  107. else
  108. {
  109. merchantadd.QueryCount = 2;
  110. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  111. if (merchant != null)
  112. {
  113. merchant.QueryCount = 2;
  114. }
  115. }
  116. db.SaveChanges();
  117. }
  118. }
  119. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED")
  120. {
  121. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  122. if (merchantadd != null)
  123. {
  124. string reason = "";
  125. if(dic["alipay_open_agent_order_query_response"]["restrict_infos"].Count > 0)
  126. {
  127. reason = dic["alipay_open_agent_order_query_response"]["restrict_infos"][0]["restrict_reason"].ToString();
  128. }
  129. merchantadd.AlipayRemark = reason;
  130. merchantadd.QueryCount = -1;
  131. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  132. if (merchant != null)
  133. {
  134. merchant.QueryCount = -1;
  135. }
  136. db.SaveChanges();
  137. }
  138. }
  139. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING")
  140. {
  141. return "wait";
  142. }
  143. }
  144. else
  145. {
  146. if(dic["alipay_open_agent_order_query_response"]["code"].ToString() == "40004" && dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString() == "系统繁忙")
  147. {
  148. return "wait";
  149. }
  150. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  151. if (merchantadd != null)
  152. {
  153. merchantadd.QueryCount = -1;
  154. merchantadd.AlipayRemark = dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString();
  155. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  156. if (merchant != null)
  157. {
  158. merchant.QueryCount = -1;
  159. }
  160. db.SaveChanges();
  161. }
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝签约审核队列异常");
  167. }
  168. return "";
  169. }
  170. }
  171. }