同步跳转验签

This commit is contained in:
lu7213 committed 2026-10-17 00:37:29 -04:00
1 parent 454da449c4
commit 361bfd6421
2 files changed
+19 -6

No files matched your search

@@ -8,6 +8,8 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.PrivateKey;
import java.util.HashMap;
import java.util.Map;
@@ -54,13 +56,23 @@ public class PaymentRedirectService {
static String appendCallbackParameters(String redirectUrl, Map<String, Object> values,
String apiKey, String signature) {
return UriComponentsBuilder.fromHttpUrl(redirectUrl)
String unsignedUrl = 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();
try {
// 标准Base64包含“+”。它在查询参数中会按空格解码,必须使用表单规则编码为%2B。
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
int fragmentIndex = unsignedUrl.indexOf('#');
String fragment = fragmentIndex < 0 ? "" : unsignedUrl.substring(fragmentIndex);
String urlWithoutFragment = fragmentIndex < 0 ? unsignedUrl : unsignedUrl.substring(0, fragmentIndex);
return urlWithoutFragment + (urlWithoutFragment.contains("?") ? "&" : "?")
+ "signature=" + encodedSignature + fragment;
} catch (UnsupportedEncodingException exception) {
throw new IllegalStateException("UTF-8 URL编码不可用", exception);
}
}
}