AlipayFunction.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. namespace MySystem
  10. {
  11. public class AlipayFunction
  12. {
  13. private string AppId = "2021003116616751";
  14. private string PrivateKey = "MIIEpAIBAAKCAQEA1KjMzZJIRG6QQ3+c/euVrc+8RR0xMw6A+EONSKhJq4Bg+rtxQPCfdF2bpZ4NMaSqfalSiLoxrZSYBYOZhpDP67i9LXGUoFCWdRL7ROTqJvuU6KyEeMplQ6Q3uwWbuKbh9COt0vhqn/pjlUsomTwq+DRQv10awynkZikqceXdP0EH4iGZG0hyerEP5yLHpHiggf4f7TOsovHX+fGLPmWFO1zUTHHbz7HxU4BQKn3yuje4yMhoyv5h/wZzeVwvj+Qti8Hlh351rF0f5BHwN1YMGIrWuPU5bF8OMHS/UXPxh7Bs795ZZyplYztTz26QfKfLWVVsLOng4pvgPS+OgzZydwIDAQABAoIBAEu6mMQT9Zw46zK+PP/HZxPwSBEsphqSbab7F1LqauGh4+cl3NzNU1szAnxl3jjxdK0vIO8DrzErGz/Lb16WLxS4QtXt/olgaVPjoIsWIbQkHxEEbmA9YvZFgnsdMij9dLVmBz57rCywE8pUa262GG8u4hpjY2Zggq9GtZC91J3zOeETBG828gU+FLHGcDpfz7pD1jC0noDfDRxmIImOMZvChIvvY+tm6MpySXN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8RR0xMw6A+EONSKhJq4Bg+rtxQPCfdF2bpZ4NMaSqfalSiLoxrZSYBYOZhpDP67i9LXGUoFCWdRL7ROTqJvuU6KyEeMplQ6Q3uwWbuKbh9COt0vhqn/pjlUsomTwq+DRQv10awynkZikqceXdP0EH4iGZG0hyerEP5yLHpHiggf4f7TOsovHX+fGLPmWFO1zUTHHbz7HxU4BQKn3yuje4yMhoyv5h/wZzeVwvj+Qti8Hlh351rF0f5BHwN1YMGIrWuPU5bF8OMHS/UXPxh7Bs795ZZyplYztTz26QfKfLWVVsLOng4pvgPS+OgzZydwIDAQAB";
  16. private string AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxycehrmnTl2IaIAECP+0HWc1eWGM/T6x022K7K6fdf+xoyC97TBJaG4G3Bcon4gsJX+Sad5IA9OJIQ4eN1joM35hIlwcFZ6MmCuVXx/SMAKThIjK0jsRc6aTxVQG+GVCgS7dXMKb4Sm+vK2wEM6xtDeqcfYMB0BqGZRvxbIV59a2ooPrha4FkV6nNC9DQefzC0picHXV64oYUjMM2ed4ASTQVds7HP9BZfalz//APZcWdBV08f+xv7n79Twz/gNtVAadfuTAlcjsS6aK+G/OrZtr/diuemeRPRUKlVhcr5xOQDhqBgtdIw8M9Wp6I4ULXRlthKVBWToH9j6n9vDIiwIDAQAB";
  17. public AlipayFunction()
  18. {
  19. }
  20. #region 代商家签约
  21. //(开启事务)接口创建应用事务,返回生成代商户操作事务编号 batch_no
  22. public string GetBatchNo(string AlipayAccount, string Name, string Mobile, string Email)
  23. {
  24. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  25. AlipayOpenAgentCreateRequest request = new AlipayOpenAgentCreateRequest();
  26. request.BizContent = "{" +
  27. "\"account\":\"" + AlipayAccount + "\"," +
  28. "\"contact_info\":{" +
  29. "\"contact_name\":\"" + Name + "\"," +
  30. "\"contact_mobile\":\"" + Mobile + "\"," +
  31. "\"contact_email\":\"" + Email + "\"" +
  32. "}" +
  33. "}";
  34. string result = "";
  35. try
  36. {
  37. AlipayOpenAgentCreateResponse response = client.Execute(request);
  38. result = response.Body;
  39. //{"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=="}
  40. }
  41. catch (Exception ex)
  42. {
  43. function.WriteLog(ex.ToString(), "接口创建应用事务异常");
  44. result = "";
  45. }
  46. return result;
  47. }
  48. //代签约产品通用接口,代商家发起签约产品申请
  49. public string CommonSign(string BatchNo, string MccCode, string BusinessLicenseNo = "", string BusinessLicensePicPath = "", string SpecialLicensePicPath = "", string ShopScenePicPath = "", string ShopSignBoardPicPath = "")
  50. {
  51. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  52. AlipayOpenAgentCommonSignRequest request = new AlipayOpenAgentCommonSignRequest();
  53. request.BatchNo = BatchNo;
  54. request.MccCode = MccCode; //软件系统商 D_D05_7372
  55. if(string.IsNullOrEmpty(SpecialLicensePicPath)) SpecialLicensePicPath = BusinessLicensePicPath;
  56. FileItem SpecialLicensePic = new FileItem(SpecialLicensePicPath);
  57. request.SpecialLicensePic = SpecialLicensePic;
  58. if (!string.IsNullOrEmpty(BusinessLicenseNo))
  59. {
  60. request.BusinessLicenseNo = BusinessLicenseNo; //营业执照号码
  61. }
  62. if (!string.IsNullOrEmpty(BusinessLicensePicPath))
  63. {
  64. FileItem BusinessLicensePic = new FileItem(BusinessLicensePicPath);
  65. request.BusinessLicensePic = BusinessLicensePic; //营业执照图片。被代创建商户运营主体为个人账户必填,企业账户无需填写
  66. }
  67. FileItem BusinessLicenseAuthPic = new FileItem(BusinessLicensePicPath);
  68. request.BusinessLicenseAuthPic = BusinessLicenseAuthPic;
  69. request.LongTerm = true;
  70. request.DateLimitation = "长期";
  71. if (!string.IsNullOrEmpty(ShopScenePicPath))
  72. {
  73. FileItem ShopScenePic = new FileItem(ShopScenePicPath);
  74. request.ShopScenePic = ShopScenePic;
  75. }
  76. if (!string.IsNullOrEmpty(ShopSignBoardPicPath))
  77. {
  78. FileItem ShopSignBoardPic = new FileItem(ShopSignBoardPicPath);
  79. request.ShopSignBoardPic = ShopSignBoardPic;
  80. }
  81. request.ProductCode = "QUICK_WAP_WAY"; //isv要代商户签约产品码,产品码是支付宝内部对产品的唯一标识
  82. request.AppName = "客小爽";
  83. // FileItem AppDemo = new FileItem(BusinessLicensePicPath);
  84. // request.AppDemo = AppDemo;
  85. List<string> WebSites = new List<string>();
  86. WebSites.Add("www.kexiaoshuang.com");
  87. request.WebSites = WebSites;
  88. request.AlipayLifeName = "客小爽";
  89. request.WechatOfficialAccountName = "客小爽";
  90. // FileItem WebSitesLoa = new FileItem(BusinessLicensePicPath);
  91. // request.WebSitesLoa = WebSitesLoa;
  92. request.WebTestAccount = "testAccount";
  93. request.WebTestAccountPassword = "testPassword";
  94. FileItem WebHomeScreenshot = new FileItem(BusinessLicensePicPath);
  95. request.WebHomeScreenshot = WebHomeScreenshot;
  96. FileItem WebItemScreenshot = new FileItem(BusinessLicensePicPath);
  97. request.WebItemScreenshot = WebItemScreenshot;
  98. FileItem WebPayScreenshot = new FileItem(BusinessLicensePicPath);
  99. request.WebPayScreenshot = WebPayScreenshot;
  100. request.WebStatus = "已上线";
  101. string result = "";
  102. try
  103. {
  104. AlipayOpenAgentCommonSignResponse response = client.Execute(request);
  105. result = response.Body;
  106. //{"alipay_open_agent_common_sign_response":{"code":"10000","msg":"Success"},"sign":"qt0DiXFXIJ7mX+W2+IX+cyYXx8ROGe8yxesXHGNltxpEv/s/zW16xzf4HQLtUlaAiWf3DiD2QV7Hj+REyoZw9AcktDAmlobcjQ28lTYufPECZoUJHpkn9VpGB932FC0hScVwCbsxQakH84MGT4cmjr1hCuSs7+XogdhlCTIKjau1e7ZglLnt7f1NqT8JnljVhmvkkE9gJTLq5H+Zq6XbFfBVptvM9w09HdjfhrsxAXJyQ4yU6JYX2tUYsRGz8ZrveDQMvXKTN9jnwKE9hh8ik6Sq49eUqfutV9GkKqhC96+Vy3nOTOTnPoECXZkco1wP5jsqHottWVsRV+NxdylnTw=="}
  107. }
  108. catch (Exception ex)
  109. {
  110. function.WriteLog(ex.ToString(), "代签约产品通用接口异常");
  111. result = "";
  112. }
  113. return result;
  114. }
  115. //代商户签约,提交信息确认接口
  116. public string CommonSignConfirm(string BatchNo)
  117. {
  118. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  119. AlipayOpenAgentCommonsignConfirmRequest request= new AlipayOpenAgentCommonsignConfirmRequest();
  120. request.BizContent="{" +
  121. "\"batch_no\":\"" + BatchNo + "\"" +
  122. "}";
  123. string result = "";
  124. try
  125. {
  126. AlipayOpenAgentCommonsignConfirmResponse response = client.Execute(request);
  127. result = response.Body;
  128. //{"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=="}
  129. }
  130. catch (Exception ex)
  131. {
  132. function.WriteLog(ex.ToString(), "接口创建应用事务异常");
  133. result = "";
  134. }
  135. return result;
  136. }
  137. #endregion
  138. #region (查询申请单状态)查询签约申请的结果
  139. public string QuerySignStatus(string BatchNo)
  140. {
  141. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  142. AlipayOpenAgentOrderQueryRequest request= new AlipayOpenAgentOrderQueryRequest() ;
  143. request.BizContent="{" +
  144. " \"batch_no\":\"" + BatchNo + "\"" +
  145. "}";
  146. string result = "";
  147. try
  148. {
  149. AlipayOpenAgentOrderQueryResponse response = client.Execute(request);
  150. result = response.Body;
  151. }
  152. catch (Exception ex)
  153. {
  154. function.WriteLog(ex.ToString(), "查询签约申请的结果异常");
  155. result = "";
  156. }
  157. return result;
  158. }
  159. #endregion
  160. #region 小程序支付请调用MySystemLib下的统一收单交易创建,返回交易号返给小程序请求支付my.tradePay
  161. public string GetToken()
  162. {
  163. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  164. AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
  165. request.BizContent = "{" +
  166. "\"grant_type\":\"authorization_code\"," +
  167. "\"code\":\"P16be77ee92264acc99553528b8a7103\"" +
  168. " }";
  169. string result = "";
  170. try
  171. {
  172. AlipayOpenAuthTokenAppResponse response = client.Execute(request);
  173. result = response.Body;
  174. //{"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=="}
  175. }
  176. catch (Exception ex)
  177. {
  178. function.WriteLog(ex.ToString(), "支付宝统一收单交易创建(获取authtoken)异常");
  179. result = "";
  180. }
  181. return result;
  182. }
  183. public string CreateTrade(string OrderNo, string Subject, string Amount, string BuyerId, string Notify_Url, string Token)
  184. {
  185. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  186. AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
  187. request.SetNotifyUrl(Notify_Url);
  188. request.BizContent = "{" +
  189. "\"out_trade_no\":\"" + OrderNo + "\"," +
  190. "\"total_amount\":" + Amount + "," +
  191. "\"subject\":\"" + Subject + "\"," +
  192. "\"buyer_id\":\"" + BuyerId + "\"," +
  193. "\"timeout_express\":\"10m\"" +
  194. "}";
  195. string result = "";
  196. try
  197. {
  198. AlipayTradeCreateResponse response = client.Execute(request, null, Token);
  199. result = response.Body;
  200. }
  201. catch (Exception ex)
  202. {
  203. function.WriteLog(ex.ToString(), "支付宝统一收单交易创建异常");
  204. result = "";
  205. }
  206. return result;
  207. }
  208. public string QueryTrade(string OrderNo, string Token)
  209. {
  210. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  211. AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
  212. request.BizContent = "{" +
  213. "\"out_trade_no\":\"" + OrderNo + "\"" +
  214. "}";
  215. string result = "";
  216. try
  217. {
  218. AlipayTradeQueryResponse response = client.Execute(request, null, Token);
  219. result = response.Body;
  220. }
  221. catch (Exception ex)
  222. {
  223. function.WriteLog(ex.ToString(), "统一收单线下交易查询异常");
  224. result = "";
  225. }
  226. return result;
  227. }
  228. #endregion
  229. #region 退款
  230. public string Refund(string TradeNo, string Amount, string Token)
  231. {
  232. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  233. AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
  234. request.BizContent = "{" +
  235. "\"trade_no\":\"" + TradeNo + "\"," +
  236. "\"refund_amount\":" + Amount + "" +
  237. "}";
  238. string result = "";
  239. try
  240. {
  241. AlipayTradeRefundResponse 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. #region 分账
  253. public string Settle(string OrderNo, string TradeNo, List<RoyaltyParameters> Parameters)
  254. {
  255. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  256. AlipayTradeOrderSettleRequest request= new AlipayTradeOrderSettleRequest();
  257. string royalty_parameters = "";
  258. foreach (RoyaltyParameters Parameter in Parameters)
  259. {
  260. royalty_parameters += " {" +
  261. " \"royalty_type\":\"transfer\"," +
  262. " \"trans_in_type\":\"loginName\"," +
  263. " \"trans_in\":\"" + Parameter.Account + "\"," +
  264. " \"amount\":" + Parameter.Amount + "," +
  265. " \"trans_in_name\":\"" + Parameter.Name + "\"" +
  266. " },";
  267. }
  268. request.BizContent = "{" +
  269. " \"out_request_no\":\"" + OrderNo + "\"," +
  270. " \"trade_no\":\"" + TradeNo + "\"," +
  271. " \"royalty_parameters\":[" +
  272. royalty_parameters.TrimEnd(',') +
  273. " ]" +
  274. "}";
  275. string result = "";
  276. try
  277. {
  278. AlipayTradeOrderSettleResponse response = client.Execute(request);
  279. result = response.Body;
  280. }
  281. catch (Exception ex)
  282. {
  283. function.WriteLog(ex.ToString(), "分账异常");
  284. result = "";
  285. }
  286. return result;
  287. }
  288. #endregion
  289. }
  290. }