Property:
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 支付宝配置
*/
@Data
@Component
@ConfigurationProperties(prefix = "ali")
public class AliPayProperty {
/**
* 支付宝 APPID
*/
public String appId;
/**
* 商户私钥,您的 PKCS8 格式 RSA2 私钥
*/
public String merchantPrivateKey ;
/**
* 支付宝公钥 , 查看地址: https://openhome.alipay.com 对应 APPID 下的支付宝公钥。
*/
public String alipayPublicKey;
/**
* 接口格式规范
*/
public String format;
/**
* 签名方式
*/
public String signType;
/**
* 字符编码格式
*/
public String charset;
/**
* 支付宝网关 https://openapi.alipay.com/gateway.do 这是正式地址
*/
public String gatewayUrl;
/**
* 支付宝客户端
* @return
*/
public AlipayClient getAlipayClient(){
AlipayClient alipayClient = new DefaultAlipayClient(
this.gatewayUrl,
this.appId,
this.merchantPrivateKey,
this.format,
this.charset,
this.alipayPublicKey,
this.signType);
return alipayClient;
}
}
业务流程代码
controller :
@GetMapping(value = "/loginCallBack")
public String loginCallBack(HttpServletRequest request){
return aliPayService.loginCallBack(request);
}
1
2
3
4
service :
public String loginCallBack(HttpServletRequest request){
// 获取用户扫码授权的参数
Map
// 获取用户扫码后的 code
String code = map.get("auth_code");
// 构建阿里客户端
AlipayClient alipayClient = aliPayProperty.getAlipayClient();
// 获取阿里用户 token
AlipaySystemOauthTokenResponse aliUserToken =
this.getAliUserToken(code, alipayClient,0);
// 获取用户信息
AlipayUserInfoShareResponse infoShareResponse =
this.getUserInfo(alipayClient, aliUserToken, 0);
// !!!沙箱环境用户没有这些基本信息但是可以看到支付宝接口是成功的
return "SUECCSS";
}
封装接收参数方法:
public Map
Map
Map
for (Iterator
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
// 乱码解决,外汇跟单gendan5.com这段代码在出现乱码时使用
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
map.put(name, valueStr);
log.info(" 接受支付宝回调参数: {}",map);
}
return map;
}
获取 token 方法:
private AlipaySystemOauthTokenResponse getAliUserToken(String code, AlipayClient alipayClient,int number) throws AlipayApiException {
AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
alipaySystemOauthTokenRequest.setGrantType("authorization_code");
alipaySystemOauthTokenRequest.setCode(code);
AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(alipaySystemOauthTokenRequest);
log.info(" 获得用户 +++++++++++++++token:{}+++++++++++++++",oauthTokenResponse.getAccessToken());
log.info(" 获得用户 +++++++++++++++uuid:{}+++++++++++++++",oauthTokenResponse.getUserId());
if(oauthTokenResponse.isSuccess()){
log.info(" 成功 ");
} else {
log.info("*********** 失败,自旋开始第: {} 次 ",number);
number += 1;
if(number < 3){
log.info(" 获取 token 失败,尝试: *******{}*******",number);
return this.getAliUserToken(apiPayLoginReq, alipayClient, number);
}
}
return oauthTokenResponse;
}
获取用户支付宝信息方法:
private AlipayUserInfoShareResponse getUserInfo(AlipayClient alipayClient,AlipaySystemOauthTokenResponse aliUserToken,int number) throws AlipayApiException {
AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse infoShareResponse = alipayClient.execute(alipayUserInfoShareRequest,aliUserToken.getAccessToken());
log.info("---------------- 获得支付宝用户详情: {}",infoShareResponse.getBody());
UserInfoReq userInfoReq = new UserInfoReq();
if(infoShareResponse.isSuccess()){
// 用户授权成功
log.info("---------------- 获得支付宝用户基本而信息: {}",userInfoReq);
log.info(" 成功 ");
} else {
log.info("*********** 失败,自旋开始第: {} 次 ",number);
number += 1;
if(number < 3){
log.info(" 调用用户详情失败,尝试: *******{}*******",number);
return this.getUserInfo(alipayClient,aliUserToken,number);
}
return infoShareResponse ;
}
}