using LifePayment.Application.Contracts; using LifePayment.Domain; using LifePayment.Domain.Shared; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Text.RegularExpressions; using System.Threading.Tasks; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Uow; namespace LifePayment.HttpApi { [Route("api/[controller]/[action]")] [ApiController] [IgnoreAntiforgeryToken] // [Authorize(AuthenticationSchemes = WxPaySignAuthenticationDefaults.AuthenticationScheme)] public class WxPayNotifyController : AbpController { private readonly IWxPayApi _wxPayApi; private readonly ILifePayService _lifePayService; private readonly ILifePayOrderService _lifePayOrderService; private readonly ILogger _logger; public WxPayNotifyController( IWxPayApi wxPayApi, ILogger logger, ILifePayService lifePayService, ILifePayOrderService lifePayOrderService) { _wxPayApi = wxPayApi; _logger = logger; _lifePayService = lifePayService; _lifePayOrderService = lifePayOrderService; } /// /// 微信支付回调通知 /// /// /// [HttpPost] [UnitOfWork] public async Task WxRechargeNotify(WxRechargeNotifyInput input) { try { _logger.LogError($"生活管家微信充值回调通知:进入微信回调"); var data = _wxPayApi.AesGcmDecrypt(input.Resource.AssociatedData, input.Resource.Nonce, input.Resource.Ciphertext); var wxPayNotice = JsonConvert.DeserializeObject(data); if (wxPayNotice.OutTradeNo.Contains("JF") ) { if (wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.支付成功) { await _lifePayService.LifePaySuccessHandler(wxPayNotice.OutTradeNo, wxPayNotice.TransactionId); // 插入收支流水 await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput() { OrderNo = wxPayNotice.OutTradeNo, OutOrderNo = wxPayNotice.TransactionId, LifePayType = LifePayTypeEnum.WxPay, ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses, Amount = wxPayNotice.Amount.Total }); } } } catch (Exception ex) { await CurrentUnitOfWork.RollbackAsync(); return new WxRechargeNotifyResult { Code = "FAIL", Message = ex.Message, }; } return new WxRechargeNotifyResult { Code = "SUCCESS", }; } /// /// 微信退款通知回调 /// /// /// [HttpPost] [UnitOfWork] public async Task WxPayDomesticRefundsNotify(WxRechargeNotifyInput input) { try { _logger.LogError($"微信退款回调通知:进入微信回调"); var data = _wxPayApi.AesGcmDecrypt(input.Resource.AssociatedData, input.Resource.Nonce, input.Resource.Ciphertext); var wxPayNotice = JsonConvert.DeserializeObject(data); if (wxPayNotice.OutTradeNo.Contains("JF")) { if (wxPayNotice.RefundStatus == LifePaymentConstant.WxPayRefundStatus.退款成功) { } switch (wxPayNotice.RefundStatus) { case LifePaymentConstant.WxPayRefundStatus.退款成功: case LifePaymentConstant.WxPayRefundStatus.退款关闭: await _lifePayService.LifePayRefundsHandler(wxPayNotice.OutTradeNo, LifePayRefundStatusEnum.已退款); // 插入收支流水 await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput() { OrderNo = wxPayNotice.OutTradeNo, OutRefundNo = wxPayNotice.OutRefundNo, OutOrderNo = wxPayNotice.TransactionId, LifePayType = LifePayTypeEnum.WxPay, ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Receipts, Amount = wxPayNotice.Amount.Total }); break; case LifePaymentConstant.WxPayRefundStatus.退款处理中: await _lifePayService.LifePayRefundsHandler(wxPayNotice.OutTradeNo, LifePayRefundStatusEnum.退款中); break; case LifePaymentConstant.WxPayRefundStatus.退款异常: await _lifePayService.LifePayRefundsHandler(wxPayNotice.OutTradeNo, LifePayRefundStatusEnum.退款中); break; default : await _lifePayService.LifePayRefundsHandler(wxPayNotice.OutTradeNo, LifePayRefundStatusEnum.退款中);break; } } } catch (Exception ex) { return new WxRechargeNotifyResult { Code = "FAIL", Message = ex.Message, }; } return new WxRechargeNotifyResult { Code = "SUCCESS", }; } } }