AlipayFunctionForKxs.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Aop.Api;
  5. using Library;
  6. using LitJson;
  7. using Microsoft.AspNetCore.Http;
  8. using Aop.Api.Request;
  9. using Aop.Api.Response;
  10. using Aop.Api.Util;
  11. using System.Collections;
  12. using MySystem.Models.KxsMainModels;
  13. namespace MySystem
  14. {
  15. public class AlipayFunctionForKxs
  16. {
  17. private HttpContext _context;
  18. private string AppId = "2021003116616751";
  19. private string PrivateKey = "MIIEpAIBAAKCAQEA1KjMzZJIRG6QQ3+c/euVrc+8RR0xMw6A+EONSKhJq4Bg+rtxQPCfdF2bpZ4NMaSqfalSiLoxrZSYBYOZhpDP67i9LXGUoFCWdRL7ROTqJvuU6KyEeMplQ6Q3uwWbuKbh9COt0vhqn/pjlUsomTwq+DRQv10awynkZikqceXdP0EH4iGZG0hyerEP5yLHpHiggf4f7TOsovHX+fGLPmWFO1zUTHHbz7HxU4BQKn3yuje4yMhoyv5h/wZzeVwvj+Qti8Hlh351rF0f5BHwN1YMGIrWuPU5bF8OMHS/UXPxh7Bs795ZZyplYztTz26QfKfLWVVsLOng4pvgPS+OgzZydwIDAQABAoIBAEu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yaZSAvSYK+lw8l53bTbJpJ3pq/1Tol6tyjY7HRsVvQ68Jn1MApARMlldn1GoAPcgDFzlJCB0JCOUAE1rBS5lA97wQ1YhI8qBRqaE7Qrtf4D5oZdEjbtq8VCeIaD9huIQ==";
  20. private string PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1KjMzZJIRG6QQ3+c/euVrc+8RR0xMw6A+EONSKhJq4Bg+rtxQPCfdF2bpZ4NMaSqfalSiLoxrZSYBYOZhpDP67i9LXGUoFCWdRL7ROTqJvuU6KyEeMplQ6Q3uwWbuKbh9COt0vhqn/pjlUsomTwq+DRQv10awynkZikqceXdP0EH4iGZG0hyerEP5yLHpHiggf4f7TOsovHX+fGLPmWFO1zUTHHbz7HxU4BQKn3yuje4yMhoyv5h/wZzeVwvj+Qti8Hlh351rF0f5BHwN1YMGIrWuPU5bF8OMHS/UXPxh7Bs795ZZyplYztTz26QfKfLWVVsLOng4pvgPS+OgzZydwIDAQAB";
  21. private string AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxycehrmnTl2IaIAECP+0HWc1eWGM/T6x022K7K6fdf+xoyC97TBJaG4G3Bcon4gsJX+Sad5IA9OJIQ4eN1joM35hIlwcFZ6MmCuVXx/SMAKThIjK0jsRc6aTxVQG+GVCgS7dXMKb4Sm+vK2wEM6xtDeqcfYMB0BqGZRvxbIV59a2ooPrha4FkV6nNC9DQefzC0picHXV64oYUjMM2ed4ASTQVds7HP9BZfalz//APZcWdBV08f+xv7n79Twz/gNtVAadfuTAlcjsS6aK+G/OrZtr/diuemeRPRUKlVhcr5xOQDhqBgtdIw8M9Wp6I4ULXRlthKVBWToH9j6n9vDIiwIDAQAB";
  22. public AlipayFunctionForKxs(HttpContext context)
  23. {
  24. _context = context;
  25. }
  26. #region 代商家签约
  27. //(开启事务)接口创建应用事务,返回生成代商户操作事务编号 batch_no
  28. public string GetBatchNo(string AlipayAccount, string Name, string Mobile, string Email)
  29. {
  30. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  31. AlipayOpenAgentCreateRequest request = new AlipayOpenAgentCreateRequest();
  32. request.BizContent = "{" +
  33. "\"account\":\"" + AlipayAccount + "\"," +
  34. "\"contact_info\":{" +
  35. "\"contact_name\":\"" + Name + "\"," +
  36. "\"contact_mobile\":\"" + Mobile + "\"," +
  37. "\"contact_email\":\"" + Email + "\"" +
  38. "}" +
  39. "}";
  40. string result = "";
  41. try
  42. {
  43. AlipayOpenAgentCreateResponse response = client.Execute(request);
  44. result = response.Body;
  45. //{"alipay_open_agent_create_response":{"code":"10000","msg":"Success","batch_no":"2022021011321326200059849","batch_status":"init"},"sign":"su+weyK1CZEXES8mdckalg16J4BELTQeVNJSpXVYg3nh5g3E7jDh/mQctkjH8HbM0RybQSahZ2j60aUTossPMXqdq2nPRkR4c/kM34toFFNU/2p7jqo9k/MbB1JotW+qFI+C/GCX/9mZQqm2tSQdYIiZlCsEtP13ZP2EQ6momEZrApyLQYUl1H54PojJgqo6zGKDXTnXuZcpi/VPP5YoWhYMx8WPFMS6+T3zr+LNPDOI94j5vDdUHQdjFAGiQMyyhfNOBcFFi18bAfWhPqlLWwblCZamTHN/QaOHo+fkBZeo9uKqrnSHPn8XW0tDAIf1qNUfY2eBzYZUIqWKbz509A=="}
  46. }
  47. catch (Exception ex)
  48. {
  49. function.WriteLog(ex.ToString(), "接口创建应用事务异常");
  50. result = "";
  51. }
  52. return result;
  53. }
  54. //代签约产品通用接口,代商家发起签约产品申请
  55. public string CommonSign(string BatchNo, string MccCode, string BusinessLicenseNo = "", string BusinessLicensePicPath = "", string ShopScenePicPath = "", string ShopSignBoardPicPath = "")
  56. {
  57. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  58. AlipayOpenAgentCommonSignRequest request = new AlipayOpenAgentCommonSignRequest();
  59. request.BatchNo = BatchNo;
  60. request.MccCode = MccCode; //软件系统商 D_D05_7372
  61. FileItem SpecialLicensePic = new FileItem(BusinessLicensePicPath);
  62. request.SpecialLicensePic = SpecialLicensePic;
  63. if (!string.IsNullOrEmpty(BusinessLicenseNo))
  64. {
  65. request.BusinessLicenseNo = BusinessLicenseNo; //营业执照号码
  66. }
  67. if (!string.IsNullOrEmpty(BusinessLicensePicPath))
  68. {
  69. FileItem BusinessLicensePic = new FileItem(BusinessLicensePicPath);
  70. request.BusinessLicensePic = BusinessLicensePic; //营业执照图片。被代创建商户运营主体为个人账户必填,企业账户无需填写
  71. }
  72. FileItem BusinessLicenseAuthPic = new FileItem(BusinessLicensePicPath);
  73. request.BusinessLicenseAuthPic = BusinessLicenseAuthPic;
  74. request.LongTerm = true;
  75. request.DateLimitation = "长期";
  76. if (!string.IsNullOrEmpty(ShopScenePicPath))
  77. {
  78. FileItem ShopScenePic = new FileItem(ShopScenePicPath);
  79. request.ShopScenePic = ShopScenePic;
  80. }
  81. if (!string.IsNullOrEmpty(ShopSignBoardPicPath))
  82. {
  83. FileItem ShopSignBoardPic = new FileItem(ShopSignBoardPicPath);
  84. request.ShopSignBoardPic = ShopSignBoardPic;
  85. }
  86. request.ProductCode = "QUICK_WAP_WAY"; //isv要代商户签约产品码,产品码是支付宝内部对产品的唯一标识
  87. request.AppName = "客小爽";
  88. // FileItem AppDemo = new FileItem(BusinessLicensePicPath);
  89. // request.AppDemo = AppDemo;
  90. List<string> WebSites = new List<string>();
  91. WebSites.Add("www.kexiaoshuang.com");
  92. request.WebSites = WebSites;
  93. request.AlipayLifeName = "客小爽";
  94. request.WechatOfficialAccountName = "客小爽";
  95. // FileItem WebSitesLoa = new FileItem(BusinessLicensePicPath);
  96. // request.WebSitesLoa = WebSitesLoa;
  97. request.WebTestAccount = "testAccount";
  98. request.WebTestAccountPassword = "testPassword";
  99. FileItem WebHomeScreenshot = new FileItem(BusinessLicensePicPath);
  100. request.WebHomeScreenshot = WebHomeScreenshot;
  101. FileItem WebItemScreenshot = new FileItem(BusinessLicensePicPath);
  102. request.WebItemScreenshot = WebItemScreenshot;
  103. FileItem WebPayScreenshot = new FileItem(BusinessLicensePicPath);
  104. request.WebPayScreenshot = WebPayScreenshot;
  105. request.WebStatus = "已上线";
  106. string result = "";
  107. try
  108. {
  109. AlipayOpenAgentCommonSignResponse response = client.Execute(request);
  110. result = response.Body;
  111. //{"alipay_open_agent_common_sign_response":{"code":"10000","msg":"Success"},"sign":"qt0DiXFXIJ7mX+W2+IX+cyYXx8ROGe8yxesXHGNltxpEv/s/zW16xzf4HQLtUlaAiWf3DiD2QV7Hj+REyoZw9AcktDAmlobcjQ28lTYufPECZoUJHpkn9VpGB932FC0hScVwCbsxQakH84MGT4cmjr1hCuSs7+XogdhlCTIKjau1e7ZglLnt7f1NqT8JnljVhmvkkE9gJTLq5H+Zq6XbFfBVptvM9w09HdjfhrsxAXJyQ4yU6JYX2tUYsRGz8ZrveDQMvXKTN9jnwKE9hh8ik6Sq49eUqfutV9GkKqhC96+Vy3nOTOTnPoECXZkco1wP5jsqHottWVsRV+NxdylnTw=="}
  112. }
  113. catch (Exception ex)
  114. {
  115. function.WriteLog(ex.ToString(), "代签约产品通用接口异常");
  116. result = "";
  117. }
  118. return result;
  119. }
  120. //代商户签约,提交信息确认接口
  121. public string CommonSignConfirm(string BatchNo)
  122. {
  123. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  124. AlipayOpenAgentCommonsignConfirmRequest request= new AlipayOpenAgentCommonsignConfirmRequest();
  125. request.BizContent="{" +
  126. "\"batch_no\":\"" + BatchNo + "\"" +
  127. "}";
  128. string result = "";
  129. try
  130. {
  131. AlipayOpenAgentCommonsignConfirmResponse response = client.Execute(request);
  132. result = response.Body;
  133. //{"alipay_open_agent_order_query_response":{"code":"10000","msg":"Success","order_no":"20220210034000100000015895929361","product_agent_status_infos":[{"product_code":"QUICK_WAP_WAY","product_name":"手机网站支付","status":"WAIT_CONFIRM"}],"order_status":"MERCHANT_CONFIRM","confirm_url":"https:\/\/openhome.alipay.com\/isv\/settling\/confirm.htm?orderNo=20220210034000100000015895929361","merchant_pid":"2088012934025363"},"sign":"mgdhauHMV9352S3FVrQmesXvHOrk633FX/EOXRYdc2ix5phCBZJ2KZRRP9MWJ566YEtWbnQlAJzHVS9X29hjgiJWOQ/wCidql4AoSAYYeqPtxiqKAZw/QEM/du5AdbWC0xBmmkQljXF9Hx8cm+IrOyPMSJXIw4Tqpm/T96YBwhsjw4Xr5dcUcvF6HX9h/i5FSdIJODxQ26PJcqQ98GQ3Y50YhTpPQkIPDFVE2QzcL6dvuhBOpwu5KbTxvgzS8Wtm3G6nMbzJ33Cv/I2lhTCIMHz90c9bvyjJpWxPWBzywO2zzv7+EZCbDkZGkdRic9l5s6qac+ZVMHkPbveaZ2wyig=="}
  134. }
  135. catch (Exception ex)
  136. {
  137. function.WriteLog(ex.ToString(), "接口创建应用事务异常");
  138. result = "";
  139. }
  140. return result;
  141. }
  142. #endregion
  143. #region (查询申请单状态)查询签约申请的结果
  144. public string QuerySignStatus(string BatchNo)
  145. {
  146. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  147. AlipayOpenAgentOrderQueryRequest request= new AlipayOpenAgentOrderQueryRequest() ;
  148. request.BizContent="{" +
  149. " \"batch_no\":\"" + BatchNo + "\"" +
  150. "}";
  151. string result = "";
  152. try
  153. {
  154. AlipayOpenAgentOrderQueryResponse response = client.Execute(request);
  155. result = response.Body;
  156. }
  157. catch (Exception ex)
  158. {
  159. function.WriteLog(ex.ToString(), "查询签约申请的结果异常");
  160. result = "";
  161. }
  162. return result;
  163. }
  164. #endregion
  165. #region 小程序支付请调用MySystemLib下的统一收单交易创建,返回交易号返给小程序请求支付my.tradePay
  166. public string GetToken()
  167. {
  168. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  169. AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
  170. request.BizContent = "{" +
  171. "\"grant_type\":\"authorization_code\"," +
  172. "\"code\":\"P16be77ee92264acc99553528b8a7103\"" +
  173. " }";
  174. string result = "";
  175. try
  176. {
  177. AlipayOpenAuthTokenAppResponse response = client.Execute(request);
  178. result = response.Body;
  179. //{"alipay_open_auth_token_app_response":{"code":"10000","msg":"Success","tokens":[{"app_auth_token":"202202BBd8c1365cd09b4a27a0c0f0b7f8cabX03","app_refresh_token":"202202BB539f0f5aa99f4b509aa3628b0af25X03","auth_app_id":"2021003116654736","expires_in":31536000,"re_expires_in":32140800,"user_id":"2088141704579033"}]},"sign":"VZINXguZZFvVDyhVp/iMK5BWXYnDYLvxYEMEZTZOVUl9MUy6xQesDzpwBBUclLLU+QgrKpeekaxj9zgwRXdh7cKlVot2lkVPkGdfkqcK/B9t8kxPXa13WIszcROCqIutlcBZW9pCIUAbWRjorDvzRCc5rLNfSBAjEldZDA0rGOPeWJXM/5uLi/70oTwyO6eaEbKdRyHLjUwp0qtolhXIvyf9HL1HbZMGtyY/Hzq35BmhB/jQ4XIVdcOEpFtNoC/1S/viLOsBCD2m4aNCGUY0i24lr92dRsEinZlCxAQq+FK6ILNUnDmBUjNkPTycDrJFw+w9sEC6AcZodNB6OC8XTw=="}
  180. }
  181. catch (Exception ex)
  182. {
  183. function.WriteLog(ex.ToString(), "支付宝统一收单交易创建(获取authtoken)异常");
  184. result = "";
  185. }
  186. return result;
  187. }
  188. public string CreateTrade(string OrderNo, string Subject, string Amount, string BuyerId, string Notify_Url, string Token)
  189. {
  190. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  191. AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
  192. request.SetNotifyUrl(Notify_Url);
  193. request.BizContent = "{" +
  194. "\"out_trade_no\":\"" + OrderNo + "\"," +
  195. "\"total_amount\":" + Amount + "," +
  196. "\"subject\":\"" + Subject + "\"," +
  197. "\"buyer_id\":\"" + BuyerId + "\"," +
  198. "\"timeout_express\":\"10m\"" +
  199. "}";
  200. string result = "";
  201. try
  202. {
  203. AlipayTradeCreateResponse response = client.Execute(request, null, Token);
  204. result = response.Body;
  205. function.WriteLog(DateTime.Now.ToString() + "\n" + result, "支付宝统一收单交易创建");
  206. }
  207. catch (Exception ex)
  208. {
  209. function.WriteLog(ex.ToString(), "支付宝统一收单交易创建异常");
  210. result = "";
  211. }
  212. return result;
  213. }
  214. public string QueryTrade(string OrderNo, string Token)
  215. {
  216. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  217. AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
  218. request.BizContent = "{" +
  219. "\"out_trade_no\":\"" + OrderNo + "\"" +
  220. "}";
  221. string result = "";
  222. try
  223. {
  224. AlipayTradeQueryResponse response = client.Execute(request, null, Token);
  225. result = response.Body;
  226. }
  227. catch (Exception ex)
  228. {
  229. function.WriteLog(ex.ToString(), "统一收单线下交易查询异常");
  230. result = "";
  231. }
  232. return result;
  233. }
  234. #endregion
  235. //小程序
  236. string MiniAppId = "2021003116654736";
  237. //应用公钥
  238. // string PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnTchC1mU0rFnRv8/eqDz1i/gS5Wd5fDD/TobKDBlj1UgQPRnT9g8wy/uTtqMnImR09ZQ5C/Ekw8B2GlN6bLbza0+7Fym7rNNKw3EvEL8tQ6pMe1GbIu68tCEjIslPXswGCgGRI30LeSj0BElaSAaqbvvyTHp+vxNLwav/hSGokPa8C76SzgCpvvzjXk9oGiNV5eutdoWEKbwyvw45kv70JHragNANTqU8DouzPLHenoD5V+QqJw1He9mZSb08QgSHCFW1dau8Hmxy0oXsfUVXXHJfzkhoyJOerYiR+oVuRqaIIF9xFlUPsNdpsAnJCP+HaAqRJpvuKfV4hNdg7uvIQIDAQAB";
  239. //支付宝公钥
  240. string MiniAlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxycehrmnTl2IaIAECP+0HWc1eWGM/T6x022K7K6fdf+xoyC97TBJaG4G3Bcon4gsJX+Sad5IA9OJIQ4eN1joM35hIlwcFZ6MmCuVXx/SMAKThIjK0jsRc6aTxVQG+GVCgS7dXMKb4Sm+vK2wEM6xtDeqcfYMB0BqGZRvxbIV59a2ooPrha4FkV6nNC9DQefzC0picHXV64oYUjMM2ed4ASTQVds7HP9BZfalz//APZcWdBV08f+xv7n79Twz/gNtVAadfuTAlcjsS6aK+G/OrZtr/diuemeRPRUKlVhcr5xOQDhqBgtdIw8M9Wp6I4ULXRlthKVBWToH9j6n9vDIiwIDAQAB";
  241. //应用私钥
  242. string MiniPrivateKey = "MIIEogIBAAKCAQEAnTchC1mU0rFnRv8/eqDz1i/gS5Wd5fDD/TobKDBlj1UgQPRnT9g8wy/uTtqMnImR09ZQ5C/Ekw8B2GlN6bLbza0+7Fym7rNNKw3EvEL8tQ6pMe1GbIu68tCEjIslPXswGCgGRI30LeSj0BElaSAaqbvvyTHp+vxNLwav/hSGokPa8C76SzgCpvvzjXk9oGiNV5eutdoWEKbwyvw45kv70JHragNANTqU8DouzPLHenoD5V+QqJw1He9mZSb08QgSHCFW1dau8Hmxy0oXsfUVXXHJfzkhoyJOerYiR+oVuRqaIIF9xFlUPsNdpsAnJCP+HaAqRJpvuKfV4hNdg7uvIQIDAQABAoIBACrMZDmDxIjq9JOWTwCiKl8nn2bULyz2s1QPam8xZaR0ZxhnsGxDEJ+OP9DFnDvNE57YnCLSgjAKiNUSLbyuDTpbrGKKAoO+2gYbdHB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aJubvSGyZOKNMbcvAQx5tnNbF5SrVU=";
  243. //小程序AES密钥,目前用于解密手机号
  244. string Key = "2Krm6dSew4NAL+l0mQf0fA==";
  245. public string GetAlipayUserId(string Auth_code)
  246. {
  247. string result = "";
  248. try
  249. {
  250. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", MiniAppId, MiniPrivateKey, "json", "1.0", "RSA2", MiniAlipayPublicKey, "UTF-8", false);
  251. //获取access_token
  252. AlipaySystemOauthTokenRequest requestAccess_token = new AlipaySystemOauthTokenRequest();
  253. requestAccess_token.GrantType = "authorization_code";
  254. requestAccess_token.Code = Auth_code;
  255. AlipaySystemOauthTokenResponse responseAccess_token = client.Execute(requestAccess_token);
  256. string AlipayUserId = responseAccess_token.AlipayUserId; //20881043611849171819285350010300
  257. string UserId = responseAccess_token.UserId; //2088222145274001
  258. result = UserId + "|" + AlipayUserId;
  259. }
  260. catch (Exception ex)
  261. {
  262. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "支付宝授权报错日志");
  263. }
  264. return result;
  265. }
  266. public string GetAlipayMobile(string response)
  267. {
  268. function.WriteLog(response, "解密报文");
  269. //1. 获取验签和解密所需要的参数
  270. IDictionary openapiResult = Jayrock.Json.Conversion.JsonConvert.Import(response) as IDictionary;
  271. string signType;
  272. if (openapiResult.Contains("sign_type"))
  273. {
  274. signType = openapiResult["sign_type"].ToString();
  275. }
  276. else
  277. {
  278. signType = "RSA2";
  279. }
  280. string charset;
  281. if (openapiResult.Contains("charset"))
  282. {
  283. charset = openapiResult["charset"].ToString();
  284. }
  285. else
  286. {
  287. charset = "UTF-8";
  288. }
  289. string encryptType;
  290. if (openapiResult.Contains("encrypt_type"))
  291. {
  292. encryptType = openapiResult["encrypt_type"].ToString();
  293. }
  294. else
  295. {
  296. encryptType = "AES";
  297. }
  298. string sign = openapiResult["sign"].ToString();
  299. string content = openapiResult["response"].ToString();
  300. // 是否为加密报文
  301. bool isDataEncrypted = !content.StartsWith("{", StringComparison.Ordinal);
  302. // bool signCheckPass = false;
  303. //2. 验签
  304. string signContent = content;
  305. signContent = "\"" + signContent + "\"";
  306. string signVeriKey = MiniAlipayPublicKey;
  307. string decryptKey = MiniPrivateKey;
  308. string result = "";
  309. try
  310. {
  311. function.WriteLog(content, "解密报文");
  312. result = AlipayEncrypt.AesDencrypt(Key, content, charset);
  313. JsonData obj = JsonMapper.ToObject(result);
  314. if (obj["code"].ToString() == "10000")
  315. {
  316. result = "success|" + obj["mobile"].ToString();
  317. }
  318. //{\"code\":\"10000\",\"msg\":\"Success\",\"mobile\":\"14781419364\"}
  319. }
  320. catch (Exception ex)
  321. {
  322. //解密异常, 记录日志
  323. function.WriteLog(ex.ToString(), "获取手机号解密异常");
  324. }
  325. return result;
  326. // 如果是加密的报文则需要在密文的前后添加双引号
  327. // if (isDataEncrypted)
  328. // {
  329. // signContent = "\"" + signContent + "\"";
  330. // }
  331. // try
  332. // {
  333. // signContent = signContent.Trim('"');
  334. // signCheckPass = AlipaySignature.RSACheckContent(signContent, sign, signVeriKey, charset, signType);
  335. // }
  336. // catch (Exception ex)
  337. // {
  338. // //验签异常, 日志
  339. // throw new Exception("验签失败", ex);
  340. // }
  341. // if (!signCheckPass)
  342. // {
  343. // //验签不通过(异常或者报文被篡改),终止流程(不需要做解密)
  344. // throw new Exception("验签失败");
  345. // }
  346. // //3. 解密
  347. // string plainData = null;
  348. // if (isDataEncrypted)
  349. // {
  350. // try
  351. // {
  352. // plainData = AlipayEncrypt.AesDencrypt(decryptKey, content, charset);
  353. // }
  354. // catch (Exception ex)
  355. // {
  356. // //解密异常, 记录日志
  357. // throw new Exception("解密异常", ex);
  358. // }
  359. // }
  360. // else
  361. // {
  362. // plainData = content;
  363. // }
  364. // return result;
  365. }
  366. public string test()
  367. {
  368. string result = "";
  369. string str = "PsQMXBobiYsZPG5q2dbsX5Zvnee73Zpg55guE2FTxMijl+xdj+oCjEErUpZNxRK0S132GVcWBQUjFeA5fcLq1A==";
  370. try
  371. {
  372. result = AlipayEncrypt.AesDencrypt(Key, str, "UTF-8");
  373. JsonData obj = JsonMapper.ToObject(result);
  374. if (obj["code"].ToString() == "10000")
  375. {
  376. result = "success|" + obj["mobile"].ToString();
  377. }
  378. //{\"code\":\"10000\",\"msg\":\"Success\",\"mobile\":\"14781419364\"}
  379. }
  380. catch (Exception ex)
  381. {
  382. //解密异常, 记录日志
  383. function.WriteLog(ex.ToString(), "获取手机号解密异常");
  384. }
  385. return result;
  386. }
  387. // 支付宝参数
  388. public PublicAccountSet SetData(int UserId = 0)
  389. {
  390. WebCMSEntities db = new WebCMSEntities();
  391. PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  392. // set.AlipayAppId = "2021003122642236";
  393. // set.AlipayPrivateKey = "MIIEpAIBAAKCAQEAmfSplznW6H+wCyKh806jBxVdRabpK2P9RP4QNSGsHNIAscuH2yPbI1dNfKESC+QXAfWVRg90Vb+7yQz1I/S2vT159yY7ZH6duCopPpScfpNmICtjVukc1V5PZX+ptuDZc0YQSsRnn5WVKNO0bBR6iS3Jl9HQT949I3rHttA1+iqBn2LWyvGcUSDCOFNwstOYFM3FJTg7rQEdjmEQonEPXSbTQl1aw+fx1D+NcGx9BFSVAbE+nqfiX2co4V6yQ++N1RMlnBtbMZmTgvcoHZujqqBmjRcfRUW8I0B9aPJGNji94STBRPf0hP7yN4PHP2LixAK5gfzZ5NP2iK99sQ9tmwIDAQABAoIBAAR0N5PQcI85YnZwAg9v7vd9fr1NHuHKesFMOFvRJZGP4cVDBiMo3mYfWHaKkmMVrjS4TiFAwTTZ3yzp2w23PrIq3SRxTraRky6Iybitw/lFembj2nPhOpxRpiphjx9jUecr17zSAc10HmSX+1hFH2ZolLR3tXcaDtq/6pc3Xthl5uT0HIkisgbgxG5M8B+x26ZWnu8pbk+nT9bsAh9LrkQX+xaxTCgpmWpUTFMhG2t9pl04jT/LPOWXCmrs1BYEadqMirivcZdAXEiRbisSVrDI8cGKvPZpNZU9jf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MmCuVXx/SMAKThIjK0jsRc6aTxVQG+GVCgS7dXMKb4Sm+vK2wEM6xtDeqcfYMB0BqGZRvxbIV59a2ooPrha4FkV6nNC9DQefzC0picHXV64oYUjMM2ed4ASTQVds7HP9BZfalz//APZcWdBV08f+xv7n79Twz/gNtVAadfuTAlcjsS6aK+G/OrZtr/diuemeRPRUKlVhcr5xOQDhqBgtdIw8M9Wp6I4ULXRlthKVBWToH9j6n9vDIiwIDAQAB";
  395. // set.AlipayAppId = "2021003126605468";
  396. // set.AlipayPrivateKey = "MIIEogIBAAKCAQEA1O3mknrWGkWsDiojx/hUgq5jnO/D8rjMGUpz8C7A88iJmm6ZSxssTz2+OkL75omlxRJcMZAYt+icZtLWVm6mSklCipieWZGEWiId6bsHwlEPOkwmhPw5bDBF+y6ZSkXtW+fofrmsk8/a9SZ/N3h+FJ5sqLbihn9pXNp4X1VgnZHGGNWsRTFgBdWdEWAEhDImAPNRv8IB7ZyVbbRo3aIvTCH4EDUu8Y5IMs8l5Ne0aUAkPgz81IbiSMzWqU5z9W5m1cPIOz/w5Bru/JvJliu2qkQrDlh9ccIcBIsrOddYAzxyXsxO+EqUM/m9HEtg5atvMIV9XMzgwnGpM3jVydRBywIDAQABAoIBAAdaO6b9NN1tYHaQv/GMwqZMjUYDBXohJXMOHhjCFImWeCHsBcERLCOEV0g9veMFB8NZUfOoLSJUlun6B8quQXG5O+H7SSSAoNhX6z6B5c+w6tyxencHGpiAxjnZLxPgfES7Kk0N7kagW8gxTx1EPN5LZichQYsxZRr+tD9efFqUeOnutUjECdvYNb6FsJ46tgCVRSEl7kBueR+VmFl5FXjc7B3bREGzaNylu846BK4vK6u1/IKIDczJKfZDRtwSpvMPUGwscfZAkPHOxtneKCyB1itouUyrhjaBb9AJMKIaCi7j5btazo81B3BZVK9uHV+B+gVA3d+aWwSteU9WMukCgYEA6bk0ua6285e/N6YC5KvqRTaangjtSJckx3FRVuK6799mIDyyvScz6Befi/EJ0kwF0J0HPI+GKGa7XWzk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