AlipayFunction.cs 29 KB

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