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; using ARW.Model.Models.Business.OrderManage.Orders; using ARW.Model.Dto.Api.Carts; using ARW.Repository.Business.Marketing.CouponManage.Coupons; using Org.BouncyCastle.Crypto.Prng; using Aliyun.Acs.Core.Logging; using ARW.Model.Dto.Api.Pay; using ARW.Repository.Business.Carts; using ARW.Repository.Business.GoodsManager.Goodss; 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 SenparcHttpClient _httpClient; private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private readonly ShopRepository _ShopRepository; private readonly GoodsRepository _GoodsRepository; private readonly GoodsCategoryRepository _GoodsCategoryRepository; private readonly CustomerRepository _CustomerRepository; private readonly OrderRepository _OrderRepository; private readonly PaymentRepository _PaymentRepository; private readonly CouponRepository _CouponRepository; private readonly CartRepository _CartRepository; public PayServiceApi(ShopRepository ShopRepository, GoodsCategoryRepository goodsCategoryRepository, CustomerRepository customerRepository, OrderRepository orderRepository, PaymentRepository paymentRepository, CouponRepository couponRepository, SenparcHttpClient httpClient, GoodsRepository goodsRepository) { this._ShopRepository = ShopRepository; _GoodsCategoryRepository = goodsCategoryRepository; _CustomerRepository = customerRepository; _OrderRepository = orderRepository; _PaymentRepository = paymentRepository; _CouponRepository = couponRepository; _httpClient = httpClient; _GoodsRepository = goodsRepository; } #region Api接口代码 public async Task HandelPrePay(CommitPayDtoApi 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, orderNo); /* 添加业务订单 */ var orderGuid = await AddOrder(parm, orderNo, paymentGuid); #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 业务方法调用 public async Task CalculatePrice(CommitPayDtoApi parm) { decimal price = 0; foreach (var item in parm.GoodsRequestList) { var cart = await _CartRepository.GetFirstAsync(s => s.CartId == item.CartId); var goods = await _GoodsRepository.GetFirstAsync(s => s.GoodsGuid == cart.GoodsGuid); // 是否有规格 if(cart.GoodsSkuId != 0) { } } return price; } /// /// 添加订单流水 /// /// 下单信息Dto对象 /// 订单号 /// /// public async Task AddPayment(CommitPayDtoApi parm, string orderNo) { // 添加订单流水 Payment payment = new() { CustomerGuid = parm.UserId, PaymentNumber = orderNo, PaymentBuytype = parm.PayType, PaymentBeforeMoney = parm.BeforeMoney, PaymentMoney = parm.Money, PaymentStatus = 1 }; var response = await _PaymentRepository.InsertReturnSnowflakeIdAsync(payment); if (response == 0) throw new CustomException("订单流水数据加入失败"); return response; } /// /// 添加业务订单 /// /// 下单信息Dto对象 /// 订单号 /// 支付订单流水guid /// /// public async Task AddOrder(CommitPayDtoApi parm, string orderNo, long paymentGuid) { Order order = new() { PaymentGuid = paymentGuid, CustomerGuid = parm.UserId, OrderNumber = orderNo, GoodsTotalAmoun = parm.BeforeMoney, OrderAmount = parm.Money, PayPrice = parm.Money, OrderRemark = parm?.Remark, PayType = parm.PayType, PayStatus = 1, }; // 计算总额 // 优惠券减免价格 if (parm.CouponId != 0) { var coupon = await _CouponRepository.GetFirstAsync(s => s.CouponId == parm.CouponId); if (coupon != null) { order.CouponGuid = coupon.CouponGuid; order.CouponMoney = coupon.CouponDeductionMoney; order.PayPrice = order.OrderAmount - coupon.CouponDeductionMoney; } else { throw new CustomException($"优惠券Id为 {parm.CouponId} 找不到"); } } // 运费计算 // TODO:余额支付 var response = await _OrderRepository.InsertReturnSnowflakeIdAsync(order); if (response == 0) throw new CustomException("业务订单数据加入失败"); return response; } #endregion #endregion } }