diff --git a/_doc/Task.md b/_doc/Task.md index a717582f2..ffe885e4b 100644 --- a/_doc/Task.md +++ b/_doc/Task.md @@ -5,10 +5,14 @@ - [ ] 细分各种支付异常类和编码(部分) - [ ] 增加分账回调处理(支付宝) - [ ] 增加分账修复功能 -- [ ] DEMO增加获取微信OpenID和支付宝OpenId功能 +- [ ] 增加扫码获取微信OpenID和支付宝OpenId功能 + - [x] 微信 + - [ ] 支付宝 - [ ] DEMO增加转账演示功能 - [x] 支付宝微信等消息通知地址支持一键生成 - [ ] 管理端界面支持扫码绑定对账接收方功能 + - [x] 微信 + - [ ] 支付宝 - [x] 界面金额统一显示为分 - [ ] 上下文对象进行优化精简 - [x] 请求IP参数增加正则校验 diff --git a/daxpay-single-demo/src/main/java/cn/daxpay/single/demo/controller/CashierController.java b/daxpay-single-demo/src/main/java/cn/daxpay/single/demo/controller/CashierController.java index c64e49f05..348e13d2d 100644 --- a/daxpay-single-demo/src/main/java/cn/daxpay/single/demo/controller/CashierController.java +++ b/daxpay-single-demo/src/main/java/cn/daxpay/single/demo/controller/CashierController.java @@ -52,13 +52,13 @@ public class CashierController { return Res.ok(cashierService.getUniCashierUrl()); } - @Operation(summary = "获取微信授权链接") + @Operation(summary = "获取微信授权链接, 用于获取OpenId来发起JsApi支付") @GetMapping("getWxAuthUrl") public ResResult getWxAuthUrl(){ return Res.ok(cashierService.getWxAuthUrl()); } - @Operation(summary = "微信授权回调页面") + @Operation(summary = "微信授权回调接收, 然后携带openid重定向到统一收银台中") @GetMapping("/wxAuthCallback") public ModelAndView wxAuthCallback(@RequestParam("code") String code){ return new ModelAndView("redirect:" + cashierService.wxAuthCallback(code)); diff --git a/daxpay-single-sdk/src/main/java/cn/daxpay/single/sdk/model/assist/WxAccessTokenModel.java b/daxpay-single-sdk/src/main/java/cn/daxpay/single/sdk/model/assist/WxAccessTokenModel.java index bc4d13848..b3fdea8f2 100644 --- a/daxpay-single-sdk/src/main/java/cn/daxpay/single/sdk/model/assist/WxAccessTokenModel.java +++ b/daxpay-single-sdk/src/main/java/cn/daxpay/single/sdk/model/assist/WxAccessTokenModel.java @@ -15,7 +15,7 @@ import lombok.ToString; @ToString(callSuper = true) public class WxAccessTokenModel extends DaxPayResponseModel { - /** 微信AccessToken */ + /** 微信AccessToken, 目前无返回 */ private String accessToken; /** 微信用户唯一标识 */ diff --git a/daxpay-single-start/src/main/resources/application-dev.yml b/daxpay-single-start/src/main/resources/application-dev.yml index 0ac702fdc..098f2d96c 100644 --- a/daxpay-single-start/src/main/resources/application-dev.yml +++ b/daxpay-single-start/src/main/resources/application-dev.yml @@ -33,8 +33,6 @@ spring: web: resources: static-locations: classpath:/static/,classpath:/public/ - thymeleaf: - cache: false # 定时任务 quartz: # 持久化方式 diff --git a/daxpay-single-start/src/main/resources/application-prod.yml b/daxpay-single-start/src/main/resources/application-prod.yml index 0330a9095..281bd89f9 100644 --- a/daxpay-single-start/src/main/resources/application-prod.yml +++ b/daxpay-single-start/src/main/resources/application-prod.yml @@ -33,8 +33,6 @@ spring: web: resources: static-locations: classpath:/static/,classpath:/public/ - thymeleaf: - cache: false # 定时任务 quartz: # 持久化方式 diff --git a/daxpay-single-start/src/main/resources/application.yml b/daxpay-single-start/src/main/resources/application.yml index a50cda5d0..4517e934a 100644 --- a/daxpay-single-start/src/main/resources/application.yml +++ b/daxpay-single-start/src/main/resources/application.yml @@ -12,8 +12,6 @@ spring: max-file-size: 100MB max-request-size: 100MB enabled: true - thymeleaf: - mode: LEGACYHTML5 # mp配置 mybatis-plus: mapper-locations: classpath*:mapper/**/*Mapper.xml 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/WechatOpenIdController.java new file mode 100644 index 000000000..257e5aa2d --- /dev/null +++ b/daxpay-single/daxpay-single-admin/src/main/java/cn/daxpay/single/admin/controller/extra/WechatOpenIdController.java @@ -0,0 +1,42 @@ +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 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/15 + */ +@IgnoreAuth +@Tag(name = "微信OpenId服务类") +@RestController +@RequestMapping("/wechat/openId/") +@RequiredArgsConstructor +public class WechatOpenIdController { + private final WeChatOpenIdService wechatOpenIdService; + + @Operation(summary = "返回获取OpenId授权页面地址和标识码") + @PostMapping("/generateAuthUrl") + public ResResult generateAuthUrl(){ + return Res.ok(wechatOpenIdService.generateAuthUrl()); + } + + + @Operation(summary = "根据标识码查询OpenId") + @GetMapping("/queryOpenId") + public ResResult queryOpenId(String code){ + return Res.ok(wechatOpenIdService.queryOpenId(code)); + } +} diff --git a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAccessTokenParam.java b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAccessTokenParam.java index da638d1de..de85275c1 100644 --- a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAccessTokenParam.java +++ b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/assist/WxAccessTokenParam.java @@ -17,6 +17,6 @@ import lombok.experimental.Accessors; @Schema(title = "获取微信AccessToken参数") public class WxAccessTokenParam extends PaymentCommonParam { - @Schema(description = "微信code") + @Schema(description = "微信认证code") private String code; } diff --git a/daxpay-single/daxpay-single-gateway/pom.xml b/daxpay-single/daxpay-single-gateway/pom.xml index 96f1ab411..00a48dba6 100644 --- a/daxpay-single/daxpay-single-gateway/pom.xml +++ b/daxpay-single/daxpay-single-gateway/pom.xml @@ -18,5 +18,4 @@ ${daxpay.version} - diff --git a/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/ForwardH5Controller.java b/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/ForwardH5Controller.java deleted file mode 100644 index 9a35e62b9..000000000 --- a/daxpay-single/daxpay-single-gateway/src/main/java/cn/daxpay/single/gateway/controller/ForwardH5Controller.java +++ /dev/null @@ -1,23 +0,0 @@ -package cn.daxpay.single.gateway.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -/** - * 嵌入式h5项目转发控制器, 不用输入 index.html也可以正常访问 - * @author xxm - * @since 2024/2/10 - */ -@Controller -@RequestMapping -public class ForwardH5Controller { - /** - * 将/h5/*的访问请求代理到/h5/index.html* - */ - @GetMapping("/h5/") - public String toH5(){ - return "forward:/h5/index.html"; - } - -} 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 f99384ab9..f85a62261 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,6 +4,7 @@ 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.sdk.union.api.UnionPayKit; import com.egzosn.pay.union.api.UnionPayConfigStorage; import com.ijpay.alipay.AliPayApi; @@ -14,15 +15,15 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.Map; /** * 包括支付成功/退款/转账等一系列类型的回调处理 + * 也包括获取微信OpenId/支付宝UserId等的授权回调 * @author xxm * @since 2021/2/27 */ @@ -36,6 +37,8 @@ public class PayCallbackController { private final AliPayCallbackService aliPayCallbackService; + private final WeChatOpenIdService wechatOpenIdService; + private final WeChatPayCallbackService weChatPayCallbackService; private final UnionPayCallbackService unionPayCallbackService; @@ -57,6 +60,14 @@ public class PayCallbackController { return weChatPayCallbackService.callback(params); } + @Operation(summary = "微信获取OpenId授权回调方法") + @GetMapping("/wechat/openId/{code}") + public ModelAndView wxAuthCallback(@RequestParam("code") String authCode, @PathVariable("code") String code){ + wechatOpenIdService.authCallback(authCode, code); + // 调用页面自动关闭 + return new ModelAndView("forward:/h5/openIdCallbackClose.html"); + } + @SuppressWarnings({"unchecked", "rawtypes"}) @SneakyThrows @Operation(summary = "云闪付支付信息回调") 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 2f652b9b6..50a863480 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.payment.assist.service.UniPayAssistService; +import cn.daxpay.single.service.core.extra.WeChatOpenIdService; 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 UniPayAssistService uniPayAssistService; + private final WeChatOpenIdService wechatOpenIdService; @PaymentVerify @InitPaymentContext(PaymentApiCode.GET_WX_AUTH_URL) @Operation(summary = "获取微信OAuth2授权链接") @PostMapping("/getWxAuthUrl") public DaxResult getWxAuthUrl(@RequestBody WxAuthUrlParam param){ - return DaxRes.ok(uniPayAssistService.getWxAuthUrl(param)); + return DaxRes.ok(wechatOpenIdService.getWxAuthUrl(param)); } @PaymentVerify @@ -47,7 +47,7 @@ public class UniPayAssistController { @Operation(summary = "获取微信AccessToken") @PostMapping("/getWxAccessToken") public ResResult getWxAccessToken(@RequestBody WxAccessTokenParam param){ - return Res.ok(uniPayAssistService.getWxAccessToken(param)); + return Res.ok(wechatOpenIdService.getWxAccessToken(param)); } } diff --git a/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/.gitkeep b/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/.gitkeep deleted file mode 100644 index e69de29bb..000000000 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 new file mode 100644 index 000000000..f362818cf --- /dev/null +++ b/daxpay-single/daxpay-single-gateway/src/main/resources/static/h5/openIdCallbackClose.html @@ -0,0 +1,23 @@ + + + + + 获取成功 + +获取成功... + + + diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WechatPayReconcileService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WeChatPayReconcileService.java similarity index 99% rename from daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WechatPayReconcileService.java rename to daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WeChatPayReconcileService.java index d29545968..359f948da 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WechatPayReconcileService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WeChatPayReconcileService.java @@ -1,7 +1,6 @@ package cn.daxpay.single.service.core.channel.wechat.service; import cn.bootx.platform.common.core.util.LocalDateTimeUtil; -import cn.daxpay.single.code.PayChannelEnum; import cn.daxpay.single.code.ReconcileTradeEnum; import cn.daxpay.single.exception.pay.PayFailureException; import cn.daxpay.single.service.code.ReconcileFileTypeEnum; @@ -60,7 +59,7 @@ import static cn.daxpay.single.service.code.WeChatPayCode.ACCOUNT_TYPE_BASIC; @Slf4j @Service @RequiredArgsConstructor -public class WechatPayReconcileService{ +public class WeChatPayReconcileService { private final WxReconcileBillTotalManger reconcileBillTotalManger; private final WxReconcileBillDetailManager reconcileBillDetailManager; private final FileStorageService fileStorageService; diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WechatPayRefundService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WeChatPayRefundService.java similarity index 99% rename from daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WechatPayRefundService.java rename to daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WeChatPayRefundService.java index e4b289211..44bbd4bec 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WechatPayRefundService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/channel/wechat/service/WeChatPayRefundService.java @@ -31,7 +31,7 @@ import static cn.daxpay.single.service.code.WeChatPayCode.*; @Slf4j @Service @RequiredArgsConstructor -public class WechatPayRefundService { +public class WeChatPayRefundService { /** * 退款方法 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/WeChatOpenIdService.java new file mode 100644 index 000000000..078e85c13 --- /dev/null +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/extra/WeChatOpenIdService.java @@ -0,0 +1,130 @@ +package cn.daxpay.single.service.core.extra; + +import cn.bootx.platform.common.redis.RedisClient; +import cn.daxpay.single.param.assist.WxAccessTokenParam; +import cn.daxpay.single.param.assist.WxAuthUrlParam; +import cn.daxpay.single.result.assist.WxAccessTokenResult; +import cn.daxpay.single.result.assist.WxAuthUrlResult; +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.hutool.core.util.RandomUtil; +import cn.hutool.core.util.StrUtil; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; +import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; +import org.springframework.stereotype.Service; + +import java.util.Objects; + +/** + * 支付支撑服务类 + * @author xxm + * @since 2024/2/10 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class WeChatOpenIdService { + + public static final String OPEN_ID_KEY_PREFIX = "daxpay:wechat:openId:"; + + private final WeChatPayConfigService weChatPayConfigService; + + private final PlatformConfigService platformsConfigService; + + private final RedisClient redisClient; + + /** + * 构建微信oauth2授权的url连接 + */ + public WxAuthUrlResult getWxAuthUrl(WxAuthUrlParam param) { + WxMpService wxMpService = this.getWxMpService(); + String url = wxMpService.getOAuth2Service() + .buildAuthorizationUrl(param.getUrl(), WxConsts.OAuth2Scope.SNSAPI_BASE, param.getState()); + return new WxAuthUrlResult().setUrl(url); + } + + /** + * 获取微信AccessToken数据 + */ + @SneakyThrows + public WxAccessTokenResult getWxAccessToken(WxAccessTokenParam wxAccessToken){ + WxMpService wxMpService = this.getWxMpService(); + WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(wxAccessToken.getCode()); + return new WxAccessTokenResult() + .setAccessToken(accessToken.getAccessToken()) + .setOpenId(accessToken.getOpenId()); + } + + /** + * 根据传入的标识码code生成一个用于微信授权页面的链接 + */ + public WeChatAuthUrlResult generateAuthUrl() { + PlatformConfig config = platformsConfigService.getConfig(); + String code = RandomUtil.randomString(10); + // 构建出授权后重定向页面链接 + String redirectUrl = StrUtil.format("{}/callback/pay/wechat/openId/{}", 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() + .setCode(code) + .setAuthUrl(result.getUrl()); + } + + /** + * 微信授权回调页面, 通过获取到authCode获取到OpenId, 存到到Redis中对应code关联的键值对中 + * @param authCode 微信返回的授权码 + * @param code 标识码 + */ + public void authCallback(String authCode, String code) { + // 获取OpenId + WxAccessTokenParam param = new WxAccessTokenParam(); + param.setCode(authCode); + WxAccessTokenResult wxAccessToken = this.getWxAccessToken(new WxAccessTokenParam().setCode(authCode)); + // 写入Redis + redisClient.setWithTimeout(OPEN_ID_KEY_PREFIX + code, wxAccessToken.getOpenId(), 60*1000L); + } + + /** + * 通过标识码轮训获取OpenId + */ + public WeChatOpenIdResult queryOpenId(String code) { + // 从redis中获取 + String openId = redisClient.get(OPEN_ID_KEY_PREFIX + code); + // 不为空存在 + if (StrUtil.isNotBlank(openId)){ + return new WeChatOpenIdResult().setOpenId(openId).setStatus("success"); + } + // 为空获取中 + if (Objects.equals(openId, "")){ + return new WeChatOpenIdResult().setStatus("pending"); + } + // null不存在 + return new WeChatOpenIdResult().setStatus("fail"); + } + + + /** + * 获取微信公众号API的Service + */ + private WxMpService getWxMpService() { + WeChatPayConfig config = weChatPayConfigService.getConfig(); + WxMpService wxMpService = new WxMpServiceImpl(); + WxMpDefaultConfigImpl wxMpConfig = new WxMpDefaultConfigImpl(); + wxMpConfig.setAppId(config.getWxAppId()); // 设置微信公众号的appid + wxMpConfig.setSecret(config.getAppSecret()); // 设置微信公众号的app corpSecret + wxMpService.setWxMpConfigStorage(wxMpConfig); + return wxMpService; + } +} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/assist/service/UniPayAssistService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/assist/service/UniPayAssistService.java deleted file mode 100644 index 95b29ced3..000000000 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/assist/service/UniPayAssistService.java +++ /dev/null @@ -1,64 +0,0 @@ -package cn.daxpay.single.service.core.payment.assist.service; - -import cn.daxpay.single.param.assist.WxAccessTokenParam; -import cn.daxpay.single.param.assist.WxAuthUrlParam; -import cn.daxpay.single.result.assist.WxAccessTokenResult; -import cn.daxpay.single.result.assist.WxAuthUrlResult; -import cn.daxpay.single.service.core.channel.wechat.entity.WeChatPayConfig; -import cn.daxpay.single.service.core.channel.wechat.service.WeChatPayConfigService; -import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; -import me.chanjar.weixin.common.api.WxConsts; -import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; -import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; -import org.springframework.stereotype.Service; - -/** - * 支付支撑服务类 - * @author xxm - * @since 2024/2/10 - */ -@Slf4j -@Service -@RequiredArgsConstructor -public class UniPayAssistService { - private final WeChatPayConfigService weChatPayConfigService; - - /** - * 构建微信oauth2授权的url连接 - */ - public WxAuthUrlResult getWxAuthUrl(WxAuthUrlParam param) { - WxMpService wxMpService = this.getWxMpService(); - String url = wxMpService.getOAuth2Service() - .buildAuthorizationUrl(param.getUrl(), WxConsts.OAuth2Scope.SNSAPI_BASE, param.getState()); - return new WxAuthUrlResult().setUrl(url); - } - - /** - * 获取微信AccessToken数据 - */ - @SneakyThrows - public WxAccessTokenResult getWxAccessToken(WxAccessTokenParam wxAccessToken){ - WxMpService wxMpService = this.getWxMpService(); - WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(wxAccessToken.getCode()); - return new WxAccessTokenResult() - .setAccessToken(accessToken.getAccessToken()) - .setOpenId(accessToken.getOpenId()); - } - - /** - * 获取微信公众号API的Service - */ - private WxMpService getWxMpService() { - WeChatPayConfig config = weChatPayConfigService.getConfig(); - WxMpService wxMpService = new WxMpServiceImpl(); - WxMpDefaultConfigImpl wxMpConfig = new WxMpDefaultConfigImpl(); - wxMpConfig.setAppId(config.getWxAppId()); // 设置微信公众号的appid - wxMpConfig.setSecret(config.getAppSecret()); // 设置微信公众号的app corpSecret - wxMpService.setWxMpConfigStorage(wxMpConfig); - return wxMpService; - } -} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/reconcile/strategy/WechatPayReconcileStrategy.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/reconcile/strategy/WechatPayReconcileStrategy.java index 7044206ca..a3746983b 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/reconcile/strategy/WechatPayReconcileStrategy.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/reconcile/strategy/WechatPayReconcileStrategy.java @@ -5,7 +5,7 @@ import cn.daxpay.single.code.PayChannelEnum; import cn.daxpay.single.service.code.ReconcileFileTypeEnum; 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.channel.wechat.service.WechatPayReconcileService; +import cn.daxpay.single.service.core.channel.wechat.service.WeChatPayReconcileService; import cn.daxpay.single.service.func.AbsReconcileStrategy; import cn.hutool.core.date.DatePattern; import lombok.RequiredArgsConstructor; @@ -28,7 +28,7 @@ import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROT @RequiredArgsConstructor public class WechatPayReconcileStrategy extends AbsReconcileStrategy { - private final WechatPayReconcileService reconcileService; + private final WeChatPayReconcileService reconcileService; private final WeChatPayConfigService weChatPayConfigService; diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/refund/strategy/WeChatRefundStrategy.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/refund/strategy/WeChatRefundStrategy.java index d34a9a9d5..e9891ba54 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/refund/strategy/WeChatRefundStrategy.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/refund/strategy/WeChatRefundStrategy.java @@ -3,7 +3,7 @@ package cn.daxpay.single.service.core.payment.refund.strategy; import cn.daxpay.single.code.PayChannelEnum; 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.channel.wechat.service.WechatPayRefundService; +import cn.daxpay.single.service.core.channel.wechat.service.WeChatPayRefundService; import cn.daxpay.single.service.func.AbsRefundStrategy; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Scope; @@ -23,7 +23,7 @@ public class WeChatRefundStrategy extends AbsRefundStrategy { private final WeChatPayConfigService weChatPayConfigService; - private final WechatPayRefundService wechatPayRefundService; + private final WeChatPayRefundService wechatPayRefundService; private WeChatPayConfig weChatPayConfig; 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/WeChatAuthUrlResult.java new file mode 100644 index 000000000..047b0e6b7 --- /dev/null +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatAuthUrlResult.java @@ -0,0 +1,25 @@ +package cn.daxpay.single.service.dto.extra; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 微信获取OpenId授权链接和查询标识返回类 + * @author xxm + * @since 2024/6/15 + */ +@Data +@Accessors(chain = true) +@Schema(title = "微信授权链接和查询标识返回类") +public class WeChatAuthUrlResult { + + /** 授权访问链接 */ + @Schema(description = "授权访问链接") + private String authUrl; + + /** 标识码, 用于查询是否获取到了OpenId */ + @Schema(description = "标识码") + private String code; + +} 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/WeChatOpenIdResult.java new file mode 100644 index 000000000..5fc3dcc8d --- /dev/null +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/dto/extra/WeChatOpenIdResult.java @@ -0,0 +1,28 @@ +package cn.daxpay.single.service.dto.extra; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 微信OpenId查询结果 + * @author xxm + * @since 2024/6/15 + */ +@Data +@Accessors(chain = true) +@Schema(title = "微信OpenId查询结果") +public class WeChatOpenIdResult { + + @Schema(description = "OpenId") + private String openId; + + /** + * 状态 + * pending:查询中 + * success:查询成功 + * fail:查询失败 + */ + @Schema(description = "状态") + private String status; +}