liujie
2026-01-28 72189753cd1a791a8d90aad34c5bcc165143bf49
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com.ruoyi.web.controller.api;
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.type.TypeReference;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.CodeGenerateUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.model.TCourseOrder;
import com.ruoyi.system.model.TInsurance;
import com.ruoyi.system.model.TInsuranceOrder;
import com.ruoyi.system.service.TCourseOrderService;
import com.ruoyi.system.service.TInsuranceOrderService;
import com.ruoyi.system.wxPay.enums.RefundEnum;
import com.ruoyi.system.wxPay.enums.TradeStateEnum;
import com.ruoyi.system.wxPay.model.WeixinPayProperties;
import com.ruoyi.system.wxPay.utils.WxTimeUtils;
import com.ruoyi.system.wxPay.utils.WxV3Pay;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
 
/**
 * 微信相关接口
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/wx/")
@Api(tags = {"微信支付相关接口"})
public class WxPayController {
    @Autowired
    private WxV3Pay wxV3Pay;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private WeixinPayProperties weixinPayProperties;
 
    @Autowired
    private TCourseOrderService courseOrderService;
 
    @Autowired
    private TInsuranceOrderService tInsuranceOrderService;
 
 
 
    /**
     * 支付回调
     */
    @PostMapping("pay/notify")
    @ApiOperation("订单回调")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            System.err.println("微信回调");
            BufferedReader reader = request.getReader();
            StringBuilder requestBody = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                requestBody.append(line);
            }
            System.err.println("全部请求体" + requestBody);
            JSONObject jsonObject = JSONObject.parseObject(requestBody.toString());
            JSONObject resource = jsonObject.getJSONObject("resource");
 
            AesUtil aesUtil = new AesUtil(weixinPayProperties.getV3().getApiKey().getBytes(StandardCharsets.UTF_8));
            String decryptedData = aesUtil.decryptToString(resource.getString("associated_data").getBytes(StandardCharsets.UTF_8), resource.getString("nonce").getBytes(StandardCharsets.UTF_8),
                    resource.getString("ciphertext"));
            System.err.println("微信解密的字符串信息" + decryptedData);
            JSONObject jsonInfo = JSONObject.parse(decryptedData);
            String out_trade_no = jsonInfo.getString("out_trade_no");
            String transaction_id = jsonInfo.getString("transaction_id");
            String trade_state = jsonInfo.getString("trade_state");
            String success_time = jsonInfo.getString("success_time");
            // 查询订单
 
            TCourseOrder order = courseOrderService.getOne(Wrappers.lambdaQuery(TCourseOrder.class)
                    .eq(TCourseOrder::getOrderNumber, out_trade_no).last("LIMIT 1"));
            if (trade_state.equals(TradeStateEnum.SUCCESS.name())) {
                log.info("回调成功");
                // 订单号查询订单
                order.setPayStatus(2);
                order.setPayTime(WxTimeUtils.dateToLocalDateTime(WxTimeUtils.toRfc3339Date(success_time)));
                order.setTransactionId(transaction_id);
                courseOrderService.updateById(order);
 
                PrintWriter out = response.getWriter();
                out.print("SUCCESS");
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            log.error("支付回调异常:{}", e, e);
            wxV3Pay.ack(false, e.getMessage());
        }
    }
    /**
     * 支付回调
     */
    @PostMapping("pay/notifyInsurance")
    @ApiOperation("保险订单回调")
    public void notifyInsurance(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            System.err.println("微信回调");
            BufferedReader reader = request.getReader();
            StringBuilder requestBody = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                requestBody.append(line);
            }
            System.err.println("全部请求体" + requestBody);
            JSONObject jsonObject = JSONObject.parseObject(requestBody.toString());
            JSONObject resource = jsonObject.getJSONObject("resource");
 
            AesUtil aesUtil = new AesUtil(weixinPayProperties.getV3().getApiKey().getBytes(StandardCharsets.UTF_8));
            String decryptedData = aesUtil.decryptToString(resource.getString("associated_data").getBytes(StandardCharsets.UTF_8), resource.getString("nonce").getBytes(StandardCharsets.UTF_8),
                    resource.getString("ciphertext"));
            System.err.println("微信解密的字符串信息" + decryptedData);
            JSONObject jsonInfo = JSONObject.parse(decryptedData);
            String out_trade_no = jsonInfo.getString("out_trade_no");
            String transaction_id = jsonInfo.getString("transaction_id");
            String trade_state = jsonInfo.getString("trade_state");
            String success_time = jsonInfo.getString("success_time");
            // 查询订单
 
            TInsuranceOrder order = tInsuranceOrderService.getOne(Wrappers.lambdaQuery(TInsuranceOrder.class)
                    .eq(TInsuranceOrder::getPayNo, out_trade_no).last("LIMIT 1"));
            if (trade_state.equals(TradeStateEnum.SUCCESS.name())) {
                log.info("回调成功");
                // 订单号查询订单
                order.setStatus(2);
                order.setPayTime(LocalDateTime.now());
                order.setTransactionId(transaction_id);
                tInsuranceOrderService.updateById(order);
 
                PrintWriter out = response.getWriter();
                out.print("SUCCESS");
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            log.error("支付回调异常:{}", e, e);
            wxV3Pay.ack(false, e.getMessage());
        }
    }
 
 
    /**
     * 退款回调
     */
