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