From 805eb5b9ee1d6f3d8a087a165a2d28149ee37bc9 Mon Sep 17 00:00:00 2001 From: Guoqs <123@jsowell.com> Date: Wed, 26 Jun 2024 10:37:59 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=94=AF=E4=BB=98=E5=AE=9D=E7=99=BB?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jsowell/service/MemberService.java | 81 +++++++++++++++++-- .../src/main/resources/application-dev.yml | 2 + .../src/main/resources/application-prd.yml | 4 +- .../src/main/resources/application-pre.yml | 4 +- .../src/main/resources/application-sit.yml | 4 +- .../jsowell/alipay/config/AliPayConfig.java | 64 +++++++++------ .../alipay/factory/AlipayClientFactory.java | 35 ++++++++ .../com/jsowell/pile/dto/AlipayLoginDTO.java | 3 + 8 files changed, 164 insertions(+), 33 deletions(-) create mode 100644 jsowell-pile/src/main/java/com/jsowell/alipay/factory/AlipayClientFactory.java diff --git a/jsowell-admin/src/main/java/com/jsowell/service/MemberService.java b/jsowell-admin/src/main/java/com/jsowell/service/MemberService.java index 99e6342d3..88fed0c51 100644 --- a/jsowell-admin/src/main/java/com/jsowell/service/MemberService.java +++ b/jsowell-admin/src/main/java/com/jsowell/service/MemberService.java @@ -4,6 +4,12 @@ import cn.hutool.core.util.PageUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.TypeReference; +import com.alipay.api.AlipayApiException; +import com.alipay.api.AlipayClient; +import com.alipay.api.request.AlipaySystemOauthTokenRequest; +import com.alipay.api.request.AlipayUserInfoShareRequest; +import com.alipay.api.response.AlipaySystemOauthTokenResponse; +import com.alipay.api.response.AlipayUserInfoShareResponse; import com.alipay.easysdk.factory.Factory; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -14,6 +20,7 @@ import com.huifu.adapay.model.Payment; import com.jsowell.adapay.common.CreateAdaPaymentParam; import com.jsowell.adapay.config.AbstractAdapayConfig; import com.jsowell.adapay.factory.AdapayConfigFactory; +import com.jsowell.alipay.factory.AlipayClientFactory; import com.jsowell.alipay.service.AliAppletRemoteService; import com.jsowell.common.constant.CacheConstants; import com.jsowell.common.constant.Constants; @@ -112,6 +119,13 @@ public class MemberService { @Value("${weixin.login.appid}") private String APP_ID; + private final AlipayClient alipayClient; + + @Autowired + public MemberService(AlipayClientFactory alipayClientFactory) { + this.alipayClient = alipayClientFactory.getAlipayClient(); + } + /** * 校验短信验证码 * @param dto @@ -147,6 +161,7 @@ public class MemberService { String phoneNumber = dto.getMobileNumber(); String firstLevelMerchantId = dto.getFirstLevelMerchantId(); String openId = dto.getOpenId(); + String buyerId = dto.getBuyerId(); log.info("公共登录注册方法, phoneNumber:{}, firstLevelMerchantId:{}, openId:{}", phoneNumber, firstLevelMerchantId, openId); if (StringUtils.isBlank(phoneNumber)) { @@ -176,6 +191,9 @@ public class MemberService { if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && StringUtils.isNotBlank(openId)) { memberBasicInfo.setOpenId(openId); } + if (AdapayPayChannelEnum.ALIPAY_LITE.getValue().equals(dto.getRequestSource()) && StringUtils.isNotBlank(buyerId)) { + memberBasicInfo.setBuyerId(buyerId); + } MemberTransactionDTO memberTransactionDTO = new MemberTransactionDTO(); memberTransactionDTO.setMemberBasicInfo(memberBasicInfo); @@ -190,9 +208,17 @@ public class MemberService { } transactionService.createMember(memberTransactionDTO); } else { + boolean updateFlag = false; if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && !StringUtils.equals(memberBasicInfo.getOpenId(), openId)) { // openId变化就更新 memberBasicInfo.setOpenId(openId); + updateFlag = true; + } + if (AdapayPayChannelEnum.ALIPAY_LITE.getValue().equals(dto.getRequestSource()) && !StringUtils.equals(memberBasicInfo.getBuyerId(), buyerId)) { + memberBasicInfo.setBuyerId(buyerId); + updateFlag = true; + } + if (updateFlag) { memberBasicInfoService.updateMemberBasicInfo(memberBasicInfo); } } @@ -242,12 +268,31 @@ public class MemberService { return memberRegisterAndLogin(loginDTO); // 微信小程序一键登录 } - public String alipayLogin(AlipayLoginDTO dto) throws Exception { - // 通过密文解密 获取手机号码 - String decrypt = Factory.Util.AES().decrypt(dto.getMobileNumberCiphertext()); + /** + * 支付宝 通过密文解密 获取手机号码 + * @param mobileNumberCiphertext + * @return + * @throws Exception + */ + private String decryptMobileNumberCiphertext(String mobileNumberCiphertext) throws Exception { + String decrypt = Factory.Util.AES().decrypt(mobileNumberCiphertext); String mobileNumber = JSON.parseObject(decrypt).getString("mobile"); + return mobileNumber; + } + + public String alipayLogin(AlipayLoginDTO dto) throws Exception { + String accessToken = getAccessToken(dto.getAuthCode()); // 获取buyer_id + AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest(); + AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken); + if(response.isSuccess()){ + log.info("调用成功:" + JSON.toJSONString(response)); + } else { + log.info("调用失败:" + JSON.toJSONString(response)); + } + String userId = response.getUserId(); + String mobile = response.getMobile(); // 根据appid查询merchantId String firstLevelMerchantId = pileMerchantInfoService.getFirstLevelMerchantIdByAliAppId(dto.getAppId()); @@ -256,11 +301,37 @@ public class MemberService { MemberRegisterAndLoginDTO loginDTO = MemberRegisterAndLoginDTO.builder() .requestSource(dto.getRequestSource()) .firstLevelMerchantId(firstLevelMerchantId) - .mobileNumber(mobileNumber) + .mobileNumber(mobile) + .buyerId(userId) .build(); return memberRegisterAndLogin(loginDTO); // 支付宝小程序一键登录 } + /** + * 获取 auth_code(用户授权码)后应尽快调用 alipay.system.oauth.token(换取授权访问令牌接口)换取 access_token(访问令牌) + * @param authCode + * @return + */ + private String getAccessToken(String authCode) { + AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); + // 设置授权码 + request.setCode(authCode); + // 设置授权方式 + request.setGrantType("authorization_code"); + AlipaySystemOauthTokenResponse response = null; + try { + response = alipayClient.execute(request); + } catch (AlipayApiException e) { + throw new RuntimeException(e); + } + String accessToken = ""; + if (response.isSuccess()) { + System.out.println("调用成功"); + accessToken = response.getAccessToken(); + } + return accessToken; + } + /** * 获取openId * @param code @@ -717,8 +788,6 @@ public class MemberService { /** * 充值订单金额 - * @param dto - * @throws ParseException */ // public void rechargeOrderAmount(RechargeOrderAmountDTO dto) throws ParseException { // // 根据memberId查询出当前用户正在充电的 vin启动订单 或 卡启动订单 diff --git a/jsowell-admin/src/main/resources/application-dev.yml b/jsowell-admin/src/main/resources/application-dev.yml index 700e3c364..9290e377c 100644 --- a/jsowell-admin/src/main/resources/application-dev.yml +++ b/jsowell-admin/src/main/resources/application-dev.yml @@ -212,3 +212,5 @@ alipay: notifyUrl: xxxx # AES密钥 encryptKey: 0TOgnthQKQtoXJaSn5Bbhg== + charset: utf-8 + signType: RSA2 \ No newline at end of file diff --git a/jsowell-admin/src/main/resources/application-prd.yml b/jsowell-admin/src/main/resources/application-prd.yml index 810aa7765..2f7681465 100644 --- a/jsowell-admin/src/main/resources/application-prd.yml +++ b/jsowell-admin/src/main/resources/application-prd.yml @@ -206,4 +206,6 @@ alipay: # 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 notifyUrl: xxxx # AES密钥 - encryptKey: 0TOgnthQKQtoXJaSn5Bbhg== \ No newline at end of file + encryptKey: 0TOgnthQKQtoXJaSn5Bbhg== + charset: utf-8 + signType: RSA2 \ No newline at end of file diff --git a/jsowell-admin/src/main/resources/application-pre.yml b/jsowell-admin/src/main/resources/application-pre.yml index f37f820cb..dfb2e93e5 100644 --- a/jsowell-admin/src/main/resources/application-pre.yml +++ b/jsowell-admin/src/main/resources/application-pre.yml @@ -206,4 +206,6 @@ alipay: # 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 notifyUrl: xxxx # AES密钥 - encryptKey: 0TOgnthQKQtoXJaSn5Bbhg== \ No newline at end of file + encryptKey: 0TOgnthQKQtoXJaSn5Bbhg== + charset: utf-8 + signType: RSA2 \ No newline at end of file diff --git a/jsowell-admin/src/main/resources/application-sit.yml b/jsowell-admin/src/main/resources/application-sit.yml index 42a65e641..de67295ce 100644 --- a/jsowell-admin/src/main/resources/application-sit.yml +++ b/jsowell-admin/src/main/resources/application-sit.yml @@ -209,4 +209,6 @@ alipay: # 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 notifyUrl: xxxx # AES密钥 - encryptKey: 0TOgnthQKQtoXJaSn5Bbhg== \ No newline at end of file + encryptKey: 0TOgnthQKQtoXJaSn5Bbhg== + charset: utf-8 + signType: RSA2 \ No newline at end of file diff --git a/jsowell-pile/src/main/java/com/jsowell/alipay/config/AliPayConfig.java b/jsowell-pile/src/main/java/com/jsowell/alipay/config/AliPayConfig.java index 968c0a227..ba95920b4 100644 --- a/jsowell-pile/src/main/java/com/jsowell/alipay/config/AliPayConfig.java +++ b/jsowell-pile/src/main/java/com/jsowell/alipay/config/AliPayConfig.java @@ -1,11 +1,11 @@ package com.jsowell.alipay.config; -import com.alipay.easysdk.factory.Factory; -import com.alipay.easysdk.kernel.Config; -import com.jsowell.common.constant.Constants; +import com.alipay.api.*; + import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @@ -31,31 +31,47 @@ public class AliPayConfig implements CommandLineRunner { @Value("${alipay.encryptKey}") private String encryptKey; + // @Override + // public void run(String... args) throws Exception { + // log.info(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 AliPayConfig order 2 <<<<<<<<<<<<<"); + // // 设置参数(全局只需设置一次) + // Config config = new Config(); + // config.protocol = Constants.HTTPS; + // config.gatewayHost = gatewayHost; + // config.signType = Constants.RSA2; + // config.appId = appId; + // + // // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中 + // config.merchantPrivateKey = merchantPrivateKey; + // + // // 注:证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径,优先从文件系统中加载,加载失败后会继续尝试从CLASS_PATH中加载 + // // config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->"; + // // config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->"; + // // config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt -->"; + // + // // 注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可 + // config.alipayPublicKey = alipayPublicKey; + // // 可设置异步通知接收服务地址(可选) + // // config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->"; + // + // // 可设置AES密钥,调用AES加解密相关接口时需要(可选) + // config.encryptKey = encryptKey; + // Factory.setOptions(config); + // } + @Override public void run(String... args) throws Exception { log.info(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 AliPayConfig order 2 <<<<<<<<<<<<<"); // 设置参数(全局只需设置一次) - Config config = new Config(); - config.protocol = Constants.HTTPS; - config.gatewayHost = gatewayHost; - config.signType = Constants.RSA2; - config.appId = appId; + AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig()); + } - // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中 - config.merchantPrivateKey = merchantPrivateKey; - - // 注:证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径,优先从文件系统中加载,加载失败后会继续尝试从CLASS_PATH中加载 - // config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->"; - // config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->"; - // config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt -->"; - - // 注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可 - config.alipayPublicKey = alipayPublicKey; - // 可设置异步通知接收服务地址(可选) - // config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->"; - - // 可设置AES密钥,调用AES加解密相关接口时需要(可选) - config.encryptKey = encryptKey; - Factory.setOptions(config); + private AlipayConfig getAlipayConfig() { + AlipayConfig alipayConfig = new AlipayConfig(); + alipayConfig.setServerUrl(gatewayHost); + alipayConfig.setAppId(appId); + alipayConfig.setPrivateKey(merchantPrivateKey); + alipayConfig.setAlipayPublicKey(alipayPublicKey); + return alipayConfig; } } \ No newline at end of file diff --git a/jsowell-pile/src/main/java/com/jsowell/alipay/factory/AlipayClientFactory.java b/jsowell-pile/src/main/java/com/jsowell/alipay/factory/AlipayClientFactory.java new file mode 100644 index 000000000..121c24afe --- /dev/null +++ b/jsowell-pile/src/main/java/com/jsowell/alipay/factory/AlipayClientFactory.java @@ -0,0 +1,35 @@ +package com.jsowell.alipay.factory; + +import com.alipay.api.AlipayClient; +import com.alipay.api.DefaultAlipayClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class AlipayClientFactory { + private static AlipayClient alipayClient; + + public AlipayClientFactory( + @Value("${alipay.gatewayHost}") String gatewayUrl, + @Value("${alipay.appId}") String appId, + @Value("${alipay.merchantPrivateKey}") String appPrivateKey, + @Value("${alipay.alipayPublicKey}") String alipayPublicKey, + @Value("${alipay.charset}") String charset, + @Value("${alipay.signType}") String signType + ) { + // 初始化 AlipayClient + alipayClient = new DefaultAlipayClient( + gatewayUrl, + appId, + appPrivateKey, + "json", + charset, + alipayPublicKey, + signType + ); + } + + public static AlipayClient getAlipayClient() { + return alipayClient; + } +} diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/dto/AlipayLoginDTO.java b/jsowell-pile/src/main/java/com/jsowell/pile/dto/AlipayLoginDTO.java index 210ba4808..c76fd4ee2 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/dto/AlipayLoginDTO.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/dto/AlipayLoginDTO.java @@ -20,4 +20,7 @@ public class AlipayLoginDTO extends BaseDTO{ */ private String appId; + private String authCode; + + private String accessToken; }