PayController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 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<string> GetNativePayUrl(PayRequest request)
  50. {
  51. if (request.PayWayType == 0)
  52. throw new BusinessException("支付方式有误");
  53. var userId = _loginContext.AccountId;
  54. if (userId == 0)
  55. _context.Response.StatusCode = LoginCodeConst.UnLogin.GetHashCode();
  56. else
  57. {
  58. var attach = string.Empty;
  59. decimal price = 0;
  60. var title = string.Empty;
  61. var antoNumber = DateTime.Now.Ticks.ToString();
  62. if (request.MediaId > 0 || request.VipType > 0)
  63. {
  64. //商品
  65. if (request.MediaId > 0)
  66. {
  67. var media = await mediaRepository.GetAsync(request.MediaId);
  68. if (media != null)
  69. attach = $"{antoNumber}";
  70. else
  71. throw new BusinessException("服务器异常");
  72. price = decimal.Parse("0.01");
  73. //创建订单
  74. var order = new Entity.Order.Order()
  75. {
  76. IsRefund = false,
  77. PayWay = request.PayWayType,
  78. OrderType = 1,
  79. IsVip = false,
  80. MediaId = media.Id,
  81. OrderNumber = antoNumber,
  82. UserId = userId,
  83. Name = _loginContext.Name,
  84. Explain = media.Title,
  85. Price = price,
  86. IsSuccess = false
  87. };
  88. var orderId = await orderRepository.InsertAsync(order);
  89. if (orderId <= 0)
  90. throw new BusinessException("服务器异常");
  91. }
  92. //权益
  93. if (request.VipType > 0)
  94. {
  95. var vipEquity = await vipEquityRepository.GetByVipTypeAsync(request.VipType);
  96. var vipTypeConst = ((VipCardTypeConst)vipEquity.VipType);
  97. if (vipTypeConst.GetHashCode() == 0)
  98. throw new BusinessException("服务器异常");
  99. // attach = $"{antoNumber}_{userId}_{PayUrlRequetTypeConst.Vip.GetHashCode()}_{vipEquity.Id}";
  100. price = decimal.Parse("0.01");
  101. var order = new Entity.Order.Order()
  102. {
  103. IsRefund = false,
  104. PayWay = request.PayWayType,
  105. OrderType = 2,
  106. IsVip = false,
  107. MediaId = vipEquity.Id,
  108. OrderNumber = antoNumber,
  109. UserId = userId,
  110. Name = _loginContext.Name,
  111. Explain = vipTypeConst.GetDescriptionOriginal(),
  112. Price = price,
  113. IsSuccess = false
  114. };
  115. var orderId = await orderRepository.InsertAsync(order);
  116. if (orderId <= 0)
  117. throw new BusinessException("服务器异常");
  118. }
  119. //支付宝
  120. if (request.PayWayType == PayWayTypeConst.AliyPay.GetHashCode())
  121. {
  122. var alipay = new PcPay();
  123. return alipay.PayRequest(attach, title, price.ToString(), title);
  124. }
  125. else
  126. {
  127. var nativePay = new NativePay();
  128. return nativePay.GetPayUrl(antoNumber, title, attach, Convert.ToInt32(price * 100));
  129. }
  130. }
  131. else
  132. throw new BusinessException("参数错误");
  133. }
  134. return string.Empty;
  135. }
  136. }
  137. }