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 { /// /// 支付业务实现类Api /// /// @author lwh /// @date 2023-06-12 /// [AppService(ServiceType = typeof(IPayServiceApi), ServiceLifetime = LifeTime.Transient)] public class PayServiceApi : BaseService, 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 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 业务方法调用 /// /// 添加订单流水 /// /// 客户id /// 订单号 /// 支付类型 /// 核销前价格 /// 核销后价格/操作金额 /// /// public async Task 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 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 } }