From 2ad84337e1ae42b56a7b8e463bca9ac06e457826 Mon Sep 17 00:00:00 2001
From: E1ED922C1E9526DD63272D7EC5C6CB77 <E1ED922C1E9526DD63272D7EC5C6CB77@i-search.com.cn>
Date: 星期日, 07 二月 2021 09:10:22 +0800
Subject: [PATCH] Merge branch 'master' of https://gitee.com/huoxiong/hx_common

---
 src/main/java/com/hx/mp/util/WXPayUtil.java  |  148 ++++++++++++++++++++++++
 src/main/java/com/hx/util/SimpleTool.java    |   40 ++++++
 src/main/java/com/hx/util/XmlUtil.java       |  113 ++++++++++++++++++
 pom.xml                                      |    7 +
 src/main/java/com/hx/util/RequestMethod.java |   47 +++++++
 5 files changed, 354 insertions(+), 1 deletions(-)

diff --git a/pom.xml b/pom.xml
index ab59e4d..7a164ce 100644
--- a/pom.xml
+++ b/pom.xml
@@ -15,6 +15,13 @@
 
     <dependencies>
 
+        <!--瀵硅薄涓嶺ML澶勭悊-->
+        <dependency>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
+        </dependency>
+
+
         <!--鑵捐浜� cos-->
         <dependency>
             <groupId>com.qcloud</groupId>
diff --git a/src/main/java/com/hx/mp/util/WXPayUtil.java b/src/main/java/com/hx/mp/util/WXPayUtil.java
index 3006e70..f363886 100644
--- a/src/main/java/com/hx/mp/util/WXPayUtil.java
+++ b/src/main/java/com/hx/mp/util/WXPayUtil.java
@@ -251,6 +251,154 @@
         return returnObj;
     }
 
