Commit 4b47d9e6 by 彭祥礼

微信支付调整

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