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
package com.ruoyi.payment.interfaces.controller;
 
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.payment.application.service.PaymentNotifyService;
import com.ruoyi.payment.infrastructure.channel.alipay.AlipayUtil;
import com.ruoyi.payment.infrastructure.channel.wechat.WxPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 渠道回调接口控制器
 * 
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/api/pay/notify")
public class PaymentNotifyController {
 
    @Autowired
    private WxPayUtil wxPayUtil;
 
    @Autowired
    private AlipayUtil alipayUtil;
 
    @Autowired
    private PaymentNotifyService paymentNotifyService;
 
    /**
     * 微信支付回调
     */
    @Anonymous
    @PostMapping("/wechat")
    public String wechatNotify(HttpServletRequest request) {
        try {
            log.info("接收到微信支付回调");
            
            // 1. 读取XML报文
            String xmlData = readRequestBody(request);
            log.info("微信回调报文: {}", xmlData);
            
            // 2. 解析XML为Map
            Map<String, String> params = wxPayUtil.xmlToMap(xmlData);
            
            // 3. 验证签名
            if (!wxPayUtil.verifySign(params)) {
                log.error("微信回调签名验证失败");
                return wxPayUtil.buildFailResponse("签名验证失败");
            }
            
            log.info("微信回调签名验证通过");
            
            // 4. 处理回调(更新订单状态、触发业务回调)
            paymentNotifyService.processWechatNotify(params, xmlData);
            
            // 5. 返回微信要求的XML应答
            return wxPayUtil.buildSuccessResponse();
        } catch (Exception e) {
            log.error("处理微信回调失败", e);
            return wxPayUtil.buildFailResponse("处理失败");
        }
    }
 
    @Anonymous
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
 
    /**
     * 支付宝回调
     */
    @Anonymous
    @PostMapping("/alipay")
    public String alipayNotify(HttpServletRequest request) {
        try {
            log.info("接收到支付宝回调");
            
            // 1. 获取所有参数
            Map<String, String> params = new HashMap<>();
            Enumeration<String> parameterNames = request.getParameterNames();
            while (parameterNames.hasMoreElements()) {
                String name = parameterNames.nextElement();
                params.put(name, request.getParameter(name));
            }
            
            log.info("支付宝回调参数: {}", params);
            
            // 2. 验证签名
            if (!alipayUtil.verifySign(params)) {
                log.error("支付宝回调签名验证失败");
                return "fail";
            }
            
            log.info("支付宝回调签名验证通过");
            
            // 3. 处理回调(更新订单状态、触发业务回调)
            paymentNotifyService.processAlipayNotify(params);
            
            // 4. 返回支付宝要求的应答
            return "success";
        } catch (Exception e) {
            log.error("处理支付宝回调失败", e);
            return "fail";
        }
    }
 
    /**
     * 读取请求体
     */
    private String readRequestBody(HttpServletRequest request) throws Exception {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = request.getReader()) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        }
        return sb.toString();
    }
}