创建充值单后立即调用 /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

@@ -42,6 +42,7 @@ public class RentalTopupTest {
private int paymentStatus = 1;
private boolean failCreate;
private boolean failChain;
private boolean failQuery;
private Set<String> created = new HashSet<>();
private int createCalls;
private int chainCalls;
@@ -83,6 +84,7 @@ public class RentalTopupTest {
"payment_amount", money("10"), "expiration_time", System.currentTimeMillis() + 600000,
"url", "https://pay.example.com/checkout?trade=" + no);
}
if (failQuery) throw new IllegalStateException("controlled query timeout");
return map("order_id", no, "trade_id", "P" + no, "status", paymentStatus,
"transaction_id", HASH, "network", paymentNetwork,
"payment_amount", money(paymentAmount), "payment_address", ADDRESS);
@@ -131,6 +133,63 @@ public class RentalTopupTest {
assertEquals(1, created.size());
assertEquals("WAITING_PAYMENT", service.detail(10, text(first, "topup_no")).get("status"));
}
@Test public void manualRetryRecoversOriginalOrderAndChecksOwnershipAndLease() throws Exception {
failCreate = true;
Map<String, Object> first = create("retry-original");
String no = text(first, "topup_no");
assertTrue(text(first, "payment_hint").contains("PAYMENT_CREATE"));
assertThrows(IllegalArgumentException.class, () -> service.retry(20, no));
failCreate = false;
service.retry(10, no);
assertEquals(1, createCalls); // 退避期不会重复调用支付服务。
due(id(first));
Map<String, Object> recovered = service.retry(10, no);
assertEquals("WAITING_PAYMENT", recovered.get("status"));
assertEquals("https://pay.example.com/checkout?trade=" + no, recovered.get("checkout_url"));
assertEquals(1, created.size());
assertNull(recovered.get("payment_hint"));
}
@Test public void successfulCreateRemainsPayableWhenQueryTimesOut() throws Exception {
failQuery = true;
Map<String, Object> first = create("query-timeout");
assertEquals(1, createCalls);
assertEquals("WAITING_PAYMENT", first.get("status"));
assertNotNull(first.get("checkout_url"));
assertTrue(text(first, "payment_hint").contains("PAYMENT_QUERY"));
}
@Test public void paymentAdapterPostsEncryptedCreateRequestToPaymentApi() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048);
KeyPair pair = generator.generateKeyPair();
String pem = "-----BEGIN PUBLIC KEY-----\n" + Base64.getEncoder().encodeToString(pair.getPublic().getEncoded()) + "\n-----END PUBLIC KEY-----";
com.sun.net.httpserver.HttpServer server = com.sun.net.httpserver.HttpServer.create(new java.net.InetSocketAddress("127.0.0.1", 0), 0);
java.util.concurrent.atomic.AtomicReference<String> plain = new java.util.concurrent.atomic.AtomicReference<>();
server.createContext("/api/v1/order/create-transaction", exchange -> {
try {
Map<String, Object> envelope = json.readValue(exchange.getRequestBody(), Map.class);
byte[] aes = com.ruoyi.system.security.RsaCipherUtil.rsaOaepDecrypt(text(envelope, "encryptedKey"), pair.getPrivate());
plain.set(com.ruoyi.system.security.AesGcmUtil.decrypt(text(envelope, "data"), aes, Base64.getDecoder().decode(text(envelope, "iv"))));
byte[] response = json.writeValueAsBytes(map("code", 200, "data", map("order_id", "adapter-test", "url", "https://pay.example.com/public/usdtCheckout?tradeId=test", "payment_amount", money("10.12"))));
exchange.sendResponseHeaders(200, response.length); exchange.getResponseBody().write(response);
} catch (Exception failure) { exchange.sendResponseHeaders(500, -1); }
finally { exchange.close(); }
});
server.start();
try {
RentalPaymentClient real = new RentalPaymentClient(json, config, new DefaultResourceLoader());
Map<String, Object> response = real.call(map("network", "NILE", "topup_no", "adapter-test", "requested_amount", money("10.12"),
"payment_api_key", "payment-key", "payment_public_key", pem,
"payment_base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "notify_url", config.getNile().getNotifyUrl()), "create-transaction");
assertTrue(plain.get().contains("network=NILE&"));
assertTrue(plain.get().contains("amount_currency=USDT&"));
assertTrue(plain.get().contains("amount=10.12&"));
assertTrue(plain.get().contains("order_id=adapter-test&"));
assertTrue(text(response, "url").contains("/public/usdtCheckout?tradeId="));
assertEquals(money("10.12"), amount(response, "payment_amount"));
} finally { server.stop(0); }
}
@Test public void networkAndAmountMismatchNeverCredit() throws Exception {
paymentStatus = 2; paymentNetwork = "MAINNET";
Map<String, Object> wrongNetwork = create("network");
@@ -0,0 +1,45 @@
package com.ruoyi.system.service;
import com.ruoyi.system.controller.PaymentCheckoutController;
import com.ruoyi.system.domain.UsdtOrdersPlus;
import com.ruoyi.common.exception.CustomException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
/** 公共支付详情仅暴露白名单,不返回资金账户、商户身份或回调凭证。 */
public class PaymentCheckoutControllerTest {
private PaymentCheckoutController controller(UsdtOrdersPlus order) {
IUsdtOrdersService service = (IUsdtOrdersService) java.lang.reflect.Proxy.newProxyInstance(
IUsdtOrdersService.class.getClassLoader(), new Class<?>[]{IUsdtOrdersService.class},
(proxy, method, arguments) -> {
if ("getOne".equals(method.getName())) return order;
throw new UnsupportedOperationException();
});
return new PaymentCheckoutController(service);
}
@Test public void checkoutUsesStoredNetworkExpiryAndResponseWhitelist() {
UsdtOrdersPlus order = new UsdtOrdersPlus();
order.setTradeId("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
order.setPaymentNetwork("NILE"); order.setToken("TAddress");
order.setStatus(1L); order.setCreatedAt(new Date(System.currentTimeMillis() - 700000));
order.setActualAmount(new BigDecimal("10.00"));
order.setNotifyUrl("https://private.example.com"); order.setUserId(123L);
Map<String, Object> data = (Map<String, Object>) controller(order).detail(order.getTradeId()).get("data");
assertEquals("NILE", data.get("network"));
assertEquals(3L, ((Number) data.get("status")).longValue());
assertEquals(6, data.size());
assertFalse(data.containsKey("notify_url")); assertFalse(data.containsKey("user_id"));
order.setStatus(2L);
data = (Map<String, Object>) controller(order).detail(order.getTradeId()).get("data");
assertEquals(2L, ((Number) data.get("status")).longValue());
}
@Test public void rejectsMerchantNumbersAndMissingTrades() {
assertThrows(CustomException.class, () -> controller(null).detail("merchant-order-001"));
assertThrows(CustomException.class, () -> controller(null).detail("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
}
}
@@ -10,6 +10,19 @@ import java.util.Date;
/** 支付订单业务金额与 USDT 实付金额换算测试。 */
public class PaymentOrderOpenApiServiceTest {
@Test
public void paymentLinksUsePayPageAndPreserveExistingQuery() throws Exception {
PaymentOrderOpenApiService service = new PaymentOrderOpenApiService();
java.lang.reflect.Field field = PaymentOrderOpenApiService.class.getDeclaredField("checkoutUrl");
field.setAccessible(true);
field.set(service, "https://pay.example.com/prefix/public/usdtCheckout?lang=zh&tradeId=old");
com.ruoyi.system.domain.UsdtOrdersPlus order = new com.ruoyi.system.domain.UsdtOrdersPlus();
order.setTradeId("new-trade"); order.setCreatedAt(new Date());
java.lang.reflect.Method method = PaymentOrderOpenApiService.class.getDeclaredMethod("result", com.ruoyi.system.domain.UsdtOrdersPlus.class);
method.setAccessible(true);
PaymentOrderCreateResult result = (PaymentOrderCreateResult) method.invoke(service, order);
Assert.assertEquals("https://pay.example.com/prefix/public/usdtPay?lang=zh&tradeId=new-trade", result.getUrl());
}
@Test
public void shouldDivideByExchangeRateAndRoundDown() {
BigDecimal result = PaymentOrderOpenApiService.calculateActualAmount(