AlipayFunction.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. namespace MySystem
  12. {
  13. public class AlipayFunction
  14. {
  15. public readonly static AlipayFunction Instance = new AlipayFunction();
  16. private AlipayFunction()
  17. { }
  18. public string Pid = "2088720140260314";
  19. private string AppId = "2021004100667901";
  20. private string PrivateKey = "MIIEowIBAAKCAQEAhw2C7AypN/cSBNZU1LU2th58fzvLlhlfg7jZz3xNJQwpICtSF6hXXB2qChecEZv7ZGyiuuamTn/c+ZAKJAl3EZ8fMEgTrdK9M8+3DX6FIzWywCuPktsnxLIkTj4Lt/LjPI3P3yZLESuWzHIQ+TGnwEgm/Fx5ESWribFOddLRnNpINk36y+gtaVoXpQ742Wt6Yg1l4FZrzCAOHjLfVeTyAzmXXm8TGdzep83fyCqSBp+rdpb1CGQukHYpCapyk52hM4wqsAVdndAqO5qoRi5BfuyiVs2BR6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BAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
  24. #region 代商家签约
  25. //(开启事务)接口创建应用事务,返回生成代商户操作事务编号 batch_no
  26. public string GetBatchNo(string Name, string Mobile, string Email)
  27. {
  28. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  29. AlipayOpenAgentCreateRequest request = new AlipayOpenAgentCreateRequest();
  30. request.BizContent = "{" +
  31. "\"account\":\"" + Email + "\"," +
  32. "\"contact_info\":{" +
  33. "\"contact_name\":\"" + Name + "\"," +
  34. "\"contact_mobile\":\"" + Mobile + "\"," +
  35. "\"contact_email\":\"" + Email + "\"" +
  36. "}" +
  37. "}";
  38. string result = "";
  39. try
  40. {
  41. AlipayOpenAgentCreateResponse response = client.Execute(request);
  42. result = response.Body;
  43. LogHelper.Instance.WriteLog(result, "开启事务");
  44. //{"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=="}
  45. }
  46. catch (Exception ex)
  47. {
  48. LogHelper.Instance.WriteLog(ex.ToString(), "接口创建应用事务异常");
  49. result = "";
  50. }
  51. return result;
  52. }
  53. //提交代商户签约、创建应用事务
  54. public string AgentConfirm(string BatchNo)
  55. {
  56. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  57. AlipayOpenAgentConfirmRequest request = new AlipayOpenAgentConfirmRequest();
  58. request.BizContent = "{" +
  59. " \"batch_no\":\"" + BatchNo + "\"" +
  60. "}";
  61. string result = "";
  62. try
  63. {
  64. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + Newtonsoft.Json.JsonConvert.SerializeObject(request), "提交代商户签约");
  65. AlipayOpenAgentConfirmResponse response = client.Execute(request);
  66. result = response.Body;
  67. LogHelper.Instance.WriteLog(result + "\n\n", "提交代商户签约");
  68. }
  69. catch (Exception ex)
  70. {
  71. LogHelper.Instance.WriteLog(ex.ToString(), "提交代商户签约异常");
  72. result = "";
  73. }
  74. return result;
  75. }
  76. //代签约产品通用接口,代商家发起签约产品申请
  77. // public string CommonSign(string BatchNo, string MccCode, string BusinessLicenseNo = "", string BusinessLicensePicPath = "", string SpecialLicensePicPath = "", string ShopScenePicPath = "", string ShopSignBoardPicPath = "")
  78. // {
  79. // IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  80. // AlipayOpenAgentCommonSignRequest request = new AlipayOpenAgentCommonSignRequest();
  81. // request.BatchNo = BatchNo;
  82. // request.MccCode = MccCode; //软件系统商 D_D05_7372
  83. // if(string.IsNullOrEmpty(SpecialLicensePicPath)) SpecialLicensePicPath = BusinessLicensePicPath;
  84. // FileItem SpecialLicensePic = new FileItem(SpecialLicensePicPath);
  85. // request.SpecialLicensePic = SpecialLicensePic;
  86. // if (!string.IsNullOrEmpty(BusinessLicenseNo))
  87. // {
  88. // request.BusinessLicenseNo = BusinessLicenseNo; //营业执照号码
  89. // }
  90. // if (!string.IsNullOrEmpty(BusinessLicensePicPath))
  91. // {
  92. // FileItem BusinessLicensePic = new FileItem(BusinessLicensePicPath);
  93. // request.BusinessLicensePic = BusinessLicensePic; //营业执照图片。被代创建商户运营主体为个人账户必填,企业账户无需填写
  94. // }
  95. // FileItem BusinessLicenseAuthPic = new FileItem(BusinessLicensePicPath);
  96. // request.BusinessLicenseAuthPic = BusinessLicenseAuthPic;
  97. // request.LongTerm = true;
  98. // request.DateLimitation = "长期";
  99. // if (!string.IsNullOrEmpty(ShopScenePicPath))
  100. // {
  101. // FileItem ShopScenePic = new FileItem(ShopScenePicPath);
  102. // request.ShopScenePic = ShopScenePic;
  103. // }
  104. // if (!string.IsNullOrEmpty(ShopSignBoardPicPath))
  105. // {
  106. // FileItem ShopSignBoardPic = new FileItem(ShopSignBoardPicPath);
  107. // request.ShopSignBoardPic = ShopSignBoardPic;
  108. // }
  109. // request.ProductCode = "FACE_TO_FACE_PAYMENT"; //isv要代商户签约产品码,产品码是支付宝内部对产品的唯一标识,例如:QUICK_WAP_WAY:手机网站支付,FACE_TO_FACE_PAYMENT:当面付
  110. // request.AppName = "客小爽";
  111. // // FileItem AppDemo = new FileItem(BusinessLicensePicPath);
  112. // // request.AppDemo = AppDemo;
  113. // List<string> WebSites = new List<string>();
  114. // WebSites.Add("http://www.kexiaoshuang.com/");
  115. // request.WebSites = WebSites;
  116. // request.AlipayLifeName = "客小爽";
  117. // request.WechatOfficialAccountName = "客小爽";
  118. // // FileItem WebSitesLoa = new FileItem(BusinessLicensePicPath);
  119. // // request.WebSitesLoa = WebSitesLoa;
  120. // request.WebTestAccount = "testAccount";
  121. // request.WebTestAccountPassword = "testPassword";
  122. // FileItem WebHomeScreenshot = new FileItem(BusinessLicensePicPath);
  123. // request.WebHomeScreenshot = WebHomeScreenshot;
  124. // FileItem WebItemScreenshot = new FileItem(BusinessLicensePicPath);
  125. // request.WebItemScreenshot = WebItemScreenshot;
  126. // FileItem WebPayScreenshot = new FileItem(BusinessLicensePicPath);
  127. // request.WebPayScreenshot = WebPayScreenshot;
  128. // request.WebStatus = "已上线";
  129. // string result = "";
  130. // try
  131. // {
  132. // AlipayOpenAgentCommonSignResponse response = client.Execute(request);
  133. // result = response.Body;
  134. // LogHelper.Instance.WriteLog(result, "代商家发起签约产品申请");
  135. // //{"alipay_open_agent_common_sign_response":{"code":"10000","msg":"Success"},"sign":"qt0DiXFXIJ7mX+W2+IX+cyYXx8ROGe8yxesXHGNltxpEv/s/zW16xzf4HQLtUlaAiWf3DiD2QV7Hj+REyoZw9AcktDAmlobcjQ28lTYufPECZoUJHpkn9VpGB932FC0hScVwCbsxQakH84MGT4cmjr1hCuSs7+XogdhlCTIKjau1e7ZglLnt7f1NqT8JnljVhmvkkE9gJTLq5H+Zq6XbFfBVptvM9w09HdjfhrsxAXJyQ4yU6JYX2tUYsRGz8ZrveDQMvXKTN9jnwKE9hh8ik6Sq49eUqfutV9GkKqhC96+Vy3nOTOTnPoECXZkco1wP5jsqHottWVsRV+NxdylnTw=="}
  136. // }
  137. // catch (Exception ex)
  138. // {
  139. // LogHelper.Instance.WriteLog(ex.ToString(), "代签约产品通用接口异常");
  140. // result = "";
  141. // }
  142. // return result;
  143. // }
  144. public string CommonSign(string BatchNo, string MccCode, string BusinessLicenseNo = "", string BusinessLicensePicPath = "", string SpecialLicensePicPath = "", string ShopScenePicPath = "", string ShopSignBoardPicPath = "")
  145. {
  146. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  147. AlipayOpenAgentFacetofaceSignRequest request = new AlipayOpenAgentFacetofaceSignRequest() ;
  148. request.BatchNo = BatchNo;
  149. request.MccCode = MccCode;
  150. if (!string.IsNullOrEmpty(BusinessLicenseNo))
  151. {
  152. request.BusinessLicenseNo = BusinessLicenseNo; //营业执照号码
  153. }
  154. if (!string.IsNullOrEmpty(BusinessLicensePicPath))
  155. {
  156. FileItem BusinessLicensePic = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + BusinessLicensePicPath);
  157. request.BusinessLicensePic = BusinessLicensePic; //营业执照图片。被代创建商户运营主体为个人账户必填,企业账户无需填写
  158. FileItem BusinessLicenseAuthPic = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + BusinessLicensePicPath);
  159. request.BusinessLicenseAuthPic = BusinessLicenseAuthPic;
  160. }
  161. if (!string.IsNullOrEmpty(SpecialLicensePicPath))
  162. {
  163. FileItem SpecialLicensePic = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + SpecialLicensePicPath);
  164. request.SpecialLicensePic = SpecialLicensePic;
  165. }
  166. if (!string.IsNullOrEmpty(ShopScenePicPath))
  167. {
  168. if(ShopScenePicPath.Contains(","))
  169. {
  170. ShopScenePicPath = ShopScenePicPath.Split(',')[0];
  171. }
  172. FileItem ShopScenePic = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + ShopScenePicPath);
  173. request.ShopScenePic = ShopScenePic;
  174. }
  175. if (!string.IsNullOrEmpty(ShopSignBoardPicPath))
  176. {
  177. if(ShopSignBoardPicPath.Contains(","))
  178. {
  179. ShopSignBoardPicPath = ShopSignBoardPicPath.Split(',')[0];
  180. }
  181. FileItem ShopSignBoardPic = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + ShopSignBoardPicPath);
  182. request.ShopSignBoardPic = ShopSignBoardPic;
  183. }
  184. // FileItem SpecialLicensePic = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
  185. // request.setSpecialLicensePic = "-";
  186. request.Rate = "0.38";
  187. // request.SignAndAuth = true;
  188. // request.BusinessLicenseNo = "1532501100006302";
  189. // FileItem BusinessLicensePic = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
  190. // request.setBusinessLicensePic = "-";
  191. // FileItem BusinessLicenseAuthPic = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
  192. // request.setBusinessLicenseAuthPic = "-";
  193. // request.LongTerm = true;
  194. // request.DateLimitation = "2017-11-11";
  195. // FileItem ShopScenePic = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
  196. // request.setShopScenePic = "-";
  197. // FileItem ShopSignBoardPic = new FileItem("C:/Downloads/ooopic_963991_7eea1f5426105f9e6069/16365_1271139700.jpg");
  198. // request.setShopSignBoardPic = "-";
  199. // request.ShopName = "-";
  200. // SignAddressInfo signAddressInfo = new SignAddressInfo();
  201. // signAddressInfo.country_code = "156";
  202. // signAddressInfo.province_code = "370000";
  203. // signAddressInfo.city_code = "371000";
  204. // signAddressInfo.district_code = "371002";
  205. // signAddressInfo.detail_address = "xx街道xx小区xx楼xx号";
  206. // signAddressInfo.longitude = "120.760001";
  207. // signAddressInfo.latitude = "60.270001";
  208. // request.shop_address = signAddressInfo;
  209. // request.BusinessLicenseMobile = "13189652239";
  210. string result = "";
  211. try
  212. {
  213. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + Newtonsoft.Json.JsonConvert.SerializeObject(request), "代商家发起签约产品申请");
  214. AlipayOpenAgentFacetofaceSignResponse response = client.Execute(request);
  215. result = response.Body;
  216. LogHelper.Instance.WriteLog(result, "代商家发起签约产品申请");
  217. }
  218. catch (Exception ex)
  219. {
  220. LogHelper.Instance.WriteLog(ex.ToString(), "代签约产品通用接口异常");
  221. result = "";
  222. }
  223. return result;
  224. }
  225. //代商户签约,提交信息确认接口
  226. public string CommonSignConfirm(string BatchNo)
  227. {
  228. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  229. AlipayOpenAgentCommonsignConfirmRequest request= new AlipayOpenAgentCommonsignConfirmRequest();
  230. request.BizContent="{" +
  231. "\"batch_no\":\"" + BatchNo + "\"" +
  232. "}";
  233. string result = "";
  234. try
  235. {
  236. AlipayOpenAgentCommonsignConfirmResponse response = client.Execute(request);
  237. result = response.Body;
  238. LogHelper.Instance.WriteLog(result, "提交信息确认");
  239. //{"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=="}
  240. }
  241. catch (Exception ex)
  242. {
  243. LogHelper.Instance.WriteLog(ex.ToString(), "接口创建应用事务异常");
  244. result = "";
  245. }
  246. return result;
  247. }
  248. #endregion
  249. #region (查询申请单状态)查询签约申请的结果
  250. public string QuerySignStatus(string BatchNo)
  251. {
  252. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  253. AlipayOpenAgentOrderQueryRequest request= new AlipayOpenAgentOrderQueryRequest() ;
  254. request.BizContent="{" +
  255. " \"batch_no\":\"" + BatchNo + "\"" +
  256. "}";
  257. string result = "";
  258. try
  259. {
  260. AlipayOpenAgentOrderQueryResponse response = client.Execute(request);
  261. result = response.Body;
  262. LogHelper.Instance.WriteLog(result, "查询签约申请的结果");
  263. }
  264. catch (Exception ex)
  265. {
  266. LogHelper.Instance.WriteLog(ex.ToString(), "查询签约申请的结果异常");
  267. result = "";
  268. }
  269. return result;
  270. //{"alipay_open_agent_order_query_response":{"code":"10000","msg":"Success","order_no":"20230613034000100000035309978329","product_agent_status_infos":[{"product_code":"FACE_TO_FACE_PAYMENT","product_name":"当面付","status":"WAIT_CONFIRM"}],"order_status":"MERCHANT_CONFIRM","confirm_url":"https:\/\/b.alipay.com\/page\/message\/messageList?type=todoTasks","merchant_pid":"2088641405724089"},"sign":"nwPLGsIQRV25znCK+3G8Pf/sgF7G11jh2QQWwq+UsP2K3FyG9e14x3pPwf0NLOinFHV0AVRI8FhZLbChNl/PUjPzrhoa1OHwB1n4lvo/2uvYVU3KPPV6NKUuxy3YAB6jHDi1AuSqFVOckfm0tvm6p4a/5RYF+3pNEufDISueTdduyvOabcuf7PqQs9J4/7lAmvJxNdxkrgaHrQ4mWk102k2tc657wrRLguUULFF6+j7HtGA4WrAWxrA+KLkzjbFlFu4qtwwZUHpF666mPMQSV0IDEtqiEn3PYDtBztUu9JKTfEoUxlm36pbpp7zynthXAFyMCR8nchsFXxU4bXJcng=="}
  271. }
  272. #endregion
  273. #region 小程序支付请调用MySystemLib下的统一收单交易创建,返回交易号返给小程序请求支付my.tradePay
  274. public string GetToken()
  275. {
  276. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  277. AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
  278. request.BizContent = "{" +
  279. "\"grant_type\":\"authorization_code\"," +
  280. "\"code\":\"P16be77ee92264acc99553528b8a7103\"" +
  281. " }";
  282. string result = "";
  283. try
  284. {
  285. AlipayOpenAuthTokenAppResponse response = client.Execute(request);
  286. result = response.Body;
  287. //{"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=="}
  288. }
  289. catch (Exception ex)
  290. {
  291. LogHelper.Instance.WriteLog(ex.ToString(), "支付宝统一收单交易创建(获取authtoken)异常");
  292. result = "";
  293. }
  294. return result;
  295. }
  296. public string CreateTrade(string OrderNo, string Subject, string Amount, string BuyerId, string Notify_Url, string Token)
  297. {
  298. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  299. AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
  300. request.SetNotifyUrl(Notify_Url);
  301. request.BizContent = "{" +
  302. "\"out_trade_no\":\"" + OrderNo + "\"," +
  303. "\"total_amount\":" + Amount + "," +
  304. "\"subject\":\"" + Subject + "\"," +
  305. "\"buyer_id\":\"" + BuyerId + "\"," +
  306. "\"timeout_express\":\"10m\"" +
  307. "}";
  308. string result = "";
  309. try
  310. {
  311. AlipayTradeCreateResponse response = client.Execute(request, null, Token);
  312. result = response.Body;
  313. }
  314. catch (Exception ex)
  315. {
  316. LogHelper.Instance.WriteLog(ex.ToString(), "支付宝统一收单交易创建异常");
  317. result = "";
  318. }
  319. return result;
  320. }
  321. public string QueryTrade(string OrderNo, string Token)
  322. {
  323. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  324. AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
  325. request.BizContent = "{" +
  326. "\"out_trade_no\":\"" + OrderNo + "\"" +
  327. "}";
  328. string result = "";
  329. try
  330. {
  331. AlipayTradeQueryResponse response = client.Execute(request, null, Token);
  332. result = response.Body;
  333. }
  334. catch (Exception ex)
  335. {
  336. LogHelper.Instance.WriteLog(ex.ToString(), "统一收单线下交易查询异常");
  337. result = "";
  338. }
  339. return result;
  340. }
  341. #endregion
  342. #region 退款
  343. public string Refund(string TradeNo, string Amount, string Reason, string MerToken)
  344. {
  345. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  346. AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
  347. request.BizContent = "{" +
  348. "\"trade_no\":\"" + TradeNo + "\"," +
  349. "\"refund_amount\":" + Amount + "," +
  350. "\"out_request_no\":\"" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8) + "\"," +
  351. "\"refund_reason\":\"" + Reason + "\"" +
  352. // "\"refund_royalty_parameters\":[{" +
  353. // "\"royalty_type\":\"transfer\"," +
  354. // "\"trans_out_type\":\"userId\"," +
  355. // "\"trans_out\":\"" + MerPid + "\"," +
  356. // "\"amount\":" + Amount + "," +
  357. // "\"desc\":\"" + Reason + "\"" +
  358. // "}]" +
  359. "}";
  360. string result = "";
  361. try
  362. {
  363. AlipayTradeRefundResponse response = client.Execute(request, null, MerToken);
  364. result = response.Body;
  365. }
  366. catch (Exception ex)
  367. {
  368. LogHelper.Instance.WriteLog(ex.ToString(), "退款异常");
  369. result = "";
  370. }
  371. return result;
  372. }
  373. #endregion
  374. #region 分账
  375. public string Settle(string OrderNo, string TradeNo, List<RoyaltyParameters> Parameters, string MerToken)
  376. {
  377. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  378. AlipayTradeOrderSettleRequest request= new AlipayTradeOrderSettleRequest();
  379. string royalty_parameters = "";
  380. foreach (RoyaltyParameters Parameter in Parameters)
  381. {
  382. royalty_parameters += " {" +
  383. // " \"royalty_type\":\"transfer\"," +
  384. // " \"trans_in_type\":\"loginName\"," +
  385. " \"trans_in\":\"" + Parameter.Account + "\"," +
  386. " \"amount\":" + Parameter.Amount + "" +
  387. // " \"trans_in_name\":\"" + Parameter.Name + "\"" +
  388. " },";
  389. }
  390. request.BizContent = "{" +
  391. " \"out_request_no\":\"" + OrderNo + "\"," +
  392. " \"trade_no\":\"" + TradeNo + "\"," +
  393. " \"royalty_parameters\":[" +
  394. royalty_parameters.TrimEnd(',') +
  395. " ]" +
  396. "}";
  397. string result = "";
  398. try
  399. {
  400. AlipayTradeOrderSettleResponse response = client.Execute(request, null, MerToken);
  401. result = response.Body;
  402. }
  403. catch (Exception ex)
  404. {
  405. LogHelper.Instance.WriteLog(ex.ToString(), "分账异常");
  406. result = "";
  407. }
  408. return result;
  409. }
  410. #endregion
  411. #region 绑定分账关系
  412. public string BindSettleRelation(string RequestNo, string Account, string MerToken)
  413. {
  414. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  415. AlipayTradeRoyaltyRelationBindRequest request = new AlipayTradeRoyaltyRelationBindRequest();
  416. request.BizContent="{" +
  417. " \"receiver_list\":[" +
  418. " {" +
  419. " \"type\":\"userId\"," +
  420. " \"account\":\"" + Account + "\"" +
  421. // " \"name\":\"测试名称\"," +
  422. // " \"memo\":\"分账给测试商户\"," +
  423. // " \"login_name\":\"test@alitest.xyz\"," +
  424. // " \"bind_login_name\":\"test@alitest.xyz\"" +
  425. " }" +
  426. " ]," +
  427. " \"out_request_no\":\"" + RequestNo + "\"" +
  428. "}";
  429. string result = "";
  430. try
  431. {
  432. AlipayTradeRoyaltyRelationBindResponse response = client.Execute(request, null, MerToken);
  433. result = response.Body;
  434. }
  435. catch (Exception ex)
  436. {
  437. LogHelper.Instance.WriteLog(ex.ToString(), "绑定分账关系异常");
  438. result = "";
  439. }
  440. return result;
  441. }
  442. #endregion
  443. #region 查看分账
  444. public string QuerySettle(string OrderNo, string TradeNo, string MerToken)
  445. {
  446. IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
  447. AlipayTradeOrderSettleQueryRequest request = new AlipayTradeOrderSettleQueryRequest();
  448. request.BizContent="{" +
  449. " \"out_request_no\":\"" + OrderNo + "\"," +
  450. " \"trade_no\":\"" + TradeNo + "\"" +
  451. "}";
  452. string result = "";
  453. try
  454. {
  455. AlipayTradeOrderSettleQueryResponse response = client.Execute(request, null, MerToken);
  456. result = response.Body;
  457. LogHelper.Instance.WriteLog(request.BizContent + "\n" + result, "支付宝查询分账结果");
  458. }
  459. catch (Exception ex)
  460. {
  461. LogHelper.Instance.WriteLog(ex.ToString(), "分账异常");
  462. result = "";
  463. }
  464. return result;
  465. }
  466. #endregion
  467. }
  468. }