+    /**缁熶竴鏀粯锛堝垎璐︼級
+     * @param request 鏂规硶鑾峰彇
+     * @param appId  灏忕▼搴忓彿
+     * @param partner  鍟嗘埛鍙�
+     * @param key  绉橀挜
+     * @param notifyUrl  鍥炶皟閾炬帴
+     * @param out_trade_no  璁㈠崟鍙�
+     * @param body 鍟嗗搧鎻忚堪
+     * @param total_fee 鏀粯閲戦
+     * @param openid 鐢ㄦ埛openId
+     * @param attach 闄勫甫鏁版嵁鍖�
+     * @param notifyUrl 鍥炶皟閫氱煡鍦板潃
+     * @param trade_type 浜ゆ槗绫诲瀷
+     * @param profit_sharing 鏄惁鍒嗚处锛歂涓嶏紝Y鏄�
+     * @return JSON  status = "SUC"涓烘垚鍔�
+     */
+    public static JSONObject unifiedPay(HttpServletRequest request,String appId,String partner,String key,String notifyUrl,String out_trade_no, String body, String total_fee, String openid,
+                                        String attach,String trade_type,String profit_sharing) throws Exception {
+
+        if (!SimpleTool.checkNotNull(notifyUrl)) {
+            throw new ServiceException("鏀粯鍔熻兘鏁呴殰锛�");
+        }
+
+        // 鍒涘缓鏌ヨ璇锋眰瀵硅薄
+        RequestHandler reqHandler = new RequestHandler(null, null);
+        // 閫氫俊瀵硅薄
+        TenpayHttpClient httpClient = new TenpayHttpClient();
+        // 搴旂瓟瀵硅薄
+        ClientResponseHandler resHandler = new ClientResponseHandler();
+
+        // -----------------------------
+        // 璁剧疆璇锋眰鍙傛暟
+        // -----------------------------
+        // reqHandler.init();
+        reqHandler.setKey(key);
+        reqHandler.setGateUrl(FIRST_ORDER_URL);// 璇锋眰URL
+
+        // -----------------------------
+        // 璁剧疆鎺ュ彛鍙傛暟(sign鍚庡彴鑷姩鐢熸垚)
+        // -----------------------------
+
+        reqHandler.setParameter("appid", appId); // 鍏紬鍙�/灏忕▼搴�
+        reqHandler.setParameter("mch_id", partner); // 鍟嗘埛鍙�
+        reqHandler.setParameter("nonce_str", SimpleTool.getUUIDName().substring(0, 30));// 闅忔満涔辩爜
+        reqHandler.setParameter("body", body);// 鍟嗗搧鎻忚堪
+        reqHandler.setParameter("out_trade_no", out_trade_no);// 鍟嗘埛璁㈠崟鍙�
+        reqHandler.setParameter("total_fee", total_fee);// 鎬婚噾棰�
+        reqHandler.setParameter("spbill_create_ip", "8.8.8.8");// 缁堢IP
+        reqHandler.setParameter("notify_url",notifyUrl);// 閫氱煡鍦板潃
+        reqHandler.setParameter("trade_type", trade_type);// 浜ゆ槗绫诲瀷
+        //JSAPI锛孨ATIVE锛孉PP
+        reqHandler.setParameter("openid", openid);// openId
+        reqHandler.setParameter("attach", attach);// 闄勫甫鏁版嵁鍖�
+        reqHandler.setParameter("profit_sharing", profit_sharing);// 闄勫甫鏁版嵁鍖�
+
+        // -----------------------------
+        // 璁剧疆閫氫俊鍙傛暟
+        // -----------------------------
+        // 璁剧疆璇锋眰杩斿洖鐨勭瓑寰呮椂闂�
+        httpClient.setTimeOut(5);
+
+        // 璁剧疆ca璇佷功
+        // httpClient.setCaInfo(new File(CA_PATH));
+
+        // 璁剧疆涓汉(鍟嗘埛)璇佷功
+        // httpClient.setCertInfo(new File(CERT_PATH), CERT_PWD);
+
+        // 璁剧疆鍙戦�佺被鍨婸OST
+        httpClient.setMethod("POST");
+
+        // 璁剧疆璇锋眰鍐呭(鐢熸垚sign)
+        String requestUrl = reqHandler.getRequestURL();// 缁勬嫾https://www.baidu.com?a=x&b=xx
+
+        httpClient.setReqContent(requestUrl);// https://www.baidu.com?a=x&b=xx
+        String rescontent = "null";
+
+        httpClient.setRequestHandler(reqHandler);// 鎶婂鐞嗗璞★紝鍍忔槸鍙傛暟鍚勭涓滆タ閮借缃繘鍘绘柟渚胯幏鍙�(quan)
+
+        // 杩斿洖鍑哄幓鐨勫璞�(鐘舵�侊紝閿欒鍘熷洜锛岃鎿嶄綔鐩稿叧淇℃伅(鍙傛暟,杩斿洖鍊�))
+        JSONObject returnObj = new JSONObject();
+
+        // 鍚庡彴璋冪敤
+        if (httpClient.call()) {
+            System.out.println("缁熶竴涓嬪崟,鎴愬姛cll浜�::");
+
+            // 璁剧疆缁撴灉鍙傛暟
+            rescontent = httpClient.getResContent();
+            System.out.println("缁熶竴涓嬪崟杩斿洖缁撴灉:" + rescontent);
+            resHandler.setContent(rescontent);// 瑙f瀽xml
+            resHandler.setKey(key);
+
+            // 鑾峰彇杩斿洖鍙傛暟
+            String return_code = resHandler.getParameter("return_code");
+            String return_msg = resHandler.getParameter("return_msg");
+
+            // 鍒ゆ柇绛惧悕鍙婄粨鏋�
+            if (resHandler.isTenpaySign() && "SUCCESS".equals(return_code)) {
+                String prepay_id = resHandler.getParameter("prepay_id");// 棰勬敮浠樹氦鏄撲細璇濇爣璇�
+                String code_url = resHandler.getParameter("code_url");// 浜岀淮鐮侀摼鎺�
+
+                String result_code = resHandler.getParameter("result_code");// 涓氬姟缁撴灉
+                String appid = resHandler.getParameter("appid");// 鍏紬璐﹀彿ID
+                String mch_id = resHandler.getParameter("mch_id");// 鍟嗘埛鍙�
+                String nonce_str = resHandler.getParameter("nonce_str");// 闅忔満鐮�
+                String sign = resHandler.getParameter("sign");// 绛惧悕
+
+                if (result_code.equals("SUCCESS")) {
+                    returnObj.put("status", "suc");
+                    returnObj.put("sign", sign);
+                    returnObj.put("nonce_str", nonce_str);
+                    returnObj.put("mch_id", mch_id);
+                    returnObj.put("appid", appid);
+                    returnObj.put("prepay_id", prepay_id);
+                    returnObj.put("code_url", code_url);
+                    returnObj.put("out_trade_no", out_trade_no);
+                } else {
+                    String errMsg = "[ERROR]result_code:" + resHandler.getParameter("result_code") + " err_code:"
+                            + resHandler.getParameter("err_code") + "err_code_des:"
+                            + resHandler.getParameter("err_code_des");
+
+                    // 閿欒鏃讹紝杩斿洖缁撴灉鏈鍚嶏紝璁板綍retcode銆乺etmsg鐪嬪け璐ヨ鎯呫��
+                    returnObj.put("status", "ERROR-C");
+                    returnObj.put("errMsg", errMsg);
+                }
+            } else {
+                String errMsg = "return_code:" + return_code + "err_code:" + resHandler.getParameter("err_code")
+                        + " return_msg:" + return_msg;
+                // 閿欒鏃讹紝杩斿洖缁撴灉鏈鍚嶏紝璁板綍retcode銆乺etmsg鐪嬪け璐ヨ鎯呫��
+                returnObj.put("status", "ERROR-B");
+                returnObj.put("errMsg", errMsg);
+            }
+        } else {
+            // 鏈夊彲鑳藉洜涓虹綉缁滃師鍥狅紝璇锋眰宸茬粡澶勭悊锛屼絾鏈敹鍒板簲绛斻��
+            returnObj.put("status", "ERROR-A");
+            returnObj.put("errMsg", httpClient.getResponseCode() + ":" + httpClient.getErrInfo());
+        }
+
+        // 鑾峰彇debug淇℃伅,寤鸿鎶婅姹傘�佸簲绛斿唴瀹广�乨ebug淇℃伅锛岄�氫俊杩斿洖鐮佸啓鍏ユ棩蹇楋紝鏂逛究瀹氫綅闂
+        String detail = "http res:" + httpClient.getResponseCode() + "," + httpClient.getErrInfo() + ";" + "req url:"
+                + requestUrl + ";" + ";" + "req debug:" + reqHandler.getDebugInfo() + ";" + "res content:" + rescontent
+                + ";" + "res debug:" + resHandler.getDebugInfo() + ";";
+
+        returnObj.put("detail", detail);
+
+
+        return returnObj;
+    }
+
     /**澶勭悊淇℃伅
      */
     public static JSONObject paymentData(JSONObject payObj,String key){
diff --git a/src/main/java/com/hx/util/RequestMethod.java b/src/main/java/com/hx/util/RequestMethod.java
index 9c0b36b..6aa7102 100644
--- a/src/main/java/com/hx/util/RequestMethod.java
+++ b/src/main/java/com/hx/util/RequestMethod.java
@@ -62,7 +62,7 @@
 			conn.setDoOutput(true);//鍏佽瀵瑰杈撳嚭鏁版嵁
 			conn.setDoInput(true);
 			conn.setUseCaches(false);
-			conn.setRequestProperty("Charset", "utf-8");
+			conn.setRequestProperty("Charset", encoding);
 			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 			//conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
 			OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
@@ -76,6 +76,51 @@
 		}
         return null;
     }
