Kaynağa Gözat

feat: ServiceProviderProfile 支持证书模式(appCert/alipayPublicCert/rootCert)

alphah 1 gün önce
ebeveyn
işleme
9cb2cb39a4

+ 18 - 14
java/sql/009_service_provider_profile.sql

@@ -1,17 +1,21 @@
--- 服务商多业务凭证配置表
+-- 服务商多业务凭证配置表(支持公钥模式 + 证书模式)
 CREATE TABLE IF NOT EXISTS pay_service_provider_profile (
-    id                  BIGSERIAL PRIMARY KEY,
-    service_provider_id BIGINT NOT NULL,
-    biz_type            VARCHAR(32) NOT NULL,
-    app_id              VARCHAR(64),
-    app_private_key     TEXT,
-    alipay_public_key   TEXT,
-    server_url          VARCHAR(256),
-    format              VARCHAR(16)  DEFAULT 'JSON',
-    charset             VARCHAR(16)  DEFAULT 'UTF-8',
-    sign_type           VARCHAR(16)  DEFAULT 'RSA2',
-    description         VARCHAR(512),
-    created_time        TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-    updated_time        TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+    id                        BIGSERIAL PRIMARY KEY,
+    service_provider_id       BIGINT NOT NULL,
+    biz_type                  VARCHAR(32) NOT NULL,
+    app_id                    VARCHAR(64),
+    app_private_key           TEXT,
+    alipay_public_key         TEXT,
+    -- 证书模式(可选,与 alipay_public_key 二选一)
+    app_cert_content          TEXT,
+    alipay_public_cert_content TEXT,
+    root_cert_content         TEXT,
+    server_url                VARCHAR(256),
+    format                    VARCHAR(16)  DEFAULT 'JSON',
+    charset                   VARCHAR(16)  DEFAULT 'UTF-8',
+    sign_type                 VARCHAR(16)  DEFAULT 'RSA2',
+    description               VARCHAR(512),
+    created_time              TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+    updated_time              TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
     UNIQUE(service_provider_id, biz_type)
 );

+ 11 - 1
java/src/main/java/com/payment/platform/core/alipay/AlipayClientFactory.java

@@ -167,9 +167,19 @@ public class AlipayClientFactory {
         AlipayConfig config = buildSdkConfig(
                 profile.getAppId(), profile.getAppPrivateKey(), profile.getAlipayPublicKey(),
                 profile.getServerUrl(), profile.getFormat(), profile.getCharset(), profile.getSignType());
+
+        // 证书模式: 若有证书内容则设置(优先级高于公钥)
+        if (profile.getAppCertContent() != null && !profile.getAppCertContent().isBlank()) {
+            config.setAppCertContent(profile.getAppCertContent());
+            config.setAlipayPublicCertContent(profile.getAlipayPublicCertContent());
+            config.setRootCertContent(profile.getRootCertContent());
+        }
+
         try {
             AlipayClient client = new DefaultAlipayClient(config);
-            log.info("服务商[{}]业务[{}]客户端创建成功, appId={}", profile.getServiceProviderId(), profile.getBizType(), profile.getAppId());
+            log.info("服务商[{}]业务[{}]客户端创建成功, appId={}, mode={}",
+                    profile.getServiceProviderId(), profile.getBizType(), profile.getAppId(),
+                    profile.getAppCertContent() != null ? "cert" : "key");
             return client;
         } catch (AlipayApiException e) {
             log.error("服务商[{}]业务[{}]客户端创建失败", profile.getServiceProviderId(), profile.getBizType(), e);

+ 14 - 0
java/src/main/java/com/payment/platform/module/payment/serviceprovider/entity/ServiceProviderProfileEntity.java

@@ -7,6 +7,10 @@ import lombok.EqualsAndHashCode;
 
 /**
  * 服务商多业务凭证配置 — 同一服务商不同业务场景可使用不同 app_id/密钥
+ * <p>
+ * 支持两种模式:
+ * - 公钥模式: app_private_key + alipay_public_key(已填写时使用)
+ * - 证书模式: app_private_key + app_cert_content + alipay_root_cert(公钥未填写时回退)
  */
 @Data
 @EqualsAndHashCode(callSuper = true)
@@ -18,6 +22,16 @@ public class ServiceProviderProfileEntity extends PaymentBaseEntity {
     private String appId;
     private String appPrivateKey;
     private String alipayPublicKey;
+
+    /** 应用公钥证书内容(证书模式,PEM 格式) */
+    private String appCertContent;
+
+    /** 支付宝公钥证书内容(证书模式,PEM 格式) */
+    private String alipayPublicCertContent;
+
+    /** 支付宝根证书内容(证书模式,PEM 格式) */
+    private String rootCertContent;
+
     private String serverUrl;
     private String format;
     private String charset;