micro_mall_api/ARW.Service/Api/BusinessService/PayManage/PayServiceApi.cs
2023-10-09 20:59:59 +08:00

144 lines
4.8 KiB
C#

using Infrastructure.Attribute;
using System.Threading.Tasks;
using ARW.Repository.Business.ShopManager.Shops;
using ARW.Repository.Business.GoodsManager.GoodsCategorys;
using ARW.Repository.Business.Custom.Customers;
using ARW.Model.Models.Business.Payments;
using Infrastructure;
using static Infrastructure.WeChat.TenPay.Pay;
using Infrastructure.WeChat.TenPay;
using Senparc.CO2NET.HttpUtility;
using ARW.Service.Api.IBusinessService.PayManage;
using ARW.Repository.Business.Payments;
using ARW.Repository.Business.OrderManage.Orders;
namespace ARW.Service.Api.BusinessService.PaymentManage
{
/// <summary>
/// 支付业务实现类Api
///
/// @author lwh
/// @date 2023-06-12
/// </summary>
[AppService(ServiceType = typeof(IPayServiceApi), ServiceLifetime = LifeTime.Transient)]
public class PayServiceApi : BaseService<Payment>, IPayServiceApi
{
private readonly ShopRepository _ShopRepository;
private readonly GoodsCategoryRepository _GoodsCategoryRepository;
private readonly CustomerRepository _CustomerRepository;
private readonly OrderRepository _OrderRepository;
private readonly PaymentRepository _PaymentRepository;
private readonly SenparcHttpClient _httpClient;
public PayServiceApi(ShopRepository ShopRepository, GoodsCategoryRepository goodsCategoryRepository, CustomerRepository customerRepository)
{
this._ShopRepository = ShopRepository;
_GoodsCategoryRepository = goodsCategoryRepository;
_CustomerRepository = customerRepository;
}
#region Api接口代码
public async Task<PayParams> HandelPrePay(PayDto parm)
{
// 获取金额
var price = 1;//单位:分
// 生成订单号
var orderNo = Common.Common.CreateNoQuery();
// 调用统一下单(接口)
Pay pay = new Pay(_httpClient);
var payEntity = await pay.PrePay(parm.OpenId, orderNo, parm.PayType, price);
if (payEntity == null)
{
throw new CustomException("下单失败!");
}
else
{
#region
/* 添加订单流水 */
var paymentGuid = await AddPayment(parm.UserId, orderNo, parm.PayType, parm.BeforeMoney, parm.Money);
#endregion
// 返回拼接好的支付调起信息
var res = await _PaymentRepository.GetFirstAsync(s => s.PaymentGuid == paymentGuid);
PayParams payRes = new()
{
jsApiUiPackage = payEntity,
outTradeNo = orderNo,
CreateTime = res.Create_time,
OverTime = res.Create_time.AddMinutes(1)
};
return payRes;
}
}
#region
/// <summary>
/// 添加订单流水
/// </summary>
/// <param name="userId">客户id</param>
/// <param name="orderNo">订单号</param>
/// <param name="payType">支付类型</param>
/// <param name="beforeMoney">核销前价格</param>
/// <param name="payType">核销后价格/操作金额</param>
/// <returns></returns>
/// <exception cref="CustomException"></exception>
public async Task<long> AddPayment(long userId, string orderNo, int payType, decimal beforeMoney, decimal money)
{
// 添加订单流水
Payment payment = new Payment
{
CustomerGuid = userId,
PaymentNumber = orderNo,
PaymentBuytype = payType,
PaymentBeforeMoney = beforeMoney,
PaymentMoney = money,
PaymentStatus = 1
};
var response = await _PaymentRepository.InsertReturnSnowflakeIdAsync(payment);
if (response == 0)
throw new CustomException("订单流水数据加入失败");
return response;
}
// 添加业务订单
public async Task<long> AddOrder(long userId, string orderNo, int payType, decimal beforeMoney, decimal money)
{
Payment payment = new Payment
{
CustomerGuid = userId,
PaymentNumber = orderNo,
PaymentBuytype = payType,
PaymentBeforeMoney = beforeMoney,
PaymentMoney = money,
PaymentStatus = 1
};
var response = await _PaymentRepository.InsertReturnSnowflakeIdAsync(payment);
if (response == 0)
throw new CustomException("订单流水数据加入失败");
return response;
}
#endregion
#endregion
}
}