wlzboy
2025-11-27 668e570bd1db6bd00e4293b6977e6d3d051053ce
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.ruoyi.payment.application.service;
 
import com.alibaba.fastjson.JSON;
import com.ruoyi.payment.domain.model.NotifyLog;
import com.ruoyi.payment.domain.model.PaymentOrder;
import com.ruoyi.payment.domain.model.PaymentTransaction;
import com.ruoyi.payment.infrastructure.persistence.mapper.NotifyLogMapper;
import com.ruoyi.payment.infrastructure.persistence.mapper.PaymentOrderMapper;
import com.ruoyi.payment.infrastructure.persistence.mapper.PaymentTransactionMapper;
import com.ruoyi.payment.infrastructure.util.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.Map;
 
/**
 * 支付回调处理服务
 * 
 * @author ruoyi
 */
@Slf4j
@Service
public class PaymentNotifyService {
 
    @Autowired
    private PaymentOrderMapper paymentOrderMapper;
 
    @Autowired
    private PaymentTransactionMapper paymentTransactionMapper;
 
    @Autowired
    private NotifyLogMapper notifyLogMapper;
 
    @Autowired
    private BizCallbackService bizCallbackService;
 
    /**
     * 处理微信支付回调
     */
    @Transactional(rollbackFor = Exception.class)
    public void processWechatNotify(Map<String, String> params, String rawXml) {
        String outTradeNo = params.get("out_trade_no"); // 商户订单号
        String transactionId = params.get("transaction_id"); // 微信交易号
        String resultCode = params.get("result_code");
        String returnCode = params.get("return_code");
 
        log.info("处理微信回调,商户订单号: {}, 微信交易号: {}, 结果: {}", outTradeNo, transactionId, resultCode);
 
        // 检查幂等性
        if (isNotifyProcessed("WECHAT", transactionId)) {
            log.info("微信回调已处理过,交易号: {}", transactionId);
            return;
        }
 
        // 记录回调日志
        NotifyLog notifyLog = saveNotifyLog("WECHAT", transactionId, rawXml, true);
 
        try {
            // 通信成功且业务成功
            if ("SUCCESS".equals(returnCode) && "SUCCESS".equals(resultCode)) {
                // 查询订单和交易(使用业务订单号查询)
                PaymentOrder order = paymentOrderMapper.selectByBizOrderIdAndChannel(outTradeNo, "WECHAT");
                if (order == null) {
                    log.error("订单不存在: {}", outTradeNo);
                    updateNotifyLog(notifyLog.getId(), false, "订单不存在");
                    return;
                }
 
                PaymentTransaction transaction = paymentTransactionMapper.selectPendingByOrderId(order.getId());
                if (transaction == null) {
                    log.error("待支付交易不存在,订单ID: {}", order.getId());
                    updateNotifyLog(notifyLog.getId(), false, "交易不存在");
                    return;
                }
 
                // 更新交易状态
                transaction.setStatus("SUCCEEDED");
                transaction.setChannelTradeNo(transactionId);
                transaction.setPaidAt(LocalDateTime.now());
                transaction.setResponseSnapshot(JSON.toJSONString(params));
                paymentTransactionMapper.update(transaction);
 
                // 更新订单状态
                order.setStatus("SUCCEEDED");
                order.setChannelTradeNo(transactionId);
                order.setPaidAt(LocalDateTime.now());
                order.setLatestTransactionId(transaction.getId());
                paymentOrderMapper.update(order);
 
                // 更新通知日志
                notifyLog.setOrderId(order.getId());
                notifyLog.setTransactionId(transaction.getId());
                updateNotifyLog(notifyLog.getId(), true, "处理成功");
 
                // 触发业务回调
                bizCallbackService.triggerCallback(order, transaction);
 
                log.info("微信支付回调处理成功,订单ID: {}, 交易ID: {}", order.getId(), transaction.getId());
            } else {
                log.warn("微信支付失败,returnCode: {}, resultCode: {}", returnCode, resultCode);
                updateNotifyLog(notifyLog.getId(), true, "支付失败");
            }
        } catch (Exception e) {
            log.error("处理微信回调异常", e);
            updateNotifyLog(notifyLog.getId(), false, "处理异常: " + e.getMessage());
            throw e;
        }
    }
 
