| 
						
						
						
					 | 
				
				 | 
				
					@ -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<WeChatOrderAppService> _logger; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        private readonly IConfiguration _configuration; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        private readonly IRepository<Person> _personRepository; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        public WeChatOrderAppService( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            IRepository<WeChatOrder> weChatOrderRepository, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            IRepository<AppointPatientRegister> appointPatientRegisterRepository, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            IRepository<WeChatOrderRefund> weChatOrderRefundRepository, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            IWechatTenpayClientFactory wechatTenpayClientFactory, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            ILogger<WeChatOrderAppService> logger, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            IConfiguration configuration) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            IConfiguration configuration, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            IRepository<Person> 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,8 +199,17 @@ 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) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                if (input.RefundMoney * 100 > weChatOrderEnt.OrderMoney) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    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; | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -217,7 +235,7 @@ namespace Shentun.WebPeis.Wechats | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                        #region 生成订单
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    var weChatOrderRefundEnt = new WeChatOrderRefund | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                        weChatOrderRefundEnt = new WeChatOrderRefund | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                        { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                            RefundMoney = request.Amount.Refund, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                            Status = "PROCESSING", | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -240,6 +258,11 @@ namespace Shentun.WebPeis.Wechats | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                else | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                    throw new UserFriendlyException($"已申请过退款,不要重复退款"); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            else | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                throw new UserFriendlyException("预约Id不正确"); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
				
				 | 
				
					
  |