Authored by mlge

支付宝自动绑定接口

... ... @@ -26,4 +26,6 @@ public class UserAuthorizeInfo {
private String alipayUid;//支付宝uid 最长16位
private String nickName;//支付宝用户的nickName
}
... ...
... ... @@ -8,12 +8,13 @@
<result column="alipay_account" property="alipayAccount" jdbcType="VARCHAR" />
<result column="cert_name" property="certName" jdbcType="VARCHAR" />
<result column="alipay_uid" property="alipayUid" jdbcType="VARCHAR" />
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
<result column="update_time" property="updateTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, uid, valid_status,alipay_account, cert_name, alipay_uid,create_time, update_time
id, uid, valid_status,alipay_account, cert_name, alipay_uid, nick_name, create_time, update_time
</sql>
<select id="selectValidAuthorizeInfoByUid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
... ... @@ -50,6 +51,9 @@
<if test="alipayUid != null">
alipay_uid,
</if>
<if test="nickName != null">
nick_name,
</if>
<if test="createTime != null">
create_time,
</if>
... ... @@ -73,6 +77,9 @@
<if test="alipayUid != null">
#{alipayUid,jdbcType=VARCHAR},
</if>
<if test="nickName != null">
#{nickName,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=INTEGER},
</if>
... ...
package com.yohoufo.user.common;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlipayConfigInfo {
public static final String appid = "2013080700000742";
public static final String private_key = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALu5rJ6/y7wPFLM+QG7qT60GXCLLNWg5bC8dLKIPK6QDLs0" +
"7Yl5JYtxdeSkUCrb0FG/5u7HYTMsSBicxqEtgRdhnv10qGWXupp43BK4PktME9Odua48oX0Jhk/ECfmEwpOrkTaDX36CHwvZglXeK2yjLPCrdZhul++BWwTtdwyC5AgMBAA" +
"ECgYEAjfL0iIXW9Lkimfb0meornVY1zUmDdPAW0flpipGWGksd1KqW010CmEdGGr3A7F7cJV/dgblhOBzE8lYmP/joVlICtHEg8q1flAa876dR5m0AG3vSqYqVuV0iiig4YYuHe" +
"hgfxJEUXT+P1DW3Pk58+/ZX1IBjccF7yqT/rltKLxECQQDwp/hj/dKsvBvpHCOcrwc+5jxdp7UbruwnIgJ9Um7mpdyJp8I+WeuD/sWYzQIlhI3/bXx9yT0jeWPC6oUveXDfAkEAx7" +
"HCB3BBVd2GbNDjsW4hCXLSWN6amzAkCuYbA935VfSHq+hnBARiB8+TyrhXR7sukHE0mjb3Z454hftvGo0pZwJBAMByEjgj6i0n4EnxrqjFbs3l37AlRLb2kvJYSBVfrj/TsyCBftvNnIDZ" +
"Y7pldqn57nOsYjlUjAeqMxTT5d2pB5ECQH1T/61Bc4/bPyGOHEjgEFXovUFPPAMHvKcmcXAE4RZkKL3oVrb7tTanvV6uQQDg5kF6+wOKWc2hadzUx48HZyMCQQCXrd3xfFnhodlG0WvTuFf" +
"zexAWsh9g7SR/gHCOaZ2Ceiay0aE8N7DZIYD+BVUPtFG+Bo+Y7pE3vW9sTWYUEA7y";
// 字符编码格式 目前支持 gbk 或 utf-8
public static final String input_charset = "utf-8";
// 签名方式 不需修改
public static final String sign_type = "RSA"; // "MD5";
public static final String USERINFO_SHARE_METHOD = "alipay.user.info.share";
public static final String OAUTH_TOKEN_METHOD = "alipay.system.oauth.token" ;
public static final String USERINFO_SHARE_URL = "https://openapi.alipay.com/gateway.do";
public static final String OAUTH_TOKEN_URL = "https://openapi.alipay.com/gateway.do" ;
}
... ...
... ... @@ -170,13 +170,10 @@ public class RealNameAuthorizeController {
@RequestMapping(params = "method=ufo.user.autoBindAliPayAccount")
public ApiResponse autoBindAliPayAccount(RealNameAuthorizeReqVO reqVO) throws GatewayException {
logger.info("enter autoBindAliPayAccount reqVO is {}", reqVO);
//(1) 优先校验请求的参数
if (reqVO == null || StringUtils.isEmpty(reqVO.getAlipayUid()) ){
throw new GatewayException(400, "alipayUid参数为空!");
}
//(2) 校验用户id
if(reqVO.getUid() <= 0 ){
throw new GatewayException(400, "uid参数为空!");
if (reqVO == null || reqVO.getUid()<=0){
throw new GatewayException(400, "参数错误,uid不能为空!");
}
//检查是否已经实名认证,如果已经认证直接返回
... ... @@ -186,11 +183,14 @@ public class RealNameAuthorizeController {
//(3) 开始绑定支付宝账号 入库
try{
realNameAuthorizeService.saveAuthorizeInfoNew(reqVO);
return new ApiResponse(200,"success!");
realNameAuthorizeService.autoBindAliPayAccount(reqVO);
return new ApiResponse(200,"绑定成功");
}catch(Exception e){
logger.warn("autoBindAliPayAccount error! reqVO ={}, e : {}", reqVO, e);
return new ApiResponse(201,"failed!");
if (e instanceof GatewayException) {
throw e;
}
return new ApiResponse(201,"绑定失败");
}
}
... ...
... ... @@ -27,4 +27,9 @@ public class RealNameAuthorizeReqVO extends BaseBO {
private String alipayUid;//支付宝uid 最长16位
private String access_token;
private String auth_code;
}
... ...
package com.yohoufo.user.responseVO;
import lombok.Data;
/**
* Created by meiling.ge on 2018/11/21.
*/
@Data
public class AlipayCommonRespVO {
String code;
String msg;
String sub_msg;
String sub_code;
String sign;
}
... ...
package com.yohoufo.user.responseVO;
import lombok.Data;
/**
* Created by meiling.ge on 2018/11/21.
*/
@Data
public class AlipayUserInfoRespVO extends AlipayCommonRespVO{
private String user_id;
private String avatar;
private String province;
private String city;
private String nick_name;
private String is_student_certified;
private String user_type;
private String user_status;
private String is_certified;
private String gender;
//
private String access_token;
private String expires_in;
private String refresh_token;
private String re_expires_in;
}
... ...
... ... @@ -4,6 +4,7 @@ package com.yohoufo.user.service;
import com.yoho.tools.common.beans.ApiResponse;
import com.yohobuy.ufo.model.user.resp.AuthorizeResultRespVO;
import com.yohoufo.common.exception.GatewayException;
import com.yohoufo.dal.user.model.UserAuthorizeInfo;
import com.yohoufo.dal.user.model.ZhiMaCert;
import com.yohoufo.user.requestVO.RealNameAuthorizeReqVO;
... ... @@ -28,6 +29,8 @@ public interface IRealNameAuthorizeService {
AuthorizeResultRespVO queryAuthorizeInfo(Integer uid);
void autoBindAliPayAccount(RealNameAuthorizeReqVO reqVO) throws GatewayException;
//JSONObject authorizeRealNameWithBank(RealNameAuthorizeReqVO reqVO) throws ServiceException;
}
... ...
package com.yohoufo.user.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.response.ZhimaCustomerCertificationQueryResponse;
import com.yoho.error.exception.ServiceException;
import com.yoho.tools.common.beans.ApiResponse;
import com.yohobuy.ufo.model.user.resp.AuthorizeResultRespVO;
import com.yohoufo.common.exception.GatewayException;
import com.yohoufo.common.utils.HttpClient;
import com.yohoufo.common.utils.RSAUtils;
import com.yohoufo.common.utils.UserInfoHiddenHelper;
import com.yohoufo.dal.user.IUserAuthorizeInfoDao;
import com.yohoufo.dal.user.IZhiMaCertDao;
import com.yohoufo.dal.user.model.UserAuthorizeInfo;
import com.yohoufo.dal.user.model.ZhiMaCert;
import com.yohoufo.user.cache.CacheService;
import com.yohoufo.user.common.AlipayConfigInfo;
import com.yohoufo.user.helper.HideDataUtil;
import com.yohoufo.user.requestVO.RealNameAuthorizeReqVO;
import com.yohoufo.user.responseVO.AlipayUserInfoRespVO;
import com.yohoufo.user.service.IRealNameAuthorizeService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* 用户身份实名认证
... ... @@ -39,6 +52,8 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
@Autowired
private IZhiMaCertDao zhiMaCertDao;
@Autowired
private HttpClient httpClient;
/* @Autowired
private GraphVerifyService graphVerifyService;*/
... ... @@ -145,14 +160,17 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
}
result.setAuthorizeFlag(true);
result.setCreateTime(info.getCreateTime());
//支付宝userId
if(StringUtils.isNotBlank( info.getAlipayUid())){
if(StringUtils.isNotBlank( info.getAlipayUid())){//通过授权绑定的账户
result.setAlipayId(info.getAlipayUid());
result.setAlipayMaskAccount(info.getNickName());
return result;
}
//支付宝账户信息
if(StringUtils.isNotBlank(info.getAlipayAccount())){
result.setAlipayAccount(info.getAlipayAccount());
if(StringUtils.isNotBlank(info.getAlipayAccount())){//用户手动输入的
// result.setAlipayAccount(info.getAlipayAccount());
//把账号部分信息隐藏
if(HideDataUtil.isEmail(info.getAlipayAccount())){
result.setAlipayMaskAccount(HideDataUtil.hideEmail(info.getAlipayAccount()));
... ... @@ -168,10 +186,137 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
}
result.setCertName(info.getCertName());
result.setCreateTime(info.getCreateTime());
return result;
}
/**
* 支付宝授权自动绑定
* 1)安卓客户端 --先获取 access_token, iphone跳过此步
* 2)获取用户信息
* 3)绑定入库
* @param reqVO
* @throws GatewayException
*/
@Override
public void autoBindAliPayAccount(RealNameAuthorizeReqVO reqVO) throws GatewayException {
logger.info("enter autoBindAliPayAccount, reqVO = {}", reqVO);
int uid = reqVO.getUid();
String access_token = reqVO.getAccess_token();
String auth_code = reqVO.getAuth_code();
AlipayUserInfoRespVO respVO;
String respStr;
//1)获取access_token
if(StringUtils.isEmpty( access_token)){
if(StringUtils.isEmpty(auth_code)){
throw new GatewayException(201,"请求参数错误,绑定失败!");
}
Map<String,String> tempParam = new TreeMap<>();
tempParam.put("grant_type", "authorization_code");
tempParam.put("code",auth_code );
respStr = queryInfo(AlipayConfigInfo.OAUTH_TOKEN_METHOD, AlipayConfigInfo.OAUTH_TOKEN_URL, tempParam);
if(StringUtils.isBlank(respStr)){
throw new GatewayException(201,"绑定失败!");
}
logger.info("autoBindAliPayAccount getAlipayOauthToken uid={}, app_auth_token={}, respStr={}, ", uid, auth_code, respStr);
respVO = JSON.parseObject(JSONObject.toJSONString(JSON.parseObject(respStr).getJSONObject("alipay_system_oauth_token_response")), AlipayUserInfoRespVO.class);
if( StringUtils.isBlank(respVO.getAccess_token()) ){//调用失败
throw new GatewayException(201,"绑定失败!");
}
access_token = respVO.getAccess_token();
}
//2)alipay.user.info.share(支付宝会员授权信息查询接口)
Map<String,String> param = new HashMap<>();
param.put("auth_token",access_token);
respStr = queryInfo(AlipayConfigInfo.USERINFO_SHARE_METHOD, AlipayConfigInfo.USERINFO_SHARE_URL,param);
logger.info("autoBindAliPayAccount getAlipayUserInfo uid={}, app_auth_token={},access_token={}, respStr={}, ", uid,access_token, auth_code, respStr);
if(StringUtils.isBlank(respStr)){
throw new GatewayException(201,"绑定失败!");
}
respVO = JSON.parseObject(JSONObject.toJSONString(JSON.parseObject(respStr).getJSONObject("alipay_user_info_share_response")), AlipayUserInfoRespVO.class);
//调用失败
if( !StringUtils.equals("10000", respVO.getCode()) ){
throw new GatewayException(201,"绑定失败!");
}
//3)调用成功--入库操作
String alipayUid = respVO.getUser_id();
String nickName = respVO.getNick_name();
logger.info("autoBindAliPayAccount save bandInfo reqVO={}, alipayUid={},nickName={}",reqVO, alipayUid,nickName);
UserAuthorizeInfo userAuthorizeInfo = new UserAuthorizeInfo();
userAuthorizeInfo.setUid(uid);
userAuthorizeInfo.setValidStatus(1);
userAuthorizeInfo.setAlipayUid(alipayUid);
userAuthorizeInfo.setNickName(nickName);
long create_time = System.currentTimeMillis() /1000;
userAuthorizeInfo.setCreateTime(create_time);
userAuthorizeInfoDao.insertSelective(userAuthorizeInfo);
logger.info("autoBindAliPayAccount success!reqVO= {}", reqVO);
}
private String queryInfo(String methodName, String methodUrl, Map<String,String> params) {
Map<String,String> map = getAlipayCommonParam();//公共请求参数
map.put("method",methodName);
if(params != null && params.size() > 0 ){//其他参数
map.putAll(params);
}
String sign = buildSignStr(map);//签名
map.put("sign",sign);
logger.info("autoBindAliPayAccount queryInfo start, methodName ={},methodUrl is ={}, paramMap ={} ",methodName, methodUrl, params);
try {
String response = httpClient.postFormData(methodUrl, map);
return response;
} catch (Exception e) {
logger.warn("queryAlipayInfo error! methodName={},methodUrl ={},paramMap ={},e :{}, ",methodName, methodUrl, params, e);
}
return null;
}
private Map<String,String> getAlipayCommonParam() {
String app_id = AlipayConfigInfo.appid;
String format = "json";
String charset = "utf-8";
String sign_type = AlipayConfigInfo.sign_type;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = sf.format(new Date());
String version = "1.0";
Map<String,String> map = new TreeMap<>();
map.put("app_id",app_id);
map.put("sign_type",sign_type);
map.put("format",format);
map.put("charset",charset);
map.put("version",version);
map.put("timestamp",timestamp);
return map;
}
private String buildSignStr(Map<String, String> paramsMap) {
StringBuilder payStrBuilder = new StringBuilder();
int count = 0;
for(Map.Entry<String, String> entry : paramsMap.entrySet()) {
if(count > 0)
payStrBuilder.append("&");
payStrBuilder.append(entry.getKey()).append("=").append(entry.getValue());
count++;
}
String sign = RSAUtils.sign(payStrBuilder.toString(), AlipayConfigInfo.private_key, AlipayConfigInfo.input_charset);
return sign ;
}
@Override
public ZhiMaCert getValidZhiMaCert(int uid) {
... ...