fix:支付宝小程序登录以及支付宝支付

This commit is contained in:
2026-06-16 16:55:49 +08:00
parent 815b0e3731
commit 0ac3bece42
80 changed files with 5841 additions and 30 deletions
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-commons</artifactId>
<version>6.0.0</version>
</parent>
<artifactId>zlt-alipay-common-core</artifactId>
<description>支付宝核心功能模块(实体、Feign接口、配置属性)</description>
<dependencies>
<!-- 基础依赖 -->
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-common-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-db-spring-boot-starter</artifactId>
</dependency>
<!-- Knife4j (Swagger annotations) -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,55 @@
package com.central.alipay.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 支付宝全局配置属性(仅承载全局默认值,运行时配置走 alipay_config 表)
*
* @author duzhichao
* @date 2026-05-12
*/
@Data
@ConfigurationProperties(prefix = "zlt.alipay")
public class AlipayProperties {
/**
* 是否启用支付宝模块
*/
private boolean enabled = true;
/**
* 网关地址
*/
private String gatewayUrl = "https://openapi.alipay.com/gateway.do";
/**
* 签名算法
*/
private String signType = "RSA2";
/**
* 字符集
*/
private String charset = "UTF-8";
/**
* 格式
*/
private String format = "json";
/**
* AlipayClient 缓存过期时间(秒)
*/
private int cacheExpireSeconds = 600;
/**
* 支付异步通知地址(全局默认,可被 DB 覆盖)
*/
private String notifyUrl;
/**
* 退款异步通知地址(全局默认,可被 DB 覆盖)
*/
private String refundNotifyUrl;
}
@@ -0,0 +1,92 @@
package com.central.alipay.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 支付宝配置实体(对标 WechatConfig)
*
* @author duzhichao
* @date 2026-05-12
*/
@Data
@TableName("alipay_config")
@Schema(description = "支付宝配置")
public class AlipayConfig {
@TableId(type = IdType.AUTO)
@Schema(description = "主键ID", example = "1")
private Long id;
@Schema(description = "租户ID", example = "tenant-001")
private String tenantId;
@Schema(description = "配置类型:MINI=小程序登录,PAY=交易支付", example = "PAY", allowableValues = {"MINI", "PAY"})
private String type;
@Schema(description = "支付宝 AppId", example = "2021000000000001")
private String appId;
@Schema(description = "应用私钥(PKCS8,入库加密)", accessMode = Schema.AccessMode.WRITE_ONLY)
private String appPrivateKey;
@Schema(description = "支付宝公钥(用于回调验签)", accessMode = Schema.AccessMode.WRITE_ONLY)
private String alipayPublicKey;
@Schema(description = "签名算法", example = "RSA2", allowableValues = {"RSA2", "RSA"})
private String signType;
@Schema(description = "字符集", example = "UTF-8")
private String charset;
@Schema(description = "格式", example = "json")
private String format;
@Schema(description = "网关地址", example = "https://openapi.alipay.com/gateway.do")
private String gatewayUrl;
@Schema(description = "支付异步通知地址(type=PAY 时有效)", example = "https://api.example.com/alipay/pay/notify")
private String notifyUrl;
@Schema(description = "退款异步通知地址(type=PAY 时有效)", example = "https://api.example.com/alipay/refund/notify")
private String refundNotifyUrl;
@Schema(description = "是否 ISV(服务商)模式", example = "false")
private Boolean isServiceMode;
@Schema(description = "ISV 合作伙伴 PID", example = "2088xxxxxxxxxxxx")
private String pid;
@Schema(description = "ISV 代调用时使用的 app_auth_token", accessMode = Schema.AccessMode.WRITE_ONLY)
private String authToken;
@Schema(description = "商家应用 AppId(被代调用方)", example = "2021xxxxxxxxxxxx")
private String subAppId;
@Schema(description = "间连商户号 smid", example = "2088xxxxxxxxxxxx")
private String subMerchantId;
@Schema(description = "AES 接口内容加解密 Key", accessMode = Schema.AccessMode.WRITE_ONLY)
private String encryptKey;
@Schema(description = "是否公钥证书模式", example = "false")
private Boolean certMode;
@Schema(description = "应用公钥证书路径(证书模式)")
private String appCertPath;
@Schema(description = "支付宝公钥证书路径(证书模式)")
private String alipayCertPath;
@Schema(description = "支付宝根证书路径(证书模式)")
private String alipayRootCertPath;
@Schema(description = "状态:0=禁用,1=启用", example = "1", allowableValues = {"0", "1"})
private Integer status;
@Schema(description = "备注", example = "生产环境支付配置")
private String remark;
}
@@ -0,0 +1,43 @@
package com.central.alipay.feign;
import com.central.alipay.entity.AlipayConfig;
import com.central.alipay.feign.fallback.AlipayConfigServiceFallbackFactory;
import com.central.common.constant.ServiceNameConstants;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 支付宝配置 Feign 客户端(服务端在 order-center)
*
* @author duzhichao
* @date 2026-05-12
*/
@FeignClient(name = ServiceNameConstants.ORDER_SERVICE, fallbackFactory = AlipayConfigServiceFallbackFactory.class, dismiss404 = true)
public interface AlipayConfigService {
/**
* 根据类型和租户ID查询配置
*/
@GetMapping(value = "/alipay/config/type/{type}")
AlipayConfig getConfig(@PathVariable("type") String type, @RequestParam("tenantId") String tenantId);
/**
* 根据 AppId 和类型查询配置
*/
@GetMapping(value = "/alipay/config/app/{appId}")
AlipayConfig getConfigByAppId(@PathVariable("appId") String appId, @RequestParam("type") String type);
/**
* 根据 AppId 查询配置(不区分类型,用于回调定位)
*/
@GetMapping(value = "/alipay/config/app/{appId}/any")
AlipayConfig getConfigByAppId(@PathVariable("appId") String appId);
/**
* 根据子商户号获取配置(ISV 模式专用)
*/
@GetMapping(value = "/alipay/config/sub/{subMerchantId}")
AlipayConfig getConfigBySubMerchantId(@PathVariable("subMerchantId") String subMerchantId);
}
@@ -0,0 +1,42 @@
package com.central.alipay.feign.fallback;
import com.central.alipay.entity.AlipayConfig;
import com.central.alipay.feign.AlipayConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
/**
* AlipayConfigService 降级工厂
*
* @author duzhichao
* @date 2026-05-12
*/
@Slf4j
public class AlipayConfigServiceFallbackFactory implements FallbackFactory<AlipayConfigService> {
@Override
public AlipayConfigService create(Throwable throwable) {
log.error("AlipayConfigService 调用失败", throwable);
return new AlipayConfigService() {
@Override
public AlipayConfig getConfig(String type, String tenantId) {
return null;
}
@Override
public AlipayConfig getConfigByAppId(String appId, String type) {
return null;
}
@Override
public AlipayConfig getConfigByAppId(String appId) {
return null;
}
@Override
public AlipayConfig getConfigBySubMerchantId(String subMerchantId) {
return null;
}
};
}
}
@@ -0,0 +1 @@
com.central.alipay.feign.fallback.AlipayConfigServiceFallbackFactory
@@ -0,0 +1,37 @@
-- =====================================================
-- 支付宝配置表(对标 wechat_config)
-- 归属:order-center
-- =====================================================
CREATE TABLE IF NOT EXISTS `alipay_config` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
`tenant_id` VARCHAR(64) DEFAULT NULL COMMENT '租户ID',
`type` VARCHAR(16) NOT NULL COMMENT '配置类型:MINI-小程序登录,PAY-交易支付',
`app_id` VARCHAR(64) NOT NULL COMMENT '支付宝 AppId',
`app_private_key` TEXT COMMENT '应用私钥(PKCS8,入库建议加密)',
`alipay_public_key` TEXT COMMENT '支付宝公钥(用于回调验签)',
`sign_type` VARCHAR(16) DEFAULT 'RSA2' COMMENT '签名算法',
`charset` VARCHAR(16) DEFAULT 'UTF-8' COMMENT '字符集',
`format` VARCHAR(16) DEFAULT 'json' COMMENT '格式',
`gateway_url` VARCHAR(256) DEFAULT 'https://openapi.alipay.com/gateway.do' COMMENT '网关地址',
`notify_url` VARCHAR(256) DEFAULT NULL COMMENT '支付异步通知地址',
`refund_notify_url` VARCHAR(256) DEFAULT NULL COMMENT '退款异步通知地址',
`is_service_mode` TINYINT(1) DEFAULT 0 COMMENT '是否 ISV(服务商)模式',
`pid` VARCHAR(64) DEFAULT NULL COMMENT 'ISV 合作伙伴 PID',
`auth_token` VARCHAR(128) DEFAULT NULL COMMENT 'ISV 代调用时使用的 app_auth_token',
`sub_app_id` VARCHAR(64) DEFAULT NULL COMMENT '商家应用 AppId(被代调用方)',
`sub_merchant_id` VARCHAR(64) DEFAULT NULL COMMENT '间连商户号(smid)',
`encrypt_key` VARCHAR(128) DEFAULT NULL COMMENT 'AES 接口内容加解密 Key',
`cert_mode` TINYINT(1) DEFAULT 0 COMMENT '是否公钥证书模式:0-普通公钥,1-证书模式',
`app_cert_path` VARCHAR(256) DEFAULT NULL COMMENT '应用公钥证书路径',
`alipay_cert_path` VARCHAR(256) DEFAULT NULL COMMENT '支付宝公钥证书路径',
`alipay_root_cert_path` VARCHAR(256) DEFAULT NULL COMMENT '支付宝根证书路径',
`status` TINYINT DEFAULT 1 COMMENT '状态:0-禁用,1-启用',
`remark` VARCHAR(255) DEFAULT NULL COMMENT '备注',
`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_tenant_type_appid` (`tenant_id`, `type`, `app_id`),
KEY `idx_tenant_type` (`tenant_id`, `type`),
KEY `idx_app_id` (`app_id`),
KEY `idx_sub_merchant_id` (`sub_merchant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付宝配置表';
@@ -0,0 +1,18 @@
-- =====================================================
-- 第三方用户 OpenId 映射表
-- 统一管理微信/支付宝等各家 openId/unionId
-- =====================================================
CREATE TABLE IF NOT EXISTS `sys_user_third_openid` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_id` BIGINT NOT NULL COMMENT '系统用户ID',
`provider` VARCHAR(16) NOT NULL COMMENT '第三方平台:WECHAT / ALIPAY',
`app_id` VARCHAR(64) NOT NULL COMMENT '第三方应用ID',
`open_id` VARCHAR(128) NOT NULL COMMENT '第三方用户唯一标识',
`union_id` VARCHAR(128) DEFAULT NULL COMMENT '第三方 unionId(微信开放平台 unionId / 支付宝 PID 下 union_id)',
`mobile` VARCHAR(20) DEFAULT NULL COMMENT '手机号',
`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_provider_appid_openid` (`provider`, `app_id`, `open_id`),
KEY `idx_user_provider` (`user_id`, `provider`),
KEY `idx_provider_unionid` (`provider`, `union_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='第三方用户OpenId映射表';