diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java index 4047328..f8ce259 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java @@ -43,6 +43,8 @@ public class SysUsdtDemoController { private static final Logger log = LoggerFactory.getLogger(SysUsdtDemoController.class); private static final String DEFAULT_API_URL = "http://192.168.3.103:2020/api/v1/order/create-transaction"; + private static final String DEFAULT_QUERY_API_URL = + "http://192.168.3.103:2020/api/v1/order/query"; private final RestTemplate restTemplate; public SysUsdtDemoController() { @@ -74,12 +76,7 @@ public class SysUsdtDemoController { @RequestParam(value = "walletUserName", required = false) String walletUserName) { BigDecimal paymentAmount = parseAmount(amount); String apiUrl = optionalEnv("PAYMENT_API_URL", DEFAULT_API_URL); - String apiKey = requiredEnvWithAlias("PAYMENT_API_KEY", "PAYMENT_API_KEK"); - String apiSecret = requiredEnv("PAYMENT_API_SECRET"); - String publicKeyText = requiredEnv("PAYMENT_RSA_PUBLIC_KEY").replace("\\n", "\n"); - try { - PublicKey publicKey = RsaKeyUtil.loadPublicKey(publicKeyText); String contextPath = ServletUriComponentsBuilder.fromCurrentContextPath() .build().toUriString(); Map params = new HashMap(); @@ -92,17 +89,7 @@ public class SysUsdtDemoController { params.put("redirect_url", optionalEnv("PAYMENT_REDIRECT_URL", contextPath + "/system/demoUsdt/redirect")); - EncryptedRequest encrypted = SecureClient.encrypt(apiKey, apiSecret, publicKey, params); - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - headers.setAccept(java.util.Collections.singletonList(MediaType.APPLICATION_JSON)); - ResponseEntity response = restTemplate.exchange(apiUrl, HttpMethod.POST, - new HttpEntity(encrypted, headers), String.class); - JSONObject result = JSON.parseObject(response.getBody()); - if (result == null || result.getIntValue("code") != 200 || result.get("data") == null) { - throw new CustomException(result == null ? "创建支付订单失败" : result.getString("msg")); - } - return AjaxResult.success(result.get("data")); + return AjaxResult.success(sendEncryptedRequest(apiUrl, params, "创建支付订单失败")); } catch (RestClientResponseException ex) { log.warn("支付演示请求失败,HTTP状态={}", ex.getRawStatusCode()); throw new CustomException("支付服务暂时不可用,请稍后重试"); @@ -114,6 +101,50 @@ public class SysUsdtDemoController { } } + /** 查询支付状态;API 凭据和加密过程不会暴露给浏览器。 */ + @PostMapping("/order-status") + public AjaxResult orderStatus(@RequestParam("orderId") String orderId) { + String normalized = orderId == null ? "" : orderId.trim(); + if (normalized.isEmpty() || normalized.length() > 32 + || !normalized.matches("[A-Za-z0-9_-]+")) { + throw new CustomException("订单号格式不正确"); + } + Map params = new HashMap(); + params.put("order_id", normalized); + String queryUrl = optionalEnv("PAYMENT_QUERY_API_URL", DEFAULT_QUERY_API_URL); + try { + return AjaxResult.success(sendEncryptedRequest(queryUrl, params, "查询支付订单失败")); + } catch (RestClientResponseException ex) { + log.warn("支付状态查询失败,HTTP状态={}", ex.getRawStatusCode()); + throw new CustomException("支付状态查询暂时不可用"); + } catch (CustomException ex) { + throw ex; + } catch (Exception ex) { + log.warn("支付状态查询失败,异常类型={}", ex.getClass().getSimpleName()); + throw new CustomException("支付状态查询暂时不可用"); + } + } + + private Object sendEncryptedRequest(String apiUrl, Map params, + String failureMessage) throws Exception { + String apiKey = requiredEnvWithAlias("PAYMENT_API_KEY", "PAYMENT_API_KEK"); + String apiSecret = requiredEnv("PAYMENT_API_SECRET"); + PublicKey publicKey = RsaKeyUtil.loadPublicKey( + requiredEnv("PAYMENT_RSA_PUBLIC_KEY").replace("\\n", "\n")); + EncryptedRequest encrypted = SecureClient.encrypt(apiKey, apiSecret, publicKey, params); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.setAccept(java.util.Collections.singletonList(MediaType.APPLICATION_JSON)); + ResponseEntity response = restTemplate.exchange(apiUrl, HttpMethod.POST, + new HttpEntity(encrypted, headers), String.class); + JSONObject result = JSON.parseObject(response.getBody()); + if (result == null || result.getIntValue("code") != 200 || result.get("data") == null) { + throw new CustomException(result == null || StringUtils.isBlank(result.getString("msg")) + ? failureMessage : result.getString("msg")); + } + return result.get("data"); + } + private BigDecimal parseAmount(String amount) { try { BigDecimal value = new BigDecimal(amount); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controller/PaymentApiController.java b/ruoyi-system/src/main/java/com/ruoyi/system/controller/PaymentApiController.java index 7c61b16..e1b197a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controller/PaymentApiController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controller/PaymentApiController.java @@ -4,7 +4,6 @@ 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.domain.dto.EncryptedRequest; import com.ruoyi.system.domain.vo.DecryptResult; @@ -15,7 +14,6 @@ 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; @@ -25,13 +23,9 @@ import java.util.Map; @RestController @RequestMapping("/api/v1/order") public class PaymentApiController { - @Autowired - private IAppApiKeysService keys; @Autowired private IUsdtOrdersService orders; @Autowired - private AppPrivateKeyCipher privateKeyCipher; - @Autowired private ApiSecurityService apiSecurityService; @Autowired private PaymentOrderOpenApiService paymentOrderOpenApiService; @@ -45,13 +39,12 @@ public class PaymentApiController { } @PostMapping("/query") - public AjaxResult query(@RequestBody Map 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("签名无效"); - UsdtOrdersPlus order = orders.getOne(new LambdaQueryWrapper().eq(UsdtOrdersPlus::getAppId, appId).eq(UsdtOrdersPlus::getOrderId, String.valueOf(body.get("order_id")))); + public AjaxResult query(@RequestBody EncryptedRequest request) { + DecryptResult decrypted = apiSecurityService.decryptAndVerify(request); + String orderId = parseOrderId(decrypted.getPlainData()); + UsdtOrdersPlus order = orders.getOne(new LambdaQueryWrapper() + .eq(UsdtOrdersPlus::getAppId, decrypted.getAppId()) + .eq(UsdtOrdersPlus::getOrderId, orderId).last("limit 1")); if (order == null) return AjaxResult.error("订单不存在"); Map result = new HashMap(); result.put("order_id", order.getOrderId()); @@ -61,19 +54,15 @@ public class PaymentApiController { 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().eq(AppApiKeysPlus::getAppId, appId) - .eq(AppApiKeysPlus::getApiKey, String.valueOf(apiKey)).eq(AppApiKeysPlus::getStatus, 1) - .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 String parseOrderId(String plainData) { + final String prefix = "order_id="; + if (plainData == null || !plainData.startsWith(prefix) || !plainData.endsWith("&")) { + throw new com.ruoyi.common.exception.CustomException("业务参数格式错误"); } + String orderId = plainData.substring(prefix.length(), plainData.length() - 1).trim(); + if (orderId.isEmpty() || orderId.length() > 32 || orderId.contains("&")) { + throw new com.ruoyi.common.exception.CustomException("order_id 格式错误"); + } + return orderId; } }