跳转验签

This commit is contained in:
lu7213 committed 2026-10-17 00:11:59 -04:00
1 parent bd1231c2dd
commit 9daba4a98e
5 files changed
+192 -3

No files matched your search

@@ -29,6 +29,8 @@ public class PaymentApiController {
private ApiSecurityService apiSecurityService;
@Autowired
private PaymentOrderOpenApiService paymentOrderOpenApiService;
@Autowired
private PaymentRedirectService paymentRedirectService;
@Log(title = "开放 API 创建支付订单", businessType = BusinessType.INSERT,
isSaveRequestData = false, isSaveResponseData = false)
@@ -51,6 +53,10 @@ public class PaymentApiController {
result.put("trade_id", order.getTradeId());
result.put("status", order.getStatus());
result.put("transaction_id", order.getBlockTransactionId());
if (Long.valueOf(2L).equals(order.getStatus())) {
result.put("signed_redirect_url",
paymentRedirectService.buildSignedRedirectUrl(order, request.getApiKey()));
}
return AjaxResult.success(result);
}
@@ -0,0 +1,66 @@
package com.ruoyi.system.service;
import com.ruoyi.system.domain.AppsPlus;
import com.ruoyi.system.domain.UsdtOrdersPlus;
import com.ruoyi.system.security.RsaKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
import java.security.PrivateKey;
import java.util.HashMap;
import java.util.Map;
/** 为支付成功后的同步跳转生成平台 RSA 签名,私钥只在服务端内存中使用。 */
@Service
public class PaymentRedirectService {
@Autowired
private IAppsService appsService;
@Autowired
private AppPrivateKeyCipher privateKeyCipher;
/** 未支付或未配置跳转地址时不生成地址,避免提前签发成功凭据。 */
public String buildSignedRedirectUrl(UsdtOrdersPlus order, String authenticatedApiKey) {
if (order == null || !Long.valueOf(2L).equals(order.getStatus())
|| !StringUtils.hasText(order.getRedirectUrl())) {
return null;
}
if (!StringUtils.hasText(authenticatedApiKey)) {
throw new IllegalStateException("支付查询缺少已认证API Key");
}
AppsPlus app = appsService.getById(order.getAppId());
if (app == null || !StringUtils.hasText(app.getPrivateKey())) {
throw new IllegalStateException("订单所属应用没有有效RSA私钥");
}
try {
PrivateKey privateKey = RsaKeyUtil.loadPrivateKey(privateKeyCipher.decrypt(app.getPrivateKey()));
Map<String, Object> values = callbackValues(order);
String signature = PaymentSignatureService.signRsa(values, privateKey);
return appendCallbackParameters(order.getRedirectUrl(), values, authenticatedApiKey, signature);
} catch (Exception exception) {
throw new IllegalStateException("支付同步跳转签名生成失败", exception);
}
}
private Map<String, Object> callbackValues(UsdtOrdersPlus order) {
Map<String, Object> values = new HashMap<String, Object>();
values.put("order_id", order.getOrderId());
values.put("trade_id", order.getTradeId());
values.put("status", order.getStatus());
values.put("transaction_id", order.getBlockTransactionId());
return values;
}
static String appendCallbackParameters(String redirectUrl, Map<String, Object> values,
String apiKey, String signature) {
return UriComponentsBuilder.fromHttpUrl(redirectUrl)
.queryParam("order_id", values.get("order_id"))
.queryParam("trade_id", values.get("trade_id"))
.queryParam("status", values.get("status"))
.queryParam("transaction_id", values.get("transaction_id"))
.queryParam("api_key", apiKey)
.queryParam("signature", signature)
.build().encode().toUriString();
}
}