fix:支付宝二维码
This commit is contained in:
+7
@@ -3,6 +3,7 @@ package com.central.alipay.config;
|
||||
import com.central.alipay.notify.AlipayNotifyVerifier;
|
||||
import com.central.alipay.service.AlipayLoginService;
|
||||
import com.central.alipay.service.AlipayPayService;
|
||||
import com.central.alipay.service.AlipayQrcodeService;
|
||||
import com.central.alipay.service.AlipayRefundService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
@@ -50,6 +51,12 @@ public class AlipayAutoConfiguration {
|
||||
return new AlipayRefundService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AlipayQrcodeService alipayQrcodeService() {
|
||||
log.info("AlipayQrcodeService 已加载");
|
||||
return new AlipayQrcodeService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AlipayNotifyVerifier alipayNotifyVerifier() {
|
||||
log.info("AlipayNotifyVerifier 已加载");
|
||||
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
package com.central.alipay.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
|
||||
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
|
||||
import com.central.alipay.config.AlipayClientFactory;
|
||||
import com.central.alipay.entity.AlipayConfig;
|
||||
import com.central.alipay.feign.AlipayConfigService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
/**
|
||||
* 支付宝小程序二维码服务
|
||||
* <p>
|
||||
* 封装 alipay.open.app.qrcode.create:
|
||||
* - 优先按 type=MINI + 实际 tenantId 取配置;查不到则回退到默认租户(zlt.alipay.qrcode.default-tenant-id,默认 webApp)
|
||||
* - 复用 AlipayClientFactory 缓存
|
||||
* - 支持 ISV 模式 app_auth_token 透传
|
||||
* - describe 自动截断到 16 字
|
||||
*
|
||||
* @author duzhichao
|
||||
* @date 2026-06-17
|
||||
*/
|
||||
@Slf4j
|
||||
public class AlipayQrcodeService {
|
||||
|
||||
private static final int MAX_DESCRIBE_LENGTH = 16;
|
||||
private static final String CONFIG_TYPE_MINI = "MINI";
|
||||
|
||||
@Autowired
|
||||
private AlipayClientFactory clientFactory;
|
||||
|
||||
@Autowired
|
||||
private AlipayConfigService alipayConfigService;
|
||||
|
||||
/**
|
||||
* 默认兜底租户ID。当业务 tenantId 下未配置支付宝小程序时,使用该租户的 type=MINI 配置生成二维码。
|
||||
*/
|
||||
@Value("${zlt.alipay.qrcode.default-tenant-id:webApp}")
|
||||
private String defaultTenantId;
|
||||
|
||||
/**
|
||||
* 生成支付宝小程序二维码(返回支付宝 CDN 上的临时 PNG URL)
|
||||
*
|
||||
* @param tenantId 租户ID
|
||||
* @param codeId 业务唯一码(最终拼到 query_param)
|
||||
* @param urlParam 小程序页面路径(不带前导 /),如 pages/link/link
|
||||
* @param queryKey 查询键名,如 uuid(最终 query_param=uuid=<codeId>)
|
||||
* @param describe 业务描述,超过 16 字自动截断
|
||||
*/
|
||||
public String createMiniQrcode(String tenantId, String codeId,
|
||||
String urlParam, String queryKey, String describe) {
|
||||
if (tenantId == null || tenantId.isEmpty()) {
|
||||
throw new RuntimeException("tenantId 不能为空");
|
||||
}
|
||||
if (codeId == null || codeId.isEmpty()) {
|
||||
throw new RuntimeException("codeId 不能为空");
|
||||
}
|
||||
if (urlParam == null || urlParam.isEmpty()) {
|
||||
throw new RuntimeException("urlParam 不能为空");
|
||||
}
|
||||
|
||||
AlipayConfig cfg = resolveMiniConfig(tenantId);
|
||||
AlipayClient client = clientFactory.createAlipayClient(cfg);
|
||||
|
||||
AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
|
||||
JSONObject biz = new JSONObject();
|
||||
biz.put("url_param", urlParam);
|
||||
biz.put("query_param", (queryKey == null || queryKey.isEmpty() ? "uuid" : queryKey) + "=" + codeId);
|
||||
biz.put("describe", trimDescribe(describe));
|
||||
request.setBizContent(biz.toJSONString());
|
||||
|
||||
if (Boolean.TRUE.equals(cfg.getIsServiceMode()) && cfg.getAuthToken() != null) {
|
||||
request.putOtherTextParam("app_auth_token", cfg.getAuthToken());
|
||||
}
|
||||
|
||||
try {
|
||||
AlipayOpenAppQrcodeCreateResponse response = client.execute(request);
|
||||
if (response.isSuccess()) {
|
||||
log.info("支付宝小程序码生成成功, tenantId: {}, codeId: {}, qrCodeUrl: {}",
|
||||
tenantId, codeId, response.getQrCodeUrl());
|
||||
return response.getQrCodeUrl();
|
||||
}
|
||||
log.error("支付宝小程序码生成失败, subCode: {}, subMsg: {}",
|
||||
response.getSubCode(), response.getSubMsg());
|
||||
throw new RuntimeException("支付宝小程序码生成失败: " + response.getSubMsg());
|
||||
} catch (AlipayApiException e) {
|
||||
log.error("支付宝小程序码生成异常, tenantId: {}, codeId: {}", tenantId, codeId, e);
|
||||
throw new RuntimeException("支付宝小程序码生成异常: " + e.getErrMsg(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取小程序配置:优先实际租户,失败回退默认租户(webApp)。两个都没有再抛出。
|
||||
*/
|
||||
private AlipayConfig resolveMiniConfig(String tenantId) {
|
||||
AlipayConfig cfg = safeGetConfig(tenantId);
|
||||
if (cfg != null) {
|
||||
return cfg;
|
||||
}
|
||||
if (defaultTenantId != null && !defaultTenantId.isEmpty() && !defaultTenantId.equals(tenantId)) {
|
||||
log.warn("租户 {} 未配置支付宝小程序,回退默认租户 {}", tenantId, defaultTenantId);
|
||||
AlipayConfig fallback = safeGetConfig(defaultTenantId);
|
||||
if (fallback != null) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("支付宝小程序配置不存在,tenantId: " + tenantId
|
||||
+ (defaultTenantId != null ? ",默认租户 " + defaultTenantId + " 同样未配置" : ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Feign 调用兜底:网络异常/降级等情况下返回 null,由 resolveMiniConfig 走回退分支
|
||||
*/
|
||||
private AlipayConfig safeGetConfig(String tenantId) {
|
||||
try {
|
||||
return alipayConfigService.getConfig(CONFIG_TYPE_MINI, tenantId);
|
||||
} catch (Exception e) {
|
||||
log.warn("查询支付宝小程序配置失败,tenantId: {}, err: {}", tenantId, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String trimDescribe(String describe) {
|
||||
if (describe == null || describe.isEmpty()) {
|
||||
return "二维码";
|
||||
}
|
||||
return describe.length() > MAX_DESCRIBE_LENGTH
|
||||
? describe.substring(0, MAX_DESCRIBE_LENGTH)
|
||||
: describe;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user