AlipayController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Alipay.AopSdk.Core.Util;
  4. using GxPress.Common.AliPay;
  5. using GxPress.Common.WechatPay;
  6. using GxPress.Service.Interface.Order;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. namespace GxPress.Api.WebControllers
  11. {
  12. [Route("api/web/alipay")]
  13. [ApiController]
  14. [Authorize]
  15. public class AlipayController : Controller
  16. {
  17. private readonly IOrderService orderService;
  18. private IHttpContextAccessor _contextAccessor;
  19. private HttpContext _context { get { return _contextAccessor.HttpContext; } }
  20. public AlipayController(IOrderService orderService, IHttpContextAccessor contextAccessor)
  21. {
  22. this.orderService = orderService;
  23. _contextAccessor = contextAccessor;
  24. }
  25. [HttpGet()]
  26. [AllowAnonymous]
  27. public string GetNativePayUrl()
  28. {
  29. var alipay = new PcPay();
  30. return alipay.PayRequest("637263608658642543_7", "汪峰讲故事", "0.01", "汪峰讲故事", "");
  31. }
  32. /// <summary>
  33. /// 回调地址
  34. /// </summary>
  35. [HttpPost("notify")]
  36. [AllowAnonymous]
  37. public async Task Callback()
  38. {
  39. Dictionary<string, string> sArray = GetRequestPost();
  40. if (sArray.Count != 0)
  41. {
  42. try
  43. {
  44. //校验数据的正确性
  45. bool flag = AlipaySignature.RSACheckV1(sArray, Config.AlipayPublicKey, Config.CharSet, Config.SignType, false);
  46. if (flag)
  47. {
  48. //Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}");
  49. if (await orderService.InsertAliPayOrderAsync(sArray))
  50. {
  51. // var res = new WxPayData();
  52. // res.SetValue("out_trade_no", sArray["out_trade_no"]);
  53. // res.SetValue("attach", sArray["out_trade_no"]);
  54. // res.SetValue("total_fee", sArray["total_amount"]);
  55. // res.SetValue("transaction_id", sArray["trade_no"]);
  56. _context.Response.StatusCode = 200;
  57. }
  58. else
  59. {
  60. _context.Response.StatusCode = 500;
  61. }
  62. }
  63. else
  64. {
  65. // Console.WriteLine($"同步验证失败,订单号:{sArray["out_trade_no"]}");
  66. _context.Response.StatusCode = 500;
  67. }
  68. }
  69. catch (System.Exception)
  70. {
  71. _context.Response.StatusCode = 500;
  72. }
  73. }
  74. else
  75. {
  76. _context.Response.StatusCode = 500;
  77. }
  78. }
  79. private Dictionary<string, string> GetRequestPost()
  80. {
  81. Dictionary<string, string> sArray = new Dictionary<string, string>();
  82. ICollection<string> requestItem = Request.Form.Keys;
  83. foreach (var item in requestItem)
  84. {
  85. sArray.Add(item, Request.Form[item]);
  86. }
  87. return sArray;
  88. }
  89. private Dictionary<string, string> GetRequestGet()
  90. {
  91. Dictionary<string, string> sArray = new Dictionary<string, string>();
  92. ICollection<string> requestItem = Request.Query.Keys;
  93. foreach (var item in requestItem)
  94. {
  95. sArray.Add(item, Request.Query[item]);
  96. }
  97. return sArray;
  98. }
  99. }
  100. }