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
package com.ruoyi.payment.interfaces.controller;
 
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.payment.application.service.PaymentService;
import com.ruoyi.payment.common.AjaxResult;
import com.ruoyi.payment.domain.model.PaymentOrder;
import com.ruoyi.payment.domain.model.PaymentTransaction;
import com.ruoyi.payment.interfaces.dto.PaymentRequest;
import com.ruoyi.payment.interfaces.dto.PaymentResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
/**
 * 支付接口控制器
 * 
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/api/pay/payment")
public class PaymentController {
 
    @Autowired
    private PaymentService paymentService;
 
    @Anonymous()
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
    /**
     * 发起微信Native支付
     */
    @Anonymous()
    @PostMapping("/wechat/native")
    public AjaxResult createWechatNativePayment(@Validated @RequestBody PaymentRequest request) {
        try {
            PaymentResponse response = paymentService.createWechatNativePayment(request);
            return AjaxResult.success(response);
        } catch (Exception e) {
            log.error("发起微信Native支付失败", e);
            return AjaxResult.error("支付创建失败: " + e.getMessage());
        }
    }
 
    /**
     * 发起支付宝当面付
     */
    @Anonymous()
    @PostMapping("/alipay/precreate")
    public AjaxResult createAlipayPrecreate(@Validated @RequestBody PaymentRequest request) {
        try {
            PaymentResponse response = paymentService.createAlipayPrecreate(request);
            return AjaxResult.success(response);
        } catch (Exception e) {
            log.error("发起支付宝当面付失败", e);
            return AjaxResult.error("支付创建失败: " + e.getMessage());
        }
    }
 
    /**
     * 发起支付宝当面付(第三方接口)
     */
    @Anonymous()
    @PostMapping("/alipay/thirdparty/precreate")
    public AjaxResult createAlipayThirdPartyPrecreate(@Validated @RequestBody PaymentRequest request) {
        try {
            PaymentResponse response = paymentService.createAlipayThirdPartyPrecreate(request);
            return AjaxResult.success(response);
        } catch (Exception e) {
            log.error("发起支付宝当面付(第三方接口)失败", e);
            return AjaxResult.error("支付创建失败: " + e.getMessage());
        }
    }
 
    /**
     * 查询订单
     */
    @Anonymous()
    @GetMapping("/orders/{orderId}")
    public AjaxResult getOrder(@PathVariable Long orderId) {
        try {
            PaymentOrder order = paymentService.getOrder(orderId);
            if (order == null) {
                return AjaxResult.error("订单不存在");
            }
            return AjaxResult.success(order);
        } catch (Exception e) {
            log.error("查询订单失败", e);
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
 
    /**
     * 查询最新交易
     */
    @Anonymous()
    @GetMapping("/orders/{orderId}/transactions/latest")
    public AjaxResult getLatestTransaction(@PathVariable Long orderId) {
        try {
            PaymentTransaction transaction = paymentService.getLatestTransaction(orderId);
            if (transaction == null) {
                return AjaxResult.error("交易不存在");
            }
            return AjaxResult.success(transaction);
        } catch (Exception e) {
            log.error("查询交易失败", e);
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
 
    /**
     * 查询支付宝第三方交易状态
     */
    @Anonymous()
    @GetMapping("/alipay/thirdparty/query/{orderId}")
    public AjaxResult queryAlipayThirdPartyTradeStatus(@PathVariable Long orderId) {
        try {
            String tradeStatus = paymentService.queryAlipayThirdPartyTradeStatus(orderId);
            return AjaxResult.success(tradeStatus);
        } catch (Exception e) {
            log.error("查询支付宝第三方交易状态失败", e);
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
 
    /**
     * 轮询查询支付状态(用于回调失败的补偿机制)
     * <p>
     * 这个接口用于在支付回调未成功时,主动轮询查询支付状态。
     * 支持微信和支付宝(包括第三方支付宝)。
     * <p>
     * 返回结果包含:
     * - orderStatus: 订单状态
     * - transactionStatus: 交易状态
     * - tradeStatus: 第三方交易状态
     * - needPoll: 是否需要继续轮询
     * - message: 提示信息
     * <p>
     * 建议轮询策略:
     * 1. 首次轮询:立即轮询
     * 2. 后续轮询:间隔3-5秒
     * 3. 最多轮询20次,约100秒后停止
     * 4. 如果 needPoll=false,表示支付已完成或确认失败,无需继续轮询
     */
    @Anonymous()
    @GetMapping("/poll/{orderId}")
    public AjaxResult pollPaymentStatus(@PathVariable Long orderId) {
        try {
            java.util.Map<String, Object> result = paymentService.pollPaymentStatus(orderId);
            return AjaxResult.success(result);
        } catch (Exception e) {
            log.error("轮询查询支付状态失败", e);
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
 
    /**
     * 根据交易单号查询微信支付状态
     * <p>
     * 该接口用于通过商户订单号(交易单号)直接查询微信支付状态。
     * <p>
     * 返回结果包含:
     * - success: 查询是否成功
     * - isPaid: 是否已支付
     * - tradeState: 交易状态(SUCCESS-支付成功,NOTPAY-未支付,CLOSED-已关闭等)
     * - transactionId: 微信支付订单号
     * - totalFee: 订单金额(分)
     * - timeEnd: 支付完成时间
     * - message: 提示信息
     * 
     * @param outTradeNo 商户订单号(交易单号)
     */
    @Anonymous()
    @GetMapping("/wechat/query/{outTradeNo}")
    public AjaxResult queryWechatTrade(@PathVariable String outTradeNo) {
        try {
            java.util.Map<String, Object> result = paymentService.queryWechatTradeByOutTradeNo(outTradeNo);
            return AjaxResult.success(result);
        } catch (Exception e) {
            log.error("查询微信交易状态失败,交易单号: {}", outTradeNo, e);
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
 
    /**
     * 根据交易单号查询支付宝支付状态
     * <p>
     * 该接口用于通过商户订单号(交易单号)直接查询支付宝支付状态。
     * <p>
     * 返回结果包含:
     * - success: 查询是否成功
     * - isPaid: 是否已支付
     * - tradeStatus: 交易状态(TRADE_SUCCESS-支付成功,WAIT_BUYER_PAY-等待付款,TRADE_CLOSED-已关闭等)
     * - tradeNo: 支付宝交易号
     * - totalAmount: 订单金额(元)
     * - sendPayDate: 交易支付时间
     * - buyerLogonId: 买家支付宝账号
     * - message: 提示信息
     * 
     * @param outTradeNo 商户订单号(交易单号)
     */
    @Anonymous()
    @GetMapping("/alipay/query/{outTradeNo}")
    public AjaxResult queryAlipayTrade(@PathVariable String outTradeNo) {
        try {
            java.util.Map<String, Object> result = paymentService.queryAlipayTradeByOutTradeNo(outTradeNo);
            return AjaxResult.success(result);
        } catch (Exception e) {
            log.error("查询支付宝交易状态失败,交易单号: {}", outTradeNo, e);
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
}