    /**
     * 处理支付宝回调
     */
    @Transactional(rollbackFor = Exception.class)
    public void processAlipayNotify(Map<String, String> params) {
        String outTradeNo = params.get("out_trade_no"); // 商户订单号
        String tradeNo = params.get("trade_no"); // 支付宝交易号
        String tradeStatus = params.get("trade_status");
 
        log.info("处理支付宝回调,商户订单号: {}, 支付宝交易号: {}, 状态: {}", outTradeNo, tradeNo, tradeStatus);
 
        // 检查幂等性
        if (isNotifyProcessed("ALIPAY", tradeNo)) {
            log.info("支付宝回调已处理过,交易号: {}", tradeNo);
            return;
        }
 
        // 记录回调日志
        NotifyLog notifyLog = saveNotifyLog("ALIPAY", tradeNo, JSON.toJSONString(params), true);
 
        try {
            // 交易成功
            if ("TRADE_SUCCESS".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus)) {
                // 查询订单和交易(使用业务订单号查询)
                PaymentOrder order = paymentOrderMapper.selectByBizOrderIdAndChannel(outTradeNo, "ALIPAY");
                if (order == null) {
                    log.error("订单不存在: {}", outTradeNo);
                    updateNotifyLog(notifyLog.getId(), false, "订单不存在");
                    return;
                }
 
                PaymentTransaction transaction = paymentTransactionMapper.selectPendingByOrderId(order.getId());
                if (transaction == null) {
                    log.error("待支付交易不存在,订单ID: {}", order.getId());
                    updateNotifyLog(notifyLog.getId(), false, "交易不存在");
                    return;
                }
 
                // 更新交易状态
                transaction.setStatus("SUCCEEDED");
                transaction.setChannelTradeNo(tradeNo);
                transaction.setPaidAt(LocalDateTime.now());
                transaction.setResponseSnapshot(JSON.toJSONString(params));
                paymentTransactionMapper.update(transaction);
 
                // 更新订单状态
                order.setStatus("SUCCEEDED");
                order.setChannelTradeNo(tradeNo);
                order.setPaidAt(LocalDateTime.now());
                order.setLatestTransactionId(transaction.getId());
                paymentOrderMapper.update(order);
 
                // 更新通知日志
                notifyLog.setOrderId(order.getId());
                notifyLog.setTransactionId(transaction.getId());
                updateNotifyLog(notifyLog.getId(), true, "处理成功");
 
                // 触发业务回调
                bizCallbackService.triggerCallback(order, transaction);
 
                log.info("支付宝回调处理成功,订单ID: {}, 交易ID: {}", order.getId(), transaction.getId());
            } else {
                log.warn("支付宝交易状态异常: {}", tradeStatus);
                updateNotifyLog(notifyLog.getId(), true, "状态: " + tradeStatus);
            }
        } catch (Exception e) {
            log.error("处理支付宝回调异常", e);
            updateNotifyLog(notifyLog.getId(), false, "处理异常: " + e.getMessage());
            throw e;
        }
    }
 
    /**
     * 检查通知是否已处理(幂等性)
     */
    private boolean isNotifyProcessed(String channel, String notifyId) {
        return notifyLogMapper.selectProcessedCount(channel, notifyId) > 0;
    }
 
    /**
     * 保存通知日志
     */
    private NotifyLog saveNotifyLog(String channel, String notifyId, String payload, boolean verified) {
        NotifyLog log = new NotifyLog();
        log.setId(SnowflakeIdGenerator.generateId());
        log.setChannel(channel);
        log.setNotifyIdOrSerial(notifyId);
        log.setPayload(payload);
        log.setVerified(verified);
        log.setProcessed(false);
        log.setCreatedAt(LocalDateTime.now());
        notifyLogMapper.insert(log);
        return log;
    }
 
    /**
     * 更新通知日志
     */
    private void updateNotifyLog(Long id, boolean processed, String result) {
        NotifyLog log = new NotifyLog();
        log.setId(id);
        log.setProcessed(processed);
        log.setResult(result);
        notifyLogMapper.update(log);
    }
}