创建充值单后立即调用 /api/v1/order/create-transaction。

成功后自动跳转 public/usdtCheckout?tradeId=...。
收银台按交易号展示网络、金额、二维码、地址及支付状态。
失败时显示处理阶段,支持使用原单重试,避免重复创建。
This commit is contained in:
lu7213 committed 2026-10-29 03:46:59 -04:00
1 parent 76f9e62253
commit b6b9120b85
8 files changed
+203 -6

No files matched your search

@@ -0,0 +1,41 @@
package com.ruoyi.system.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.UsdtOrdersPlus;
import com.ruoyi.system.service.IUsdtOrdersService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 收银台只读入口:随机32位交易号为访问凭据,只返回付款必需字段。
* 不接受商户订单号查询,不公开用户、应用、回调地址或任何签名凭证。
*/
@RestController
public class PaymentCheckoutController {
private final IUsdtOrdersService orders;
public PaymentCheckoutController(IUsdtOrdersService orders) { this.orders = orders; }
@GetMapping("/api/v1/order/checkout/{tradeId}")
public AjaxResult detail(@PathVariable String tradeId) {
if (!tradeId.matches("[0-9a-f]{32}")) { throw new CustomException("支付信息不存在"); }
UsdtOrdersPlus order = orders.getOne(new LambdaQueryWrapper<UsdtOrdersPlus>()
.eq(UsdtOrdersPlus::getTradeId, tradeId).isNull(UsdtOrdersPlus::getDeletedAt)
.eq(UsdtOrdersPlus::getOrderType, 0).last("limit 1"));
if (order == null || order.getCreatedAt() == null) { throw new CustomException("支付信息不存在"); }
long expires = order.getCreatedAt().getTime() + 600000L;
Map<String, Object> result = new LinkedHashMap<>();
result.put("trade_id", order.getTradeId());
result.put("network", order.getPaymentNetwork());
result.put("payment_address", order.getToken());
result.put("payment_amount", order.getActualAmount());
result.put("expiration_time", expires);
result.put("status", Long.valueOf(1).equals(order.getStatus()) && expires <= System.currentTimeMillis() ? 3 : order.getStatus());
return AjaxResult.success(result);
}
}
@@ -42,6 +42,12 @@ public class RentalTopupController {
return AjaxResult.success(topups.detail(SecurityUtils.getUserId(), no));
}
@PostMapping("/rental/topup/{no}/retry")
@PreAuthorize("@ss.hasPermi('rent:portal:view')")
public AjaxResult retry(@PathVariable String no) {
return AjaxResult.success(topups.retry(SecurityUtils.getUserId(), no));
}
@PostMapping(value = "/api/rental/payment/notify/{channel}", consumes = "application/json", produces = "text/plain")
public ResponseEntity<String> notify(@PathVariable String channel, HttpServletRequest request) {
try {
@@ -81,7 +81,8 @@ public class RentalTopups {
"user_id", user, "account_id", account.get("id"), "network", network, "client_request_no", clientNo,
"requested_amount", value, "payment_channel", network, "payment_api_key", channel.getApiKey(),
"payment_public_key", pem, "payment_base_url", channel.getBaseUrl().replaceAll("/+$", ""),
"notify_url", channel.getNotifyUrl(), "status", "CREATING", "next_query_at", now()));
// DATETIME(3)可能向上舍入;首次任务设为过去,确保提交后立即可领取。
"notify_url", channel.getNotifyUrl(), "status", "CREATING", "next_query_at", now().minusSeconds(1)));
return store.get("topup_order", id, false);
});
LOG.info("充值本地单已就绪 diagnosticId={} topupNo={} network={}", diagnosticId, text(saved, "topup_no"), network);
@@ -102,6 +103,14 @@ public class RentalTopups {
return view(row);
}
/** 手动重试只核对本人原单,不新建订单,不绕过后台退避或正在执行的租约。 */
public Map<String, Object> retry(long user, String no) {
Map<String, Object> row = store.one("SELECT * FROM rent_topup_order WHERE user_id=? AND topup_no=?", user, no);
require(row != null, "充值订单不存在");
reconcile(number(row, "id"));
return detail(user, no);
}
public List<Map<String, Object>> list(long user, String network, int page) {
network(network);
require(page >= 1 && page <= 10000, "分页参数无效");
@@ -121,6 +130,9 @@ public class RentalTopups {
for (String key : Arrays.asList("expires_at", "created_at", "credited_at")) {
result.put(key, row.get(key) == null ? null : time(row.get(key)).toEpochMilli());
}
// 只返回本服务生成的阶段提示,绝不把异常原文或支付响应暴露给浏览器。
String hint = text(row, "last_error");
result.put("payment_hint", hint.startsWith("充值处理未完成:") ? hint : null);
return result;
}
@@ -159,7 +171,11 @@ public class RentalTopups {
// 一个HTTP请求最多30秒,租约180秒覆盖创建/查询及有界链上核验。
String lease = UUID.randomUUID().toString().replace("-", "");
if (store.execute("UPDATE rent_topup_order SET lease_token=?,next_query_at=?,query_attempts=query_attempts+1 WHERE id=? AND next_query_at<=? AND status IN ('CREATING','CREATE_UNKNOWN','WAITING_PAYMENT','VERIFYING')",
lease, after(180), id, now()) != 1) return;
lease, after(180), id, now()) != 1) {
LOG.debug("充值核对跳过:未到重试时间、任务已领取或订单已结束 orderId={}", id);
return;
}
LOG.info("充值核对已领取 orderId={}", id);
try {
Map<String, Object> order = store.get("topup_order", id, false);
if (text(order, "payment_trade_id").isEmpty()) {
@@ -221,8 +237,12 @@ public class RentalTopups {
// 不输出支付响应、配置、签名或异常原文;保留原单号用于后台补偿。
Map<String, Object> row = store.get("topup_order", id, false);
if (!Arrays.asList("CREDITED", "MANUAL_REVIEW", "EXPIRED").contains(text(row, "status"))) {
updateOwned(id, lease, map("status", text(row, "payment_trade_id").isEmpty() ? "CREATE_UNKNOWN" : "VERIFYING",
"next_query_at", after(60), "last_error", "支付或链上核验暂未完成,后台将使用原单号继续核对"));
// 创建已成功但查单暂时失败,仍允许用户进入已生成的收银台,不误标为已付款核验中。
String retryStatus = text(row, "payment_trade_id").isEmpty() ? "CREATE_UNKNOWN"
: "PAYMENT_QUERY".equals(stage) ? text(row, "status") : "VERIFYING";
updateOwned(id, lease, map("status", retryStatus,
"next_query_at", after(60), "last_error", "充值处理未完成:" + stage
+ ",请联系管理员检查对应阶段日志;系统将在稍后使用原单重试"));
}
}
}
@@ -56,7 +56,7 @@ public class PaymentOrderOpenApiService {
@Value("${payment.usdt-exchange-rate:1}")
private BigDecimal usdtExchangeRate;
/** 对外创建订单成功后返回的公共收银台入口。 */
@Value("${payment.checkout-url:http://192.168.3.103/public/usdtCheckout}")
@Value("${payment.checkout-url:http://192.168.3.103/public/usdtPay}")
private String checkoutUrl;
@Autowired private TronServiceProvider tronServiceProvider;
@@ -235,9 +235,16 @@ public class PaymentOrderOpenApiService {
}
private PaymentOrderCreateResult result(UsdtOrdersPlus order) {
// 兼容已配置的旧收银台路径;支付订单应直接进入付款页,保留域名及代理前缀。
org.springframework.web.util.UriComponentsBuilder paymentPage =
org.springframework.web.util.UriComponentsBuilder.fromUriString(checkoutUrl);
String path = URI.create(checkoutUrl).getPath();
if (path != null && path.endsWith("/public/usdtCheckout")) {
paymentPage.replacePath(path.substring(0, path.length() - "usdtCheckout".length()) + "usdtPay");
}
return new PaymentOrderCreateResult(order.getOrderId(), order.getTradeId(), order.getToken(),
order.getActualAmount(), new Date(order.getCreatedAt().getTime() + ORDER_TTL_MILLIS),
checkoutUrl+"?tradeId="+order.getTradeId());
paymentPage.replaceQueryParam("tradeId", order.getTradeId()).build().toUriString());
}
private Map<String, String> parseParameters(String plainData) {