Commit 4b47d9e6 by 彭祥礼

微信支付调整

parent 20dd9273
......@@ -102,10 +102,15 @@ public class YxtCardController extends BaseController {
public ResponseData<Map<String,Object>> enterPay(String code) throws Exception{
Map<String,Object> objectMap = new HashMap<>();
String openIdt = "";
String result = WxInterfacesUtil.getAccess_tokenByCode(code);
String openIdt = redisTemplate.opsForValue().get("openid").toString();
Object object = redisTemplate.opsForValue().get("openid");
if(object != null){
openIdt = object.toString();
}
if (StringUtils.isNotBlank(result)) {
JSONObject obj = JSONObject.fromObject(result);
redisTemplate.opsForValue().set("access_token", obj.get("access_token"), 120,TimeUnit.MINUTES);
String openId = String.valueOf(obj.get("openid"));
// code重复使用时,从session拿出第一次保存openId赋予openId
if("null".equals(openId) && !"null".equals(openIdt)){
......@@ -134,9 +139,14 @@ public class YxtCardController extends BaseController {
public ResponseData<Map<String,Object>> getJsapiSign(HttpServletRequest request){
// 加密参数
String url = "http://" + request.getServerName()+request.getRequestURI();
String access_token = "";
Object obj = redisTemplate.opsForValue().get("access_token");
Map<String,Object> objectMap = null;
try{
objectMap = new WxInterfacesUtil().getJspaiSign(url);
if(obj != null){
access_token = obj.toString();
}
objectMap = new WxInterfacesUtil().getJspaiSign(url,access_token);
objectMap.put("appid", WxConfig.APPID);
return ResponseData.success(objectMap);
}catch(Exception e){
......@@ -293,11 +303,9 @@ public class YxtCardController extends BaseController {
payRequest.setSpbill_create_ip("120.24.88.216");
payRequest.setNotify_url(WxConfig.NOTIFY_URL);
payRequest.setTrade_type("JSAPI");
//临时测试用
payRequest.setOpenid("o22lhwRZP2zbXff7UHH3J8oqH8A0");
//payRequest.setOpenid("o22lhwRZP2zbXff7UHH3J8oqH8A0");//临时测试用
log.info("pay——openid:"+openId);
//payRequest.setOpenid(openId);
payRequest.setOpenid(openId);
// 统一下单加密参数
SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
......
......@@ -5,7 +5,9 @@ import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
......@@ -283,39 +285,36 @@ public class HTTPSClient {
// 原方法 不走代理
// public static String sendGet(String url) {
// String result = "";
// HttpClient client = new HttpClient();
// GetMethod getMethod = new GetMethod(url);
// getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 25000);
// try {
// int statusCode = client.executeMethod(getMethod);
// if (statusCode == HttpStatus.SC_OK) {
// result = getMethod.getResponseBodyAsString(); // 出现中文乱码
// // result = new String(getMethod.getResponseBody(),"utf-8");
// }
//
// } catch (ClientProtocolException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (IllegalStateException e) {
// e.printStackTrace();
// }
//
// return result;
// }
public static String sendGet(String url) {
String result = "";
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 25000);
try {
int statusCode = client.executeMethod(getMethod);
if (statusCode == HttpStatus.SC_OK) {
result = getMethod.getResponseBodyAsString(); // 出现中文乱码
// result = new String(getMethod.getResponseBody(),"utf-8");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
return result;
}
// 新方法 走代理
public static String sendGet(String url) {
public static String sendGet2(String url) {
String string = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
try {
HttpHost proxy = new HttpHost("172.18.101.170", 3128);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(200000).setConnectTimeout(200000).setProxy(proxy).build();
httpPost.setConfig(requestConfig);
CloseableHttpResponse result = httpClient.execute(httpPost);
string = EntityUtils.toString(result.getEntity(), "utf-8"); ;
......@@ -327,7 +326,6 @@ public class HTTPSClient {
} catch (IllegalStateException e) {
e.printStackTrace();
}
return string;
}
......
......@@ -9,7 +9,7 @@ public class WxConfig {
/**************公众号参数信息*************/
// 公众号appid 校园服务号
//public static final String APPID = "wx0641dc1dc4d34384";
public static final String APPID = "wx0641dc1dc4d34384";
// 公众号秘钥 secret 校园服务号
public static final String APPSECRET = "3b43b46fc94d4e98588ee6ad992fa5c7";
......@@ -19,7 +19,7 @@ public class WxConfig {
public static final String NOTIFY_URL = "http://dianyuanjiangli.com/xyjl/yx/wxpay/yxt!payNotify.action";
// // 公众号appid 网讯测试服务号
public static final String APPID = "wxfc18f5186b729d15";
// public static final String APPID = "wxfc18f5186b729d15";
//
// // 公众号秘钥 secret 网讯测试服务号
// public static final String APPSECRET = "122278f3fb555468848ff040620505ad";
......
package com.winsun.utils;
import net.sf.ehcache.Element;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
......@@ -15,7 +15,6 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class WxInterfacesUtil {
......@@ -26,27 +25,21 @@ public class WxInterfacesUtil {
/**
* 获取凭证
* @param grant_type 获取access_token填写client_credential
* @param appid 第三方用户唯一凭证
* @paredisTemplateram appid 第三方用户唯一凭证
* @param secret 第三方用户唯一凭证密钥,既appsecret
* @return json token:获取到的凭证 expires_in:凭证有效时间,单位:秒
*/
public String getToken(String grant_type,String appid,String secret){
public String getToken(String grant_type,String appid,String secret,String access_token){
String result = null;
Element ele = null;//tokenCache.get("token_cache");
if(ele!=null){
result = (String)ele.getValue();
if(StringUtils.isNotBlank(access_token)){
result = access_token;
}else{
String url="https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+appid+"&secret="+secret;
String res = HTTPSClient.sendGet(url);
JSONObject jobj = JSONObject.fromObject(res);
if(jobj.has("access_token")){
result = jobj.getString("access_token");
}
ele = new Element("token_cache",result);
//两小时内有效
redisTemplate.opsForValue().set("token_cache", ele, 120,TimeUnit.MINUTES);
//tokenCache.put(ele);
}
return result;
......@@ -106,20 +99,21 @@ public class WxInterfacesUtil {
return result;
}
public Map<String,Object> getJspaiSign(String url) {
public Map<String,Object> getJspaiSign(String url,String access_token) {
Map<String,Object> jsonObject = new HashMap<>();
// 时间戳
long timeStamp = System.currentTimeMillis();
// 随机字符串
String nonceStr = Integer.toString(((int) ((Math.random() + 1) * 1000000)));
// 凭证
String accessToken = getToken("client_credential",WxConfig.APPID,WxConfig.APPSECRET);
if(StringUtils.isBlank(access_token)){
access_token = getToken("client_credential",WxConfig.APPID,WxConfig.APPSECRET,access_token);
}
jsonObject.put("access_token", access_token);
// 票据
String ticket = getJsapiTicket(accessToken);
String ticket = getJsapiTicket(access_token);
String signature = "";
String params = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "&timestamp=" + timeStamp + "&url=" + url;
try {
// 获取签名
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
......@@ -470,7 +464,7 @@ public class WxInterfacesUtil {
* @return
*/
public static String getAccess_tokenByCode(String code){
//https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WxConfig.APPID+"&secret="+WxConfig.APPSECRET+"&code="+code+"&grant_type=authorization_code";
String result="";
result = HTTPSClient.sendGet(url);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment