195 lines
6.5 KiB
C#
195 lines
6.5 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;
|
|
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;
|
|
|
|
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 CouponRepository _CouponRepository;
|
|
private readonly SenparcHttpClient _httpClient;
|
|
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
public PayServiceApi(ShopRepository ShopRepository, GoodsCategoryRepository goodsCategoryRepository, CustomerRepository customerRepository, OrderRepository orderRepository, PaymentRepository paymentRepository, CouponRepository couponRepository, SenparcHttpClient httpClient)
|
|
{
|
|
this._ShopRepository = ShopRepository;
|
|
_GoodsCategoryRepository = goodsCategoryRepository;
|
|
_CustomerRepository = customerRepository;
|
|
_OrderRepository = orderRepository;
|
|
_PaymentRepository = paymentRepository;
|
|
_CouponRepository = couponRepository;
|
|
_httpClient = httpClient;
|
|
}
|
|
|
|
#region Api接口代码
|
|
|
|
|
|
public async Task<PayParams> 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 业务方法调用
|
|
|
|
/// <summary>
|
|
/// 添加订单流水
|
|
/// </summary>
|
|
/// <param name="parm">下单信息Dto对象</param>
|
|
/// <param name="orderNo">订单号</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="CustomException"></exception>
|
|
public async Task<long> 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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 添加业务订单
|
|
/// </summary>
|
|
/// <param name="parm">下单信息Dto对象</param>
|
|
/// <param name="orderNo">订单号</param>
|
|
/// <param name="paymentGuid">支付订单流水guid</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="CustomException"></exception>
|
|
public async Task<long> 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
|
|
|
|
}
|
|
}
|