mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-05-08 03:50:13 +08:00
update 支付宝登录
This commit is contained in:
@@ -4,6 +4,12 @@ import cn.hutool.core.util.PageUtil;
|
|||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.alibaba.fastjson2.TypeReference;
|
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.alipay.easysdk.factory.Factory;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import com.github.pagehelper.PageInfo;
|
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.common.CreateAdaPaymentParam;
|
||||||
import com.jsowell.adapay.config.AbstractAdapayConfig;
|
import com.jsowell.adapay.config.AbstractAdapayConfig;
|
||||||
import com.jsowell.adapay.factory.AdapayConfigFactory;
|
import com.jsowell.adapay.factory.AdapayConfigFactory;
|
||||||
|
import com.jsowell.alipay.factory.AlipayClientFactory;
|
||||||
import com.jsowell.alipay.service.AliAppletRemoteService;
|
import com.jsowell.alipay.service.AliAppletRemoteService;
|
||||||
import com.jsowell.common.constant.CacheConstants;
|
import com.jsowell.common.constant.CacheConstants;
|
||||||
import com.jsowell.common.constant.Constants;
|
import com.jsowell.common.constant.Constants;
|
||||||
@@ -112,6 +119,13 @@ public class MemberService {
|
|||||||
@Value("${weixin.login.appid}")
|
@Value("${weixin.login.appid}")
|
||||||
private String APP_ID;
|
private String APP_ID;
|
||||||
|
|
||||||
|
private final AlipayClient alipayClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public MemberService(AlipayClientFactory alipayClientFactory) {
|
||||||
|
this.alipayClient = alipayClientFactory.getAlipayClient();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验短信验证码
|
* 校验短信验证码
|
||||||
* @param dto
|
* @param dto
|
||||||
@@ -147,6 +161,7 @@ public class MemberService {
|
|||||||
String phoneNumber = dto.getMobileNumber();
|
String phoneNumber = dto.getMobileNumber();
|
||||||
String firstLevelMerchantId = dto.getFirstLevelMerchantId();
|
String firstLevelMerchantId = dto.getFirstLevelMerchantId();
|
||||||
String openId = dto.getOpenId();
|
String openId = dto.getOpenId();
|
||||||
|
String buyerId = dto.getBuyerId();
|
||||||
|
|
||||||
log.info("公共登录注册方法, phoneNumber:{}, firstLevelMerchantId:{}, openId:{}", phoneNumber, firstLevelMerchantId, openId);
|
log.info("公共登录注册方法, phoneNumber:{}, firstLevelMerchantId:{}, openId:{}", phoneNumber, firstLevelMerchantId, openId);
|
||||||
if (StringUtils.isBlank(phoneNumber)) {
|
if (StringUtils.isBlank(phoneNumber)) {
|
||||||
@@ -176,6 +191,9 @@ public class MemberService {
|
|||||||
if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && StringUtils.isNotBlank(openId)) {
|
if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && StringUtils.isNotBlank(openId)) {
|
||||||
memberBasicInfo.setOpenId(openId);
|
memberBasicInfo.setOpenId(openId);
|
||||||
}
|
}
|
||||||
|
if (AdapayPayChannelEnum.ALIPAY_LITE.getValue().equals(dto.getRequestSource()) && StringUtils.isNotBlank(buyerId)) {
|
||||||
|
memberBasicInfo.setBuyerId(buyerId);
|
||||||
|
}
|
||||||
|
|
||||||
MemberTransactionDTO memberTransactionDTO = new MemberTransactionDTO();
|
MemberTransactionDTO memberTransactionDTO = new MemberTransactionDTO();
|
||||||
memberTransactionDTO.setMemberBasicInfo(memberBasicInfo);
|
memberTransactionDTO.setMemberBasicInfo(memberBasicInfo);
|
||||||
@@ -190,9 +208,17 @@ public class MemberService {
|
|||||||
}
|
}
|
||||||
transactionService.createMember(memberTransactionDTO);
|
transactionService.createMember(memberTransactionDTO);
|
||||||
} else {
|
} else {
|
||||||
|
boolean updateFlag = false;
|
||||||
if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && !StringUtils.equals(memberBasicInfo.getOpenId(), openId)) {
|
if (AdapayPayChannelEnum.WX_LITE.getValue().equals(dto.getRequestSource()) && !StringUtils.equals(memberBasicInfo.getOpenId(), openId)) {
|
||||||
// openId变化就更新
|
// openId变化就更新
|
||||||
memberBasicInfo.setOpenId(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);
|
memberBasicInfoService.updateMemberBasicInfo(memberBasicInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,12 +268,31 @@ public class MemberService {
|
|||||||
return memberRegisterAndLogin(loginDTO); // 微信小程序一键登录
|
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");
|
String mobileNumber = JSON.parseObject(decrypt).getString("mobile");
|
||||||
|
return mobileNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String alipayLogin(AlipayLoginDTO dto) throws Exception {
|
||||||
|
String accessToken = getAccessToken(dto.getAuthCode());
|
||||||
|
|
||||||
// 获取buyer_id
|
// 获取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
|
// 根据appid查询merchantId
|
||||||
String firstLevelMerchantId = pileMerchantInfoService.getFirstLevelMerchantIdByAliAppId(dto.getAppId());
|
String firstLevelMerchantId = pileMerchantInfoService.getFirstLevelMerchantIdByAliAppId(dto.getAppId());
|
||||||
@@ -256,11 +301,37 @@ public class MemberService {
|
|||||||
MemberRegisterAndLoginDTO loginDTO = MemberRegisterAndLoginDTO.builder()
|
MemberRegisterAndLoginDTO loginDTO = MemberRegisterAndLoginDTO.builder()
|
||||||
.requestSource(dto.getRequestSource())
|
.requestSource(dto.getRequestSource())
|
||||||
.firstLevelMerchantId(firstLevelMerchantId)
|
.firstLevelMerchantId(firstLevelMerchantId)
|
||||||
.mobileNumber(mobileNumber)
|
.mobileNumber(mobile)
|
||||||
|
.buyerId(userId)
|
||||||
.build();
|
.build();
|
||||||
return memberRegisterAndLogin(loginDTO); // 支付宝小程序一键登录
|
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
|
* 获取openId
|
||||||
* @param code
|
* @param code
|
||||||
@@ -717,8 +788,6 @@ public class MemberService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 充值订单金额
|
* 充值订单金额
|
||||||
* @param dto
|
|
||||||
* @throws ParseException
|
|
||||||
*/
|
*/
|
||||||
// public void rechargeOrderAmount(RechargeOrderAmountDTO dto) throws ParseException {
|
// public void rechargeOrderAmount(RechargeOrderAmountDTO dto) throws ParseException {
|
||||||
// // 根据memberId查询出当前用户正在充电的 vin启动订单 或 卡启动订单
|
// // 根据memberId查询出当前用户正在充电的 vin启动订单 或 卡启动订单
|
||||||
|
|||||||
@@ -212,3 +212,5 @@ alipay:
|
|||||||
notifyUrl: xxxx
|
notifyUrl: xxxx
|
||||||
# AES密钥
|
# AES密钥
|
||||||
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
||||||
|
charset: utf-8
|
||||||
|
signType: RSA2
|
||||||
@@ -206,4 +206,6 @@ alipay:
|
|||||||
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
|
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
|
||||||
notifyUrl: xxxx
|
notifyUrl: xxxx
|
||||||
# AES密钥
|
# AES密钥
|
||||||
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
||||||
|
charset: utf-8
|
||||||
|
signType: RSA2
|
||||||
@@ -206,4 +206,6 @@ alipay:
|
|||||||
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
|
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
|
||||||
notifyUrl: xxxx
|
notifyUrl: xxxx
|
||||||
# AES密钥
|
# AES密钥
|
||||||
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
||||||
|
charset: utf-8
|
||||||
|
signType: RSA2
|
||||||
@@ -209,4 +209,6 @@ alipay:
|
|||||||
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
|
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
|
||||||
notifyUrl: xxxx
|
notifyUrl: xxxx
|
||||||
# AES密钥
|
# AES密钥
|
||||||
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
encryptKey: 0TOgnthQKQtoXJaSn5Bbhg==
|
||||||
|
charset: utf-8
|
||||||
|
signType: RSA2
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package com.jsowell.alipay.config;
|
package com.jsowell.alipay.config;
|
||||||
|
|
||||||
import com.alipay.easysdk.factory.Factory;
|
import com.alipay.api.*;
|
||||||
import com.alipay.easysdk.kernel.Config;
|
|
||||||
import com.jsowell.common.constant.Constants;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -31,31 +31,47 @@ public class AliPayConfig implements CommandLineRunner {
|
|||||||
@Value("${alipay.encryptKey}")
|
@Value("${alipay.encryptKey}")
|
||||||
private String 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
|
@Override
|
||||||
public void run(String... args) throws Exception {
|
public void run(String... args) throws Exception {
|
||||||
log.info(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 AliPayConfig order 2 <<<<<<<<<<<<<");
|
log.info(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 AliPayConfig order 2 <<<<<<<<<<<<<");
|
||||||
// 设置参数(全局只需设置一次)
|
// 设置参数(全局只需设置一次)
|
||||||
Config config = new Config();
|
AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||||
config.protocol = Constants.HTTPS;
|
}
|
||||||
config.gatewayHost = gatewayHost;
|
|
||||||
config.signType = Constants.RSA2;
|
|
||||||
config.appId = appId;
|
|
||||||
|
|
||||||
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
private AlipayConfig getAlipayConfig() {
|
||||||
config.merchantPrivateKey = merchantPrivateKey;
|
AlipayConfig alipayConfig = new AlipayConfig();
|
||||||
|
alipayConfig.setServerUrl(gatewayHost);
|
||||||
// 注:证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径,优先从文件系统中加载,加载失败后会继续尝试从CLASS_PATH中加载
|
alipayConfig.setAppId(appId);
|
||||||
// config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->";
|
alipayConfig.setPrivateKey(merchantPrivateKey);
|
||||||
// config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->";
|
alipayConfig.setAlipayPublicKey(alipayPublicKey);
|
||||||
// config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt -->";
|
return alipayConfig;
|
||||||
|
|
||||||
// 注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
|
|
||||||
config.alipayPublicKey = alipayPublicKey;
|
|
||||||
// 可设置异步通知接收服务地址(可选)
|
|
||||||
// config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->";
|
|
||||||
|
|
||||||
// 可设置AES密钥,调用AES加解密相关接口时需要(可选)
|
|
||||||
config.encryptKey = encryptKey;
|
|
||||||
Factory.setOptions(config);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,4 +20,7 @@ public class AlipayLoginDTO extends BaseDTO{
|
|||||||
*/
|
*/
|
||||||
private String appId;
|
private String appId;
|
||||||
|
|
||||||
|
private String authCode;
|
||||||
|
|
||||||
|
private String accessToken;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user