123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System;
- using System.Threading.Tasks;
- using GxPress.Auth;
- using GxPress.Common.AliPay;
- using GxPress.Common.Exceptions;
- using GxPress.Common.Tools;
- using GxPress.Common.WechatPay;
- using GxPress.EnumConst;
- using GxPress.Repository.Interface.Media;
- using GxPress.Repository.Interface.VipEquity;
- using GxPress.Request.Pay;
- using GxPress.Service.Interface.Order;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using GxPress.Repository.Interface.Order;
- namespace GxPress.Api.WebControllers
- {
- /// <summary>
- /// 支付接口
- /// </summary>
- [Route("api/web/pay")]
- [ApiController]
- [Authorize]
- public class PayController : Controller
- {
- private readonly IOrderService orderService;
- private IHttpContextAccessor _contextAccessor;
- private readonly ILoginContext _loginContext;
- private HttpContext _context { get { return _contextAccessor.HttpContext; } }
- private readonly IMediaRepository mediaRepository;
- private readonly IVipEquityRepository vipEquityRepository;
- private IOrderRepository orderRepository;
- public PayController(IOrderService orderService, IHttpContextAccessor contextAccessor, ILoginContext _loginContext, IMediaRepository mediaRepository, IVipEquityRepository vipEquityRepository, IOrderRepository orderRepository)
- {
- this.orderService = orderService;
- _contextAccessor = contextAccessor;
- this._loginContext = _loginContext;
- this.mediaRepository = mediaRepository;
- this.vipEquityRepository = vipEquityRepository;
- this.orderRepository = orderRepository;
- }
- /// <summary>
- /// 统一的返回支付地址
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost()]
- public async Task<string> GetNativePayUrl(PayRequest request)
- {
- if (request.PayWayType == 0)
- throw new BusinessException("支付方式有误");
- var userId = _loginContext.AccountId;
- if (userId == 0)
- _context.Response.StatusCode = LoginCodeConst.UnLogin.GetHashCode();
- else
- {
- var attach = string.Empty;
- decimal price = 0;
- var title = string.Empty;
- var antoNumber = DateTime.Now.Ticks.ToString();
- if (request.MediaId > 0 || request.VipType > 0)
- {
- //商品
- if (request.MediaId > 0)
- {
- var media = await mediaRepository.GetAsync(request.MediaId);
- if (media != null)
- attach = $"{antoNumber}";
- else
- throw new BusinessException("服务器异常");
- price = decimal.Parse("0.01");
- //创建订单
- var order = new Entity.Order.Order()
- {
- IsRefund = false,
- PayWay = request.PayWayType,
- OrderType = 1,
- IsVip = false,
- MediaId = media.Id,
- OrderNumber = antoNumber,
- UserId = userId,
- Name = _loginContext.Name,
- Explain = media.Title,
- Price = price,
- IsSuccess = false
- };
- var orderId = await orderRepository.InsertAsync(order);
- if (orderId <= 0)
- throw new BusinessException("服务器异常");
- }
- //权益
- if (request.VipType > 0)
- {
- var vipEquity = await vipEquityRepository.GetByVipTypeAsync(request.VipType);
- var vipTypeConst = ((VipCardTypeConst)vipEquity.VipType);
- if (vipTypeConst.GetHashCode() == 0)
- throw new BusinessException("服务器异常");
- // attach = $"{antoNumber}_{userId}_{PayUrlRequetTypeConst.Vip.GetHashCode()}_{vipEquity.Id}";
- price = decimal.Parse("0.01");
- var order = new Entity.Order.Order()
- {
- IsRefund = false,
- PayWay = request.PayWayType,
- OrderType = 2,
- IsVip = false,
- MediaId = vipEquity.Id,
- OrderNumber = antoNumber,
- UserId = userId,
- Name = _loginContext.Name,
- Explain = vipTypeConst.GetDescriptionOriginal(),
- Price = price,
- IsSuccess = false
- };
- var orderId = await orderRepository.InsertAsync(order);
- if (orderId <= 0)
- throw new BusinessException("服务器异常");
- }
- //支付宝
- if (request.PayWayType == PayWayTypeConst.AliyPay.GetHashCode())
- {
- var alipay = new PcPay();
- return alipay.PayRequest(attach, title, price.ToString(), title);
- }
- else
- {
- var nativePay = new NativePay();
- return nativePay.GetPayUrl(antoNumber, title, attach, Convert.ToInt32(price * 100));
- }
- }
- else
- throw new BusinessException("参数错误");
- }
- return string.Empty;
- }
- }
- }
|