AlipayController.cs 3.3 KB

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