AlipayFunctionForHD.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using Aop.Api;
  4. using Library;
  5. using Microsoft.AspNetCore.Http;
  6. using Aop.Api.Request;
  7. using Aop.Api.Response;
  8. using Aop.Api.Util;
  9. using MySystem.Models;
  10. using System.Linq;
  11. using MySystem.Models.Main;
  12. namespace MySystem
  13. {
  14. public class AlipayFunctionForHD
  15. {
  16. public readonly static AlipayFunctionForHD Instance = new AlipayFunctionForHD();
  17. private AlipayFunctionForHD()
  18. { }
  19. // public string Pid = "2088720140260314";
  20. private string AppId = "2021004109682585";
  21. private string PrivateKey = "MIIEpAIBAAKCAQEAvf75JGQL9IDg8xu2C6qWrIO3H4roSwpvwUeh6y2kQ8iH0gqQXlyLj13ku8pzY1H9JTSeARCsHSD/VJFxMoPOBO+/xGDV3CSdxhOE5Fu5E30ZZMA5XmnMKyN8q+MEh7refGuCmldedTmzr9mNw4HDjS1uqJoqYRl3eUEaC/Syl620iiaSh7Q9/rsT/qNcnP5hO9P3DWionNt78elmjLt0sBdBkQ10o2XwC5RuzBfP8DCBEdL/DPOOE4vtmLP1IdDI7qtjBhX8PdL+/IRxe/4OGvYSPnHW7JNlN970h2UN5i0Yu0AkidJvlS59nX9C1WJ4U37HiMnkMNXLB/Ubi5BkgQIDAQABAoIBAQC1itVKpWIa/a5z/Q2EtjqEmtK2jAcL9Ov11dlsm07zCcBlvXxW/bzIf6IYogiBZY0qKM21HNYoeygeCV8NMo3/f4pQPWr7j+1Fn88CWHZOneFOuLHYeeUBb0YoA6KHgtImNqAzk7oI7t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IBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvf75JGQL9IDg8xu2C6qWrIO3H4roSwpvwUeh6y2kQ8iH0gqQXlyLj13ku8pzY1H9JTSeARCsHSD/VJFxMoPOBO+/xGDV3CSdxhOE5Fu5E30ZZMA5XmnMKyN8q+MEh7refGuCmldedTmzr9mNw4HDjS1uqJoqYRl3eUEaC/Syl620iiaSh7Q9/rsT/qNcnP5hO9P3DWionNt78elmjLt0sBdBkQ10o2XwC5RuzBfP8DCBEdL/DPOOE4vtmLP1IdDI7qtjBhX8PdL+/IRxe/4OGvYSPnHW7JNlN970h2UN5i0Yu0AkidJvlS59nX9C1WJ4U37HiMnkMNXLB/Ubi5BkgQIDAQAB";
  23. public string AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiXVtTk6opnYllnH/X40k/jSw/82WZ+lGbYQLYH/nVONFJ7Gr9C/dlKpJFIbS+vNDn+YLtM923KmEVSgf8zU0WrTkRjWvKEiyLswWZVqSZW5Jfon1e2SfjNrAVi6XBZa7urAItKQsdD2vFFuyG/ohlXBL103xC1wnjvECD/iholoruEFOLyfobJKTY/6iyrKeOElem1V7OWl6gdjG2oozj3nbVZhtkQvPJECYlolH96eK2mieTkFK82aZ5Tdzx1LPk/3hxRAmzz/9X7xfXdOly88hKBmW8LC9RhTlOWbrXJhhFz13A4brnbnOiY/X7b2OkAYifammc2uh0AIJlCmBTQIDAQAB";
  24. //"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
  25. #region 商家认证申请单提交
  26. public string AuthOrderCreate(MerchantAddInfo info, string MerToken)
  27. {
  28. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  29. AlipayMerchantIndirectAuthorderCreateRequest request = new AlipayMerchantIndirectAuthorderCreateRequest();
  30. string out_biz_no = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  31. string identity_type = info.SubjectType;
  32. string store_address = info.BizStoreAddress;
  33. string merchant_type = "STORE";
  34. if (!string.IsNullOrEmpty(info.MerchantType))
  35. {
  36. merchant_type = info.MerchantType;
  37. }
  38. if (merchant_type == "STALL")
  39. {
  40. store_address = "无";
  41. }
  42. if (identity_type == "SUBJECT_TYPE_ENTERPRISE") identity_type = "ENTERPRISE";
  43. if (identity_type == "SUBJECT_TYPE_INDIVIDUAL") identity_type = "IND_BIZ";
  44. if (identity_type == "SUBJECT_TYPE_SMALL")
  45. {
  46. identity_type = "MSE";
  47. }
  48. string PeriodEnd = info.PeriodEnd.Value.ToString("yyyy-MM-dd");
  49. if (PeriodEnd.Substring(0, 4) == "9999")
  50. {
  51. PeriodEnd = "forever";
  52. }
  53. string CardPeriodEnd = info.CardPeriodEnd.Value.ToString("yyyy-MM-dd");
  54. if (CardPeriodEnd.Substring(0, 4) == "2050")
  55. {
  56. CardPeriodEnd = "forever";
  57. }
  58. string CertMerchantName = info.CertMerchantName;
  59. if (info.SubjectType == "SUBJECT_TYPE_SMALL")
  60. {
  61. info.LicenseCopy = info.IdCardCopy;
  62. for (int i = 0; i < 9; i++)
  63. {
  64. CertMerchantName = CertMerchantName.Replace(i.ToString(), "");
  65. }
  66. }
  67. string BizContent = "{" +
  68. " \"out_biz_no\":\"" + out_biz_no + "\"," +
  69. " \"contact_person_info\":{" +
  70. " \"contact_name\":\"" + info.CertLegalPerson + "\"," +
  71. " \"contact_phone_no\":\"" + info.MobilePhone + "\"," +
  72. " \"contact_card_no\":\"" + info.IdCardNumber + "\"," +
  73. " \"contact_cert_type\":\"RESIDENT\"" +
  74. " }," +
  75. " \"auth_identity_info\":{" +
  76. " \"identity_type\":\"" + identity_type + "\"," +
  77. " \"is_financial_org\":false,";
  78. if (info.SubjectType != "SUBJECT_TYPE_SMALL")
  79. {
  80. BizContent += " \"certificate_type\":\"BUSINESS_CERT\"," +
  81. " \"certificate_info\":{" +
  82. " \"cert_type\":\"UNIT_SOCIAL_CREDIT\"," +
  83. " \"cert_no\":\"" + info.LicenseNumber + "\"," +
  84. " \"cert_image\":\"" + UploadPic(info.LicenseCopy) + "\"," +
  85. " \"merchant_name\":\"" + info.CertMerchantName + "\"," +
  86. " \"legal_person_name\":\"" + info.CertLegalPerson + "\"," +
  87. " \"register_address\":\"" + info.BizAddressCode.Replace(",", "") + info.BizStoreAddress + "\"," +
  88. " \"effect_time\":\"" + info.PeriodBegin.Value.ToString("yyyy-MM-dd") + "\"," +
  89. " \"expire_time\":\"" + PeriodEnd + "\"" +
  90. " },";
  91. }
  92. if (info.SubjectType == "SUBJECT_TYPE_SMALL")
  93. {
  94. string bizAddressCode = info.BizAddressCode;
  95. string province_code = "", city_code = "", district_code = "";
  96. string province = "", city = "", district = "";
  97. WebCMSEntities db = new WebCMSEntities();
  98. HaoDaAreaCode cityItem = db.HaoDaAreaCode.FirstOrDefault(m => m.CityName.EndsWith(bizAddressCode));
  99. if (cityItem != null)
  100. {
  101. district_code = cityItem.CityCode;
  102. city_code = district_code.Substring(0, 4) + "00";
  103. province_code = district_code.Substring(0, 2) + "0000";
  104. string[] CityNames = cityItem.CityName.Split(',');
  105. province = CityNames[0];
  106. if (CityNames.Length >= 2)
  107. {
  108. city = CityNames[1];
  109. }
  110. if (CityNames.Length >= 3)
  111. {
  112. district = CityNames[2];
  113. }
  114. }
  115. BizContent += " \"support_credentials\":{" +
  116. " \"merchant_type\":\"" + merchant_type + "\"," +
  117. " \"store_name\":\"" + info.BizStoreName + "\"," +
  118. " \"province_code\":\"" + province_code + "\"," +
  119. " \"province\":\"" + province + "\"," +
  120. " \"city_code\":\"" + city_code + "\"," +
  121. " \"city\":\"" + city + "\"," +
  122. " \"district_code\":\"" + district_code + "\"," +
  123. " \"district\":\"" + district + "\"," +
  124. " \"store_address\":\"" + store_address + "\"," +
  125. " \"store_door_img\":\"" + UploadPic(info.StoreEntrancePic.Split(',')[0]) + "\"," +
  126. " \"store_inner_img\":\"" + UploadPic(info.IndoorPic.Split(',')[0]) + "\"" +
  127. " }";
  128. }
  129. BizContent += " }," +
  130. " \"legal_person_info\":{" +
  131. " \"legal_type\":\"LEGAL_PERSON\"," +
  132. " \"card_type\":\"RESIDENT\"," +
  133. " \"person_name\":\"" + info.CertLegalPerson + "\"," +
  134. " \"card_no\":\"" + info.IdCardNumber + "\"," +
  135. " \"effect_time\":\"" + info.CardPeriodBegin.Value.ToString("yyyy-MM-dd") + "\"," +
  136. " \"expire_time\":\"" + CardPeriodEnd + "\"," +
  137. " \"card_front_img\":\"" + UploadPic(info.IdCardCopy) + "\"," +
  138. " \"card_back_img\":\"" + UploadPic(info.IdCardNational) + "\"," +
  139. " \"is_benefit_person\":true" +
  140. " }" +
  141. "}";
  142. request.BizContent = BizContent;
  143. LogHelper.Instance.WriteLog(BizContent, "支付宝开户意愿确认");
  144. string result = "";
  145. try
  146. {
  147. AlipayMerchantIndirectAuthorderCreateResponse response = client.Execute(request);
  148. result = response.Body;
  149. LogHelper.Instance.WriteLog(result, "支付宝开户意愿确认");
  150. }
  151. catch (Exception ex)
  152. {
  153. LogHelper.Instance.WriteLog(ex.ToString(), "支付宝开户意愿确认异常");
  154. result = "";
  155. }
  156. return result;
  157. }
  158. #endregion
  159. #region 查询商家认证申请单状态
  160. public string AuthOrderQuery(string OrderNo)
  161. {
  162. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  163. AlipayMerchantIndirectAuthorderQuerystatusRequest request = new AlipayMerchantIndirectAuthorderQuerystatusRequest();
  164. request.BizContent = "{" +
  165. " \"order_no\":\"" + OrderNo + "\"" +
  166. "}";
  167. LogHelper.Instance.WriteLog(request.BizContent, "查询支付宝商家认证申请单状态");
  168. string result = "";
  169. try
  170. {
  171. AlipayMerchantIndirectAuthorderQuerystatusResponse response = client.Execute(request);
  172. result = response.Body;
  173. LogHelper.Instance.WriteLog(result, "查询支付宝商家认证申请单状态");
  174. }
  175. catch (Exception ex)
  176. {
  177. LogHelper.Instance.WriteLog(ex.ToString(), "查询支付宝商家认证申请单状态异常");
  178. result = "";
  179. }
  180. return result;
  181. }
  182. #endregion
  183. #region 获取商家认证状态
  184. public string AuthOrderAuthQuery(string MerchantId)
  185. {
  186. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  187. AlipayMerchantIndirectSmidbindQueryRequest request = new AlipayMerchantIndirectSmidbindQueryRequest();
  188. request.BizContent = "{" +
  189. " \"sub_merchant_id\":\"" + MerchantId + "\"" +
  190. "}";
  191. LogHelper.Instance.WriteLog(request.BizContent, "获取商家认证状态");
  192. string result = "";
  193. try
  194. {
  195. //{\"alipay_merchant_indirect_smidbind_query_response\":{\"code\":\"10000\",\"msg\":\"Success\",\"check_result\":\"AUTHORIZED\"},\"sign\":\"PZo+ozUifPnIJhT/v9xG3PxJu612XMoLX3dgloqBzyyTK05FBtv9i2Au57PxnJYTvZ226JFPJC+QAEiTA3FOHdbd/dXwRtUrTg06SmXsXBipRiwNEwFxxvlGrsXzIPOi4C6nWsyphMDi9CJhLqkUhjO9cpZQE4x3MJ6PIFFjbuvqStJ0dS8xQjCqzygSU3foEetyh7IWs3To1DU8AARDJZ9eOTJB0bznT4g7f8EJgx/MpohRDWmIWWqJYq6HnPhGYtqQcPyuVKej6e9OjjKYe2fHbZPC966QoKHnptzBcNay7gvce8bSp9c78nmiz20moHttGdzZYjvvQ4zOuftExw==\"}
  196. AlipayMerchantIndirectSmidbindQueryResponse response = client.Execute(request);
  197. result = response.Body;
  198. LogHelper.Instance.WriteLog(result, "获取商家认证状态");
  199. }
  200. catch (Exception ex)
  201. {
  202. LogHelper.Instance.WriteLog(ex.ToString(), "获取商家认证状态异常");
  203. result = "";
  204. }
  205. return result;
  206. }
  207. #endregion
  208. #region 图片上传
  209. public string UploadPic(string path)
  210. {
  211. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  212. string fileName = path.Substring(path.LastIndexOf("/") + 1);
  213. string imageBase64 = function.imageToBase64String(function.getPath(path));
  214. // imageBase64 = imageBase64.Substring(imageBase64.IndexOf(";base64,") + 8);
  215. AntMerchantExpandIndirectImageUploadRequest request = new AntMerchantExpandIndirectImageUploadRequest();
  216. FileItem imageContent = new FileItem(fileName, Convert.FromBase64String(imageBase64));
  217. // FileItem imageContent = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + path);
  218. request.ImageContent = imageContent;
  219. request.ImageType = "jpg";
  220. try
  221. {
  222. AntMerchantExpandIndirectImageUploadResponse response = client.Execute(request);
  223. fileName = response.ImageId;
  224. }
  225. catch (Exception ex)
  226. {
  227. LogHelper.Instance.WriteLog(ex.ToString(), "支付宝图片上传异常");
  228. }
  229. return fileName;
  230. }
  231. #endregion
  232. }
  233. }