WxpayController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using GxPress.Common.WechatPay;
  2. using GxPress.Service.Interface.Order;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. namespace GxPress.Api.WebControllers
  9. {
  10. [Route("api/web/wxpay")]
  11. [ApiController]
  12. [Authorize]
  13. public class WxpayController : Controller
  14. {
  15. private IHttpContextAccessor _contextAccessor;
  16. private HttpContext _context { get { return _contextAccessor.HttpContext; } }
  17. private IOrderService orderService;
  18. public WxpayController(IHttpContextAccessor contextAccessor, IOrderService orderService)
  19. {
  20. _contextAccessor = contextAccessor;
  21. this.orderService = orderService;
  22. }
  23. [HttpGet()]
  24. [AllowAnonymous]
  25. public string GetNativePayUrl()
  26. {
  27. var nativePay = new NativePay();
  28. return nativePay.GetPayUrl("637263608658642540", "汪峰讲故事", 7);
  29. }
  30. /// <summary>
  31. /// 回调地址
  32. /// </summary>
  33. [HttpPost("notify")]
  34. [AllowAnonymous]
  35. public async Task<string> Callback()
  36. {
  37. var contentType = Request.ContentType;
  38. var notifyData = await GetNotifyData();
  39. //检查支付结果中transaction_id是否存在
  40. if (!notifyData.IsSet("transaction_id"))
  41. {
  42. //若transaction_id不存在,则立即返回结果给微信支付后台
  43. notifyData = new WxPayData();
  44. notifyData.SetValue("return_code", "FAIL");
  45. notifyData.SetValue("return_msg", "支付结果中微信订单号不存在");
  46. return (notifyData.ToXml());
  47. }
  48. string transaction_id = notifyData.GetValue("transaction_id").ToString();
  49. //查询订单,判断订单真实性
  50. if (!Common.WechatPay.WxPayApi.QueryOrder(transaction_id))
  51. {
  52. //若订单查询失败,则立即返回结果给微信支付后台
  53. notifyData = new WxPayData();
  54. notifyData.SetValue("return_code", "FAIL");
  55. notifyData.SetValue("return_msg", "订单查询失败");
  56. return (notifyData.ToXml());
  57. }
  58. //查询订单成功
  59. else
  60. {
  61. notifyData = new WxPayData();
  62. //创建订单
  63. if (await orderService.InsertWxOrderAsync(notifyData))
  64. {
  65. notifyData.SetValue("return_code", "SUCCESS");
  66. notifyData.SetValue("return_msg", "OK");
  67. return (notifyData.ToXml());
  68. }
  69. }
  70. notifyData = new WxPayData();
  71. notifyData.SetValue("return_code", "FAIL");
  72. notifyData.SetValue("return_msg", "支付结果中微信订单号不存在");
  73. return (notifyData.ToXml());
  74. }
  75. private async Task<WxPayData> GetNotifyData()
  76. {
  77. var body = string.Empty;
  78. //接收从微信后台POST过来的数据
  79. using (var reader = new StreamReader(Request.Body))
  80. {
  81. body = await reader.ReadToEndAsync();
  82. }
  83. //转换数据格式并验证签名
  84. WxPayData data = new WxPayData();
  85. if (string.IsNullOrEmpty(body))
  86. {
  87. data.SetValue("return_code", "FAIL");
  88. return data;
  89. }
  90. try
  91. {
  92. data.FromXml(body);
  93. }
  94. catch (WxPayException ex)
  95. {
  96. //若签名错误,则立即返回结果给微信支付后台
  97. WxPayData res = new WxPayData();
  98. res.SetValue("return_code", "FAIL");
  99. res.SetValue("return_msg", ex.Message);
  100. // Log.Error(this.GetType().ToString(), "Sign check error : " + res.ToXml());
  101. // page.Response.Write(res.ToXml());
  102. // page.Response.End();
  103. }
  104. // Log.Info(this.GetType().ToString(), "Check sign success");
  105. return data;
  106. }
  107. }
  108. }