1.PROXY\_ACCOUNT 租赁账户,首页中的租赁套餐 ,立即租赁按钮不可操作,帮我完成这个功能,点击立即租赁,跳出输入地址,apiKey,apiSecret,时长,对接地址,回调地址等,然后显示预计费用,然后调用租赁api 2. 帮我实现回调地址同步和异步验证的demo的controller

This commit is contained in:
lu7213 committed 2026-10-25 22:14:04 -04:00
1 parent 127d575a1c
commit 8dcb313454
9 files changed
+408 -3

No files matched your search

@@ -407,6 +407,105 @@ public class RentalBusinessTest {
new DataSourceTransactionManager(source));
}
@Test
public void extendedDurationHoldsQuotedFundsAndRetryDoesNotChargeAgain() {
Map<String, Object> request = map("package_id", product, "merchant_order_no", "LONG-RENT",
"receiver_address", ADDRESS, "duration_seconds", 7200, "expected_amount", "20.000000");
Map<String, Object> order = tx.execute(status -> orders.create(app, 10, request));
assertEquals(7200, number(order, "duration_seconds"));
assertEquals(money("20"), amount(order, "sale_amount"));
assertEquals(money("16"), amount(order, "supplier_amount"));
assertEquals(money("80"), amount(store.get("account", payer, false), "available_amount"));
assertEquals(number(order, "id"), number(tx.execute(status -> orders.create(app, 10, request)), "id"));
assertEquals(money("20"), amount(store.get("account", payer, false), "frozen_amount"));
request.put("duration_seconds", 3600);
expectFailure(() -> tx.execute(status -> orders.create(app, 10, request)));
request.put("merchant_order_no", "PRICE-CHANGED");
expectFailure(() -> tx.execute(status -> orders.create(app, 10, request)));
assertEquals(money("80"), amount(store.get("account", payer, false), "available_amount"));
}
@Test
public void callbackDemoPersistsBeforeAckDeduplicatesAndRecoversAsyncInbox() throws Exception {
store.execute("CREATE TABLE rent_callback_demo_inbox(event_no VARCHAR(64) PRIMARY KEY,"
+ "order_no VARCHAR(64),event_type VARCHAR(64),body_hash VARCHAR(64),"
+ "processed INT,created_at TIMESTAMP)");
RentalCallbackDemoController controller = new RentalCallbackDemoController(store, json, tx.getTransactionManager());
for (String fieldName : Arrays.asList("apiKey", "apiSecret")) {
java.lang.reflect.Field field = RentalCallbackDemoController.class.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(controller, fieldName.equals("apiKey") ? "RKdemo" : "test-secret");
}
String body = "{\"event_no\":\"WH-DEMO\",\"order_no\":\"RO-DEMO\",\"event_type\":\"DELIVERED\"}";
assertEquals("ok", controller.async(demoRequest("async", body, "test-secret")).getBody());
assertEquals(0, number(store.one("SELECT * FROM rent_callback_demo_inbox"), "processed"));
// 模拟进程重启后由持久化扫描恢复,无需内存队列。
controller.consume();
assertEquals(1, number(store.one("SELECT * FROM rent_callback_demo_inbox"), "processed"));
assertEquals("ok", controller.async(demoRequest("async", body, "test-secret")).getBody());
assertEquals(1, store.rows("SELECT * FROM rent_callback_demo_inbox").size());
assertEquals(400, controller.sync(demoRequest("sync", body, "wrong-secret")).getStatusCodeValue());
String second = body.replace("WH-DEMO", "WH-SYNC");
assertEquals("ok", controller.sync(demoRequest("sync", second, "test-secret")).getBody());
assertEquals(1, number(store.one("SELECT * FROM rent_callback_demo_inbox WHERE event_no='WH-SYNC'"), "processed"));
assertEquals(503, controller.sync(demoRequest("sync", body.replace("DELIVERED", "CANCELLED"), "test-secret")).getStatusCodeValue());
}
@Test
@SuppressWarnings("unchecked")
public void signedMerchantQuoteAndCreateWorkWithoutBackendLogin() throws Exception {
Map<String, Object> credential = tx.execute(status -> identity.issue("APP", app, 10));
String key = text(credential, "api_key"), secret = text(credential, "api_secret");
RentalApiController controller = new RentalApiController(identity, store, orders, json);
Map<String, Object> data = map("package_id", product, "merchant_order_no", "API-RENT",
"duration_seconds", 7200, "receiver_address", ADDRESS);
Map<String, Object> quote = (Map<String, Object>) controller.execute("quote",
signedRequest("/api/rental/v1/quote", json.writeValueAsString(data), key, secret, "API")).get("data");
assertEquals("20.000000", quote.get("amount"));
assertEquals(money("100"), amount(store.get("account", payer, false), "available_amount"));
data.put("expected_amount", quote.get("amount"));
String body = json.writeValueAsString(data);
Map<String, Object> result = (Map<String, Object>) tx.execute(status -> {
try {
return controller.execute("create", signedRequest("/api/rental/v1/create", body, key, secret, "API"));
} catch (Exception exception) {
throw new IllegalArgumentException(exception);
}
}).get("data");
assertEquals(7200, number(result, "duration_seconds"));
assertEquals(money("20"), amount(result, "amount"));
assertEquals(money("80"), amount(store.get("account", payer, false), "available_amount"));
}
/** Servlet代理保留原始字节,无需启动真实HTTP端口。 */
private javax.servlet.http.HttpServletRequest demoRequest(String operation, String body, String secret) {
return signedRequest("/api/rental/v1/demo/callback/" + operation, body, "RKdemo", secret, "WEBHOOK");
}
private javax.servlet.http.HttpServletRequest signedRequest(String path, String body,
String key, String secret, String scope) {
String nonce = RentalCrypto.random();
long timestamp = System.currentTimeMillis();
Map<String, Object> headers = map("X-Rent-Key", key, "X-Rent-Timestamp", Long.toString(timestamp),
"X-Rent-Nonce", nonce, "X-Rent-Signature",
RentalCrypto.sign(secret, scope, key, timestamp, nonce, "POST", path, body));
java.io.ByteArrayInputStream input = new java.io.ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
javax.servlet.ServletInputStream stream = new javax.servlet.ServletInputStream() {
@Override public int read() { return input.read(); }
@Override public boolean isFinished() { return input.available() == 0; }
@Override public boolean isReady() { return true; }
@Override public void setReadListener(javax.servlet.ReadListener listener) { }
};
return (javax.servlet.http.HttpServletRequest) java.lang.reflect.Proxy.newProxyInstance(
getClass().getClassLoader(), new Class<?>[] {javax.servlet.http.HttpServletRequest.class},
(proxy, method, args) -> {
if ("getHeader".equals(method.getName())) { return headers.get(args[0]); }
if ("getRequestURI".equals(method.getName())) { return path; }
if ("getInputStream".equals(method.getName())) { return stream; }
return null;
});
}
private Map<String, Object> create(String number) {
return tx.execute(
status ->
@@ -0,0 +1,43 @@
package com.ruoyi.system.rental;
import static com.ruoyi.system.rental.RentalValues.*;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import java.util.Map;
import org.junit.Test;
/** 计费与回调签名协议的纯函数回归,不访问外部服务。 */
public class RentalPricingTest {
@Test
public void durationScalesBothPricesWithoutChangingQuantityOrOriginal() {
Map<String, Object> product = map("duration_seconds", 3600, "sale_amount", "3.000000",
"supplier_amount", "2.000000", "resource_quantity", 65000);
Map<String, Object> price = RentalPricing.price(product, map("duration_seconds", 7200));
assertEquals(new BigDecimal("6.000000"), amount(price, "sale_amount"));
assertEquals(new BigDecimal("4.000000"), amount(price, "supplier_amount"));
assertEquals(65000, number(price, "resource_quantity"));
assertEquals(3600, number(product, "duration_seconds"));
for (long duration : new long[] {0, -1, 3601, 3600L * 721}) {
assertThrows(IllegalArgumentException.class,
() -> RentalPricing.price(product, map("duration_seconds", duration)));
}
}
@Test
public void callbackRejectsWrongScopeTamperingPathAndExpiredTime() {
long timestamp = System.currentTimeMillis();
String key = "RKdemo", secret = "test-secret", nonce = "1234567890abcdef";
String path = "/api/rental/v1/demo/callback/sync", body = "{\"event_no\":\"WH1\"}";
String signature = RentalCrypto.sign(secret, "WEBHOOK", key, timestamp, nonce, "POST", path, body);
RentalCallbackDemoController.verify(key, secret, key, timestamp, nonce, path, body, signature, timestamp);
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
key, secret, key, timestamp, nonce, path, body + " ", signature, timestamp));
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
key, secret, key, timestamp, nonce, path + "x", body, signature, timestamp));
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
key, secret, key, timestamp, nonce, path, body, signature, timestamp + 61000));
String apiSignature = RentalCrypto.sign(secret, "API", key, timestamp, nonce, "POST", path, body);
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
key, secret, key, timestamp, nonce, path, body, apiSignature, timestamp));
}
}