| | |
| | | /** |
| | | * 发起微信Native支付 |
| | | */ |
| | | @Anonymous() |
| | | @PostMapping("/wechat/native") |
| | | public AjaxResult createWechatNativePayment(@Validated @RequestBody PaymentRequest request) { |
| | | try { |
| | |
| | | /** |
| | | * 发起支付宝当面付 |
| | | */ |
| | | @Anonymous() |
| | | @PostMapping("/alipay/precreate") |
| | | public AjaxResult createAlipayPrecreate(@Validated @RequestBody PaymentRequest request) { |
| | | try { |
| | |
| | | /** |
| | | * 发起支付宝当面付(第三方接口) |
| | | */ |
| | | @Anonymous() |
| | | @PostMapping("/alipay/thirdparty/precreate") |
| | | public AjaxResult createAlipayThirdPartyPrecreate(@Validated @RequestBody PaymentRequest request) { |
| | | try { |
| | |
| | | /** |
| | | * 查询订单 |
| | | */ |
| | | @Anonymous() |
| | | @GetMapping("/orders/{orderId}") |
| | | public AjaxResult getOrder(@PathVariable Long orderId) { |
| | | try { |
| | |
| | | /** |
| | | * 查询最新交易 |
| | | */ |
| | | @Anonymous() |
| | | @GetMapping("/orders/{orderId}/transactions/latest") |
| | | public AjaxResult getLatestTransaction(@PathVariable Long orderId) { |
| | | try { |
| | |
| | | /** |
| | | * 查询支付宝第三方交易状态 |
| | | */ |
| | | @Anonymous() |
| | | @GetMapping("/alipay/thirdparty/query/{orderId}") |
| | | public AjaxResult queryAlipayThirdPartyTradeStatus(@PathVariable Long orderId) { |
| | | try { |
| | |
| | | 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()); |
| | | } |
| | | } |
| | | } |