|
@@ -1,238 +0,0 @@
|
|
-package cn.efunbox.base.util;
|
|
|
|
-
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
-
|
|
|
|
-import javax.net.ssl.HttpsURLConnection;
|
|
|
|
-import javax.net.ssl.SSLContext;
|
|
|
|
-import javax.net.ssl.SSLSocketFactory;
|
|
|
|
-import javax.net.ssl.TrustManager;
|
|
|
|
-import java.io.*;
|
|
|
|
-import java.net.ConnectException;
|
|
|
|
-import java.net.URL;
|
|
|
|
-import java.util.*;
|
|
|
|
-
|
|
|
|
-public class PayCommonUtil {
|
|
|
|
- public static String CreateNoncestr(int length) {
|
|
|
|
- String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
- String res = "";
|
|
|
|
- for (int i = 0; i < length; i++) {
|
|
|
|
- Random rd = new Random();
|
|
|
|
- res += chars.indexOf(rd.nextInt(chars.length() - 1));
|
|
|
|
- }
|
|
|
|
- return res;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static String CreateNoncestr() {
|
|
|
|
- String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
- String res = "";
|
|
|
|
- for (int i = 0; i < 16; i++) {
|
|
|
|
- Random rd = new Random();
|
|
|
|
- res += chars.charAt(rd.nextInt(chars.length() - 1));
|
|
|
|
- }
|
|
|
|
- return res;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 是否签名正确,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
|
|
|
|
- * @return boolean
|
|
|
|
- */
|
|
|
|
- public static boolean isTenpaySign(SortedMap<Object, Object> packageParams,String signKey) {
|
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
|
- Set es = packageParams.entrySet();
|
|
|
|
- Iterator it = es.iterator();
|
|
|
|
- while(it.hasNext()) {
|
|
|
|
- Map.Entry entry = (Map.Entry)it.next();
|
|
|
|
- String k = (String)entry.getKey();
|
|
|
|
- String v = (String)entry.getValue();
|
|
|
|
- if(!"sign".equals(k) && null != v && !"".equals(v)) {
|
|
|
|
- sb.append(k + "=" + v + "&");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- sb.append("key=" + signKey);
|
|
|
|
-
|
|
|
|
- //算出摘要
|
|
|
|
- String mysign = MD5.MD5Encode(sb.toString()).toLowerCase();
|
|
|
|
- String tenpaySign = ((String)packageParams.get("sign")).toLowerCase();
|
|
|
|
-
|
|
|
|
- //System.out.println(tenpaySign + " " + mysign);
|
|
|
|
- return tenpaySign.equals(mysign);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @Description:sign签名
|
|
|
|
- * @param parameters 请求参数
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String createSign(SortedMap<String,String> parameters,String signKey){
|
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
|
- Set es = parameters.entrySet();
|
|
|
|
- Iterator it = es.iterator();
|
|
|
|
- while(it.hasNext()) {
|
|
|
|
- Map.Entry<String,String> entry = (Map.Entry)it.next();
|
|
|
|
- String k = entry.getKey();
|
|
|
|
- String v = entry.getValue();
|
|
|
|
- if(null != v && !"".equals(v)
|
|
|
|
- && !"sign".equals(k) && !"key".equals(k)) {
|
|
|
|
- sb.append(k + "=" + v + "&");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- sb.append("key="+signKey);
|
|
|
|
- String sign = MD5.MD5Encode(sb.toString()).toUpperCase();
|
|
|
|
- return sign;
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * @Description:将请求参数转换为xml格式的string
|
|
|
|
- * @param parameters 请求参数
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String getRequestXml(SortedMap<String,String> parameters){
|
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
|
- sb.append("<xml>");
|
|
|
|
- Set es = parameters.entrySet();
|
|
|
|
- Iterator it = es.iterator();
|
|
|
|
- while(it.hasNext()) {
|
|
|
|
- Map.Entry<String,String> entry = (Map.Entry)it.next();
|
|
|
|
- String k = entry.getKey();
|
|
|
|
- String v = entry.getValue();
|
|
|
|
- if ("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)) {
|
|
|
|
- sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");
|
|
|
|
- }else {
|
|
|
|
- sb.append("<"+k+">"+v+"</"+k+">");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- sb.append("</xml>");
|
|
|
|
- return sb.toString();
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * @Description:返回给微信的参数
|
|
|
|
- * @param return_code 返回编码
|
|
|
|
- * @param return_msg 返回信息
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String setXML(String return_code, String return_msg) {
|
|
|
|
- return "<xml><return_code><![CDATA[" + return_code
|
|
|
|
- + "]]></return_code><return_msg><![CDATA[" + return_msg
|
|
|
|
- + "]]></return_msg></xml>";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 发送https请求
|
|
|
|
- * @param requestUrl 请求地址
|
|
|
|
- * @param requestMethod 请求方式(GET、POST)
|
|
|
|
- * @param outputStr 提交的数据
|
|
|
|
- * @return 返回微信服务器响应的信息
|
|
|
|
- */
|
|
|
|
- public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
|
|
|
|
- try {
|
|
|
|
- // 创建SSLContext对象,并使用我们指定的信任管理器初始化
|
|
|
|
- TrustManager[] tm = { new MyX509TrustManager() };
|
|
|
|
- SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
|
- sslContext.init(null, tm, new java.security.SecureRandom());
|
|
|
|
- // 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
|
|
- SSLSocketFactory ssf = sslContext.getSocketFactory();
|
|
|
|
- URL url = new URL(requestUrl);
|
|
|
|
- HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
|
|
|
|
- //conn.setSSLSocketFactory(ssf);
|
|
|
|
- conn.setDoOutput(true);
|
|
|
|
- conn.setDoInput(true);
|
|
|
|
- conn.setUseCaches(false);
|
|
|
|
- // 设置请求方式(GET/POST)
|
|
|
|
- conn.setRequestMethod(requestMethod);
|
|
|
|
- conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
|
|
|
|
- // 当outputStr不为null时向输出流写数据
|
|
|
|
- if (null != outputStr) {
|
|
|
|
- OutputStream outputStream = conn.getOutputStream();
|
|
|
|
- // 注意编码格式
|
|
|
|
- outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
|
- outputStream.close();
|
|
|
|
- }
|
|
|
|
- // 从输入流读取返回内容
|
|
|
|
- InputStream inputStream = conn.getInputStream();
|
|
|
|
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
|
|
|
|
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
|
- String str = null;
|
|
|
|
- StringBuffer buffer = new StringBuffer();
|
|
|
|
- while ((str = bufferedReader.readLine()) != null) {
|
|
|
|
- buffer.append(str);
|
|
|
|
- }
|
|
|
|
- // 释放资源
|
|
|
|
- bufferedReader.close();
|
|
|
|
- inputStreamReader.close();
|
|
|
|
- inputStream.close();
|
|
|
|
- inputStream = null;
|
|
|
|
- conn.disconnect();
|
|
|
|
- return buffer.toString();
|
|
|
|
- } catch (ConnectException ce) {
|
|
|
|
-// log.error("连接超时:{}", ce);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
-// log.error("https请求异常:{}", e);
|
|
|
|
- }
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 发送https请求
|
|
|
|
- *
|
|
|
|
- * @param requestUrl 请求地址
|
|
|
|
- * @param requestMethod 请求方式(GET、POST)
|
|
|
|
- * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
|
|
|
|
- */
|
|
|
|
- public static JSONObject httpsRequest(String requestUrl, String requestMethod) {
|
|
|
|
- JSONObject jsonObject = null;
|
|
|
|
- try {
|
|
|
|
- // 创建SSLContext对象,并使用我们指定的信任管理器初始化
|
|
|
|
- TrustManager[] tm = { new MyX509TrustManager() };
|
|
|
|
- SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
|
- sslContext.init(null, tm, new java.security.SecureRandom());
|
|
|
|
- // 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
|
|
- SSLSocketFactory ssf = sslContext.getSocketFactory();
|
|
|
|
- URL url = new URL(requestUrl);
|
|
|
|
- HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
|
|
|
|
- //conn.setSSLSocketFactory(ssf);
|
|
|
|
- conn.setDoOutput(true);
|
|
|
|
- conn.setDoInput(true);
|
|
|
|
- conn.setUseCaches(false);
|
|
|
|
- conn.setConnectTimeout(3000);
|
|
|
|
- // 设置请求方式(GET/POST)
|
|
|
|
- conn.setRequestMethod(requestMethod);
|
|
|
|
- //conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
|
|
|
|
- // 当outputStr不为null时向输出流写数据
|
|
|
|
- // 从输入流读取返回内容
|
|
|
|
- InputStream inputStream = conn.getInputStream();
|
|
|
|
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
|
|
|
|
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
|
- String str = null;
|
|
|
|
- StringBuffer buffer = new StringBuffer();
|
|
|
|
- while ((str = bufferedReader.readLine()) != null) {
|
|
|
|
- buffer.append(str);
|
|
|
|
- }
|
|
|
|
- // 释放资源
|
|
|
|
- bufferedReader.close();
|
|
|
|
- inputStreamReader.close();
|
|
|
|
- inputStream.close();
|
|
|
|
- inputStream = null;
|
|
|
|
- conn.disconnect();
|
|
|
|
- jsonObject = JSONObject.parseObject(buffer.toString());
|
|
|
|
- } catch (ConnectException ce) {
|
|
|
|
-// log.error("连接超时:{}", ce);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- System.out.println(e);
|
|
|
|
-// log.error("https请求异常:{}", e);
|
|
|
|
- }
|
|
|
|
- return jsonObject;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
- public static String urlEncodeUTF8(String source){
|
|
|
|
- String result = source;
|
|
|
|
- try {
|
|
|
|
- result = java.net.URLEncoder.encode(source,"utf-8");
|
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-}
|
|
|