pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/mp/util/WXPayUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/RequestMethod.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/SimpleTool.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/XmlUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pom.xml
@@ -15,6 +15,13 @@ <dependencies> <!--对象与XML处理--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <!--腾讯云 cos--> <dependency> <groupId>com.qcloud</groupId> 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 是否分账:N不,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,NATIVE,APP 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); // 设置发送类型POST 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);// 解析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、retmsg看失败详情。 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、retmsg看失败详情。 returnObj.put("status", "ERROR-B"); returnObj.put("errMsg", errMsg); } } else { // 有可能因为网络原因,请求已经处理,但未收到应答。 returnObj.put("status", "ERROR-A"); returnObj.put("errMsg", httpClient.getResponseCode() + ":" + httpClient.getErrInfo()); } // 获取debug信息,建议把请求、应答内容、debug信息,通信返回码写入日志,方便定位问题 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){ 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; } /** * 发送Post请求 * @param path 请求路径 * @param content 发送内容 * @param header 请求头参数设置 * @param contentType 请求类型,默认application/x-www-form-urlencoded * @param encoding 编码,默认UTF-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的集合 然后在遍历key即可 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; } /** * 发送Post请求 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 特定格式的JSONObject src/main/java/com/hx/util/XmlUtil.java
New file @@ -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(); // 格式化xml输出的格式 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头声明信息 // 将对象转换成输出流形式的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(); // 格式化xml输出的格式 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头声明信息 // 将对象转换成输出流形式的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") /** * 将String类型的xml转换成对象 */ public static Object convertXmlStrToObject(Class clazz, String xmlStr) { Object xmlObject = null; try { JAXBContext context = JAXBContext.newInstance(clazz); // 进行将Xml转成对象的核心接口 Unmarshaller unmarshaller = context.createUnmarshaller(); StringReader sr = new StringReader(xmlStr); xmlObject = unmarshaller.unmarshal(sr); } catch (JAXBException e) { e.printStackTrace(); } return xmlObject; } @SuppressWarnings("unchecked") /** * 将file类型的xml转换成对象 */ 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; } }