WxpayController.cs 4.8 KB

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