缩短充值跳转链接兼容第三方

This commit is contained in:
2025-07-17 18:49:21 +08:00
parent 2691bc63f0
commit 52e499b20a

View File

@@ -2,6 +2,7 @@ package com.accompany.payment.strategy;
import com.accompany.common.constant.Constant;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.StringUtils;
import com.accompany.core.base.UidContextHolder;
import com.accompany.core.exception.ServiceException;
import com.accompany.payment.annotation.PayChannelSupport;
@@ -15,7 +16,10 @@ import com.accompany.payment.v5pay.V5PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@Service
@@ -33,7 +37,8 @@ public class V5PayStrategy extends AbstractPayStrategy {
chargeUserLimitService.chargeLimitCheck(UidContextHolder.get(), ChargeUserLimitConstant.LIMIT_TYPE_OF_H5);
ChargeRecord chargeRecord = context.getChargeRecord();
ChargeProd chargeProd = context.getChargeProd();
V5PayResponseVo orderRes = v5PayService.createOrder(chargeRecord, chargeProd, context.getSuccessUrl());
String succeccUrl = extractAndBuildUrl(context.getSuccessUrl());
V5PayResponseVo orderRes = v5PayService.createOrder(chargeRecord, chargeProd, succeccUrl);
if (!orderRes.getCode().equalsIgnoreCase("1000")) {
throw new ServiceException(BusiStatus.SERVERERROR);
}
@@ -43,4 +48,46 @@ public class V5PayStrategy extends AbstractPayStrategy {
appMap.put(PayConstant.H5_PAY_ERBANNO_FIELD, context.getErbanNo());
return appMap;
}
public static String extractAndBuildUrl(String url) {
if (StringUtils.isEmpty(url)) {
return url;
}
try {
// 解析原始URL
URI uri = new URI(url);
// 获取基础路径(协议+域名+路径,不带查询参数)
String basePath = uri.getScheme() + "://" + uri.getHost() + uri.getPath();
// 提取需要的参数
Map<String, String> targetParams = new LinkedHashMap<>();
String query = uri.getQuery();
if (query != null) {
for (String param : query.split("&")) {
String[] keyValue = param.split("=", 2); // 只分割第一个=号
if (keyValue.length == 2) {
String key = keyValue[0];
if ("channelType".equals(key) || "lang".equals(key)) {
targetParams.put(key, keyValue[1]);
}
}
}
}
// 拼接新URL
if (!targetParams.isEmpty()) {
StringBuilder newUrl = new StringBuilder(basePath).append("?");
targetParams.forEach((key, value) -> {
newUrl.append(key).append("=").append(value).append("&");
});
// 删除最后一个多余的&
return newUrl.substring(0, newUrl.length() - 1);
}
return basePath;
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URL format: " + url, e);
}
}
}