AlipayController.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Microsoft.AspNetCore.Authorization;
  9. using System.Web;
  10. using MySystem.MainModels;
  11. using LitJson;
  12. using Library;
  13. using System.IO;
  14. using Aop.Api.Util;
  15. namespace MySystem.Areas.Api.Controllers.v1
  16. {
  17. [Area("Api")]
  18. [Route("Api/[controller]/[action]")]
  19. public class AlipayController : BaseController
  20. {
  21. public AlipayController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  22. {
  23. }
  24. #region 支付宝服务平台支付回调
  25. public string Notice()
  26. {
  27. StreamReader sr = new StreamReader(Request.Body);
  28. string requestMes = sr.ReadToEnd();
  29. function.WriteLog(DateTime.Now.ToString() + "\r\n" + requestMes + "\r\n\r\n", "支付宝服务平台支付回调");
  30. return PayCallBack(new AlipayFunction(_accessor.HttpContext).AlipayPublicKey, requestMes);
  31. }
  32. public string PayCallBack(string publickey, string requestMes, bool IsRedis = false)
  33. {
  34. // Library.function.WriteLog(requestMes, "支付宝回调日志");
  35. var result = "fail";
  36. var sPara = GetRequestPost(requestMes);
  37. if (sPara.Count > 0) //判断是否有带返回参数
  38. {
  39. // Library.function.WriteLog("有参数", "支付宝回调日志");
  40. bool verifyResult = false;
  41. var aliNotify = new Com.Alipay.Notify();
  42. string signtype = sPara["sign_type"];
  43. var total_fee = 0m;
  44. string strPrice = sPara["total_amount"];//金额
  45. decimal.TryParse(strPrice, out total_fee);
  46. verifyResult = AlipaySignature.RSACheckV1(sPara, publickey, "UTF-8", signtype, false);
  47. // Library.function.WriteLog(verifyResult.ToString(), "支付宝回调日志");
  48. if (verifyResult) //验证成功
  49. {
  50. // Library.function.WriteLog("验证成功\r\n\r\n", "支付宝回调日志");
  51. //商户订单号
  52. string out_trade_no = sPara["out_trade_no"];
  53. //支付宝交易号
  54. string trade_no = sPara["trade_no"];
  55. //交易状态 TRADE_SUCCESS
  56. string trade_status = sPara["trade_status"];
  57. //交易成功
  58. if (trade_status == "TRADE_FINISHED" || trade_status == "TRADE_SUCCESS")
  59. {
  60. RedisDbconn.Instance.AddList("PayCallBack", "{\"out_trade_no\":\"" + out_trade_no + "\",\"transaction_id\":\"" + trade_no + "\",\"total_fee\":\"" + total_fee + "\",\"pay_mode\":\"1\",\"openid\":\"\",\"attach\":\"\"}");
  61. result = "success";
  62. }
  63. }
  64. }
  65. return result;
  66. }
  67. public string MerAuth(string app_auth_code)
  68. {
  69. function.WriteLog(DateTime.Now.ToString() + "\n" + Request.QueryString.ToString(), "支付宝代商家授权回调日志");
  70. string result = new AlipayFunction().GetToken(app_auth_code);
  71. function.WriteLog(result, "支付宝代商家授权回调日志");
  72. //{"alipay_open_auth_token_app_response":{"code":"10000","msg":"Success","app_auth_token":"202306BB5e2aa2157bef47b7a8d918cadb0f1X25","app_refresh_token":"202306BB57fdb346273840b7aca1d63b34110D25","auth_app_id":"2021004102667228","expires_in":31536000,"re_expires_in":32140800,"tokens":[{"app_auth_token":"202306BB5e2aa2157bef47b7a8d918cadb0f1X25","app_refresh_token":"202306BB57fdb346273840b7aca1d63b34110D25","auth_app_id":"2021004102667228","expires_in":31536000,"re_expires_in":32140800,"user_id":"2088402028451259"}],"user_id":"2088402028451259"},"sign":"NLGZMLHbvb39KXeTBj0UPMVCeIYdxKymSXSBJAdRnuE8zCdJJTkk+flaclj63ljhK2WC/KOue/K1RSAjCepjtLVJaQxBfnTD/YXWHMRwcd8MM3aOX2D/wfl4VBgvzMJ2/fNAgycZ7ps2TiWyDrLz82DXtWpvXIRq0/NuZ6gILIdIl1jS/h4rgUB44ld+23ihNUuyqn1zV2EEm8ZZScEObnwg48sZ7Kd0PfOoXTJX/vfotFm3QXOzUwMNRDm6I2q+UvCGA355QaUvZhUzHOVUldvM4MaZom2TQDvpYf5EFTxSOZ7OglWjvJim1/ZcjUbqY00mTgoMoorrOH7p0OQyfA=="}
  73. JsonData jsonObj = JsonMapper.ToObject(result);
  74. if(jsonObj["alipay_open_auth_token_app_response"]["code"].ToString() == "10000")
  75. {
  76. string app_auth_token = jsonObj["alipay_open_auth_token_app_response"]["app_auth_token"].ToString();
  77. MerchantAddInfo addInfo = maindb.MerchantAddInfo.FirstOrDefault(m => m.Id == 0);
  78. if(addInfo != null)
  79. {
  80. addInfo.AlipayAuthToken = app_auth_token;
  81. maindb.SaveChanges();
  82. }
  83. }
  84. return "授权成功";
  85. }
  86. private SortedDictionary<string, string> GetRequestPost(string canshu)
  87. {
  88. var sArray = new SortedDictionary<string, string>();
  89. string[] canshulist = canshu.Split('&');
  90. foreach (var substring in canshulist)
  91. {
  92. string[] datalist = substring.Split('=');
  93. sArray.Add(datalist[0], HttpUtility.UrlDecode(datalist[1]));
  94. }
  95. return sArray;
  96. }
  97. #endregion
  98. #region 支付宝服务平台支付回调(好哒)
  99. public Dictionary<string, string> HdNotice()
  100. {
  101. StreamReader sr = new StreamReader(Request.Body);
  102. string requestMes = sr.ReadToEnd();
  103. function.WriteLog(DateTime.Now.ToString() + "\r\n" + requestMes + "\r\n\r\n", "支付宝服务平台支付回调(好哒)");
  104. RedisDbconn.Instance.AddList("WeChatPayBackHd", requestMes);
  105. Dictionary<string, string> dic = new Dictionary<string, string>();
  106. dic.Add("resultCode", "1");
  107. return dic;
  108. }
  109. #endregion
  110. // 支付回调
  111. public string NoticePay()
  112. {
  113. StreamReader sr = new StreamReader(Request.Body);
  114. string requestMes = sr.ReadToEnd();
  115. function.WriteLog(DateTime.Now.ToString() + "\r\n" + requestMes + "\r\n\r\n", "支付宝支付回调");
  116. return PayCallBack2("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnw14hKejqS+si/XZpNQpFAVOVXJIThj5Qn7H14n/vsb7uRLug1+Jcve8ehTkaN3s5ibrrELCnz0dvshi3cOz9svDSbLG5OdC1biAvZzpDkvVPswOd9f9jO8Jvqk3QTXTI3MSN9gOpKomI0te3qs5kkOQx6kRozzKjU/G//o/PbJCxpBxf/QNRV3XmCwJLZMBE7ukYqi1WO9Jra8jdczFKwrd/nhiKBKdupAtVVeKlylGD2i2YLkGAH1cLsPUN9DtzrXNblGXm2HK3l9SnMDjqnakiVdEQJTNYNpEiHaOJsVmPQL38nM6miz5wNd4gTchKzXPtoZWqto+eIvYaEA/KwIDAQAB", requestMes);
  117. }
  118. public string NoticePay2()
  119. {
  120. StreamReader sr = new StreamReader(Request.Body);
  121. string requestMes = sr.ReadToEnd();
  122. function.WriteLog(DateTime.Now.ToString() + "\r\n" + requestMes + "\r\n\r\n", "支付宝支付回调2");
  123. return PayCallBack2("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnw14hKejqS+si/XZpNQpFAVOVXJIThj5Qn7H14n/vsb7uRLug1+Jcve8ehTkaN3s5ibrrELCnz0dvshi3cOz9svDSbLG5OdC1biAvZzpDkvVPswOd9f9jO8Jvqk3QTXTI3MSN9gOpKomI0te3qs5kkOQx6kRozzKjU/G//o/PbJCxpBxf/QNRV3XmCwJLZMBE7ukYqi1WO9Jra8jdczFKwrd/nhiKBKdupAtVVeKlylGD2i2YLkGAH1cLsPUN9DtzrXNblGXm2HK3l9SnMDjqnakiVdEQJTNYNpEiHaOJsVmPQL38nM6miz5wNd4gTchKzXPtoZWqto+eIvYaEA/KwIDAQAB", requestMes);
  124. }
  125. public string PayCallBack2(string publickey, string requestMes, bool IsRedis = false)
  126. {
  127. // Library.function.WriteLog(requestMes, "支付宝回调日志");
  128. var result = "fail";
  129. var sPara = GetRequestPost(requestMes);
  130. if (sPara.Count > 0) //判断是否有带返回参数
  131. {
  132. // Library.function.WriteLog("有参数", "支付宝回调日志");
  133. bool verifyResult = false;
  134. var aliNotify = new Com.Alipay.Notify();
  135. string signtype = sPara["sign_type"];
  136. var total_fee = 0m;
  137. string strPrice = sPara["total_amount"];//金额
  138. decimal.TryParse(strPrice, out total_fee);
  139. verifyResult = AlipaySignature.RSACheckV1(sPara, publickey, "UTF-8", signtype, false);
  140. // Library.function.WriteLog(verifyResult.ToString(), "支付宝回调日志");
  141. if (verifyResult) //验证成功
  142. {
  143. // Library.function.WriteLog("验证成功\r\n\r\n", "支付宝回调日志");
  144. //商户订单号
  145. string out_trade_no = sPara["out_trade_no"];
  146. //支付宝交易号
  147. string trade_no = sPara["trade_no"];
  148. //交易状态 TRADE_SUCCESS
  149. string trade_status = sPara["trade_status"];
  150. //交易成功
  151. if (trade_status == "TRADE_FINISHED" || trade_status == "TRADE_SUCCESS")
  152. {
  153. if(out_trade_no.StartsWith("D"))
  154. {
  155. RedisDbconn.Instance.AddList("AlipayCallBack1", "{\"out_trade_no\":\"" + out_trade_no + "\",\"transaction_id\":\"" + trade_no + "\",\"total_fee\":\"" + total_fee + "\",\"pay_mode\":\"1\",\"openid\":\"\",\"attach\":\"\"}");
  156. }
  157. else
  158. {
  159. RedisDbconn.Instance.AddList("AlipayCallBack2", "{\"out_trade_no\":\"" + out_trade_no + "\",\"transaction_id\":\"" + trade_no + "\",\"total_fee\":\"" + total_fee + "\",\"pay_mode\":\"1\",\"openid\":\"\",\"attach\":\"\"}");
  160. }
  161. result = "success";
  162. }
  163. }
  164. }
  165. return result;
  166. }
  167. }
  168. }