wlzboy
2025-12-03 656d6f8029f8bf9b2daa9dcc89101a879a70b860
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
219
220
221
222
223
224
225
226
227
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/notify")
    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/notify")
    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";
        }
    }
 
    /**
     * 第三方支付宝回调(GET请求)
     * <p>
     * 第三方支付宝通过GET方式回调,参数通过URL传递
     * <p>
     * 参数格式:
     * method=xxx&APPID=xxx&PaidMoneyType=xxx&PaidMoney=xxx&PaidRemarks=xxx&UnixTime=xxx&Sign=xxx
     * <p>
     * 参数说明:
     * - method: 方法名
     * - APPID: 应用ID
     * - PaidMoneyType: 支付类型
     * - PaidMoney: 支付金额(单位:分)
     * - PaidRemarks: 支付备注(包含订单号和交易号信息)
     * - UnixTime: 时间戳
     * - Sign: 签名
     * <p>
     * 示例:
     * GET /api/pay/notify/alipay/thirdparty?method=alipay.pay&APPID=123456&PaidMoneyType=alipay&PaidMoney=100&PaidRemarks=1234567890&UnixTime=1638360000&Sign=abc123
     */
    @Anonymous
    @GetMapping("/alipay/thirdparty")
    public String alipayThirdPartyNotify(HttpServletRequest request) {
        try {
            log.info("接收到第三方支付宝回调(GET请求)");
            
            // 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. 第三方支付宝不需要验签,或者根据第三方平台的签名规则进行验证
            // 这里直接处理,如果需要验签可以在Service层添加
            
            // 3. 处理回调(更新订单状态、触发业务回调)
            paymentNotifyService.processAlipayThirdPartyNotify(params);
            
            // 4. 返回第三方支付宝要求的应答
            return "success";
        } catch (Exception e) {
            log.error("处理第三方支付宝回调失败", e);
            return "fail";
        }
    }
 
    /**
     * 第三方微信回调(GET请求)
     * <p>
     * 第三方微信通过GET方式回调,参数通过URL传递
     * <p>
     * 参数格式:
     * method=xxx&APPID=xxx&PaidMoneyType=xxx&PaidMoney=xxx&PaidRemarks=xxx&UnixTime=xxx
     * <p>
     * 参数说明:
     * - method: 方法名
     * - APPID: 应用ID
     * - PaidMoneyType: 支付类型
     * - PaidMoney: 支付金额(单位:分)
     * - PaidRemarks: 支付备注(包含订单号和交易号信息)
     * - UnixTime: 时间戳
     * <p>
     * 示例:
     * GET /api/pay/notify/wechat/thirdparty?method=wechat.pay&APPID=123456&PaidMoneyType=wechat&PaidMoney=100&PaidRemarks=1234567890&UnixTime=1638360000
     */
    @Anonymous
    @GetMapping("/wechat/thirdparty")
    public String wechatThirdPartyNotify(HttpServletRequest request) {
        try {
            log.info("接收到第三方微信回调(GET请求)");
            
            // 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. 第三方微信不需要验签,或者根据第三方平台的签名规则进行验证
            // 这里直接处理,如枟需要验签可以在Service层添加
            
            // 3. 处理回调(更新订单状态、触发业务回调)
            paymentNotifyService.processWechatThirdPartyNotify(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();
    }
}