WechatUtil.java 16 KB

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