wlzboy
2025-12-02 d294abb765e4ed349907c92ce313689c6299ba7d
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
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支付
     */
    @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());
        }
    }
 
    /**
     * 发起支付宝当面付
     */
    @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());
        }
    }
 
    /**
     * 发起支付宝当面付(第三方接口)
     */
    @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());
        }
    }
 
    /**
     * 查询订单
     */
    @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());
        }
    }
 
    /**
     * 查询最新交易
     */
    @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());
        }
    }
 
    /**
     * 查询支付宝第三方交易状态
     */
    @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());
        }
    }
}