AlipayFunction.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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.MainModels;
  13. using Newtonsoft.Json;
  14. namespace MySystem
  15. {
  16. public class AlipayFunction
  17. {
  18. private HttpContext _context;
  19. private string AppId = "2021004100667901";
  20. private string PrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCHDYLsDKk39xIE1lTUtTa2Hnx/O8uWGV+DuNnPfE0lDCkgK1IXqFdcHaoKF5wRm/tkbKK65qZOf9z5kAokCXcRnx8wSBOt0r0zz7cNfoUjNbLAK4+S2yfEsiROPgu38uM8jc/fJksRK5bMchD5MafASCb8XHkRJauJsU510tGc2kg2TfrL6C1pWhelDvjZa3piDWXgVmvMIA4eMt9V5PIDOZdebxMZ3N6nzd/IKpIGn6t2lvUIZC6QdikJqnKTnaEzjCqwBV2d0Co7mqhGLkF+7KJWzYFHo577yIXisPkUn9aTpCC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/4dRxJkCgYEAmSQnW4+4oSrv3N6o0R8McVkdb7hjNPjEOWrS+blhiGmFzKvzRnSMvMaCahEcpN8PAEdgXlNd1SxaAkQbH2O5w+LSMWQrocMB6tSobhYlzyK773enJhh88izNYcTDPES62OlSdTYelcsyGBRdCvBJatCkdOS48xjxkJDApnhMExU=";
  21. private string PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhw2C7AypN/cSBNZU1LU2th58fzvLlhlfg7jZz3xNJQwpICtSF6hXXB2qChecEZv7ZGyiuuamTn/c+ZAKJAl3EZ8fMEgTrdK9M8+3DX6FIzWywCuPktsnxLIkTj4Lt/LjPI3P3yZLESuWzHIQ+TGnwEgm/Fx5ESWribFOddLRnNpINk36y+gtaVoXpQ742Wt6Yg1l4FZrzCAOHjLfVeTyAzmXXm8TGdzep83fyCqSBp+rdpb1CGQukHYpCapyk52hM4wqsAVdndAqO5qoRi5BfuyiVs2BR6Oe+8iF4rD5FJ/Wk6QguemWX8C/4jTnuNs1YsFuCTHqYwr8k8J6ekEdgwIDAQAB";
  22. public string AlipayPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
  23. public AlipayFunction(HttpContext context)
  24. {
  25. _context = context;
  26. }
  27. #region 代商家签约
  28. //(开启事务)接口创建应用事务,返回生成代商户操作事务编号 batch_no
  29. public string GetBatchNo(string AlipayAccount, string Name, string Mobile, string Email)
  30. {
  31. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  32. AlipayOpenAgentCreateRequest request = new AlipayOpenAgentCreateRequest();
  33. request.BizContent = "{" +
  34. "\"account\":\"" + AlipayAccount + "\"," +
  35. "\"contact_info\":{" +
  36. "\"contact_name\":\"" + Name + "\"," +
  37. "\"contact_mobile\":\"" + Mobile + "\"," +
  38. "\"contact_email\":\"" + Email + "\"" +
  39. "}" +
  40. "}";
  41. string result = "";
  42. try
  43. {
  44. AlipayOpenAgentCreateResponse response = client.Execute(request);
  45. result = response.Body;
  46. //{"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=="}
  47. }
  48. catch (Exception ex)
  49. {
  50. function.WriteLog(ex.ToString(), "接口创建应用事务异常");
  51. result = "";
  52. }
  53. return result;
  54. }
  55. //代签约产品通用接口,代商家发起签约产品申请
  56. public string CommonSign(string BatchNo, string MccCode, string BusinessLicenseNo = "", string BusinessLicensePicPath = "", string ShopScenePicPath = "", string ShopSignBoardPicPath = "")
  57. {
  58. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  59. AlipayOpenAgentCommonSignRequest request = new AlipayOpenAgentCommonSignRequest();
  60. request.BatchNo = BatchNo;
  61. request.MccCode = MccCode; //软件系统商 D_D05_7372
  62. FileItem SpecialLicensePic = new FileItem(BusinessLicensePicPath);
  63. request.SpecialLicensePic = SpecialLicensePic;
  64. if (!string.IsNullOrEmpty(BusinessLicenseNo))
  65. {
  66. request.BusinessLicenseNo = BusinessLicenseNo; //营业执照号码
  67. }
  68. if (!string.IsNullOrEmpty(BusinessLicensePicPath))
  69. {
  70. FileItem BusinessLicensePic = new FileItem(BusinessLicensePicPath);
  71. request.BusinessLicensePic = BusinessLicensePic; //营业执照图片。被代创建商户运营主体为个人账户必填,企业账户无需填写
  72. }
  73. FileItem BusinessLicenseAuthPic = new FileItem(BusinessLicensePicPath);
  74. request.BusinessLicenseAuthPic = BusinessLicenseAuthPic;
  75. request.LongTerm = true;
  76. request.DateLimitation = "长期";
  77. if (!string.IsNullOrEmpty(ShopScenePicPath))
  78. {
  79. FileItem ShopScenePic = new FileItem(ShopScenePicPath);
  80. request.ShopScenePic = ShopScenePic;
  81. }
  82. if (!string.IsNullOrEmpty(ShopSignBoardPicPath))
  83. {
  84. FileItem ShopSignBoardPic = new FileItem(ShopSignBoardPicPath);
  85. request.ShopSignBoardPic = ShopSignBoardPic;
  86. }
  87. request.ProductCode = "QUICK_WAP_WAY"; //isv要代商户签约产品码,产品码是支付宝内部对产品的唯一标识
  88. request.AppName = "客小爽";
  89. // FileItem AppDemo = new FileItem(BusinessLicensePicPath);
  90. // request.AppDemo = AppDemo;
  91. List<string> WebSites = new List<string>();
  92. WebSites.Add("www.kexiaoshuang.com");
  93. request.WebSites = WebSites;
  94. request.AlipayLifeName = "客小爽";
  95. request.WechatOfficialAccountName = "客小爽";
  96. // FileItem WebSitesLoa = new FileItem(BusinessLicensePicPath);
  97. // request.WebSitesLoa = WebSitesLoa;
  98. request.WebTestAccount = "testAccount";
  99. request.WebTestAccountPassword = "testPassword";
  100. FileItem WebHomeScreenshot = new FileItem(BusinessLicensePicPath);
  101. request.WebHomeScreenshot = WebHomeScreenshot;
  102. FileItem WebItemScreenshot = new FileItem(BusinessLicensePicPath);
  103. request.WebItemScreenshot = WebItemScreenshot;
  104. FileItem WebPayScreenshot = new FileItem(BusinessLicensePicPath);
  105. request.WebPayScreenshot = WebPayScreenshot;
  106. request.WebStatus = "已上线";
  107. string result = "";
  108. try
  109. {
  110. AlipayOpenAgentCommonSignResponse response = client.Execute(request);
  111. result = response.Body;
  112. //{"alipay_open_agent_common_sign_response":{"code":"10000","msg":"Success"},"sign":"qt0DiXFXIJ7mX+W2+IX+cyYXx8ROGe8yxesXHGNltxpEv/s/zW16xzf4HQLtUlaAiWf3DiD2QV7Hj+REyoZw9AcktDAmlobcjQ28lTYufPECZoUJHpkn9VpGB932FC0hScVwCbsxQakH84MGT4cmjr1hCuSs7+XogdhlCTIKjau1e7ZglLnt7f1NqT8JnljVhmvkkE9gJTLq5H+Zq6XbFfBVptvM9w09HdjfhrsxAXJyQ4yU6JYX2tUYsRGz8ZrveDQMvXKTN9jnwKE9hh8ik6Sq49eUqfutV9GkKqhC96+Vy3nOTOTnPoECXZkco1wP5jsqHottWVsRV+NxdylnTw=="}
  113. }
  114. catch (Exception ex)
  115. {
  116. function.WriteLog(ex.ToString(), "代签约产品通用接口异常");
  117. result = "";
  118. }
  119. return result;
  120. }
  121. //代商户签约,提交信息确认接口
  122. public string CommonSignConfirm(string BatchNo)
  123. {
  124. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  125. AlipayOpenAgentCommonsignConfirmRequest request= new AlipayOpenAgentCommonsignConfirmRequest();
  126. request.BizContent="{" +
  127. "\"batch_no\":\"" + BatchNo + "\"" +
  128. "}";
  129. string result = "";
  130. try
  131. {
  132. AlipayOpenAgentCommonsignConfirmResponse response = client.Execute(request);
  133. result = response.Body;
  134. //{"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=="}
  135. }
  136. catch (Exception ex)
  137. {
  138. function.WriteLog(ex.ToString(), "接口创建应用事务异常");
  139. result = "";
  140. }
  141. return result;
  142. }
  143. #endregion
  144. #region (查询申请单状态)查询签约申请的结果
  145. public string QuerySignStatus(string BatchNo)
  146. {
  147. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  148. AlipayOpenAgentOrderQueryRequest request= new AlipayOpenAgentOrderQueryRequest() ;
  149. request.BizContent="{" +
  150. " \"batch_no\":\"" + BatchNo + "\"" +
  151. "}";
  152. string result = "";
  153. try
  154. {
  155. AlipayOpenAgentOrderQueryResponse response = client.Execute(request);
  156. result = response.Body;
  157. }
  158. catch (Exception ex)
  159. {
  160. function.WriteLog(ex.ToString(), "查询签约申请的结果异常");
  161. result = "";
  162. }
  163. return result;
  164. }
  165. #endregion
  166. #region 小程序支付请调用MySystemLib下的统一收单交易创建,返回交易号返给小程序请求支付my.tradePay
  167. public string GetToken()
  168. {
  169. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  170. AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
  171. request.BizContent = "{" +
  172. "\"grant_type\":\"authorization_code\"," +
  173. "\"code\":\"P16be77ee92264acc99553528b8a7103\"" +
  174. " }";
  175. string result = "";
  176. try
  177. {
  178. AlipayOpenAuthTokenAppResponse response = client.Execute(request);
  179. result = response.Body;
  180. //{"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=="}
  181. }
  182. catch (Exception ex)
  183. {
  184. function.WriteLog(ex.ToString(), "支付宝统一收单交易创建(获取authtoken)异常");
  185. result = "";
  186. }
  187. return result;
  188. }
  189. public string CreateTrade(string OrderNo, string Subject, decimal Amount, string BuyerId, string MerToken, string Notify_Url)
  190. {
  191. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  192. AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
  193. request.SetNotifyUrl(Notify_Url);
  194. Dictionary<string, object> bizContent = new Dictionary<string, object>();
  195. bizContent.Add("out_trade_no", OrderNo);
  196. bizContent.Add("total_amount", Amount);
  197. bizContent.Add("subject", Subject);
  198. // bizContent.Add("seller_id", "2088641405724089");
  199. bizContent.Add("buyer_id", BuyerId);
  200. bizContent.Add("timeout_express", "10m");
  201. // 结算信息,按需传入
  202. // Dictionary<string, object> settleInfo = new Dictionary<string, object>();
  203. // List<object> settleDetailInfos = new List<object>();
  204. // Dictionary<string, object> settleDetail = new Dictionary<string, object>();
  205. // settleDetail.Add("trans_in_type", "userId");
  206. // settleDetail.Add("trans_in", MerPid);
  207. // settleDetail.Add("amount", Amount);
  208. // settleDetailInfos.Add(settleDetail);
  209. // settleInfo.Add("settle_detail_infos", settleDetailInfos);
  210. // bizContent.Add("settle_info", settleInfo);
  211. string Contentjson = JsonConvert.SerializeObject(bizContent);
  212. request.BizContent = Contentjson;
  213. string result = "";
  214. try
  215. {
  216. AlipayTradeCreateResponse response = client.Execute(request, null, MerToken);
  217. result = response.Body;
  218. function.WriteLog(DateTime.Now.ToString() + "\n" + Contentjson + "\n" + result, "支付宝统一收单交易创建");
  219. }
  220. catch (Exception ex)
  221. {
  222. function.WriteLog(ex.ToString(), "支付宝统一收单交易创建异常");
  223. result = "";
  224. }
  225. return result;
  226. }
  227. public string QueryTrade(string OrderNo, string Token)
  228. {
  229. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  230. AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
  231. request.BizContent = "{" +
  232. "\"out_trade_no\":\"" + OrderNo + "\"" +
  233. "}";
  234. string result = "";
  235. try
  236. {
  237. AlipayTradeQueryResponse response = client.Execute(request, null, Token);
  238. result = response.Body;
  239. }
  240. catch (Exception ex)
  241. {
  242. function.WriteLog(ex.ToString(), "统一收单线下交易查询异常");
  243. result = "";
  244. }
  245. return result;
  246. }
  247. #endregion
  248. //小程序
  249. string MiniAppId = "2021004100642805";
  250. //应用公钥
  251. // string PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnTchC1mU0rFnRv8/eqDz1i/gS5Wd5fDD/TobKDBlj1UgQPRnT9g8wy/uTtqMnImR09ZQ5C/Ekw8B2GlN6bLbza0+7Fym7rNNKw3EvEL8tQ6pMe1GbIu68tCEjIslPXswGCgGRI30LeSj0BElaSAaqbvvyTHp+vxNLwav/hSGokPa8C76SzgCpvvzjXk9oGiNV5eutdoWEKbwyvw45kv70JHragNANTqU8DouzPLHenoD5V+QqJw1He9mZSb08QgSHCFW1dau8Hmxy0oXsfUVXXHJfzkhoyJOerYiR+oVuRqaIIF9xFlUPsNdpsAnJCP+HaAqRJpvuKfV4hNdg7uvIQIDAQAB";
  252. //支付宝公钥
  253. string MiniAlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxuS3Edgrruau8SO2DsK0f2KcE+A6pGJ1f8CdL395qwoX5RnEx9yRUotkwN0ZF6qMUojFKu/J/X2DpxBWBc6jIa+cnFwPUykefXcXj/AQsgiIbFzNXBH9NSz+hngXP6xDs8D1u9h+fJB+TYD+WFXKwOcGwcx23uHLHUDi1UIEZOhT2Eui+aMCYVygUWH7JynTtbFQZyZcSsD2Dxj5OJNJlK110jarFgvqY98WxqtaRtB9Y8whwRRlGfa5GP2r/Vt3xbGeAflRl5khFgRLZgeFIfxJpEj1TZV2tCtpFS0uhXJGEQbMnzFynx8UaOx/0hBCyik1MQuu3flym6nx7XxPSQIDAQAB";
  254. string MiniPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmIK2EVxir9uspaS96y2giySdjv8jpekg8ZaLUluTv5DxbYbacAAVIxPs19yT2saFVhb3Qhwcx/03913J0v8DjEDQqiySZqV5y9z+C5NXGTGp5PyQK4q1wdAtg2TKkHlMRPrN/nhaFDEu6Hehoub1xEPm06qQU44A77yMnrZ5YJcS/Cv22lvcikVp6sThPmBMmmA+BF/v6hufYGWRlnWoAhl2l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F57klBA9N9VMaw80mjGLuaTJwyxeFYH2QOpbeVs3upmfmmJSuOAVzg7QBkgBw0x6Wzrz831rCWGFRNZwDLnIP5h2G7SNmeNKe+HqV7M4jQ7Yelbgvgi/U6BedEyjfM4ekldBJkRsPP8QcWaeNeG5Y0CgYEAnZb4EsUxP2NFwUJKPZqYeXkGgkeWacyCUC7E5ba+hnYp2QMUI+Xp2m00V2XuNQCWSxTWwtuPWhwudL7D3jyZLK4xaxZSOu6ylDBXWUN2IRcvqsFWQmKVQ56yu7Kgw3yWPIE/ZiU8iGcOkNlZC84F8ccq7c0BlhukG724J0nURLECgYAWXTYwo7TqJ7ZDqPiTm4RaVasCl/F/uMHugrMATX9iM1jNF85XaH9GhvtWktPhaZk7ie5jwiGMT9W5TePvydpJwOf07Ya3FCP/3Q/bMDJW+YjP8no2R4G5KRwu+6ahltC6La8H9B+Q/tUVvPplYjerARgAgS6aHIZCPTOJ2RAAtQKBgGpJtAIZ8sE4wVCDwd1zuVTF40SkUeeoU6dRNVFV1Z++S4XHbjEGLf8ee95kpCv1jL1W70kh50zqSJP+3DnqOeTOvVudhTwBdtDd9YapmJMHh6vObP1WSXStq/LzULK2N8jxWiMJo32lE4HwsO3rH9aRe2ioObqo4zd+C6ZSzfqo";
  257. //小程序AES密钥,目前用于解密手机号
  258. string Key = "CxMrVGoYbx+XCEqGqJkP/A==";
  259. public string GetAlipayUserId(string Auth_code)
  260. {
  261. string result = "";
  262. try
  263. {
  264. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", MiniAppId, MiniPrivateKey, "json", "1.0", "RSA2", MiniAlipayPublicKey, "UTF-8", false);
  265. //获取access_token
  266. AlipaySystemOauthTokenRequest requestAccess_token = new AlipaySystemOauthTokenRequest();
  267. requestAccess_token.GrantType = "authorization_code";
  268. requestAccess_token.Code = Auth_code;
  269. AlipaySystemOauthTokenResponse responseAccess_token = client.Execute(requestAccess_token);
  270. function.WriteLog(responseAccess_token.Body, "支付宝授权");
  271. // string AlipayUserId = responseAccess_token.AlipayUserId; //20881043611849171819285350010300
  272. // string UserId = responseAccess_token.UserId; //2088222145274001
  273. // result = UserId + "|" + AlipayUserId;
  274. JsonData jsonObj = JsonMapper.ToObject(responseAccess_token.Body);
  275. result = jsonObj["alipay_system_oauth_token_response"]["open_id"].ToString();
  276. }
  277. catch (Exception ex)
  278. {
  279. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "支付宝授权报错日志");
  280. }
  281. return result;
  282. }
  283. public string GetAlipayMobile(string response)
  284. {
  285. function.WriteLog(response, "解密报文");
  286. //1. 获取验签和解密所需要的参数
  287. IDictionary openapiResult = Jayrock.Json.Conversion.JsonConvert.Import(response) as IDictionary;
  288. string signType;
  289. if (openapiResult.Contains("sign_type"))
  290. {
  291. signType = openapiResult["sign_type"].ToString();
  292. }
  293. else
  294. {
  295. signType = "RSA2";
  296. }
  297. string charset;
  298. if (openapiResult.Contains("charset"))
  299. {
  300. charset = openapiResult["charset"].ToString();
  301. }
  302. else
  303. {
  304. charset = "UTF-8";
  305. }
  306. string encryptType;
  307. if (openapiResult.Contains("encrypt_type"))
  308. {
  309. encryptType = openapiResult["encrypt_type"].ToString();
  310. }
  311. else
  312. {
  313. encryptType = "AES";
  314. }
  315. string sign = openapiResult["sign"].ToString();
  316. string content = openapiResult["response"].ToString();
  317. // 是否为加密报文
  318. bool isDataEncrypted = !content.StartsWith("{", StringComparison.Ordinal);
  319. // bool signCheckPass = false;
  320. //2. 验签
  321. string signContent = content;
  322. signContent = "\"" + signContent + "\"";
  323. string signVeriKey = MiniAlipayPublicKey;
  324. string decryptKey = MiniPrivateKey;
  325. string result = "";
  326. try
  327. {
  328. function.WriteLog(content, "解密报文");
  329. result = AlipayEncrypt.AesDencrypt(Key, content, charset);
  330. JsonData obj = JsonMapper.ToObject(result);
  331. if (obj["code"].ToString() == "10000")
  332. {
  333. result = "success|" + obj["mobile"].ToString();
  334. }
  335. //{\"code\":\"10000\",\"msg\":\"Success\",\"mobile\":\"14781419364\"}
  336. }
  337. catch (Exception ex)
  338. {
  339. //解密异常, 记录日志
  340. function.WriteLog(ex.ToString(), "获取手机号解密异常");
  341. }
  342. return result;
  343. // 如果是加密的报文则需要在密文的前后添加双引号
  344. // if (isDataEncrypted)
  345. // {
  346. // signContent = "\"" + signContent + "\"";
  347. // }
  348. // try
  349. // {
  350. // signContent = signContent.Trim('"');
  351. // signCheckPass = AlipaySignature.RSACheckContent(signContent, sign, signVeriKey, charset, signType);
  352. // }
  353. // catch (Exception ex)
  354. // {
  355. // //验签异常, 日志
  356. // throw new Exception("验签失败", ex);
  357. // }
  358. // if (!signCheckPass)
  359. // {
  360. // //验签不通过(异常或者报文被篡改),终止流程(不需要做解密)
  361. // throw new Exception("验签失败");
  362. // }
  363. // //3. 解密
  364. // string plainData = null;
  365. // if (isDataEncrypted)
  366. // {
  367. // try
  368. // {
  369. // plainData = AlipayEncrypt.AesDencrypt(decryptKey, content, charset);
  370. // }
  371. // catch (Exception ex)
  372. // {
  373. // //解密异常, 记录日志
  374. // throw new Exception("解密异常", ex);
  375. // }
  376. // }
  377. // else
  378. // {
  379. // plainData = content;
  380. // }
  381. // return result;
  382. }
  383. public string test()
  384. {
  385. string result = "";
  386. string str = "PsQMXBobiYsZPG5q2dbsX5Zvnee73Zpg55guE2FTxMijl+xdj+oCjEErUpZNxRK0S132GVcWBQUjFeA5fcLq1A==";
  387. try
  388. {
  389. result = AlipayEncrypt.AesDencrypt(Key, str, "UTF-8");
  390. JsonData obj = JsonMapper.ToObject(result);
  391. if (obj["code"].ToString() == "10000")
  392. {
  393. result = "success|" + obj["mobile"].ToString();
  394. }
  395. //{\"code\":\"10000\",\"msg\":\"Success\",\"mobile\":\"14781419364\"}
  396. }
  397. catch (Exception ex)
  398. {
  399. //解密异常, 记录日志
  400. function.WriteLog(ex.ToString(), "获取手机号解密异常");
  401. }
  402. return result;
  403. }
  404. // 支付宝参数
  405. public PublicAccountSet SetData(int UserId = 0)
  406. {
  407. WebCMSEntities db = new WebCMSEntities();
  408. PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  409. // set.AlipayAppId = "2021003122642236";
  410. // set.AlipayPrivateKey = "MIIEpAIBAAKCAQEAmfSplznW6H+wCyKh806jBxVdRabpK2P9RP4QNSGsHNIAscuH2yPbI1dNfKESC+QXAfWVRg90Vb+7yQz1I/S2vT159yY7ZH6duCopPpScfpNmICtjVukc1V5PZX+ptuDZc0YQSsRnn5WVKNO0bBR6iS3Jl9HQT949I3rHttA1+iqBn2LWyvGcUSDCOFNwstOYFM3FJTg7rQEdjmEQonEPXSbTQl1aw+fx1D+NcGx9BFSVAbE+nqfiX2co4V6yQ++N1RMlnBtbMZmTgvcoHZujqqBmjRcfRUW8I0B9aPJGNji94STBRPf0hP7yN4PHP2LixAK5gfzZ5NP2iK99sQ9tmwIDAQABAoIBAAR0N5PQcI85YnZwAg9v7vd9fr1NHuHKesFMOFvRJZGP4cVDBiMo3mYfWHaKkmMVrjS4TiFAwTTZ3yzp2w23PrIq3SRxTraRky6Iybitw/lFembj2nPhOpxRpiphjx9jUecr17zSAc10HmSX+1hFH2ZolLR3tXcaDtq/6pc3Xthl5uT0HIkisgbgxG5M8B+x26ZWnu8pbk+nT9bsAh9LrkQX+xaxTCgpmWpUTFMhG2t9pl04jT/LPOWXCmrs1BYEadqMirivcZdAXEiRbisSVrDI8cGKvPZpNZU9jfg9W5DEgfUdhnaZfRZzeAWORrBogXV1Osjlcjh2XXA3pAzCP8ECgYEA1jKv/S4RVCswh5LZTVFqVTAVyMBL/nLSu1L4mgaim19QDINxCDpN8kOfIBIBatwvkg7HzzQquotbDqj8pKgCFHTyf5gof6iFA1uGGP6AYBdL0k5tAG+uKwpo9rtRAUlOeLqwCC4rVhO1hE9tsey+t57BoAGVqi9JYIyCwEmCmo8CgYEAuABGodDT+RVbzJhUi4gHmDNDsil214uDyRYprCvD+wsJp5ma7M30hPm2NluGRWJUSdo237T0a8RkrNuXHW1ptgFKED8Qy4JV+eKemMZ21yMKyNZcjNs9X55b9BK3cGKkxXieXcxGLhz1YeLqTatS516uKfUHte62E3neZDySsjUCgYEApQGLHWi9pK9YxTfU+oD7fwB555prcGqX+AJkzJLL9REpo1omWwY/QZ/iF1yHhwB0DPoh5nbza50q2lJHvbhHUeb9YFmg6SaywPFijiKQVnXbzPjpc2qVK0brsRZ/wpPsRYjbTpHyhG7OaEyf3m7YRK+hzVcrKl0fE6dqmiKG3/UCgYEAt+2WrVRZQAjIrdir0pqUtOCxz9tA76+HebAM0g0hp8XdzcpvIj2yKmaPbQDKVHs5NGXMKozxMvSrilsrUIA5hnT9ewX/nN7ZYKPW03K4AtAFjMMy2p6hV/6tfmpjMhV5DehHrg1vLdNGuqWgfyk+AwgS4uAL6CIJoahV1S7zmO0CgYBGMNtHyIsYbnYycat2SEOQOGbLAro+jRNDPCI3FGOf1fAOpXD9JgQMD7oXSRSnZRzwvsxNQ944UXqLrHPS5yEtcmAu6BdR1QjJlpj5ClRYuQuDK7k1LR3+0eI3FySbinAMspr6UVNo0UqUkLa2k/igH2yUyThIUDOiqkfuxsu/aA==";
  411. // set.AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxycehrmnTl2IaIAECP+0HWc1eWGM/T6x022K7K6fdf+xoyC97TBJaG4G3Bcon4gsJX+Sad5IA9OJIQ4eN1joM35hIlwcFZ6MmCuVXx/SMAKThIjK0jsRc6aTxVQG+GVCgS7dXMKb4Sm+vK2wEM6xtDeqcfYMB0BqGZRvxbIV59a2ooPrha4FkV6nNC9DQefzC0picHXV64oYUjMM2ed4ASTQVds7HP9BZfalz//APZcWdBV08f+xv7n79Twz/gNtVAadfuTAlcjsS6aK+G/OrZtr/diuemeRPRUKlVhcr5xOQDhqBgtdIw8M9Wp6I4ULXRlthKVBWToH9j6n9vDIiwIDAQAB";
  412. // set.AlipayAppId = "2021003126605468";
  413. // set.AlipayPrivateKey = "MIIEogIBAAKCAQEA1O3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