| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package com.xuekairui.payment.controller;
- import com.xuekairui.common.Result;
- import com.xuekairui.payment.dto.*;
- import com.xuekairui.payment.service.PaymentOrderService;
- import com.xuekairui.payment.service.PaymentPlanService;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.validation.Valid;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.*;
- import java.util.List;
- /**
- * 支付控制器(用户端)
- */
- @Slf4j
- @RestController
- @RequestMapping("/api/payment")
- @RequiredArgsConstructor
- public class PaymentController {
- private final PaymentPlanService paymentPlanService;
- private final PaymentOrderService paymentOrderService;
- /**
- * 获取可用支付方案列表(公开)
- */
- @GetMapping("/plans")
- public Result<List<PaymentPlanResponse>> getPlans() {
- return Result.success(paymentPlanService.getActivePlans());
- }
- /**
- * 创建支付订单
- */
- @PostMapping("/order/create")
- public Result<OrderResponse> createOrder(
- @Valid @RequestBody CreateOrderRequest request,
- HttpServletRequest httpReq) {
- Long userId = (Long) httpReq.getAttribute("userId");
- return Result.success(paymentOrderService.createOrder(userId, request));
- }
- /**
- * 查询单个订单
- */
- @GetMapping("/order/{orderNo}")
- public Result<OrderResponse> queryOrder(@PathVariable String orderNo) {
- return Result.success(paymentOrderService.queryOrder(orderNo));
- }
- /**
- * 切换支付渠道并重新生成二维码
- * 用户可在订单详情页切换为其他支付方式(如微信->支付宝),后端重新调用预下单接口生成新二维码
- */
- @PostMapping("/order/{orderNo}/switch-channel")
- public Result<OrderResponse> switchChannel(
- @PathVariable String orderNo,
- @Valid @RequestBody SwitchChannelRequest request,
- HttpServletRequest httpReq) {
- Long userId = (Long) httpReq.getAttribute("userId");
- return Result.success(paymentOrderService.switchChannel(userId, orderNo, request));
- }
- /**
- * 查询用户的所有订单
- */
- @GetMapping("/orders")
- public Result<List<OrderResponse>> listMyOrders(HttpServletRequest httpReq) {
- Long userId = (Long) httpReq.getAttribute("userId");
- return Result.success(paymentOrderService.listUserOrders(userId));
- }
- /**
- * 手动确认支付(开发/测试用)
- * mock模式:模拟支付;真实支付模式:手动确认(用于测试回调流程)
- */
- @PostMapping("/order/{orderNo}/pay")
- public Result<PaymentResultResponse> confirmPay(@PathVariable String orderNo) {
- return Result.success(paymentOrderService.confirmPay(orderNo));
- }
- }
|