java app绑定支付宝 生成授权请求信息字串

 

前端通过授权请求信息字串获取支付宝用户信息,后台保存用户信心实现体现功能,有完整版和极简版

package com.framework.loippi.plugin.aliapppay.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.framework.loippi.cache.ConfigCache;
import com.framework.loippi.plugin.aliapppay.sign.Base64;
import com.framework.loippi.plugin.wxapppay.MD5;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;

public class AliPayInfoStrUtil {
/**
* 获取授权请求信息字串
* @return
*/
public static String getInfoStr(){

StringBuilder infoStr=new StringBuilder();
infoStr.append("apiname=com.alipay.account.auth");
infoStr.append("&app_id="+你的appId);
infoStr.append("&app_name=mc");
infoStr.append("&auth_type=AUTHACCOUNT");
infoStr.append("&biz_type=openservice");
infoStr.append("&method=alipay.open.auth.sdk.code.get");
infoStr.append("&pid="+支付宝合作商家的PID);//2088开头的合作伙伴id 开发平台中查找
infoStr.append("&product_id=APP_FAST_LOGIN");
infoStr.append("&scope=kuaijie");
infoStr.append("&sign_type=RSA2");
infoStr.append("&target_id="+genNonceStr()); //target_id 随机字符即可 表示每次都是特定的一次请求
String sign="";
try {
//签名
sign = AlipaySignature.rsaSign(infoStr.toString(), ConfigCache.getConfig("alipay.shop.privateKey"),"UTF-8","RSA2");
} catch (AlipayApiException e1) {
e1.printStackTrace();
}
try {
//官方要求URL处理
infoStr.append("&sign="+ URLEncoder.encode(sign,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return infoStr.toString();
}

/**
* 生成32位随机数字
*/
public static String genNonceStr() {
Random random = new Random();
return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
}

/**
* 获取极简版授权请求字符串
* @return
*/
public static String getSimpleInfoStr(){
String result ="";
try {
String genNonceStr = genNonceStr();
byte[] bytes = genNonceStr.getBytes("UTF-8");
result = "https://authweb.alipay.com/auth?auth_type=PURE_OAUTH_SDK&app_id=你的appid&scope=auth_user&state=" + Base64.encode(bytes);
}catch (Exception e){
e.printStackTrace();
}
return result;
}


}
上一篇:支付宝支付原理


下一篇:tp5.1配置支付宝支付