AlipayFunction.cs 28 KB

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