//    @PostMapping("refund/notify")
//    @ApiOperation("退款回调")
//    public void refundNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        try {
////            System.err.println("微信回调");
////            BufferedReader reader = request.getReader();
////            StringBuilder requestBody = new StringBuilder();
////            String line;
////            while ((line = reader.readLine()) != null) {
////                requestBody.append(line);
////            }
////            System.err.println("全部请求体" + requestBody);
////            JSONObject jsonObject = JSONObject.parseObject(requestBody.toString());
////            JSONObject resource = jsonObject.getJSONObject("resource");
////            AesUtil aesUtil = new AesUtil(weixinPayProperties.getV3().getApiKey().getBytes(StandardCharsets.UTF_8));
////            String decryptedData = aesUtil.decryptToString(resource.getString("associated_data").getBytes(StandardCharsets.UTF_8), resource.getString("nonce").getBytes(StandardCharsets.UTF_8), resource.getString("ciphertext"))
//        }
//    }
    @PostMapping("refund/notify")
    public void refundNotify(HttpServletRequest request) throws IOException {
        try {
 
            System.err.println("微信退款回调");
            BufferedReader reader = request.getReader();
            StringBuilder requestBody = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                requestBody.append(line);
            }
            System.err.println("全部请求体" + requestBody);
            JSONObject jsonObject = JSONObject.parseObject(requestBody.toString());
            JSONObject resource = jsonObject.getJSONObject("resource");
 
            AesUtil aesUtil = new AesUtil(weixinPayProperties.getV3().getApiKey().getBytes(StandardCharsets.UTF_8));
            String decryptedData = aesUtil.decryptToString(resource.getString("associated_data").getBytes(StandardCharsets.UTF_8), resource.getString("nonce").getBytes(StandardCharsets.UTF_8),
                    resource.getString("ciphertext"));
            System.err.println("微信解密的字符串信息" + decryptedData);
 
            JSONObject params = JSONObject.parseObject(decryptedData);
 
            // 商户订单号
            String out_trade_no = params.get("out_trade_no").toString();
            // 商户退款单号
            String out_refund_no = params.get("out_refund_no").toString();
            // 退款状态
            String tradeState = params.get("refund_status").toString();
            // 微信支付订单号
            String transaction_id = params.get("transaction_id").toString();
            if (tradeState.equals(RefundEnum.SUCCESS.name())) {
                TCourseOrder order = courseOrderService.getOne(Wrappers.lambdaQuery(TCourseOrder.class)
                        .eq(TCourseOrder::getRefundNumber, out_refund_no).last("LIMIT 1"));
                order.setPayStatus(3);
                order.setRefundStatus(2);
                order.setRefundTransactionId(transaction_id);
                courseOrderService.updateById(order);
 
                wxV3Pay.ack();
            }
        }catch (Exception e){
            wxV3Pay.ack(false, "不是成功的退款状态");
        }
    }
 
 
    @PostMapping("refund/notifyInsurance")
    public void notifyInsurance(HttpServletRequest request) throws IOException {
        try {
 
            System.err.println("微信退款回调");
            BufferedReader reader = request.getReader();
            StringBuilder requestBody = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                requestBody.append(line);
            }
            System.err.println("全部请求体" + requestBody);
            JSONObject jsonObject = JSONObject.parseObject(requestBody.toString());
            JSONObject resource = jsonObject.getJSONObject("resource");
 
            AesUtil aesUtil = new AesUtil(weixinPayProperties.getV3().getApiKey().getBytes(StandardCharsets.UTF_8));
            String decryptedData = aesUtil.decryptToString(resource.getString("associated_data").getBytes(StandardCharsets.UTF_8), resource.getString("nonce").getBytes(StandardCharsets.UTF_8),
                    resource.getString("ciphertext"));
            System.err.println("微信解密的字符串信息" + decryptedData);
 
            JSONObject params = JSONObject.parseObject(decryptedData);
 
            // 商户订单号
            String out_trade_no = params.get("out_trade_no").toString();
            // 商户退款单号
            String out_refund_no = params.get("out_refund_no").toString();
            // 退款状态
            String tradeState = params.get("refund_status").toString();
            // 微信支付订单号
            String transaction_id = params.get("transaction_id").toString();
            if (tradeState.equals(RefundEnum.SUCCESS.name())) {
                TInsuranceOrder order = tInsuranceOrderService.getOne(Wrappers.lambdaQuery(TInsuranceOrder.class)
                        .eq(TInsuranceOrder::getRefundNumber, out_refund_no).last("LIMIT 1"));
                order.setStatus(1);
                order.setRefundStatus(2);
                order.setRefundTransactionId(transaction_id);
                tInsuranceOrderService.updateById(order);
 
                wxV3Pay.ack();
            }
        }catch (Exception e){
            wxV3Pay.ack(false, "不是成功的退款状态");
        }
    }
}