From 676c0b32322cb5691f2aa841ae3cc62f7ae29861 Mon Sep 17 00:00:00 2001 From: bootx Date: Sun, 16 Jun 2024 15:57:47 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=96=B0=E5=A2=9E=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=AE=9D=E8=8E=B7=E5=8F=96OpenId=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _doc/Task.md | 5 +- .../extra/AliPayAuthController.java | 43 ++++++ ...troller.java => WechatAuthController.java} | 24 ++-- .../param/assist/AliPayAccessTokenParam.java | 23 ++++ .../param/assist/AliPayAuthUrlParam.java | 28 ++++ .../single/param/assist/WxAuthUrlParam.java | 4 +- .../controller/PayCallbackController.java | 22 ++- .../controller/UniPayAssistController.java | 8 +- .../main/resources/static/h5/alipayAuth.html | 21 +++ .../static/h5/openIdCallbackClose.html | 4 + .../daxpay/single/service/code/AliPayWay.java | 2 +- .../service/core/extra/AliPayAuthService.java | 130 ++++++++++++++++++ ...nIdService.java => WeChatAuthService.java} | 22 +-- ...tAuthUrlResult.java => AuthUrlResult.java} | 2 +- ...hatOpenIdResult.java => OpenIdResult.java} | 2 +- 15 files changed, 301 insertions(+), 39 deletions(-) create mode 100644 daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/AliPayAuthController.java rename daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/{WechatOpenIdController.java => WechatAuthController.java} (59%) create mode 100644 daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAccessTokenParam.java create mode 100644 daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAuthUrlParam.java create mode 100644 daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/alipayAuth.html create mode 100644 daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/AliPayAuthService.java rename daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/{WeChatOpenIdService.java => WeChatAuthService.java} (87%) rename daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/{WeChatAuthUrlResult.java => AuthUrlResult.java} (94%) rename daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/{WeChatOpenIdResult.java => OpenIdResult.java} (94%) diff --git a/_doc/Task.md b/_doc/Task.md index ffe885e4b..7c1dd35fe 100644 --- a/_doc/Task.md +++ b/_doc/Task.md @@ -2,12 +2,13 @@ 2.0.8: 转账/撤销接口和系统优化 - [x] 支持撤销接口 - [x] 增加转账接口功能 +- [ ] 支付宝支持JSAPI方式支付 - [ ] 细分各种支付异常类和编码(部分) - [ ] 增加分账回调处理(支付宝) - [ ] 增加分账修复功能 -- [ ] 增加扫码获取微信OpenID和支付宝OpenId功能 +- [x] 增加扫码获取微信OpenID和支付宝OpenId功能 - [x] 微信 - - [ ] 支付宝 + - [x] 支付宝 - [ ] DEMO增加转账演示功能 - [x] 支付宝微信等消息通知地址支持一键生成 - [ ] 管理端界面支持扫码绑定对账接收方功能 diff --git a/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/AliPayAuthController.java b/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/AliPayAuthController.java new file mode 100644 index 000000000..12b5895a4 --- /dev/null +++ b/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/AliPayAuthController.java @@ -0,0 +1,43 @@ +package cn.daxpay.single.admin.controller.extra; + +import cn.bootx.platform.common.core.annotation.IgnoreAuth; +import cn.bootx.platform.common.core.rest.Res; +import cn.bootx.platform.common.core.rest.ResResult; +import cn.daxpay.single.service.core.extra.AliPayAuthService; +import cn.daxpay.single.service.dto.extra.AuthUrlResult; +import cn.daxpay.single.service.dto.extra.OpenIdResult; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 支付宝认证控制器 + * @author xxm + * @since 2024/6/16 + */ +@IgnoreAuth +@Tag(name = "支付宝认证控制器") +@RestController +@RequestMapping("/alipay/auth") +@RequiredArgsConstructor +public class AliPayAuthController { + private final AliPayAuthService aliPayAuthService; + + + @Operation(summary = "返回获取OpenId授权页面地址和标识码") + @PostMapping("/generateAuthUrl") + public ResResult generateAuthUrl(){ + return Res.ok(aliPayAuthService.generateAuthUrl()); + } + + + @Operation(summary = "根据标识码查询OpenId") + @GetMapping("/queryOpenId") + public ResResult queryOpenId(String code){ + return Res.ok(aliPayAuthService.queryOpenId(code)); + } +} diff --git a/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/WechatOpenIdController.java b/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/WechatAuthController.java similarity index 59% rename from daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/WechatOpenIdController.java rename to daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/WechatAuthController.java index 257e5aa2d..40793d87b 100644 --- a/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/WechatOpenIdController.java +++ b/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/WechatAuthController.java @@ -3,9 +3,9 @@ package cn.daxpay.single.admin.controller.extra; import cn.bootx.platform.common.core.annotation.IgnoreAuth; import cn.bootx.platform.common.core.rest.Res; import cn.bootx.platform.common.core.rest.ResResult; -import cn.daxpay.single.service.core.extra.WeChatOpenIdService; -import cn.daxpay.single.service.dto.extra.WeChatAuthUrlResult; -import cn.daxpay.single.service.dto.extra.WeChatOpenIdResult; +import cn.daxpay.single.service.core.extra.WeChatAuthService; +import cn.daxpay.single.service.dto.extra.AuthUrlResult; +import cn.daxpay.single.service.dto.extra.OpenIdResult; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; @@ -15,28 +15,28 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** - * + * 微信认证控制器 * @author xxm * @since 2024/6/15 */ @IgnoreAuth -@Tag(name = "微信OpenId服务类") +@Tag(name = "微信认证控制器") @RestController -@RequestMapping("/wechat/openId/") +@RequestMapping("/wechat/auth/") @RequiredArgsConstructor -public class WechatOpenIdController { - private final WeChatOpenIdService wechatOpenIdService; +public class WechatAuthController { + private final WeChatAuthService wechatAuthService; @Operation(summary = "返回获取OpenId授权页面地址和标识码") @PostMapping("/generateAuthUrl") - public ResResult generateAuthUrl(){ - return Res.ok(wechatOpenIdService.generateAuthUrl()); + public ResResult generateAuthUrl(){ + return Res.ok(wechatAuthService.generateAuthUrl()); } @Operation(summary = "根据标识码查询OpenId") @GetMapping("/queryOpenId") - public ResResult queryOpenId(String code){ - return Res.ok(wechatOpenIdService.queryOpenId(code)); + public ResResult queryOpenId(String code){ + return Res.ok(wechatAuthService.queryOpenId(code)); } } diff --git a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAccessTokenParam.java b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAccessTokenParam.java new file mode 100644 index 000000000..716b2bace --- /dev/null +++ b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAccessTokenParam.java @@ -0,0 +1,23 @@ +package cn.daxpay.single.param.assist; + +import cn.daxpay.single.param.PaymentCommonParam; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +/** + * 获取微信AccessToken参数 + * @author xxm + * @since 2024/6/16 + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Accessors(chain = true) +@Schema(title = "获取支付宝AccessToken参数") +public class AliPayAccessTokenParam extends PaymentCommonParam { + + @Schema(description = "支付宝认证code") + private String code; +} + diff --git a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAuthUrlParam.java b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAuthUrlParam.java new file mode 100644 index 000000000..dd3641e03 --- /dev/null +++ b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/AliPayAuthUrlParam.java @@ -0,0 +1,28 @@ +package cn.daxpay.single.param.assist; + +import cn.daxpay.single.param.PaymentCommonParam; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +/** + * 构造微信OAuth2授权的url链接 + * @author xxm + * @since 2024/6/16 + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Accessors(chain = true) +@Schema(title = "构造微信OAuth2授权的url链接") +public class AliPayAuthUrlParam extends PaymentCommonParam { + + @Schema(description = "回调地址") + private String url; + + /** + * 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 + */ + @Schema(description = "重定向后原样带回,可以为空") + private String state; +} diff --git a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAuthUrlParam.java b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAuthUrlParam.java index 08fe0cae9..90be1ee38 100644 --- a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAuthUrlParam.java +++ b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAuthUrlParam.java @@ -7,14 +7,14 @@ import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; /** - * 构造oauth2授权的url连接 + * 构造微信OAuth2授权的url链接 * @author xxm * @since 2024/2/10 */ @EqualsAndHashCode(callSuper = true) @Data @Accessors(chain = true) -@Schema(title = "构造oauth2授权的url连接") +@Schema(title = "构造微信OAuth2授权的url链接") public class WxAuthUrlParam extends PaymentCommonParam { @Schema(description = "回调地址") diff --git a/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/PayCallbackController.java b/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/PayCallbackController.java index f85a62261..8eecdc17a 100644 --- a/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/PayCallbackController.java +++ b/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/PayCallbackController.java @@ -4,7 +4,8 @@ import cn.bootx.platform.common.core.annotation.IgnoreAuth; import cn.daxpay.single.service.core.channel.alipay.service.AliPayCallbackService; import cn.daxpay.single.service.core.channel.union.service.UnionPayCallbackService; import cn.daxpay.single.service.core.channel.wechat.service.WeChatPayCallbackService; -import cn.daxpay.single.service.core.extra.WeChatOpenIdService; +import cn.daxpay.single.service.core.extra.AliPayAuthService; +import cn.daxpay.single.service.core.extra.WeChatAuthService; import cn.daxpay.single.service.sdk.union.api.UnionPayKit; import com.egzosn.pay.union.api.UnionPayConfigStorage; import com.ijpay.alipay.AliPayApi; @@ -37,12 +38,14 @@ public class PayCallbackController { private final AliPayCallbackService aliPayCallbackService; - private final WeChatOpenIdService wechatOpenIdService; + private final WeChatAuthService wechatAuthService; private final WeChatPayCallbackService weChatPayCallbackService; private final UnionPayCallbackService unionPayCallbackService; + private final AliPayAuthService aliPayAuthService; + @SneakyThrows @Operation(summary = "支付宝信息回调") @PostMapping("/alipay") @@ -51,6 +54,15 @@ public class PayCallbackController { return aliPayCallbackService.callback(stringStringMap); } + + @Operation(summary = "支付宝认证授权回调") + @GetMapping("/alipay/auth/{code}") + public ModelAndView wechatCallback(@RequestParam("code") String authCode, @PathVariable("code") String code){ + aliPayAuthService.authCallback(authCode, code); + // 调用页面自动关闭 + return new ModelAndView("forward:/h5/openIdCallbackClose.html"); + } + @SneakyThrows @Operation(summary = "微信支付信息回调") @PostMapping("/wechat") @@ -60,10 +72,10 @@ public class PayCallbackController { return weChatPayCallbackService.callback(params); } - @Operation(summary = "微信获取OpenId授权回调方法") - @GetMapping("/wechat/openId/{code}") + @Operation(summary = "微信宝认证授权回调") + @GetMapping("/wechat/auth/{code}") public ModelAndView wxAuthCallback(@RequestParam("code") String authCode, @PathVariable("code") String code){ - wechatOpenIdService.authCallback(authCode, code); + wechatAuthService.authCallback(authCode, code); // 调用页面自动关闭 return new ModelAndView("forward:/h5/openIdCallbackClose.html"); } diff --git a/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/UniPayAssistController.java b/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/UniPayAssistController.java index 50a863480..b0997f5f1 100644 --- a/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/UniPayAssistController.java +++ b/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/UniPayAssistController.java @@ -11,7 +11,7 @@ import cn.daxpay.single.result.assist.WxAccessTokenResult; import cn.daxpay.single.result.assist.WxAuthUrlResult; import cn.daxpay.single.service.annotation.InitPaymentContext; import cn.daxpay.single.service.annotation.PaymentVerify; -import cn.daxpay.single.service.core.extra.WeChatOpenIdService; +import cn.daxpay.single.service.core.extra.WeChatAuthService; import cn.daxpay.single.util.DaxRes; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -32,14 +32,14 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/unipay/assist") @RequiredArgsConstructor public class UniPayAssistController { - private final WeChatOpenIdService wechatOpenIdService; + private final WeChatAuthService wechatAuthService; @PaymentVerify @InitPaymentContext(PaymentApiCode.GET_WX_AUTH_URL) @Operation(summary = "获取微信OAuth2授权链接") @PostMapping("/getWxAuthUrl") public DaxResult getWxAuthUrl(@RequestBody WxAuthUrlParam param){ - return DaxRes.ok(wechatOpenIdService.getWxAuthUrl(param)); + return DaxRes.ok(wechatAuthService.getWxAuthUrl(param)); } @PaymentVerify @@ -47,7 +47,7 @@ public class UniPayAssistController { @Operation(summary = "获取微信AccessToken") @PostMapping("/getWxAccessToken") public ResResult getWxAccessToken(@RequestBody WxAccessTokenParam param){ - return Res.ok(wechatOpenIdService.getWxAccessToken(param)); + return Res.ok(wechatAuthService.getWxAccessToken(param)); } } diff --git a/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/alipayAuth.html b/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/alipayAuth.html new file mode 100644 index 000000000..a2c50a879 --- /dev/null +++ b/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/alipayAuth.html @@ -0,0 +1,21 @@ + + diff --git a/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/openIdCallbackClose.html b/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/openIdCallbackClose.html index f362818cf..bdd98b0b4 100644 --- a/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/openIdCallbackClose.html +++ b/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/openIdCallbackClose.html @@ -19,5 +19,9 @@ setTimeout(function (){ WeixinJSBridge.call('closeWindow') },500) + // 支付宝浏览器关闭 + setTimeout(function (){ + AlipayJSBridge.call('closeWebview') + },500) diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/code/AliPayWay.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/code/AliPayWay.java index 2b089cfc2..efeac51d2 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/code/AliPayWay.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/code/AliPayWay.java @@ -19,7 +19,7 @@ public class AliPayWay { // 支付方式 private static final List PAY_WAYS = Arrays.asList(PayMethodEnum.WAP, PayMethodEnum.APP, PayMethodEnum.WEB, - PayMethodEnum.QRCODE, PayMethodEnum.BARCODE); + PayMethodEnum.QRCODE, PayMethodEnum.BARCODE, PayMethodEnum.JSAPI); /** * 根据编码获取 diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/AliPayAuthService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/AliPayAuthService.java new file mode 100644 index 000000000..9ca341d48 --- /dev/null +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/AliPayAuthService.java @@ -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; + } + +} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/WeChatOpenIdService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/WeChatAuthService.java similarity index 87% rename from daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/WeChatOpenIdService.java rename to daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/WeChatAuthService.java index 078e85c13..4a05c210a 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/WeChatOpenIdService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/WeChatAuthService.java @@ -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"); } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatAuthUrlResult.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/AuthUrlResult.java similarity index 94% rename from daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatAuthUrlResult.java rename to daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/AuthUrlResult.java index 047b0e6b7..aa967689e 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatAuthUrlResult.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/AuthUrlResult.java @@ -12,7 +12,7 @@ import lombok.experimental.Accessors; @Data @Accessors(chain = true) @Schema(title = "微信授权链接和查询标识返回类") -public class WeChatAuthUrlResult { +public class AuthUrlResult { /** 授权访问链接 */ @Schema(description = "授权访问链接") diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatOpenIdResult.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/OpenIdResult.java similarity index 94% rename from daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatOpenIdResult.java rename to daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/OpenIdResult.java index 5fc3dcc8d..2917608de 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatOpenIdResult.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/OpenIdResult.java @@ -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;