zhengyiming
2025-04-16 b9e2c8345c5c83581a7a57bcda316928b171f322
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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<WxPayNotifyController> _logger;
 
        public WxPayNotifyController(
               IWxPayApi wxPayApi,
               ILogger<WxPayNotifyController> logger,
               ILifePayService lifePayService,
               ILifePayOrderService lifePayOrderService)
        {
            _wxPayApi = wxPayApi;
            _logger = logger;
            _lifePayService = lifePayService;
            _lifePayOrderService = lifePayOrderService;
        }
 
        /// <summary>
        /// 微信支付回调通知
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [UnitOfWork]
        public async Task<WxRechargeNotifyResult> WxRechargeNotify(WxRechargeNotifyInput input)
        {
            try
            {
                _logger.LogError($"生活管家微信充值回调通知:进入微信回调:");
                var data = _wxPayApi.AesGcmDecrypt(input.Resource.AssociatedData, input.Resource.Nonce, input.Resource.Ciphertext);
                _logger.LogError($"生活管家微信充值回调通知data:" + data);
                var wxPayNotice = JsonConvert.DeserializeObject<WxPayNotice>(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",
            };
        }
 
        /// <summary>
        /// 微信退款通知回调
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [UnitOfWork]
        public async Task<WxRechargeNotifyResult> WxPayDomesticRefundsNotify(WxRechargeNotifyInput input)
        {
            try
            {
                _logger.LogError($"微信退款回调通知:进入微信回调");
                var data = _wxPayApi.AesGcmDecrypt(input.Resource.AssociatedData, input.Resource.Nonce, input.Resource.Ciphertext);
                _logger.LogError($"微信退款回调通知data:"+ data);
                var wxPayNotice = JsonConvert.DeserializeObject<WxPayDomesticRefundsNotice>(data);
 
                if (wxPayNotice.OutTradeNo.Contains("JF"))
                {
                   
                    switch (wxPayNotice.RefundStatus)
                    {
                        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.退款关闭:
                        case LifePaymentConstant.WxPayRefundStatus.退款处理中:
                        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",
            };
        }
    }
}