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
{
///
/// 支付接口
///
[Route("api/web/pay")]
[ApiController]
[Authorize]
public partial 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;
}
///
/// 统一的返回支付地址
///
///
///
[HttpPost()]
public async Task GetNativePayUrl(PayRequest request)
{
var result = new PayResult();
if (request.PayWayType == 0)
throw new BusinessException("支付方式有误");
var userId = _loginContext.AccountId;
if (userId == 0)
_context.Response.StatusCode = LoginCodeConst.UnLogin.GetHashCode();
else
{
decimal price = 0;
var title = string.Empty;
var antoNumber =SnowflakeHelper.MakeSnowflake();
var attach = antoNumber;
if (request.MediaId > 0 || request.VipType > 0)
{
//商品
if (request.MediaId > 0)
{
var media = await mediaRepository.GetAsync(request.MediaId);
if (media == null)
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("服务器异常");
title = media.Title;
}
//权益
if (request.VipType > 0)
{
var vipEquity = await vipEquityRepository.GetByVipTypeAsync(request.VipType);
if (vipEquity == null)
throw new BusinessException("不存在权益");
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("服务器异常");
title = vipTypeConst.GetDescriptionOriginal();
}
//支付宝
if (request.PayWayType == PayWayTypeConst.AliyPay.GetHashCode())
{
var alipay = new PcPay();
result.PayQrCore = alipay.PayRequest(attach, title, price.ToString(), title, request.NotifyUrl);
}
else
{
var nativePay = new NativePay();
result.PayQrCore = nativePay.GetPayUrl(antoNumber, title, attach, Convert.ToInt32(price * 100));
result.AutoNumber = antoNumber;
}
}
else
throw new BusinessException("参数错误");
}
return result;
}
///
/// 查询订单 是否支付成功
///
///
///
[HttpGet("{antoNumber}")]
[AllowAnonymous]
public async Task QueryOrder(string antoNumber)
{
var order = await orderRepository.GetOrderAsync(antoNumber);
if (string.IsNullOrEmpty(order.TransactionId))
return false;
WxPayData req = new WxPayData();
req.SetValue("transaction_id", order.TransactionId);
WxPayData res = WxPayApi.OrderQuery(req);
if (res.GetValue("return_code").ToString() == "SUCCESS" &&
res.GetValue("result_code").ToString() == "SUCCESS")
{
return true;
}
else
{
return false;
}
}
}
}