1
0

WechatUtil.java 19 KB

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