update 支付宝登录

This commit is contained in:
Guoqs
2024-06-26 10:37:59 +08:00
parent fd7b2d5260
commit 805eb5b9ee
8 changed files with 164 additions and 33 deletions

View File

@@ -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启动订单 或 卡启动订单

View File

@@ -212,3 +212,5 @@ alipay:
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2

View File

@@ -206,4 +206,6 @@ alipay:
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数必须外网可以正常访问
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2

View File

@@ -206,4 +206,6 @@ alipay:
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数必须外网可以正常访问
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2

View File

@@ -209,4 +209,6 @@ alipay:
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数必须外网可以正常访问
notifyUrl: xxxx
# AES密钥
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
charset: utf-8
signType: RSA2

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -20,4 +20,7 @@ public class AlipayLoginDTO extends BaseDTO{
*/
private String appId;
private String authCode;
private String accessToken;
}