diff --git a/src/Shentun.WebPeis.Application.Contracts/Wechats/CreateWeChatOrderInputDto.cs b/src/Shentun.WebPeis.Application.Contracts/Wechats/CreateWeChatOrderInputDto.cs index 21c8090..b7e1a31 100644 --- a/src/Shentun.WebPeis.Application.Contracts/Wechats/CreateWeChatOrderInputDto.cs +++ b/src/Shentun.WebPeis.Application.Contracts/Wechats/CreateWeChatOrderInputDto.cs @@ -7,9 +7,9 @@ namespace Shentun.WebPeis.Wechats public class CreateWeChatOrderInputDto { /// - /// 用户标识 + /// 人员ID /// - public string Openid { get; set; } + public Guid PersonId { get; set; } /// /// 订单金额 元 diff --git a/src/Shentun.WebPeis.Application/Wechats/WeChatOrderAppService.cs b/src/Shentun.WebPeis.Application/Wechats/WeChatOrderAppService.cs index 9297b62..f0e5fa2 100644 --- a/src/Shentun.WebPeis.Application/Wechats/WeChatOrderAppService.cs +++ b/src/Shentun.WebPeis.Application/Wechats/WeChatOrderAppService.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Authorization; +using Azure; +using Flurl.Util; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; @@ -33,14 +35,15 @@ namespace Shentun.WebPeis.Wechats private readonly IWechatTenpayClientFactory _wechatTenpayClientFactory; private readonly ILogger _logger; private readonly IConfiguration _configuration; - + private readonly IRepository _personRepository; public WeChatOrderAppService( IRepository weChatOrderRepository, IRepository appointPatientRegisterRepository, IRepository weChatOrderRefundRepository, IWechatTenpayClientFactory wechatTenpayClientFactory, ILogger logger, - IConfiguration configuration) + IConfiguration configuration, + IRepository personRepository) { _weChatOrderRepository = weChatOrderRepository; _appointPatientRegisterRepository = appointPatientRegisterRepository; @@ -48,6 +51,7 @@ namespace Shentun.WebPeis.Wechats _wechatTenpayClientFactory = wechatTenpayClientFactory; _logger = logger; _configuration = configuration; + _personRepository = personRepository; } @@ -67,9 +71,9 @@ namespace Shentun.WebPeis.Wechats throw new UserFriendlyException("预约ID不能为空"); } - if (string.IsNullOrWhiteSpace(input.Openid)) + if (input.PersonId == Guid.Empty) { - throw new UserFriendlyException("openid不能为空"); + throw new UserFriendlyException("人员ID不能为空"); } if (input.OrderMoney <= 0) @@ -83,10 +87,15 @@ namespace Shentun.WebPeis.Wechats throw new UserFriendlyException("预约Id不正确"); } - if (appointPatientRegisterEnt == null) + + var personEnt = await _personRepository.FirstOrDefaultAsync(f => f.PersonId == input.PersonId); + if (personEnt == null) { - throw new UserFriendlyException("预约Id不正确"); + throw new UserFriendlyException("人员不存在"); } + + + string appid = _configuration.GetSection("WeChat:Appid").Value; string mchid = _configuration.GetSection("Merchant:MchId").Value; string notifyUrl = _configuration.GetSection("Merchant:NotifyUrl").Value; @@ -107,7 +116,7 @@ namespace Shentun.WebPeis.Wechats }, Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { - OpenId = input.Openid + OpenId = personEnt.WechatOpenId } }; var response = await _wechatTenpayClient.ExecuteCreatePayTransactionJsapiAsync(request); @@ -123,7 +132,7 @@ namespace Shentun.WebPeis.Wechats Appid = appid, AppointPatientRegisterId = input.AppointPatientRegisterId, Mchid = mchid, - Openid = input.Openid, + Openid = personEnt.WechatOpenId, OrderMoney = Convert.ToInt32(Math.Round(input.OrderMoney * 100)), OutTradeNo = outTradeNo, Status = "NOTPAY", @@ -135,7 +144,7 @@ namespace Shentun.WebPeis.Wechats { weChatOrderEnt.Appid = appid; weChatOrderEnt.Mchid = mchid; - weChatOrderEnt.Openid = input.Openid; + weChatOrderEnt.Openid = personEnt.WechatOpenId; weChatOrderEnt.OrderMoney = Convert.ToInt32(Math.Round(input.OrderMoney * 100)); weChatOrderEnt.OutTradeNo = outTradeNo; weChatOrderEnt.TransactionId = ""; @@ -190,53 +199,67 @@ namespace Shentun.WebPeis.Wechats } #endregion - var weChatOrderEnt = await _weChatOrderRepository.FirstOrDefaultAsync(f => f.AppointPatientRegisterId == input.AppointPatientRegisterId); + var weChatOrderEnt = await _weChatOrderRepository.FirstOrDefaultAsync(f => f.AppointPatientRegisterId == input.AppointPatientRegisterId + && f.Status == "SUCCESS"); if (weChatOrderEnt != null) { - string appid = _configuration.GetSection("WeChat:Appid").Value; - string mchid = _configuration.GetSection("Merchant:MchId").Value; - string notifyUrl = _configuration.GetSection("Merchant:NotifyUrl").Value; - string outRefundNo = $"T{DateTime.Now.ToString("yyyyMMddHHmmss")}{new Random().Next(100000, 999999)}"; - - var _wechatTenpayClient = await _wechatTenpayClientFactory.Create(false); - var request = new CreateRefundDomesticRefundRequest() + if (input.RefundMoney * 100 > weChatOrderEnt.OrderMoney) { - TransactionId = weChatOrderEnt.TransactionId, - OutRefundNumber = outRefundNo, - Amount = new CreateRefundDomesticRefundRequest.Types.Amount() - { - Total = weChatOrderEnt.OrderMoney, - Refund = Convert.ToInt32(Math.Round(input.RefundMoney * 100)) - }, - Reason = "体检预约退款", - NotifyUrl = notifyUrl - }; - var response = await _wechatTenpayClient.ExecuteCreateRefundDomesticRefundAsync(request); - if (response.IsSuccessful()) - { - #region 生成订单 + throw new UserFriendlyException($"退款金额不能大于支付金额"); + } + var weChatOrderRefundEnt = await _weChatOrderRefundRepository.FirstOrDefaultAsync(f => f.WeChatOrderId == weChatOrderEnt.WeChatOrderId); + if (weChatOrderRefundEnt == null) + { + string appid = _configuration.GetSection("WeChat:Appid").Value; + string mchid = _configuration.GetSection("Merchant:MchId").Value; + string notifyUrl = _configuration.GetSection("Merchant:NotifyUrl").Value; + string outRefundNo = $"T{DateTime.Now.ToString("yyyyMMddHHmmss")}{new Random().Next(100000, 999999)}"; - var weChatOrderRefundEnt = new WeChatOrderRefund + var _wechatTenpayClient = await _wechatTenpayClientFactory.Create(false); + var request = new CreateRefundDomesticRefundRequest() { - RefundMoney = request.Amount.Refund, - Status = "PROCESSING", - OutRefundNo = outRefundNo, - Channel = response.Channel, - RefundId = response.RefundId, - UserReceivedAccount = response.UserReceivedAccount, - WeChatOrderId = weChatOrderEnt.WeChatOrderId, - WeChatOrderRefundId = GuidGenerator.Create() + TransactionId = weChatOrderEnt.TransactionId, + OutRefundNumber = outRefundNo, + Amount = new CreateRefundDomesticRefundRequest.Types.Amount() + { + Total = weChatOrderEnt.OrderMoney, + Refund = Convert.ToInt32(Math.Round(input.RefundMoney * 100)) + }, + Reason = "体检预约退款", + NotifyUrl = notifyUrl }; + var response = await _wechatTenpayClient.ExecuteCreateRefundDomesticRefundAsync(request); + if (response.IsSuccessful()) + { + #region 生成订单 + - var isAdd = await _weChatOrderRefundRepository.InsertAsync(weChatOrderRefundEnt); + weChatOrderRefundEnt = new WeChatOrderRefund + { + RefundMoney = request.Amount.Refund, + Status = "PROCESSING", + OutRefundNo = outRefundNo, + Channel = response.Channel, + RefundId = response.RefundId, + UserReceivedAccount = response.UserReceivedAccount, + WeChatOrderId = weChatOrderEnt.WeChatOrderId, + WeChatOrderRefundId = GuidGenerator.Create() + }; + var isAdd = await _weChatOrderRefundRepository.InsertAsync(weChatOrderRefundEnt); - #endregion + + #endregion + } + else + { + throw new UserFriendlyException($"发起退款失败:{response.ErrorCode},{response.ErrorMessage}"); + } } else { - throw new UserFriendlyException($"发起退款失败:{response.ErrorCode},{response.ErrorMessage}"); + throw new UserFriendlyException($"已申请过退款,不要重复退款"); } } else