142 lines
6.8 KiB
Java
142 lines
6.8 KiB
Java
package com.ruoyi.system.rental;
|
||
|
||
import com.fasterxml.jackson.core.type.TypeReference;
|
||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
import com.ruoyi.common.core.domain.AjaxResult;
|
||
import org.springframework.web.bind.annotation.PathVariable;
|
||
import org.springframework.web.bind.annotation.PostMapping;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.util.Arrays;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
import static com.ruoyi.system.rental.RentalValues.*;
|
||
|
||
/**
|
||
* 商户HTTPS API,仅接受JSON POST;HMAC覆盖实际UTF-8请求体,禁止二次序列化验签。
|
||
*/
|
||
@RestController
|
||
@RequestMapping("/api/rental/v1")
|
||
public class RentalApiController {
|
||
private final RentalIdentity identity;
|
||
private final RentalStore store;
|
||
private final RentalOrders orders;
|
||
private final ObjectMapper json;
|
||
|
||
public RentalApiController(
|
||
RentalIdentity identity, RentalStore store, RentalOrders orders, ObjectMapper json) {
|
||
this.identity = identity;
|
||
this.store = store;
|
||
this.orders = orders;
|
||
this.json = json;
|
||
}
|
||
|
||
@PostMapping(value = "/{operation}", consumes = "application/json")
|
||
public AjaxResult execute(@PathVariable String operation, HttpServletRequest request)
|
||
throws Exception {
|
||
require(
|
||
Arrays.asList("create", "query", "packages", "quote").contains(operation)
|
||
&& request.getQueryString() == null,
|
||
"接口或查询参数不支持");
|
||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||
byte[] buffer = new byte[4096];
|
||
int size;
|
||
// 可重复读取包装器每次getInputStream()都会从头创建新流,必须持有同一个流读取至EOF。
|
||
javax.servlet.ServletInputStream input = request.getInputStream();
|
||
while ((size = input.read(buffer)) != -1) {
|
||
require(output.size() + size <= RentalJson.MAX_HTTP_BODY_BYTES, "请求体超过512KiB");
|
||
output.write(buffer, 0, size);
|
||
}
|
||
String body =
|
||
StandardCharsets.UTF_8
|
||
.newDecoder()
|
||
.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT)
|
||
.decode(java.nio.ByteBuffer.wrap(output.toByteArray()))
|
||
.toString();
|
||
RentalJson.bounded(body, RentalJson.MAX_HTTP_BODY_BYTES);
|
||
Map<String, Object> credential =
|
||
identity.authenticate(
|
||
request.getHeader("X-Rent-Key"),
|
||
"APP",
|
||
"API",
|
||
Long.parseLong(request.getHeader("X-Rent-Timestamp")),
|
||
request.getHeader("X-Rent-Nonce"),
|
||
"POST",
|
||
request.getRequestURI(),
|
||
body,
|
||
request.getHeader("X-Rent-Signature"));
|
||
Map<String, Object> app = store.get("app", number(credential, "owner_id"), false);
|
||
require("ACTIVE".equals(text(app, "status")), "应用已禁用");
|
||
Map<String, Object> data =
|
||
json.readValue(body, new TypeReference<Map<String, Object>>() {
|
||
});
|
||
if ("packages".equals(operation)) {
|
||
List<Map<String, Object>> products =
|
||
store.rows(
|
||
"SELECT"
|
||
+ " id,package_code,revision,name,network,resource_type,resource_quantity,duration_seconds,sale_amount"
|
||
+ " FROM rent_package p WHERE status='ACTIVE' AND network=?"
|
||
+ " AND EXISTS(SELECT 1 FROM rent_user_package b WHERE b.package_id=p.id AND b.user_id=? AND b.status='ACTIVE' AND b.default_slot=1) ORDER BY"
|
||
+ " id",
|
||
network(input(data, "network", 16)), number(app, "user_id"));
|
||
return AjaxResult.success(products);
|
||
}
|
||
Map<String, Object> order;
|
||
if ("quote".equals(operation)) {
|
||
RentalOrders.address(input(data, "receiver_address", 64));
|
||
if (!text(data, "notify_url").trim().isEmpty()) {
|
||
RentalHttp.checkUrl(text(data, "notify_url").trim());
|
||
}
|
||
Map<String, Object> product = store.one("SELECT p.* FROM rent_package p"
|
||
+ " JOIN rent_user_package b ON b.package_id=p.id WHERE b.user_id=?"
|
||
+ " AND b.status='ACTIVE' AND b.default_slot=1 AND p.status='ACTIVE' AND p.id=?",
|
||
number(app, "user_id"), number(data, "package_id"));
|
||
require(product != null, "套餐未绑定或已下架");
|
||
RentalOrders.validatePackage(product);
|
||
Map<String, Object> price = RentalPricing.price(product, data);
|
||
return AjaxResult.success(map("package_id", price.get("id"), "network", price.get("network"),
|
||
"duration_seconds", price.get("duration_seconds"), "amount", amount(price, "sale_amount").toPlainString(),
|
||
"resource_quantity", price.get("resource_quantity"), "currency", "USDT"));
|
||
}
|
||
if ("create".equals(operation)) {
|
||
order = orders.create(number(app, "id"), number(app, "user_id"), data);
|
||
} else {
|
||
order =
|
||
store.one(
|
||
"SELECT * FROM rent_order WHERE app_id=? AND merchant_order_no=?",
|
||
app.get("id"),
|
||
input(data, "merchant_order_no", 64));
|
||
require(order != null, "订单不存在");
|
||
}
|
||
return AjaxResult.success(
|
||
map(
|
||
"order_no",
|
||
order.get("order_no"),
|
||
"merchant_order_no",
|
||
order.get("merchant_order_no"),
|
||
"network",
|
||
order.get("network"),
|
||
"resource_type",
|
||
order.get("resource_type"),
|
||
"resource_quantity",
|
||
order.get("resource_quantity"),
|
||
"receiver_address",
|
||
order.get("receiver_address"),
|
||
"status",
|
||
order.get("status"),
|
||
"reclaim_status",
|
||
order.get("reclaim_status"),
|
||
"amount",
|
||
order.get("sale_amount"),
|
||
"duration_seconds",
|
||
order.get("duration_seconds"),
|
||
"refunded_amount",
|
||
order.get("refunded_amount")));
|
||
}
|
||
}
|