| | |
| | | using LifePayment.Application.Contracts; |
| | | using LifePayment.Domain; |
| | | using LifePayment.Domain.Shared; |
| | | using Medallion.Threading; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using Microsoft.Extensions.Caching.Distributed; |
| | | using Microsoft.Extensions.Logging; |
| | |
| | | using System.Threading.Tasks; |
| | | using Volo.Abp.AspNetCore.Mvc; |
| | | using Volo.Abp.Caching; |
| | | using Volo.Abp.DistributedLocking; |
| | | using Volo.Abp.Uow; |
| | | using ZeroD.Util; |
| | | |
| | | namespace LifePayment.HttpApi |
| | | { |
| | |
| | | private readonly ILifePayService _lifePayService; |
| | | private readonly ILifePayOrderService _lifePayOrderService; |
| | | private readonly IDistributedCache<string> distributedCache; |
| | | private readonly IAbpDistributedLock distributedLock; |
| | | private readonly ILogger<WxPayNotifyController> _logger; |
| | | |
| | | public WxPayNotifyController( |
| | |
| | | ILogger<WxPayNotifyController> logger, |
| | | ILifePayService lifePayService, |
| | | ILifePayOrderService lifePayOrderService, |
| | | IDistributedCache<string> distributedCache) |
| | | IDistributedCache<string> distributedCache, |
| | | IAbpDistributedLock distributedLock) |
| | | { |
| | | _wxPayApi = wxPayApi; |
| | | _logger = logger; |
| | | _lifePayService = lifePayService; |
| | | _lifePayOrderService = lifePayOrderService; |
| | | this.distributedCache = distributedCache; |
| | | this.distributedLock = distributedLock; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="input"></param> |
| | | /// <returns></returns> |
| | | [HttpPost] |
| | | [UnitOfWork] |
| | | public async Task<WxRechargeNotifyResult> WxRechargeNotify(WxRechargeNotifyInput input) |
| | | { |
| | | try |
| | | { |
| | | _logger.LogError($"生活管家微信充值回调通知:进入微信回调:"); |
| | | var req = input.ToJson(); |
| | | _logger.LogInformation($"生活管家微信充值回调通知:进入微信回调:" + req); |
| | | var data = _wxPayApi.AesGcmDecrypt(input.Resource.AssociatedData, input.Resource.Nonce, input.Resource.Ciphertext); |
| | | _logger.LogError($"生活管家微信充值回调通知data:" + data); |
| | | _logger.LogInformation($"生活管家微信充值回调通知data:" + data); |
| | | var wxPayNotice = JsonConvert.DeserializeObject<WxPayNotice>(data); |
| | | |
| | | if (wxPayNotice.OutTradeNo.Contains("JF") ) |
| | | await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{wxPayNotice.OutTradeNo}", TimeSpan.FromSeconds(60)); |
| | | _logger.LogInformation($"锁:LockKey:UpdateOrder:{wxPayNotice.OutTradeNo} - {orderLock != null}"); |
| | | |
| | | if (wxPayNotice.OutTradeNo.Contains("JF")) |
| | | { |
| | | if (wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.支付成功) |
| | | { |
| | | |
| | | var key = $"WxRechargeNotify_{wxPayNotice.OutTradeNo}"; |
| | | if (string.IsNullOrWhiteSpace(distributedCache.Get(key))) |
| | | { |
| | | _logger.LogInformation("正在处理回调"); |
| | | await _lifePayService.LifePaySuccessHandler(wxPayNotice.OutTradeNo, wxPayNotice.TransactionId); |
| | | _logger.LogInformation("已处理回调"); |
| | | |
| | | // 插入收支流水 |
| | | await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput() |
| | |
| | | ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses, |
| | | Amount = wxPayNotice.Amount.Total |
| | | }); |
| | | _logger.LogInformation("已插入收支流水"); |
| | | |
| | | distributedCache.Set(key, data, new DistributedCacheEntryOptions |
| | | { |
| | | AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) |
| | | }); |
| | | |
| | | await CurrentUnitOfWork.SaveChangesAsync(); |
| | | } |
| | | else |
| | | { |
| | | _logger.LogInformation("已过滤重复请求"); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | _logger.LogInformation("交易状态不符合规则"); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | _logger.LogInformation("订单号不符合规则"); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | await CurrentUnitOfWork.RollbackAsync(); |
| | | return new WxRechargeNotifyResult |
| | | { |
| | | Code = "FAIL", |
| | |
| | | { |
| | | _logger.LogError($"微信退款回调通知:进入微信回调"); |
| | | var data = _wxPayApi.AesGcmDecrypt(input.Resource.AssociatedData, input.Resource.Nonce, input.Resource.Ciphertext); |
| | | _logger.LogError($"微信退款回调通知data:"+ data); |
| | | _logger.LogError($"微信退款回调通知data:" + data); |
| | | var wxPayNotice = JsonConvert.DeserializeObject<WxPayDomesticRefundsNotice>(data); |
| | | |
| | | if (wxPayNotice.OutTradeNo.Contains("JF")) |
| | | { |
| | | |
| | | |
| | | switch (wxPayNotice.RefundStatus) |
| | | { |
| | | case LifePaymentConstant.WxPayRefundStatus.退款成功: |
| | |
| | | case LifePaymentConstant.WxPayRefundStatus.退款异常: |
| | | await _lifePayService.LifePayRefundsHandler(wxPayNotice.OutTradeNo, LifePayRefundStatusEnum.退款中); |
| | | break; |
| | | default : await _lifePayService.LifePayRefundsHandler(wxPayNotice.OutTradeNo, LifePayRefundStatusEnum.退款中);break; |
| | | default: await _lifePayService.LifePayRefundsHandler(wxPayNotice.OutTradeNo, LifePayRefundStatusEnum.退款中); break; |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | |
| | | return new WxRechargeNotifyResult |
| | | { |
| | | Code = "FAIL", |