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()); } } }