JAVA支付宝授权

第一步:

首先登录到蚂蚁金服开放平台 https://open.alipay.com/platform/home.htm,前提是有商户号。创建应用之后,然后到开发者中心开通对应功能

第二步:

到应用信息里面填写应用网关与授权回调地址。应用网关填写域名即可,如:www.baidu.com;授权回调地址则需要具体到页面或者方法,如:www.baidu.com/callBack。

具体步骤可参照 蚂蚁金服开发文档 : https://docs.open.alipay.com/284/106001/

授权代码如下:

1.Controller代码:

/**
 * 包名称 com.zxtg.zshop.front.controller.alipay
 * 类名称 AlipayController.java
 * 创建时间 2019-4-24下午5:06:53
 * Copyright (c) 2019, 牵手众享科技.
 *
 */
package com.zxtg.zshop.front.controller.alipay;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.yjf.common.lang.util.StringUtil;
import com.zxtg.zshop.alibaba.alipay.IAlipayClient;
import com.zxtg.zshop.alibaba.alipay.constant.AlipayConstant;
import com.zxtg.zshop.alibaba.alipay.constant.AlipayConstant.AlipayGrantType;
import com.zxtg.zshop.alipay.AlipayService;
import com.zxtg.zshop.base.BaseAutowiredController;
import com.zxtg.zshop.base.ZshopConstants;
import com.zxtg.zshop.cache.IZshopRemoteCacheManager;
import com.zxtg.zshop.dataobject.ThirdBind;
import com.zxtg.zshop.dataobject.UserBaseInfo;
import com.zxtg.zshop.dataobject.ThirdBind.ThirdBindUser;
import com.zxtg.zshop.session.common.Constant;
import com.zxtg.zshop.user.request.RegisterWxUserRequest;
import com.zxtg.zshop.util.DESUtil;
import com.zxtg.zshop.util.PropertiesUtil;
import com.zxtg.zshop.util.SysCommand;
import com.zxtg.zshop.weixin.base.WeChatService;