+
+	/**
+	 * 鍙戦�丳ost璇锋眰
+	 * @param path 璇锋眰璺緞
+	 * @param content 鍙戦�佸唴瀹�
+	 * @param header 璇锋眰澶村弬鏁拌缃�
+	 * @param contentType 璇锋眰绫诲瀷锛岄粯璁pplication/x-www-form-urlencoded
+	 * @param encoding 缂栫爜锛岄粯璁TF-8
+	 * @return 鏈嶅姟鍣ㄧ鍐呭
+	 */
+	public static String sendPOSTRequest(String path, String content,Map<String,String> header,String contentType, String encoding){
+		//byte[] entity = content.toString().getBytes();//鐢熸垚瀹炰綋鏁版嵁
+		try{
+			if(StringUtils.isNull(contentType)){
+				contentType = "application/x-www-form-urlencoded";
+			}
+			if(StringUtils.isNull(encoding)){
+				encoding = "UTF-8";
+			}
+
+			HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
+			conn.setConnectTimeout(5000);
+			conn.setRequestMethod("POST");
+			conn.setDoOutput(true);//鍏佽瀵瑰杈撳嚭鏁版嵁
+			conn.setDoInput(true);
+			conn.setUseCaches(false);
+			conn.setRequestProperty("Charset", encoding);
+			conn.setRequestProperty("Content-Type",contentType);
+			if(header != null){
+				for(String key:header.keySet()){//keySet鑾峰彇map闆嗗悎key鐨勯泦鍚�  鐒跺悗鍦ㄩ亶鍘唊ey鍗冲彲
+					conn.setRequestProperty(key,header.get(key));
+				}
+			}
+			//conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
+			OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
+			outStream.write(content);
+			outStream.flush();
+			if(conn.getResponseCode() == 200){
+				return StreamUtils.InputStreamTOString(conn.getInputStream());
+			}
+		}catch(Exception e){
+			e.printStackTrace();
+		}
+		return null;
+	}
 	
 	/**
 	 * 鍙戦�丳ost璇锋眰
diff --git a/src/main/java/com/hx/util/SimpleTool.java b/src/main/java/com/hx/util/SimpleTool.java
index 8793c5c..fd0764b 100644
--- a/src/main/java/com/hx/util/SimpleTool.java
+++ b/src/main/java/com/hx/util/SimpleTool.java
@@ -43,6 +43,7 @@
 import net.sourceforge.pinyin4j.PinyinHelper;
 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
+import org.springframework.cglib.beans.BeanMap;
 
 public class SimpleTool {
 
@@ -59,6 +60,45 @@
     }
 
     /**
+     * 灏嗗璞¤鎹负map
+     *
+     * @param bean
+     * @return
+     */
+    public static <T> Map<String, Object> beanToMap(T bean) {
+        Map<String, Object> map = new HashMap<>();
+        if (bean != null) {
+            BeanMap beanMap = BeanMap.create(bean);
+            for (Object key : beanMap.keySet()) {
+                map.put(key + "", beanMap.get(key));
+            }
+        }
+        return map;
+    }
+
+    /**
+     * 灏嗗璞¤鎹负map
+     *
+     * @param bean
+     * @return
+     */
+    public static Map<String, String> beanToMapS(Object bean) {
+        Map<String, String> map = new HashMap<>();
+        if (bean != null) {
+            BeanMap beanMap = BeanMap.create(bean);
+            for (Object key : beanMap.keySet()) {
+                if(beanMap.get(key) != null){
+                    map.put(key + "", beanMap.get(key).toString());
+                }else{
+                    map.put(key + "", "");
+                }
+            }
+        }
+        return map;
+    }
+
+
+    /**
 	 * 鍚庡彴鏍煎紡鏋勫缓杩斿洖鍊兼牸寮忓垪琛�-鍚庡彴鑾峰彇鍒楄〃
 	 * @param count 杩斿洖鎬绘潯鏁�
 	 * @return JSONObject 鐗瑰畾鏍煎紡鐨凧SONObject
diff --git a/src/main/java/com/hx/util/XmlUtil.java b/src/main/java/com/hx/util/XmlUtil.java
new file mode 100644
index 0000000..6ad4c95
--- /dev/null
+++ b/src/main/java/com/hx/util/XmlUtil.java
@@ -0,0 +1,113 @@
+package com.hx.util;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import java.io.*;
+
+public class XmlUtil {
+
+    /**
+     * 灏嗗璞$洿鎺ヨ浆鎹㈡垚String绫诲瀷鐨� XML杈撳嚭
+     *
+     * @param obj
+     * @return
+     */
+    public static String convertToXml(Object obj) {
+        // 鍒涘缓杈撳嚭娴�
+        StringWriter sw = new StringWriter();
+        try {
+            // 鍒╃敤jdk涓嚜甯︾殑杞崲绫诲疄鐜�
+            JAXBContext context = JAXBContext.newInstance(obj.getClass());
+
+            Marshaller marshaller = context.createMarshaller();
+            // 鏍煎紡鍖杧ml杈撳嚭鐨勬牸寮�
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
+                    Boolean.TRUE);
+            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //缂栫爜鏍煎紡
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 鏄惁鏍煎紡鍖栫敓鎴愮殑xml涓�
+            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 鏄惁鐪佺暐xm澶村0鏄庝俊鎭�
+            // 灏嗗璞¤浆鎹㈡垚杈撳嚭娴佸舰寮忕殑xml
+            marshaller.marshal(obj, sw);
+        } catch (JAXBException e) {
+            e.printStackTrace();
+        }
+        return sw.toString();
+    }
+
+    /**
+     * 灏嗗璞℃牴鎹矾寰勮浆鎹㈡垚xml鏂囦欢
+     *
+     * @param obj
+     * @param path
+     * @return
+     */
+    public static void convertToXml(Object obj, String path) {
+        try {
+            // 鍒╃敤jdk涓嚜甯︾殑杞崲绫诲疄鐜�
+            JAXBContext context = JAXBContext.newInstance(obj.getClass());
+
+            Marshaller marshaller = context.createMarshaller();
+            // 鏍煎紡鍖杧ml杈撳嚭鐨勬牸寮�
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
+                    Boolean.TRUE);
+            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //缂栫爜鏍煎紡
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 鏄惁鏍煎紡鍖栫敓鎴愮殑xml涓�
+            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 鏄惁鐪佺暐xm澶村0鏄庝俊鎭�
+            // 灏嗗璞¤浆鎹㈡垚杈撳嚭娴佸舰寮忕殑xml
+            // 鍒涘缓杈撳嚭娴�
+            FileWriter fw = null;
+            try {
+                fw = new FileWriter(path);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            marshaller.marshal(obj, fw);
+        } catch (JAXBException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    /**
+     * 灏哠tring绫诲瀷鐨剎ml杞崲鎴愬璞�
+     */
+    public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
+        Object xmlObject = null;
+        try {
+            JAXBContext context = JAXBContext.newInstance(clazz);
+            // 杩涜灏哫ml杞垚瀵硅薄鐨勬牳蹇冩帴鍙�
+            Unmarshaller unmarshaller = context.createUnmarshaller();
+            StringReader sr = new StringReader(xmlStr);
+            xmlObject = unmarshaller.unmarshal(sr);
+        } catch (JAXBException e) {
+            e.printStackTrace();
+        }
+        return xmlObject;
+    }
+
+    @SuppressWarnings("unchecked")
+    /**
+     * 灏唂ile绫诲瀷鐨剎ml杞崲鎴愬璞�
+     */
+    public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
+        Object xmlObject = null;
+        try {
+            JAXBContext context = JAXBContext.newInstance(clazz);
+            Unmarshaller unmarshaller = context.createUnmarshaller();
+            FileReader fr = null;
+            try {
+                fr = new FileReader(xmlPath);
+            } catch (FileNotFoundException e) {
+                e.printStackTrace();
+            }
+            xmlObject = unmarshaller.unmarshal(fr);
+        } catch (JAXBException e) {
+            e.printStackTrace();
+        }
+        return xmlObject;
+    }
+
+
+}

--
Gitblit v1.8.0