PaymentController.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.xuekairui.payment.controller;
  2. import com.xuekairui.common.Result;
  3. import com.xuekairui.payment.dto.*;
  4. import com.xuekairui.payment.service.PaymentOrderService;
  5. import com.xuekairui.payment.service.PaymentPlanService;
  6. import jakarta.servlet.http.HttpServletRequest;
  7. import jakarta.validation.Valid;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.web.bind.annotation.*;
  11. import java.util.List;
  12. /**
  13. * 支付控制器(用户端)
  14. */
  15. @Slf4j
  16. @RestController
  17. @RequestMapping("/api/payment")
  18. @RequiredArgsConstructor
  19. public class PaymentController {
  20. private final PaymentPlanService paymentPlanService;
  21. private final PaymentOrderService paymentOrderService;
  22. /**
  23. * 获取可用支付方案列表(公开)
  24. */
  25. @GetMapping("/plans")
  26. public Result<List<PaymentPlanResponse>> getPlans() {
  27. return Result.success(paymentPlanService.getActivePlans());
  28. }
  29. /**
  30. * 创建支付订单
  31. */
  32. @PostMapping("/order/create")
  33. public Result<OrderResponse> createOrder(
  34. @Valid @RequestBody CreateOrderRequest request,
  35. HttpServletRequest httpReq) {
  36. Long userId = (Long) httpReq.getAttribute("userId");
  37. return Result.success(paymentOrderService.createOrder(userId, request));
  38. }
  39. /**
  40. * 查询单个订单
  41. */
  42. @GetMapping("/order/{orderNo}")
  43. public Result<OrderResponse> queryOrder(@PathVariable String orderNo) {
  44. return Result.success(paymentOrderService.queryOrder(orderNo));
  45. }
  46. /**
  47. * 切换支付渠道并重新生成二维码
  48. * 用户可在订单详情页切换为其他支付方式(如微信->支付宝),后端重新调用预下单接口生成新二维码
  49. */
  50. @PostMapping("/order/{orderNo}/switch-channel")
  51. public Result<OrderResponse> switchChannel(
  52. @PathVariable String orderNo,
  53. @Valid @RequestBody SwitchChannelRequest request,
  54. HttpServletRequest httpReq) {
  55. Long userId = (Long) httpReq.getAttribute("userId");
  56. return Result.success(paymentOrderService.switchChannel(userId, orderNo, request));
  57. }
  58. /**
  59. * 查询用户的所有订单
  60. */
  61. @GetMapping("/orders")
  62. public Result<List<OrderResponse>> listMyOrders(HttpServletRequest httpReq) {
  63. Long userId = (Long) httpReq.getAttribute("userId");
  64. return Result.success(paymentOrderService.listUserOrders(userId));
  65. }
  66. /**
  67. * 手动确认支付(开发/测试用)
  68. * mock模式:模拟支付;真实支付模式:手动确认(用于测试回调流程)
  69. */
  70. @PostMapping("/order/{orderNo}/pay")
  71. public Result<PaymentResultResponse> confirmPay(@PathVariable String orderNo) {
  72. return Result.success(paymentOrderService.confirmPay(orderNo));
  73. }
  74. }