新增接口

This commit is contained in:
lu7213 committed 2026-10-10 10:54:12 -04:00
1 parent a2be79377b
commit 8ebf5f383d
6 files changed
+487 -40

No files matched your search

@@ -1,63 +1,66 @@
package com.ruoyi.system.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppApiKeysPlus;
import com.ruoyi.system.domain.UsdtOrdersPlus;
import com.ruoyi.system.service.IAppApiKeysService;
import com.ruoyi.system.service.IUsdtOrdersService;
import com.ruoyi.system.service.PaymentSignatureService;
import com.ruoyi.system.service.AppPrivateKeyCipher;
import com.ruoyi.system.domain.dto.EncryptedRequest;
import com.ruoyi.system.domain.vo.DecryptResult;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
import java.math.BigDecimal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.net.URI;
import java.util.Date;
/** 对外支付 API,统一使用 JSON 和签名校验。 */
/**
* 对外支付 API,统一使用 JSON 和签名校验。
*/
@RestController
@RequestMapping("/api/v1/order")
public class PaymentApiController {
@Autowired private IAppApiKeysService keys;
@Autowired private IUsdtOrdersService orders;
@Autowired private StringRedisTemplate redis;
@Autowired private AppPrivateKeyCipher privateKeyCipher;
@Autowired
private IAppApiKeysService keys;
@Autowired
private IUsdtOrdersService orders;
@Autowired
private AppPrivateKeyCipher privateKeyCipher;
@Autowired
private ApiSecurityService apiSecurityService;
@Autowired
private PaymentOrderOpenApiService paymentOrderOpenApiService;
@Log(title = "开放 API 创建支付订单", businessType = BusinessType.INSERT,
isSaveRequestData = false, isSaveResponseData = false)
@PostMapping("/create-transaction")
public AjaxResult create(@RequestBody Map<String, Object> body) {
String signature = String.valueOf(body.get("signature"));
Long appId = parseLong(body.get("app_id"));
AppApiKeysPlus key = activeKey(appId, body.get("api_key"));
if (key == null || !PaymentSignatureService.verify(body, privateKeyCipher.decryptCredentialSecret(key.getApiSecret()), signature)) return AjaxResult.error("签名无效");
String orderId = String.valueOf(body.get("order_id"));
if (orderId.length() == 0 || orderId.length() > 32 || body.get("amount") == null || body.get("wallet_user_name") == null) return AjaxResult.error("缺少必要参数");
if (!validUrl(body.get("notify_url")) || (body.get("redirect_url") != null && !validUrl(body.get("redirect_url")))) return AjaxResult.error("回调地址无效");
UsdtOrdersPlus order = new UsdtOrdersPlus();
order.setTradeId(UUID.randomUUID().toString().replace("-", "")); order.setOrderId(orderId);
order.setToken(String.valueOf(body.get("payment_address")));
order.setAmount(new BigDecimal(String.valueOf(body.get("amount")))); order.setActualAmount(order.getAmount());
String lockKey = "payment:slot:" + appId + ":" + order.getToken() + ":" + order.getActualAmount().toPlainString();
if (!Boolean.TRUE.equals(redis.opsForValue().setIfAbsent(lockKey, order.getTradeId(), 30, TimeUnit.MINUTES))) return AjaxResult.error("该金额已有待支付订单");
order.setStatus(1L); order.setCallbackConfirm(2L); order.setAppId(appId); order.setUserId(key.getUserId());
order.setNotifyUrl(String.valueOf(body.get("notify_url"))); order.setRedirectUrl(body.get("redirect_url") == null ? null : String.valueOf(body.get("redirect_url")));
orders.save(order);
Map<String, Object> result = new HashMap<String, Object>(); result.put("order_id", orderId); result.put("trade_id", order.getTradeId()); result.put("payment_amount", order.getActualAmount()); result.put("payment_address", order.getToken());
return AjaxResult.success(result);
public AjaxResult create(@RequestBody EncryptedRequest request) {
DecryptResult decrypted = apiSecurityService.decryptAndVerify(request);
return AjaxResult.success(paymentOrderOpenApiService.createOrder(decrypted));
}
@PostMapping("/query")
public AjaxResult query(@RequestBody Map<String, Object> body) {
Long appId = parseLong(body.get("app_id")); AppApiKeysPlus key = activeKey(appId, body.get("api_key")); String signature = String.valueOf(body.get("signature"));
if (key == null || !PaymentSignatureService.verify(body, privateKeyCipher.decryptCredentialSecret(key.getApiSecret()), signature)) return AjaxResult.error("签名无效");
Long appId = parseLong(body.get("app_id"));
AppApiKeysPlus key = activeKey(appId, body.get("api_key"));
String signature = String.valueOf(body.get("signature"));
if (key == null || !PaymentSignatureService.verify(body, privateKeyCipher.decryptCredentialSecret(key.getApiSecret()), signature))
return AjaxResult.error("签名无效");
UsdtOrdersPlus order = orders.getOne(new LambdaQueryWrapper<UsdtOrdersPlus>().eq(UsdtOrdersPlus::getAppId, appId).eq(UsdtOrdersPlus::getOrderId, String.valueOf(body.get("order_id"))));
if (order == null) return AjaxResult.error("订单不存在");
Map<String, Object> result = new HashMap<String, Object>(); result.put("order_id", order.getOrderId()); result.put("trade_id", order.getTradeId()); result.put("status", order.getStatus()); result.put("transaction_id", order.getBlockTransactionId()); return AjaxResult.success(result);
Map<String, Object> result = new HashMap<String, Object>();
result.put("order_id", order.getOrderId());
result.put("trade_id", order.getTradeId());
result.put("status", order.getStatus());
result.put("transaction_id", order.getBlockTransactionId());
return AjaxResult.success(result);
}
private AppApiKeysPlus activeKey(Long appId, Object apiKey) {
if (appId == null || apiKey == null || String.valueOf(apiKey).trim().isEmpty()) return null;
return keys.getOne(new LambdaQueryWrapper<AppApiKeysPlus>().eq(AppApiKeysPlus::getAppId, appId)
@@ -65,6 +68,12 @@ public class PaymentApiController {
.isNull(AppApiKeysPlus::getDeletedAt)
.and(wrapper -> wrapper.isNull(AppApiKeysPlus::getExpiredAt).or().gt(AppApiKeysPlus::getExpiredAt, new Date())));
}
private Long parseLong(Object value) { try { return value == null ? null : Long.valueOf(String.valueOf(value)); } catch (Exception ex) { return null; } }
private boolean validUrl(Object value) { try { if (value == null) return false; URI uri = new URI(String.valueOf(value)); return ("https".equalsIgnoreCase(uri.getScheme()) || "http".equalsIgnoreCase(uri.getScheme())) && uri.getHost() != null; } catch (Exception ex) { return false; } }
private Long parseLong(Object value) {
try {
return value == null ? null : Long.valueOf(String.valueOf(value));
} catch (Exception ex) {
return null;
}
}
}