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