using Infrastructure.Attribute;
using System.Threading.Tasks;
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.Model.Dto.Api.Pay;
using Senparc.Weixin.TenPayV3.Apis.BasePay;
using System;
using System.Collections.Generic;
using ARW.Common;
using System.Linq;
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 CustomerRepository _CustomerRepository;
private readonly PaymentRepository _PaymentRepository;
public PayServiceApi(CustomerRepository customerRepository, PaymentRepository paymentRepository, SenparcHttpClient httpClient)
{
_CustomerRepository = customerRepository;
_PaymentRepository = paymentRepository;
_httpClient = httpClient;
}
#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, price, price);
/* 添加业务订单 */
//var orderGuid = await AddOrder(parm, orderNo, paymentGuid, goodsPrice, afterPriceRes, freight);
#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(30)
};
return payRes;
}
}
///
/// 支付回调的业务处理
///
///
public async Task HandleNotify(OrderReturnJson res)
{
/* 修改订单流水 */
var paymentGuid = await UpdatePayment(res);
}
#region 业务方法调用
#region 添加订单
///
/// 添加订单流水
///
/// 下单信息Dto对象
/// 订单号
/// 核销前价格
/// 核销后价格
///
///
public async Task AddPayment(CommitPayDtoApi parm, string orderNo, decimal beforeMoney, decimal afterMoney)
{
// 添加订单流水
Payment payment = new()
{
CustomerGuid = parm.UserId,
PaymentNumber = orderNo,
PaymentBuytype = parm.PayType,
PaymentBeforeMoney = afterMoney,
//PaymentMoney = afterMoney,
PaymentStatus = 1
};
var response = await _PaymentRepository.InsertReturnSnowflakeIdAsync(payment);
if (response == 0)
throw new CustomException("订单流水数据加入失败");
return response;
}
#endregion
#region 支付完成后的业务调用方法
///
/// 修改订单流水
///
///
///
public async Task UpdatePayment(OrderReturnJson res)
{
await _PaymentRepository.UpdateAsync(f => new Payment
{
PaymentWeixinNumber = res.transaction_id,
PaymentMoney = ((decimal)res.amount.payer_total) / 100,
PaymentStatus = 2,
Update_time = DateTime.Now,
}, f => f.PaymentNumber == res.out_trade_no);
var payment = await _PaymentRepository.GetFirstAsync(s => s.PaymentNumber == res.out_trade_no);
return payment.PaymentGuid;
}
#endregion
#endregion
#endregion
}
}