|
|
@ -1,4 +1,6 @@ |
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
|
|
|
|
|
using Azure; |
|
|
|
|
|
using Flurl.Util; |
|
|
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using Microsoft.Extensions.Configuration; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging; |
|
|
@ -33,14 +35,15 @@ namespace Shentun.WebPeis.Wechats |
|
|
private readonly IWechatTenpayClientFactory _wechatTenpayClientFactory; |
|
|
private readonly IWechatTenpayClientFactory _wechatTenpayClientFactory; |
|
|
private readonly ILogger<WeChatOrderAppService> _logger; |
|
|
private readonly ILogger<WeChatOrderAppService> _logger; |
|
|
private readonly IConfiguration _configuration; |
|
|
private readonly IConfiguration _configuration; |
|
|
|
|
|
|
|
|
|
|
|
private readonly IRepository<Person> _personRepository; |
|
|
public WeChatOrderAppService( |
|
|
public WeChatOrderAppService( |
|
|
IRepository<WeChatOrder> weChatOrderRepository, |
|
|
IRepository<WeChatOrder> weChatOrderRepository, |
|
|
IRepository<AppointPatientRegister> appointPatientRegisterRepository, |
|
|
IRepository<AppointPatientRegister> appointPatientRegisterRepository, |
|
|
IRepository<WeChatOrderRefund> weChatOrderRefundRepository, |
|
|
IRepository<WeChatOrderRefund> weChatOrderRefundRepository, |
|
|
IWechatTenpayClientFactory wechatTenpayClientFactory, |
|
|
IWechatTenpayClientFactory wechatTenpayClientFactory, |
|
|
ILogger<WeChatOrderAppService> logger, |
|
|
ILogger<WeChatOrderAppService> logger, |
|
|
IConfiguration configuration) |
|
|
|
|
|
|
|
|
IConfiguration configuration, |
|
|
|
|
|
IRepository<Person> personRepository) |
|
|
{ |
|
|
{ |
|
|
_weChatOrderRepository = weChatOrderRepository; |
|
|
_weChatOrderRepository = weChatOrderRepository; |
|
|
_appointPatientRegisterRepository = appointPatientRegisterRepository; |
|
|
_appointPatientRegisterRepository = appointPatientRegisterRepository; |
|
|
@ -48,6 +51,7 @@ namespace Shentun.WebPeis.Wechats |
|
|
_wechatTenpayClientFactory = wechatTenpayClientFactory; |
|
|
_wechatTenpayClientFactory = wechatTenpayClientFactory; |
|
|
_logger = logger; |
|
|
_logger = logger; |
|
|
_configuration = configuration; |
|
|
_configuration = configuration; |
|
|
|
|
|
_personRepository = personRepository; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -67,9 +71,9 @@ namespace Shentun.WebPeis.Wechats |
|
|
throw new UserFriendlyException("预约ID不能为空"); |
|
|
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) |
|
|
if (input.OrderMoney <= 0) |
|
|
@ -83,10 +87,15 @@ namespace Shentun.WebPeis.Wechats |
|
|
throw new UserFriendlyException("预约Id不正确"); |
|
|
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 appid = _configuration.GetSection("WeChat:Appid").Value; |
|
|
string mchid = _configuration.GetSection("Merchant:MchId").Value; |
|
|
string mchid = _configuration.GetSection("Merchant:MchId").Value; |
|
|
string notifyUrl = _configuration.GetSection("Merchant:NotifyUrl").Value; |
|
|
string notifyUrl = _configuration.GetSection("Merchant:NotifyUrl").Value; |
|
|
@ -107,7 +116,7 @@ namespace Shentun.WebPeis.Wechats |
|
|
}, |
|
|
}, |
|
|
Payer = new CreatePayTransactionJsapiRequest.Types.Payer() |
|
|
Payer = new CreatePayTransactionJsapiRequest.Types.Payer() |
|
|
{ |
|
|
{ |
|
|
OpenId = input.Openid |
|
|
|
|
|
|
|
|
OpenId = personEnt.WechatOpenId |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
var response = await _wechatTenpayClient.ExecuteCreatePayTransactionJsapiAsync(request); |
|
|
var response = await _wechatTenpayClient.ExecuteCreatePayTransactionJsapiAsync(request); |
|
|
@ -123,7 +132,7 @@ namespace Shentun.WebPeis.Wechats |
|
|
Appid = appid, |
|
|
Appid = appid, |
|
|
AppointPatientRegisterId = input.AppointPatientRegisterId, |
|
|
AppointPatientRegisterId = input.AppointPatientRegisterId, |
|
|
Mchid = mchid, |
|
|
Mchid = mchid, |
|
|
Openid = input.Openid, |
|
|
|
|
|
|
|
|
Openid = personEnt.WechatOpenId, |
|
|
OrderMoney = Convert.ToInt32(Math.Round(input.OrderMoney * 100)), |
|
|
OrderMoney = Convert.ToInt32(Math.Round(input.OrderMoney * 100)), |
|
|
OutTradeNo = outTradeNo, |
|
|
OutTradeNo = outTradeNo, |
|
|
Status = "NOTPAY", |
|
|
Status = "NOTPAY", |
|
|
@ -135,7 +144,7 @@ namespace Shentun.WebPeis.Wechats |
|
|
{ |
|
|
{ |
|
|
weChatOrderEnt.Appid = appid; |
|
|
weChatOrderEnt.Appid = appid; |
|
|
weChatOrderEnt.Mchid = mchid; |
|
|
weChatOrderEnt.Mchid = mchid; |
|
|
weChatOrderEnt.Openid = input.Openid; |
|
|
|
|
|
|
|
|
weChatOrderEnt.Openid = personEnt.WechatOpenId; |
|
|
weChatOrderEnt.OrderMoney = Convert.ToInt32(Math.Round(input.OrderMoney * 100)); |
|
|
weChatOrderEnt.OrderMoney = Convert.ToInt32(Math.Round(input.OrderMoney * 100)); |
|
|
weChatOrderEnt.OutTradeNo = outTradeNo; |
|
|
weChatOrderEnt.OutTradeNo = outTradeNo; |
|
|
weChatOrderEnt.TransactionId = ""; |
|
|
weChatOrderEnt.TransactionId = ""; |
|
|
@ -190,53 +199,67 @@ namespace Shentun.WebPeis.Wechats |
|
|
} |
|
|
} |
|
|
#endregion
|
|
|
#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) |
|
|
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 |
|
|
else |
|
|
{ |
|
|
{ |
|
|
throw new UserFriendlyException($"发起退款失败:{response.ErrorCode},{response.ErrorMessage}"); |
|
|
|
|
|
|
|
|
throw new UserFriendlyException($"已申请过退款,不要重复退款"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
|