mirror of
https://gitee.com/dromara/dax-pay.git
synced 2025-09-06 04:27:55 +00:00
feat 新增支付宝获取OpenId相关接口
This commit is contained in:
@@ -19,7 +19,7 @@ public class AliPayWay {
|
||||
|
||||
// 支付方式
|
||||
private static final List<PayMethodEnum> PAY_WAYS = Arrays.asList(PayMethodEnum.WAP, PayMethodEnum.APP, PayMethodEnum.WEB,
|
||||
PayMethodEnum.QRCODE, PayMethodEnum.BARCODE);
|
||||
PayMethodEnum.QRCODE, PayMethodEnum.BARCODE, PayMethodEnum.JSAPI);
|
||||
|
||||
/**
|
||||
* 根据编码获取
|
||||
|
@@ -0,0 +1,130 @@
|
||||
package cn.daxpay.single.service.core.extra;
|
||||
|
||||
import cn.bootx.platform.common.core.exception.BizException;
|
||||
import cn.bootx.platform.common.redis.RedisClient;
|
||||
import cn.daxpay.single.service.core.channel.alipay.entity.AliPayConfig;
|
||||
import cn.daxpay.single.service.core.channel.alipay.service.AliPayConfigService;
|
||||
import cn.daxpay.single.service.core.system.config.entity.PlatformConfig;
|
||||
import cn.daxpay.single.service.core.system.config.service.PlatformConfigService;
|
||||
import cn.daxpay.single.service.dto.extra.AuthUrlResult;
|
||||
import cn.daxpay.single.service.dto.extra.OpenIdResult;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.AlipayConfig;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
|
||||
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 支付宝认证服务类
|
||||
* @author xxm
|
||||
* @since 2024/6/16
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AliPayAuthService {
|
||||
|
||||
public static final String OPEN_ID_KEY_PREFIX = "daxpay:wechat:openId:";
|
||||
|
||||
private final AliPayConfigService aliPayConfigService;
|
||||
|
||||
private final PlatformConfigService platformsConfigService;
|
||||
|
||||
private final RedisClient redisClient;
|
||||
|
||||
/**
|
||||
* 根据传入的标识码code生成一个用于微信授权页面的链接
|
||||
*/
|
||||
public AuthUrlResult generateAuthUrl() {
|
||||
PlatformConfig platformConfig = platformsConfigService.getConfig();
|
||||
AliPayConfig aliPayConfig = aliPayConfigService.getConfig();
|
||||
String code = RandomUtil.randomString(10);
|
||||
// 构建出授权成功后重定向页面链接
|
||||
String redirectUrl = StrUtil.format("{}/callback/pay/alipay/auth/{}", platformConfig.getWebsiteUrl(), code);
|
||||
// 构造出授权页地址
|
||||
String url = StrUtil.format("{}/h5/alipayAuth.html?appId={}&redirectUrl={}",
|
||||
platformConfig.getWebsiteUrl(), aliPayConfig.getAppId(), redirectUrl);
|
||||
// 写入Redis, 五分钟有效期
|
||||
redisClient.setWithTimeout(OPEN_ID_KEY_PREFIX + code, "", 5*60*1000L);
|
||||
return new AuthUrlResult()
|
||||
.setCode(code)
|
||||
.setAuthUrl(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信授权回调页面, 通过获取到authCode获取到OpenId, 存到到Redis中对应code关联的键值对中
|
||||
* @param authCode 微信返回的授权码
|
||||
* @param code 标识码
|
||||
*/
|
||||
public void authCallback(String authCode, String code) {
|
||||
// 获取OpenId
|
||||
String openId = this.getOpenId(authCode);
|
||||
// 写入Redis
|
||||
redisClient.setWithTimeout(OPEN_ID_KEY_PREFIX + code, openId, 60*1000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过标识码轮训获取OpenId
|
||||
*/
|
||||
public OpenIdResult queryOpenId(String code) {
|
||||
// 从redis中获取
|
||||
String openId = redisClient.get(OPEN_ID_KEY_PREFIX + code);
|
||||
// 不为空存在
|
||||
if (StrUtil.isNotBlank(openId)){
|
||||
return new OpenIdResult().setOpenId(openId).setStatus("success");
|
||||
}
|
||||
// 为空获取中
|
||||
if (Objects.equals(openId, "")){
|
||||
return new OpenIdResult().setStatus("pending");
|
||||
}
|
||||
// null不存在
|
||||
return new OpenIdResult().setStatus("fail");
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public String getOpenId(String authCode) {
|
||||
// 初始化SDK
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(this.getAlipayConfig());
|
||||
// 构造请求参数以调用接口
|
||||
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
||||
// 设置授权码
|
||||
request.setCode(authCode);
|
||||
// 设置授权方式
|
||||
request.setGrantType("authorization_code");
|
||||
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
|
||||
if (!response.isSuccess()) {
|
||||
log.warn("获取支付宝OpenId失败,原因:{}", response.getSubMsg());
|
||||
throw new BizException("获取支付宝OpenId失败");
|
||||
}
|
||||
|
||||
return response.getOpenId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支付宝SDK的配置
|
||||
*/
|
||||
private AlipayConfig getAlipayConfig(){
|
||||
AliPayConfig aliPayConfig = aliPayConfigService.getConfig();
|
||||
String privateKey = aliPayConfig.getPrivateKey();
|
||||
String alipayPublicKey =aliPayConfig.getAlipayPublicKey();
|
||||
AlipayConfig alipayConfig = new AlipayConfig();
|
||||
alipayConfig.setServerUrl(aliPayConfig.getServerUrl());
|
||||
alipayConfig.setAppId(aliPayConfig.getAppId());
|
||||
alipayConfig.setPrivateKey(privateKey);
|
||||
alipayConfig.setFormat("json");
|
||||
alipayConfig.setAlipayPublicKey(alipayPublicKey);
|
||||
alipayConfig.setCharset("UTF-8");
|
||||
alipayConfig.setSignType(aliPayConfig.getSignType());
|
||||
return alipayConfig;
|
||||
}
|
||||
|
||||
}
|
@@ -9,8 +9,8 @@ import cn.daxpay.single.service.core.channel.wechat.entity.WeChatPayConfig;
|
||||
import cn.daxpay.single.service.core.channel.wechat.service.WeChatPayConfigService;
|
||||
import cn.daxpay.single.service.core.system.config.entity.PlatformConfig;
|
||||
import cn.daxpay.single.service.core.system.config.service.PlatformConfigService;
|
||||
import cn.daxpay.single.service.dto.extra.WeChatAuthUrlResult;
|
||||
import cn.daxpay.single.service.dto.extra.WeChatOpenIdResult;
|
||||
import cn.daxpay.single.service.dto.extra.AuthUrlResult;
|
||||
import cn.daxpay.single.service.dto.extra.OpenIdResult;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -33,7 +33,7 @@ import java.util.Objects;
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class WeChatOpenIdService {
|
||||
public class WeChatAuthService {
|
||||
|
||||
public static final String OPEN_ID_KEY_PREFIX = "daxpay:wechat:openId:";
|
||||
|
||||
@@ -66,18 +66,18 @@ public class WeChatOpenIdService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据传入的标识码code生成一个用于微信授权页面的链接
|
||||
* 生成一个用于微信授权页面的链接和code标识
|
||||
*/
|
||||
public WeChatAuthUrlResult generateAuthUrl() {
|
||||
public AuthUrlResult generateAuthUrl() {
|
||||
PlatformConfig config = platformsConfigService.getConfig();
|
||||
String code = RandomUtil.randomString(10);
|
||||
// 构建出授权后重定向页面链接
|
||||
String redirectUrl = StrUtil.format("{}/callback/pay/wechat/openId/{}", config.getWebsiteUrl(), code);
|
||||
String redirectUrl = StrUtil.format("{}/callback/pay/wechat/auth/{}", config.getWebsiteUrl(), code);
|
||||
WxAuthUrlResult result = this.getWxAuthUrl(new WxAuthUrlParam().setUrl(redirectUrl));
|
||||
|
||||
// 写入Redis, 五分钟有效期
|
||||
redisClient.setWithTimeout(OPEN_ID_KEY_PREFIX + code, "", 5*60*1000L);
|
||||
return new WeChatAuthUrlResult()
|
||||
return new AuthUrlResult()
|
||||
.setCode(code)
|
||||
.setAuthUrl(result.getUrl());
|
||||
}
|
||||
@@ -99,19 +99,19 @@ public class WeChatOpenIdService {
|
||||
/**
|
||||
* 通过标识码轮训获取OpenId
|
||||
*/
|
||||
public WeChatOpenIdResult queryOpenId(String code) {
|
||||
public OpenIdResult queryOpenId(String code) {
|
||||
// 从redis中获取
|
||||
String openId = redisClient.get(OPEN_ID_KEY_PREFIX + code);
|
||||
// 不为空存在
|
||||
if (StrUtil.isNotBlank(openId)){
|
||||
return new WeChatOpenIdResult().setOpenId(openId).setStatus("success");
|
||||
return new OpenIdResult().setOpenId(openId).setStatus("success");
|
||||
}
|
||||
// 为空获取中
|
||||
if (Objects.equals(openId, "")){
|
||||
return new WeChatOpenIdResult().setStatus("pending");
|
||||
return new OpenIdResult().setStatus("pending");
|
||||
}
|
||||
// null不存在
|
||||
return new WeChatOpenIdResult().setStatus("fail");
|
||||
return new OpenIdResult().setStatus("fail");
|
||||
}
|
||||
|
||||
|
@@ -12,7 +12,7 @@ import lombok.experimental.Accessors;
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@Schema(title = "微信授权链接和查询标识返回类")
|
||||
public class WeChatAuthUrlResult {
|
||||
public class AuthUrlResult {
|
||||
|
||||
/** 授权访问链接 */
|
||||
@Schema(description = "授权访问链接")
|
@@ -12,7 +12,7 @@ import lombok.experimental.Accessors;
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@Schema(title = "微信OpenId查询结果")
|
||||
public class WeChatOpenIdResult {
|
||||
public class OpenIdResult {
|
||||
|
||||
@Schema(description = "OpenId")
|
||||
private String openId;
|
Reference in New Issue
Block a user