PayController.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Threading.Tasks;
  3. using GxPress.Auth;
  4. using GxPress.Common.AliPay;
  5. using GxPress.Common.Exceptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Common.WechatPay;
  8. using GxPress.EnumConst;
  9. using GxPress.Repository.Interface.Media;
  10. using GxPress.Repository.Interface.VipEquity;
  11. using GxPress.Request.Pay;
  12. using GxPress.Service.Interface.Order;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Mvc;
  16. using GxPress.Repository.Interface.Order;
  17. namespace GxPress.Api.WebControllers
  18. {
  19. /// <summary>
  20. /// 支付接口
  21. /// </summary>
  22. [Route("api/web/pay")]
  23. [ApiController]
  24. [Authorize]
  25. public partial class PayController : Controller
  26. {
  27. private readonly IOrderService orderService;
  28. private IHttpContextAccessor _contextAccessor;
  29. private readonly ILoginContext _loginContext;
  30. private HttpContext _context { get { return _contextAccessor.HttpContext; } }
  31. private readonly IMediaRepository mediaRepository;
  32. private readonly IVipEquityRepository vipEquityRepository;
  33. private IOrderRepository orderRepository;
  34. public PayController(IOrderService orderService, IHttpContextAccessor contextAccessor, ILoginContext _loginContext, IMediaRepository mediaRepository, IVipEquityRepository vipEquityRepository, IOrderRepository orderRepository)
  35. {
  36. this.orderService = orderService;
  37. _contextAccessor = contextAccessor;
  38. this._loginContext = _loginContext;
  39. this.mediaRepository = mediaRepository;
  40. this.vipEquityRepository = vipEquityRepository;
  41. this.orderRepository = orderRepository;
  42. }
  43. /// <summary>
  44. /// 统一的返回支付地址
  45. /// </summary>
  46. /// <param name="request"></param>
  47. /// <returns></returns>
  48. [HttpPost()]
  49. public async Task<PayResult> GetNativePayUrl(PayRequest request)
  50. {
  51. var result = new PayResult();
  52. if (request.PayWayType == 0)
  53. throw new BusinessException("支付方式有误");
  54. var userId = _loginContext.AccountId;
  55. if (userId == 0)
  56. _context.Response.StatusCode = LoginCodeConst.UnLogin.GetHashCode();
  57. else
  58. {
  59. decimal price = 0;
  60. var title = string.Empty;
  61. var antoNumber = DateTime.Now.Ticks.ToString();
  62. var attach = antoNumber;
  63. if (request.MediaId > 0 || request.VipType > 0)
  64. {
  65. //商品
  66. if (request.MediaId > 0)
  67. {
  68. var media = await mediaRepository.GetAsync(request.MediaId);
  69. if (media != null)
  70. attach = $"{antoNumber}";
  71. else
  72. throw new BusinessException("服务器异常");
  73. price = decimal.Parse("0.01");
  74. //创建订单
  75. var order = new Entity.Order.Order()
  76. {
  77. IsRefund = false,
  78. PayWay = request.PayWayType,
  79. OrderType = 1,
  80. IsVip = false,
  81. MediaId = media.Id,
  82. OrderNumber = antoNumber,
  83. UserId = userId,
  84. Name = _loginContext.Name,
  85. Explain = media.Title,
  86. Price = price,
  87. IsSuccess = false
  88. };
  89. var orderId = await orderRepository.InsertAsync(order);
  90. if (orderId <= 0)
  91. throw new BusinessException("服务器异常");
  92. title = media.Title;
  93. }
  94. //权益
  95. if (request.VipType > 0)
  96. {
  97. var vipEquity = await vipEquityRepository.GetByVipTypeAsync(request.VipType);
  98. if (vipEquity == null)
  99. throw new BusinessException("不存在权益");
  100. var vipTypeConst = ((VipCardTypeConst)vipEquity.VipType);
  101. if (vipTypeConst.GetHashCode() == 0)
  102. throw new BusinessException("服务器异常");
  103. // attach = $"{antoNumber}_{userId}_{PayUrlRequetTypeConst.Vip.GetHashCode()}_{vipEquity.Id}";
  104. price = decimal.Parse("0.01");
  105. var order = new Entity.Order.Order()
  106. {
  107. IsRefund = false,
  108. PayWay = request.PayWayType,
  109. OrderType = 2,
  110. IsVip = false,
  111. MediaId = vipEquity.Id,
  112. OrderNumber = antoNumber,
  113. UserId = userId,
  114. Name = "",
  115. Explain = vipTypeConst.GetDescriptionOriginal(),
  116. Price = price,
  117. IsSuccess = false
  118. };
  119. var orderId = await orderRepository.InsertAsync(order);
  120. if (orderId <= 0)
  121. throw new BusinessException("服务器异常");
  122. title = vipTypeConst.GetDescriptionOriginal();
  123. }
  124. //支付宝
  125. if (request.PayWayType == PayWayTypeConst.AliyPay.GetHashCode())
  126. {
  127. var alipay = new PcPay();
  128. result.PayQrCore = alipay.PayRequest(attach, title, price.ToString(), title, request.NotifyUrl);
  129. }
  130. else
  131. {
  132. var nativePay = new NativePay();
  133. result.PayQrCore = nativePay.GetPayUrl(antoNumber, title, attach, Convert.ToInt32(price * 100));
  134. result.AutoNumber = antoNumber;
  135. }
  136. }
  137. else
  138. throw new BusinessException("参数错误");
  139. }
  140. return result;
  141. }
  142. /// <summary>
  143. /// 查询订单 是否支付成功
  144. /// </summary>
  145. /// <param name="antoNumber"></param>
  146. /// <returns></returns>
  147. [HttpGet("{antoNumber}")]
  148. [AllowAnonymous]
  149. public async Task<bool> QueryOrder(string antoNumber)
  150. {
  151. var order = await orderRepository.GetOrderAsync(antoNumber);
  152. WxPayData req = new WxPayData();
  153. req.SetValue("transaction_id", order.TransactionId);
  154. WxPayData res = WxPayApi.OrderQuery(req);
  155. if (res.GetValue("return_code").ToString() == "SUCCESS" &&
  156. res.GetValue("result_code").ToString() == "SUCCESS")
  157. {
  158. return true;
  159. }
  160. else
  161. {
  162. return false;
  163. }
  164. }
  165. }
  166. }