/**
 * @类名称 AlipayController.java
 * @类描述 

 * @作者  yw [email protected]
 * @创建时间 2019-4-24 下午5:06:53
 * @版本 1.00
 *
 * @修改记录
 * 
 *     版本		 修改人 		修改日期 		 修改内容描述
 *     ----------------------------------------------
 *     1.00 	yw 	 2019-4-24             
 *     ----------------------------------------------
 * 
*/ @RequestMapping("alipay") @Controller public class AlipayController extends BaseAutowiredController { public static final String MOBILE_HOST = PropertiesUtil.getProperty("zshop.mobile.host"); @Autowired private IAlipayClient alipayClientImpl; @Autowired protected WeChatService weChatService; @Autowired protected AlipayService alipayService; @Autowired private IZshopRemoteCacheManager yueziRemoteCache; @RequestMapping("redirect") public String redirect(HttpServletRequest request, String requestUrl,String scope, Model model){ logger.info("支付宝请求路径"+requestUrl); String authRedirectUrl = AlipayConstant.AUTHORIZE_URL; authRedirectUrl = authRedirectUrl.replace("APPID", AlipayConstant.APP_ID); authRedirectUrl = authRedirectUrl.replace("SCOPE", AlipayConstant.SCOPE_AUTH_USER); authRedirectUrl = authRedirectUrl.replace("STATE", request.getSession().getId()); String redirectUrl = ZshopConstants.HOST_URL + "/alipay/callback?requestUrl="+requestUrl; try { authRedirectUrl = authRedirectUrl.replace("ENCODED_URL", URLEncoder.encode(redirectUrl, "UTF-8")); } catch (Exception e) { logger.error("编码错误", e); return "redirect:"+requestUrl; } return "redirect:"+authRedirectUrl; } @RequestMapping("callback") public String callback(HttpServletRequest request, String requestUrl, String app_id, String auth_code, String state, Model model){ logger.info("支付宝回调请求路径"+requestUrl); logger.info("支付宝回调auth_code:"+auth_code); if(!StringUtil.isBlank(auth_code)){ AlipaySystemOauthTokenResponse alipayAuth= alipayClientImpl.getAlipayOauthToken(auth_code,AlipayGrantType.AUTH_CODE); if(alipayAuth!=null){ String accessToken = alipayAuth.getAccessToken(); logger.info("支付宝回调accessToken:"+accessToken); AlipayUserInfoShareResponse alipayUser = alipayClientImpl.getAlipayUserInfo(accessToken); if(alipayUser != null){ logger.info("支付宝查询出来的用户ID:" + alipayUser.getUserId()); logger.info("支付宝查询出来的用户信息:" + alipayUser.getBody()); String nickName = alipayUser.getNickName(); String openId = alipayUser.getUserId(); String userIco = alipayUser.getAvatar(); boolean isCertified = "T".equals(alipayUser.getIsCertified()); UserBaseInfo registerUser = null; Map conditions = new HashMap(); conditions.put("openId", openId); conditions.put("status", ThirdBind.ThirdBindUser.BIND); conditions.put("thirdType", ThirdBindUser.ALIPAY); registerUser = userBaseInfoManager.queryUserWithThirdInfo(conditions); Long userId = null; if(registerUser == null){ logger.info("没有注册过支付宝账户"); RegisterWxUserRequest registerRequest = new RegisterWxUserRequest(); registerRequest.setNickName(nickName); registerRequest.setUserIco(userIco); registerRequest.setOpenId(openId); registerRequest.setThirdType(ThirdBindUser.ALIPAY); int sex = 1; // 1为男性,2为女性 if(isCertified && StringUtil.isNotBlank(alipayUser.getGender())){ sex = "F".equals(alipayUser.getGender().toUpperCase()) ? 2 : 1;// M为男性,F为女性 } registerRequest.setSex(sex); registerRequest.setIsCertified(isCertified ? 1:0); String sceneIdStr = String.valueOf(yueziRemoteCache.get("sceneId"+openId)); if(StringUtil.isNotBlank(sceneIdStr)&& !"null".equals(sceneIdStr)){ logger.info("二维码场景值=" + sceneIdStr); try{ Integer sceneId = Integer.parseInt(sceneIdStr); registerRequest.setRegisterSceneId(sceneId); }catch(NumberFormatException e){ logger.error("sceneId转换异常--无效的sceneId", e); } }else{ //如果有邀请码,记录推荐关系 String UID = String.valueOf(yueziRemoteCache.get("UID"+openId)); if(StringUtil.isNotBlank(UID)&& !"null".equals(UID)){ try{ logger.info("推荐用户UID=" + UID); long parsedUid = Long.parseLong(UID); UserBaseInfo parent = userBaseInfoManager.queryByUserId(parsedUid); if(parent != null){ registerRequest.setParentUserId(parent.getUserId()); } }catch(NumberFormatException e){ logger.error("UID转换异常--无效的UID", e); } } } try { registerUser = registerService.registerWxUser(registerRequest); userId = registerUser.getUserId(); } catch (Exception e) { logger.error("注册绑定支付宝用户出错", e); } }else{ userId = registerUser.getUserId(); try { //更新默认用户昵称,头像为支付宝昵称头像 if(registerUser.getNickName().indexOf("用户") == 0){ registerUser.setNickName(nickName); registerUser.setUserIco(userIco); } userBaseInfoManager.updateUserBaseInfo(registerUser); } catch (Exception e) { logger.error("更新用户信息出错", e); } } if(userId != null){ try { userBaseInfoManager.buildUserRole(openId, userId); } catch (Exception e) { logger.error("绑定用户角色出错", e); } yueziRemoteCache.remove("sceneId"+openId); yueziRemoteCache.remove("UID"+openId); //调用登录接口 return this.processLogin(registerUser, request, requestUrl); } }else{ logger.error("支付宝获取账户失败"); } } } logger.info("返回页面Url={}", new Object[]{requestUrl}); return "redirect:"+requestUrl; } private String processLogin(UserBaseInfo baseInfoDO,HttpServletRequest request, String retUrl){ alipayService.alipayLoginByUser(baseInfoDO, request); logger.info("支付宝绑定用户登录成功"); String sessionId = Constant.getRemoteSessionId(); String accessToken = sessionId; String userSecKey = DESUtil.encrypt(String.valueOf(baseInfoDO.getUserId()), SysCommand.DES_KEY); if(retUrl.indexOf("?") == -1){ retUrl += "?accessToken="+accessToken+"&userSecKey="+userSecKey; }else{ retUrl += "&accessToken="+accessToken+"&userSecKey="+userSecKey; } retUrl = MOBILE_HOST + "/?#" + retUrl; //移动端页面 logger.info("回调前端地址:---{}",retUrl); return "redirect:" + retUrl; } }
 

接口类

/**
 * 包名称 com.zxtg.zshop.alipay
 * 类名称 AlipayClient.java
 * 创建时间 2019-4-24下午5:10:05
 * Copyright (c) 2019, 牵手众享科技.
 *
 */
package com.zxtg.zshop.alibaba.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayFundTransOrderQueryRequest;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransOrderQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;

/**
 * @类名称 AlipayClient.java
 * @类描述 

 * @创建时间 2019-4-24 下午5:10:05
 * @版本 1.00
 *
 * @修改记录
 * 
 *     版本		 修改人 		修改日期 		 修改内容描述
 *     ----------------------------------------------
 *     1.00 	yzh 	 2019-4-24             
 *     ----------------------------------------------
 * 
*/ public interface IAlipayClient { //获取支付宝授权token public AlipaySystemOauthTokenResponse getAlipayOauthToken(String code,String grantType); //获取支付宝用户信息 public AlipayUserInfoShareResponse getAlipayUserInfo(String authToken); //单笔转账到支付宝账户接口 public AlipayFundTransToaccountTransferResponse toaccountTransfer(AlipayFundTransToaccountTransferRequest request) throws AlipayApiException; //查询转账订单接口 public AlipayFundTransOrderQueryResponse orderQuery(AlipayFundTransOrderQueryRequest request) throws AlipayApiException; }

实现类

/**
 * 包名称 com.zxtg.zshop.alipay.impl
 * 类名称 AlipayClientImpl.java
 * 创建时间 2019-4-24下午5:19:25
 * Copyright (c) 2019, 牵手众享科技.
 *
 */
package com.zxtg.zshop.alibaba.alipay.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransOrderQueryRequest;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipayFundTransOrderQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.zxtg.zshop.alibaba.alipay.IAlipayClient;
import com.zxtg.zshop.alibaba.alipay.constant.AlipayConstant;
import com.zxtg.zshop.alibaba.alipay.constant.AlipayConstant.AlipayGrantType;
import com.zxtg.zshop.jd.api.GoodsApi;

/**
 * @类名称 AlipayClientImpl.java
 * @类描述 

 * @创建时间 2019-4-24 下午5:19:25
 * @版本 1.00
 *
 * @修改记录
 * 
 *     版本		 修改人 		修改日期 		 修改内容描述
 *     ----------------------------------------------
 *     1.00 	yzh 	 2019-4-24             
 *     ----------------------------------------------
 * 
*/ @Service public class AlipayClientImpl implements IAlipayClient { protected final Logger logger = LoggerFactory.getLogger(GoodsApi.class); /** * @方法名称 getAlipayOauthToken * @功能描述

	 * @作者    yzh
	 * @创建时间 2019-4-25 下午2:06:31
	 * @param code
	 * @param grantType
	 * @return
	 */
	@Override
	public AlipaySystemOauthTokenResponse getAlipayOauthToken(String code, String grantType) {
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.GATE_WAY, AlipayConstant.APP_ID, AlipayConstant.APP_PRIVATE_KEY, "json", AlipayConstant.CHARSET, AlipayConstant.ALIPAY_PUBLIC_KEY, "RSA2"); 
		AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
		if(AlipayGrantType.AUTH_CODE.equals(grantType)){
			request.setCode(code);
			request.setGrantType("authorization_code");
		}else if(AlipayGrantType.REFRESH_CODE.equals(grantType)){
			request.setRefreshToken(code);
			request.setGrantType("refresh_token");
		}else{
			return null;
		}
		
		try {
			AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
			return oauthTokenResponse;
		} catch (Exception e) {
			logger.error("获取支付宝授权Token出错:" + e.getMessage());
		    e.printStackTrace();
		}		
		return null;
	}


	/**
	 * @方法名称 getAlipayUserInfo
	 * @功能描述 

	 * @作者    yzh
	 * @创建时间 2019-4-25 下午2:06:56
	 * @param authToken
	 * @return
	 */
	@Override
	public AlipayUserInfoShareResponse getAlipayUserInfo(String authToken) {
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.GATE_WAY, AlipayConstant.APP_ID, AlipayConstant.APP_PRIVATE_KEY, "json", AlipayConstant.CHARSET, AlipayConstant.ALIPAY_PUBLIC_KEY, "RSA2"); 
		AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
		try {
		    AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(request, authToken);
		    return userinfoShareResponse;
		} catch (Exception e) {
			logger.error("获取支付宝用户信息出错:" + e.getMessage());
		    e.printStackTrace();
		}
		return null;
	}


	/**
	 * @方法名称 toaccountTransfer
	 * @功能描述 

	 * @作者    yzh
	 * @创建时间 2019-4-25 下午2:14:48
	 * @param request
	 * @return
	 * @throws AlipayApiException
	 */
	@Override
	public AlipayFundTransToaccountTransferResponse toaccountTransfer(
			AlipayFundTransToaccountTransferRequest request)
			throws AlipayApiException {
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.GATE_WAY,AlipayConstant.APP_ID,AlipayConstant.APP_PRIVATE_KEY,"json",AlipayConstant.CHARSET,AlipayConstant.ALIPAY_PUBLIC_KEY,"RSA2");
		AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			System.out.println("调用成功");
		} else {
			System.out.println("调用失败");
		}
		return response;
	}


	/**
	 * @方法名称 orderQuery
	 * @功能描述 

	 * @作者    yzh
	 * @创建时间 2019-4-25 下午2:14:48
	 * @param request
	 * @return
	 * @throws AlipayApiException
	 */
	@Override
	public AlipayFundTransOrderQueryResponse orderQuery(
			AlipayFundTransOrderQueryRequest request) throws AlipayApiException {
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.GATE_WAY,AlipayConstant.APP_ID,AlipayConstant.APP_PRIVATE_KEY,"json",AlipayConstant.CHARSET,AlipayConstant.ALIPAY_PUBLIC_KEY,"RSA2");
		AlipayFundTransOrderQueryResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			System.out.println("调用成功");
		} else {
			System.out.println("调用失败");
		}
		return response;
	}

	
}

Constant类

/**
 * 包名称 com.zxtg.zshop.alipay.constant
 * 类名称 AlipayConstant.java
 * 创建时间 2019-4-24下午5:10:57
 *
 *
 */
package com.zxtg.zshop.alibaba.alipay.constant;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @类名称 AlipayConstant.java
 * @类描述 

 * @创建时间 2019-4-24 下午5:10:57
 * @版本 1.00
 *
 * @修改记录
 * 
 *     版本		 修改人 		修改日期 		 修改内容描述
 *     ----------------------------------------------
 *     1.00 	yzh 	 2019-4-24             
 *     ----------------------------------------------
 * 
*/ public class AlipayConstant { public static class AlipayGrantType { public static final String AUTH_CODE = "authorization_code"; public static final String REFRESH_CODE = "refresh_token"; } public final static String APP_ID = "12154646121315";//自己的app_id public static String APP_PRIVATE_KEY = ""; public static String ALIPAY_PUBLIC_KEY= ""; public final static String APP_PRIVATE_KEY_PATH = "/cert_alipay/rsa_private_key_pkcs8.pem"; public final static String ALIPAY_PUBLIC_KEY_PATH= "/cert_alipay/alipay_rsa_public_key.pem"; public final static String CHARSET = "utf-8"; public static final String SCOPE_AUTH_USER = "auth_user"; public final static String GATE_WAY ="https://openapi.alipay.com/gateway.do"; public static final String AUTHORIZE_URL = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=APPID&scope=SCOPE&redirect_uri=ENCODED_URL&state=STATE"; static{ APP_PRIVATE_KEY = getKeyFileStr(APP_PRIVATE_KEY_PATH); ALIPAY_PUBLIC_KEY = getKeyFileStr(ALIPAY_PUBLIC_KEY_PATH); } public static String getKeyFileStr(String filePath){ StringBuffer buffer = new StringBuffer(); try{ InputStream instream = AlipayConstant.class .getResourceAsStream(filePath); InputStreamReader inputStreamReader = new InputStreamReader( instream, "utf-8"); BufferedReader bufferedReader = new BufferedReader( inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { if(str.indexOf("---")<0){ buffer.append(str); } } bufferedReader.close(); inputStreamReader.close(); // 释放资源 instream.close(); }catch (Exception e) { e.printStackTrace(); } return buffer.toString(); } }

页面可调用 window.location.href = www.baidu.com + "/weChat/redirect?requestUrl=" + url;

url 为当前授权完需要返回的页面,授权完成之后跳转至url。

--------------------------------------------------来自不正经的程序袁 For the Peace and Love-------------------------------------------------

你可能感兴趣的:(后端,JAVA,支付宝授权)