private String code;
private String timeStamp;
private String nonceStr;
private String ftlUrl;
* ajax请求拿到 相关地址信息
* @return
* @throws Exception
*/
public String getWeiXingInfo() throws Exception{
WeixinPayRequestHandler handler=new WeixinPayRequestHandler();
String key=WeixinpayConfig.getKey(this.getSiteEnum());
String appId = WeixinpayConfig.getAppid(this.getSiteEnum());
String timestamp = String.valueOf(System.currentTimeMillis()/1000);
String nonceStr = UUID.randomUUID().toString();
String access_tocken = getCurrentUser().getAccessToken();
String openid = getCurrentUser().getWebKey();
// handler.setKey(key);
// checkTocken(access_tocken, openid);
// handler.setParameter("appId", appId);
// handler.setParameter("url", "http://minitimes.oicp.net:54293/frontend/cart/view.php");
// handler.setParameter("timestamp", timestamp);
// handler.setParameter("noncestr", nonceStr);
// handler.setParameter("access_token", access_tocken);
// String addrSign=handler.createPaySign();
String url = "http://yangmenghello.xicp.net:14561/frontend/checkout/view.php";
String str = "accesstoken="+access_tocken+"&appid="+appId+"&noncestr="+nonceStr+"×tamp="+timeStamp+"&url="+url;;
String addrSign1 = SHA1(str.toLowerCase());
resultMap.put("appId", appId);
resultMap.put("addrSign", addrSign1);
resultMap.put("timestamp", timestamp);
resultMap.put("nonceStr", nonceStr);
resultMap.put("dd", "dd");
return "json";
}
/** 检查 checkTocken 是否正确 */
public void checkTocken(String access_token,String openid) throws IOException{
String refresh_tocken = getCurrentUser().getRefreshToken();
URL url = new URL("https://api.weixin.qq.com/sns/auth?access_token="+access_token+"&openid="+openid);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
JSONObject json = JSONObject.fromObject(in.readLine());
if(!json.get("errcode").equals(0)){
this.refreshTocken(openid,refresh_tocken);
System.out.println("密钥验证失败");
}else{
System.out.println("密钥验证成功");
}
}
/** 刷新 refreshTocken */
public void refreshTocken(String appid,String refresh_tocken) throws IOException{
URL url = new URL("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+appid+"&grant_type=refresh_token&refresh_token="+refresh_tocken);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
JSONObject json = JSONObject.fromObject(in.readLine());
if(!json.get("errcode").equals(40029)){
getCurrentUser().setAccessToken(String.valueOf(json.get("access_token")));
}
}
/**
* SHA1加密成 addrsign
*/
public String SHA1(String decript) {
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
public String getTockenByCode() throws IOException{
if(code == null){
resultMap.put("status", false);
}else{
URL url1 = new URL("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WeiXinLoginMConfig.getWeiXinAppID(SiteEnum.LOVO)
+"&secret="+WeiXinLoginMConfig.getWeiXinAppSecret(SiteEnum.LOVO)
+"&code="+code
+"&grant_type=authorization_code");
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
JSONObject json = JSONObject.fromObject(in.readLine());
if(json.get("access_token")==null){
resultMap.put("status", false);
}else{
System.out.println("-------------------------------access_tocken:"+json.get("access_token").toString());
String appId = WeixinpayConfig.getAppid(this.getSiteEnum());
checkTocken(json.get("access_token").toString(), json.get("openid").toString());
String str = "accesstoken="+getCurrentUser().getAccessToken()+"&appid="+appId+"&noncestr="+nonceStr+"×tamp="+timeStamp+"&url="+ftlUrl;
String addrSign = SHA1(str);
resultMap.put("access_token",getCurrentUser().getAccessToken());
resultMap.put("refresh_token",json.get("refresh_token"));
resultMap.put("openid",json.get("openid"));
resultMap.put("unionid",json.get("unionid"));
resultMap.put("scope",json.get("scope"));
resultMap.put("addrSign",addrSign);
resultMap.put("status",true);
System.out.println("---------------------------------------------------------------输出迷药= "+addrSign);
System.out.println("---------------------------------------------------------------输出常量 = "+nonceStr);
System.out.println("---------------------------------------------------------------输出常量 = "+timeStamp);
System.out.println("---------------------------------------------------------------输出常量 = "+appId);
}
}
System.out.println("---------------------------------------------------------------输出url= "+ftlUrl);
return "json";
}
微信相关地址加密
最新推荐文章于 2020-06-27 01:16:10 发布