package com.pmh.common.service.impl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.xzh.common.model.Bind;
import com.xzh.common.service.UnionPayServiceInf;
import com.xzh.common.util.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Service
@Transactional
public class UnionPayServiceImpl implements UnionPayServiceInf {
private static final Logger logger = Logger.getLogger(UnionPayServiceImpl.class);
//应用Id
private static final String appId = ResourcesConfig.getValue(“appId”);
//密钥
private static final String secret = ResourcesConfig.getValue(“secret”);
//对称密钥
private static final String symmetricKey = ResourcesConfig.getValue(“symmetricKey”);
@Override
public ResultBody getLoginUserAuth(String code) throws Exception {
//获取backendToken
String backendToken = getBackendToken();
// 获取accessToken和OpenId
Map<String,String> accMap = getAccessTokenAndOpenID(backendToken,code);
//获取用户实名信息
return getUserAuth(backendToken,accMap.get("accessToken"),accMap.get("openId"));
}
public Map<String,String> getAccessTokenAndOpenID(String backendToken, String code) throws Exception{
Map<String,String> map = new HashMap<String,String>();
//调用地址
String url = "https://open.95516.com/open/access/1.0/token";
//拼装参数Map
Map<String, String> params = new HashMap<String, String>();
params.put("appId", appId);//应用ID
params.put("backendToken", backendToken);
//登录获取的code
params.put("code", code);
//接入方直接填写常量字符串authorization_code
params.put("grantType", "authorization_code");
//发送请求
String resultStr = Utils.sendPostGson(url, params);
System.out.println("-------获取accessToken和openId:"+resultStr+"---------");
//解析返回的json
JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
map.put("accessToken",jsonObject.get("params").getAsJsonObject().get("accessToken").getAsString());
map.put("openId",jsonObject.get("params").getAsJsonObject().get("openId").getAsString());
return map;
}
/**
* 获取用户实名信息
* @param backendToken
* @param accessToken 登录用户token
* @param openId 用户ID
* @return
*/
public ResultBody getUserAuth(String backendToken,String accessToken,String openId) throws Exception{
//调用接口地址
String mobileUrl = "https://open.95516.com/open/access/1.0/user.mobile";
//调用接口地址
String url = "https://open.95516.com/open/access/1.0/user.auth";
//拼装参数Map
Map<String, String> params = new HashMap<String, String>(0);
params.put("appId", appId);
params.put("accessToken", accessToken);
params.put("openId", openId);
params.put("backendToken", backendToken);
//发送请求
String mobileResultStr = Utils.sendPostGson(mobileUrl, params);
//发送请求
String resultStr = Utils.sendPostGson(url, params);
System.out.println("-------获取用户敏感信息-姓名+证件号:"+resultStr+"---------");
//解析返回的json
JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
Map map=new HashMap();
//解析接口返回的json
String realName= jsonObject.get("params").getAsJsonObject().get("realName").getAsString();
String certId= jsonObject.get("params").getAsJsonObject().get("certId").getAsString();
//解析返回的json
JsonObject jsonObject2 = (JsonObject) new JsonParser().parse(mobileResultStr);
//判断是否返回成功
//解析接口返回的json
String mobile = jsonObject2.get("params").getAsJsonObject().get("mobile").getAsString();
//解密后手机号,使用对称密钥进行解密
mobile = Utils.getDecryptedValue(mobile,symmetricKey);
//解密后用户实名信息,使用对称密钥进行解密
realName = Utils.getDecryptedValue(realName,symmetricKey);
certId = Utils.getDecryptedValue(certId,symmetricKey);
int bindId = (int) System.currentTimeMillis();
Bind bind=new Bind();
bind.setaName(realName);
bind.setName(realName);
bind.setCardnumber(certId);
bind.setMobilephone(mobile);
bind.setaCardNumber(certId);
bind.setId(bindId);
map.put("bind", bind);
String token = UUID.randomUUID().toString().replaceAll("-", "");
ApiCache.setToken(bindId,token);
return new ResultBody(JsonKey.SUCCESS, "登录成功", map, token);
}
/**
* 获取用户手机号
* @param backendToken
* @param accessToken 登录用户token
* @param openId 用户ID
* @return
*/
public String getMobile(String backendToken,String accessToken,String openId) throws Exception{
//调用接口地址
String url = "https://open.95516.com/open/access/1.0/user.mobile";
//拼装参数Map
Map<String, String> params = new HashMap<String, String>(0);
params.put("appId", appId);//应用ID
params.put("accessToken", accessToken);//登录token
params.put("openId", openId);//用户ID
params.put("backendToken", backendToken);//backendToken
//发送请求
String resultStr = Utils.sendPostGson(url, params);
System.out.println("-------获取用户敏感信息-登录手机号:"+resultStr+"---------");
//解析返回的json
JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
//判断是否返回成功
if(!"00".equals(jsonObject.get("resp").getAsString())){
return jsonObject.get("msg").getAsString();
}else{
//解析接口返回的json
String mobile = jsonObject.get("params").getAsJsonObject().get("mobile").getAsString();
//解密后手机号,使用对称密钥进行解密
String deMobile = Utils.getDecryptedValue(mobile,symmetricKey);
return deMobile;
}
}
/**
* 获取backendToken
*
* @return
*/
public static String getBackendToken() {
String url = "https://open.95516.com/open/access/1.0/backendToken";
//拼装参数Map
Map<String, String> params = new HashMap<String, String>();
params.put("appId", appId);//应用ID
params.put("secret", secret);//应用密钥
//生成随即字符串
String nonceStr = Utils.createNonceStr();
params.put("nonceStr", nonceStr);
//生成时间戳
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
params.put("timestamp", timestamp);
//签名 (appId,secret,nonceStr,timestamp)
String value = Utils.sortMap(params);//排序
String signature = Utils.sha256(value.getBytes());//签名加密
params.put("signature", signature);
String param = new Gson().toJson(params);
System.out.println("生成入参和签名:" + param);
//发送请求
String resultStr = Utils.sendPostGson(url, params);
System.out.println("-------获取backendToken信息:" + resultStr + "---------");
//解析返回的json
JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
return jsonObject.get("params").getAsJsonObject().get("backendToken").getAsString();
}
}