AlipayController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 string MiniProgramApply(string value)
  100. {
  101. value = DesDecrypt(value);
  102. JsonData data = JsonMapper.ToObject(value);
  103. string Detail = data["Detail"].ToString();
  104. string Version = data["Version"].ToString();
  105. return new AlipayFunction().MiniProgramApply(Version, Detail);
  106. }
  107. #endregion
  108. #region 小程序上线
  109. public string MiniProgramOnline(string value)
  110. {
  111. value = DesDecrypt(value);
  112. JsonData data = JsonMapper.ToObject(value);
  113. string Version = data["Version"].ToString();
  114. return new AlipayFunction().MiniProgramOnline(Version);
  115. }
  116. #endregion
  117. #region 小程序退回开发状态
  118. public string MiniProgramCancel(string value)
  119. {
  120. value = DesDecrypt(value);
  121. JsonData data = JsonMapper.ToObject(value);
  122. string Version = data["Version"].ToString();
  123. return new AlipayFunction().MiniProgramCancel(Version);
  124. }
  125. #endregion
  126. }
  127. }