WechatUtil.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package com.kmall.common.utils.wechat;
  2. import com.alibaba.druid.support.logging.Log;
  3. import com.alibaba.druid.support.logging.LogFactory;
  4. import com.kmall.common.service.pay.wxpay.CommonWxPayPropertiesBuilder;
  5. import com.kmall.common.utils.*;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.config.RequestConfig;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.config.RegistryBuilder;
  14. import org.apache.http.conn.socket.ConnectionSocketFactory;
  15. import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  16. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  17. import org.apache.http.entity.StringEntity;
  18. import org.apache.http.impl.client.CloseableHttpClient;
  19. import org.apache.http.impl.client.HttpClientBuilder;
  20. import org.apache.http.impl.client.HttpClients;
  21. import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
  22. import org.apache.http.util.EntityUtils;
  23. import java.io.IOException;
  24. import java.io.UnsupportedEncodingException;
  25. import java.math.BigDecimal;
  26. import java.math.MathContext;
  27. import java.net.URLEncoder;
  28. import java.text.SimpleDateFormat;
  29. import java.util.*;
  30. /**
  31. * <p>Title: 微信退款工具类</p>
  32. * <p>Description: 微信退款工具类,通过充值客户端的不同初始化不同的工具类,得到相应微信退款相关的appid和muchid</p>
  33. *
  34. * @author xubo
  35. * @date 2017年6月6日 下午5:05:03
  36. */
  37. public class WechatUtil {
  38. private static Log logger = LogFactory.getLog(WechatUtil.class);
  39. /**
  40. * 充值客户端类型--微信公众号
  41. */
  42. public static Integer CLIENTTYPE_WX = 2;
  43. /**
  44. * 充值客户端类型--app
  45. */
  46. public static Integer CLIENTTYPE_APP = 1;
  47. private static final String EMPTY = "";
  48. private static final String URL_PARAM_CONNECT_FLAG = "&";
  49. /**
  50. * 方法描述:微信退款逻辑
  51. * 创建时间:2017年4月12日 上午11:04:25
  52. * 作者: xubo
  53. *
  54. * @param
  55. * @return
  56. */
  57. public static WechatRefundApiResult wxRefund(String out_trade_no, Double orderMoney, Double refundMoney) {
  58. //初始化请求微信服务器的配置信息包括appid密钥等
  59. //转换金钱格式
  60. BigDecimal bdOrderMoney = new BigDecimal(orderMoney, MathContext.DECIMAL32);
  61. BigDecimal bdRefundMoney = new BigDecimal(refundMoney, MathContext.DECIMAL32);
  62. //构建请求参数
  63. Map<Object, Object> params = buildRequsetMapParam(out_trade_no, bdOrderMoney, bdRefundMoney);
  64. String mapToXml = MapUtils.convertMap2Xml(params);
  65. //请求微信
  66. String reponseXml = sendSSLPostToWx(mapToXml, WechatConfig.getSslcsf());
  67. WechatRefundApiResult result = (WechatRefundApiResult) XmlUtil.xmlStrToBean(reponseXml, WechatRefundApiResult.class);
  68. result.setOut_refund_no((String)params.get("out_refund_no"));
  69. return result;
  70. }
  71. /**
  72. * 方法描述:得到请求微信退款请求的参数
  73. * 创建时间:2017年6月8日 上午11:27:02
  74. * 作者: xubo
  75. *
  76. * @param
  77. * @return
  78. */
  79. private static Map<Object, Object> buildRequsetMapParam(String out_trade_no, BigDecimal bdOrderMoney, BigDecimal bdRefundMoney) {
  80. Map<Object, Object> params = new HashMap<Object, Object>();
  81. params.put("appid", CommonWxPayPropertiesBuilder.instance().getAppId());//微信分配的公众账号ID(企业号corpid即为此appId)
  82. params.put("mch_id", CommonWxPayPropertiesBuilder.instance().getMchId());//微信支付分配的商户号
  83. params.put("nonce_str", CharUtil.getRandomString(16));//随机字符串,不长于32位。推荐随机数生成算法
  84. params.put("out_trade_no", out_trade_no);//商户传给微信的订单号
  85. params.put("out_refund_no", System.currentTimeMillis()+"");//商户系统内部的退款单号,商户系统内部唯一,同一退款单号多次请求只退一笔
  86. params.put("total_fee", bdOrderMoney.multiply(Constant.ONE_HUNDRED).intValue());//订单总金额,单位为分,只能为整数
  87. params.put("refund_fee", bdRefundMoney.multiply(Constant.ONE_HUNDRED).intValue());//退款总金额,订单总金额,单位为分,只能为整数
  88. params.put("op_user_id", CommonWxPayPropertiesBuilder.instance().getMchId());//操作员帐号, 默认为商户号
  89. params.put("notify_url", CommonWxPayPropertiesBuilder.instance().getRefundNotifyUrl());
  90. //签名前必须要参数全部写在前面
  91. params.put("sign", arraySign(params, CommonWxPayPropertiesBuilder.instance().getPaySignKey()));//签名
  92. return params;
  93. }
  94. /**
  95. * 请求微信https
  96. **/
  97. public static String sendSSLPostToWx(String mapToXml, SSLConnectionSocketFactory sslcsf) {
  98. logger.info("*******退款(WX Request:" + mapToXml);
  99. System.out.println("*******退款(WX Request:" + mapToXml);
  100. HttpPost httPost = new HttpPost(CommonWxPayPropertiesBuilder.instance().getRefundUrl());
  101. httPost.addHeader("Connection", "keep-alive");
  102. httPost.addHeader("Accept", "*/*");
  103. httPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  104. httPost.addHeader("Host", "api.mch.weixin.qq.com");
  105. httPost.addHeader("X-Requested-With", "XMLHttpRequest");
  106. httPost.addHeader("Cache-Control", "max-age=0");
  107. httPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
  108. httPost.setEntity(new StringEntity(mapToXml, "UTF-8"));
  109. CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslcsf).build();
  110. CloseableHttpResponse response = null;
  111. try {
  112. response = httpClient.execute(httPost);
  113. HttpEntity entity = response.getEntity();
  114. String xmlStr = EntityUtils.toString(entity, "UTF-8");
  115. logger.info("*******退款(WX Response:" + xmlStr);
  116. System.out.println("*******退款(WX Response:" + xmlStr);
  117. return xmlStr;
  118. } catch (Exception e) {
  119. logger.error(e.getMessage(), e);
  120. return null;
  121. } finally {
  122. try {
  123. if (response != null) {
  124. response.close();
  125. }
  126. } catch (IOException e) {
  127. logger.error(e.getMessage(), e);
  128. }
  129. }
  130. }
  131. /**
  132. * 方法描述:微信查询退款逻辑
  133. * 创建时间:2017年4月12日 上午11:04:25
  134. * 作者: xubo
  135. *
  136. * @param
  137. * @return
  138. */
  139. public static WechatRefundQueryResult wxRefundquery(String out_trade_no) {
  140. Map<Object, Object> params = new HashMap<Object, Object>();
  141. params.put("appid", CommonWxPayPropertiesBuilder.instance().getAppId());//微信分配的公众账号ID(企业号corpid即为此appId)
  142. params.put("mch_id", CommonWxPayPropertiesBuilder.instance().getMchId());//微信支付分配的商户号
  143. params.put("nonce_str", CharUtil.getRandomString(16));//随机字符串,不长于32位。推荐随机数生成算法
  144. params.put("out_trade_no", out_trade_no);//商户侧传给微信的订单号
  145. //签名前必须要参数全部写在前面
  146. params.put("sign", arraySign(params, CommonWxPayPropertiesBuilder.instance().getPaySignKey()));//签名
  147. String mapToXml = MapUtils.convertMap2Xml(params);
  148. HttpPost httPost = new HttpPost(CommonWxPayPropertiesBuilder.instance().getRefundqueryUrl());
  149. httPost.addHeader("Connection", "keep-alive");
  150. httPost.addHeader("Accept", "*/*");
  151. httPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  152. httPost.addHeader("Host", "api.mch.weixin.qq.com");
  153. httPost.addHeader("X-Requested-With", "XMLHttpRequest");
  154. httPost.addHeader("Cache-Control", "max-age=0");
  155. httPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
  156. httPost.setEntity(new StringEntity(mapToXml, "UTF-8"));
  157. CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(WechatConfig.getSslcsf()).build();
  158. CloseableHttpResponse response = null;
  159. try {
  160. response = httpClient.execute(httPost);
  161. HttpEntity entity = response.getEntity();
  162. String xmlStr = EntityUtils.toString(entity, "UTF-8");
  163. System.out.println(xmlStr);
  164. WechatRefundQueryResult result = (WechatRefundQueryResult) XmlUtil.xmlStrToBean(xmlStr, WechatRefundQueryResult.class);
  165. result.setXmlStr(xmlStr);
  166. // Map<String, Object> result = XmlUtil.xmlStrToMap(xmlStr);//.xmlStrToBean(xmlStr, WechatRefundApiResult.class);
  167. return result;
  168. //将信息保存到数据库
  169. } catch (Exception e) {
  170. logger.error(e.getMessage(), e);
  171. return null;
  172. } finally {
  173. try {
  174. if (response != null) {
  175. response.close();
  176. }
  177. } catch (IOException e) {
  178. logger.error(e.getMessage(), e);
  179. }
  180. }
  181. }
  182. /**
  183. * 方法描述:微信查询订单逻辑
  184. * 创建时间:2018年11月02日 上午11:04:25
  185. * 作者: huangyaqin
  186. * @param
  187. * @return
  188. */
  189. public static WechatRefundApiResult wxOrderQuery(String out_trade_no) {
  190. Map<Object, Object> params = new HashMap<Object, Object>();
  191. params.put("appid", CommonWxPayPropertiesBuilder.instance().getAppId());//微信分配的公众账号ID(企业号corpid即为此appId)
  192. params.put("mch_id", CommonWxPayPropertiesBuilder.instance().getMchId());//微信支付分配的商户号
  193. params.put("nonce_str", CharUtil.getRandomString(16));//随机字符串,不长于32位。推荐随机数生成算法
  194. params.put("out_trade_no", out_trade_no);//商户侧传给微信的订单号
  195. //签名前必须要参数全部写在前面
  196. params.put("sign", arraySign(params, CommonWxPayPropertiesBuilder.instance().getPaySignKey()));//签名
  197. String mapToXml = MapUtils.convertMap2Xml(params);
  198. HttpPost httPost = new HttpPost(CommonWxPayPropertiesBuilder.instance().getOrderquery());
  199. httPost.addHeader("Connection", "keep-alive");
  200. httPost.addHeader("Accept", "*/*");
  201. httPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  202. httPost.addHeader("Host", "api.mch.weixin.qq.com");
  203. httPost.addHeader("X-Requested-With", "XMLHttpRequest");
  204. httPost.addHeader("Cache-Control", "max-age=0");
  205. httPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
  206. httPost.setEntity(new StringEntity(mapToXml, "UTF-8"));
  207. CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(WechatConfig.getSslcsf()).build();
  208. CloseableHttpResponse response = null;
  209. try {
  210. response = httpClient.execute(httPost);
  211. HttpEntity entity = response.getEntity();
  212. String xmlStr = EntityUtils.toString(entity, "UTF-8");
  213. System.out.println("xmlStr : " + xmlStr);
  214. WechatRefundApiResult result = (WechatRefundApiResult) XmlUtil.xmlStrToBean(xmlStr, WechatRefundApiResult.class);
  215. return result;
  216. //将信息保存到数据库
  217. } catch (Exception e) {
  218. logger.error(e.getMessage(), e);
  219. return null;
  220. } finally {
  221. try {
  222. if (response != null) {
  223. response.close();
  224. }
  225. } catch (IOException e) {
  226. logger.error(e.getMessage(), e);
  227. }
  228. }
  229. }
  230. /**
  231. * 支付交易ID
  232. */
  233. public static String getBundleId() {
  234. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  235. String tradeno = dateFormat.format(new Date());
  236. String str = "000000" + (int) (Math.random() * 1000000);
  237. tradeno = tradeno + str.substring(str.length() - 6);
  238. return tradeno;
  239. }
  240. /**
  241. * 方法描述:根据签名加密请求参数
  242. * 创建时间:2017年6月8日 上午11:28:52
  243. * 作者: xubo
  244. *
  245. * @param
  246. * @return
  247. */
  248. public static String arraySign(Map<Object, Object> params, String paySignKey) {
  249. boolean encode = false;
  250. Set<Object> keysSet = params.keySet();
  251. Object[] keys = keysSet.toArray();
  252. Arrays.sort(keys);
  253. StringBuffer temp = new StringBuffer();
  254. boolean first = true;
  255. for (Object key : keys) {
  256. if (first) {
  257. first = false;
  258. } else {
  259. temp.append("&");
  260. }
  261. temp.append(key).append("=");
  262. Object value = params.get(key);
  263. String valueString = "";
  264. if (null != value) {
  265. valueString = value.toString();
  266. }
  267. if (encode) {
  268. try {
  269. temp.append(URLEncoder.encode(valueString, "UTF-8"));
  270. } catch (UnsupportedEncodingException e) {
  271. e.printStackTrace();
  272. }
  273. } else {
  274. temp.append(valueString);
  275. }
  276. }
  277. temp.append("&key=");
  278. temp.append(paySignKey);
  279. System.out.println(temp.toString());
  280. String packageSign = MD5.getMessageDigest(temp.toString());
  281. return packageSign;
  282. }
  283. /**
  284. * 请求,只请求一次,不做重试
  285. *
  286. * @param url
  287. * @param data
  288. * @return
  289. * @throws Exception
  290. */
  291. public static String requestOnce(final String url, String data) {
  292. BasicHttpClientConnectionManager connManager;
  293. connManager = new BasicHttpClientConnectionManager(
  294. RegistryBuilder.<ConnectionSocketFactory>create()
  295. .register("http", PlainConnectionSocketFactory.getSocketFactory())
  296. .register("https", SSLConnectionSocketFactory.getSocketFactory())
  297. .build(),
  298. null,
  299. null,
  300. null
  301. );
  302. HttpClient httpClient = HttpClientBuilder.create()
  303. .setConnectionManager(connManager)
  304. .build();
  305. HttpPost httpPost = new HttpPost(url);
  306. RequestConfig requestConfig = RequestConfig.custom()
  307. .setSocketTimeout(5000)
  308. .setConnectTimeout(5000)
  309. .setConnectionRequestTimeout(10000).build();
  310. httpPost.setConfig(requestConfig);
  311. StringEntity postEntity = new StringEntity(data, "UTF-8");
  312. httpPost.addHeader("Content-Type", "text/xml");
  313. // httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 " + CommonWxPayPropertiesBuilder.instance().getMchId());
  314. httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 " + "1517534731");
  315. httpPost.setEntity(postEntity);
  316. try {
  317. HttpResponse httpResponse = httpClient.execute(httpPost);
  318. HttpEntity httpEntity = httpResponse.getEntity();
  319. String reusltObj = null;
  320. reusltObj = EntityUtils.toString(httpEntity, "UTF-8");
  321. logger.info("请求结果:" + reusltObj);
  322. return reusltObj;
  323. } catch (IOException e) {
  324. e.printStackTrace();
  325. }
  326. return "";
  327. }
  328. /**
  329. * 请求,只请求一次,不做重试
  330. *
  331. * @param url
  332. * @param params
  333. * @return
  334. * @throws Exception
  335. */
  336. public static String requestOnceGet(final String url, Map params) {
  337. StringBuffer strtTotalURL = new StringBuffer(EMPTY);
  338. BasicHttpClientConnectionManager connManager;
  339. connManager = new BasicHttpClientConnectionManager(
  340. RegistryBuilder.<ConnectionSocketFactory>create()
  341. .register("http", PlainConnectionSocketFactory.getSocketFactory())
  342. .register("https", SSLConnectionSocketFactory.getSocketFactory())
  343. .build(),
  344. null,
  345. null,
  346. null
  347. );
  348. HttpClient httpClient = HttpClientBuilder.create()
  349. .setConnectionManager(connManager)
  350. .build();
  351. HttpGet httpGet = new HttpGet(url);
  352. RequestConfig requestConfig = RequestConfig.custom()
  353. .setSocketTimeout(5000)
  354. .setConnectTimeout(5000)
  355. .setConnectionRequestTimeout(10000).build();
  356. httpGet.setConfig(requestConfig);
  357. httpGet.addHeader("Content-Type", "text/xml");
  358. // httpGet.addHeader("User-Agent", "wxpay sdk java v1.0 " + CommonWxPayPropertiesBuilder.instance().getMchId());
  359. httpGet.addHeader("User-Agent", "wxpay sdk java v1.0 " + "1517534731");
  360. if (strtTotalURL.indexOf("?") == -1) {
  361. strtTotalURL.append(url).append("?").append(getUrl(params, "UTF-8"));
  362. } else {
  363. strtTotalURL.append(url).append("&").append(getUrl(params, "UTF-8"));
  364. }
  365. try {
  366. HttpResponse httpResponse = httpClient.execute(httpGet);
  367. HttpEntity httpEntity = httpResponse.getEntity();
  368. String reusltObj = EntityUtils.toString(httpEntity, "UTF-8");
  369. logger.info("请求结果:" + reusltObj);
  370. return reusltObj;
  371. } catch (Exception e) {
  372. e.printStackTrace();
  373. }
  374. return "";
  375. }
  376. /**
  377. * 据Map生成URL字符串
  378. *
  379. * @param map Map
  380. * @param valueEnc URL编码
  381. * @return URL
  382. */
  383. private static String getUrl(Map<String, String> map, String valueEnc) {
  384. if (null == map || map.keySet().size() == 0) {
  385. return (EMPTY);
  386. }
  387. StringBuffer url = new StringBuffer();
  388. Set<String> keys = map.keySet();
  389. for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
  390. String key = it.next();
  391. if (map.containsKey(key)) {
  392. String val = map.get(key);
  393. String str = val != null ? val : EMPTY;
  394. try {
  395. str = URLEncoder.encode(str, valueEnc);
  396. } catch (UnsupportedEncodingException e) {
  397. e.printStackTrace();
  398. }
  399. url.append(key).append("=").append(str).append(URL_PARAM_CONNECT_FLAG);
  400. }
  401. }
  402. String strURL = EMPTY;
  403. strURL = url.toString();
  404. if (URL_PARAM_CONNECT_FLAG.equals(EMPTY + strURL.charAt(strURL.length() - 1))) {
  405. strURL = strURL.substring(0, strURL.length() - 1);
  406. }
  407. return (strURL);
  408. }
  409. public static void main(String[] args) throws Exception {
  410. Map<Object, Object> parame = new TreeMap<Object, Object>();
  411. parame.put("mch_id", ResourceUtil.getConfigByName("\\conf\\wx-mp", "wx.mchId"));//
  412. String randomStr = CharUtil.getRandomNum(18).toUpperCase();
  413. parame.put("nonce_str", randomStr);//
  414. String sign = WechatUtil.arraySign(parame, ResourceUtil.getConfigByName("\\conf\\wx-mp", "wx.paySignKey"));
  415. parame.put("sign", sign);// 数字签证
  416. String xml = MapUtils.convertMap2Xml(parame);
  417. logger.info("xml:" + xml);
  418. Map<String, Object> resultUn = XmlUtil.xmlStrToMap(WechatUtil.requestOnce("https://apitest.mch.weixin.qq.com/sandboxnew/pay/getsignkey", xml));
  419. System.out.print(resultUn);
  420. }
  421. }