123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- package com.kmall.common.utils.wechat;
- import com.alibaba.druid.support.logging.Log;
- import com.alibaba.druid.support.logging.LogFactory;
- import com.kmall.common.service.pay.wxpay.CommonWxPayPropertiesBuilder;
- import com.kmall.common.utils.CharUtil;
- import com.kmall.common.utils.MapUtils;
- import com.kmall.common.utils.ResourceUtil;
- import com.kmall.common.utils.XmlUtil;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.config.RegistryBuilder;
- import org.apache.http.conn.socket.ConnectionSocketFactory;
- import org.apache.http.conn.socket.PlainConnectionSocketFactory;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.math.BigDecimal;
- import java.math.MathContext;
- import java.net.URLEncoder;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * <p>Title: 微信退款工具类</p>
- * <p>Description: 微信退款工具类,通过充值客户端的不同初始化不同的工具类,得到相应微信退款相关的appid和muchid</p>
- *
- * @author xubo
- * @date 2017年6月6日 下午5:05:03
- */
- public class WechatUtil {
- private static Log logger = LogFactory.getLog(WechatUtil.class);
- /**
- * 充值客户端类型--微信公众号
- */
- public static Integer CLIENTTYPE_WX = 2;
- /**
- * 充值客户端类型--app
- */
- public static Integer CLIENTTYPE_APP = 1;
- private static final String EMPTY = "";
- private static final String URL_PARAM_CONNECT_FLAG = "&";
- /**
- * 方法描述:微信退款逻辑
- * 创建时间:2017年4月12日 上午11:04:25
- * 作者: xubo
- *
- * @param
- * @return
- */
- public static WechatRefundApiResult wxRefund(String out_trade_no, Double orderMoney, Double refundMoney) {
- //初始化请求微信服务器的配置信息包括appid密钥等
- //转换金钱格式
- BigDecimal bdOrderMoney = new BigDecimal(orderMoney, MathContext.DECIMAL32);
- BigDecimal bdRefundMoney = new BigDecimal(refundMoney, MathContext.DECIMAL32);
- //构建请求参数
- Map<Object, Object> params = buildRequsetMapParam(out_trade_no, bdOrderMoney, bdRefundMoney);
- String mapToXml = MapUtils.convertMap2Xml(params);
- //请求微信
- String reponseXml = sendSSLPostToWx(mapToXml, WechatConfig.getSslcsf());
- WechatRefundApiResult result = (WechatRefundApiResult) XmlUtil.xmlStrToBean(reponseXml, WechatRefundApiResult.class);
- return result;
- }
- /**
- * 方法描述:得到请求微信退款请求的参数
- * 创建时间:2017年6月8日 上午11:27:02
- * 作者: xubo
- *
- * @param
- * @return
- */
- private static Map<Object, Object> buildRequsetMapParam(String out_trade_no, BigDecimal bdOrderMoney, BigDecimal bdRefundMoney) {
- Map<Object, Object> params = new HashMap<Object, Object>();
- params.put("appid", CommonWxPayPropertiesBuilder.instance().getAppId());//微信分配的公众账号ID(企业号corpid即为此appId)
- params.put("mch_id", CommonWxPayPropertiesBuilder.instance().getMchId());//微信支付分配的商户号
- params.put("nonce_str", CharUtil.getRandomString(16));//随机字符串,不长于32位。推荐随机数生成算法
- params.put("out_trade_no", out_trade_no);//商户传给微信的订单号
- params.put("out_refund_no", System.currentTimeMillis()+"");//商户系统内部的退款单号,商户系统内部唯一,同一退款单号多次请求只退一笔
- params.put("total_fee", bdOrderMoney.multiply(new BigDecimal(100)).intValue());//订单总金额,单位为分,只能为整数
- params.put("refund_fee", bdRefundMoney.multiply(new BigDecimal(100)).intValue());//退款总金额,订单总金额,单位为分,只能为整数
- params.put("op_user_id", CommonWxPayPropertiesBuilder.instance().getMchId());//操作员帐号, 默认为商户号
- params.put("notify_url", CommonWxPayPropertiesBuilder.instance().getRefundNotifyUrl());
- //签名前必须要参数全部写在前面
- params.put("sign", arraySign(params, CommonWxPayPropertiesBuilder.instance().getPaySignKey()));//签名
- return params;
- }
- /**
- * 请求微信https
- **/
- public static String sendSSLPostToWx(String mapToXml, SSLConnectionSocketFactory sslcsf) {
- logger.info("*******退款(WX Request:" + mapToXml);
- System.out.println("*******退款(WX Request:" + mapToXml);
- HttpPost httPost = new HttpPost(CommonWxPayPropertiesBuilder.instance().getRefundUrl());
- httPost.addHeader("Connection", "keep-alive");
- httPost.addHeader("Accept", "*/*");
- httPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
- httPost.addHeader("Host", "api.mch.weixin.qq.com");
- httPost.addHeader("X-Requested-With", "XMLHttpRequest");
- httPost.addHeader("Cache-Control", "max-age=0");
- httPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
- httPost.setEntity(new StringEntity(mapToXml, "UTF-8"));
- CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslcsf).build();
- CloseableHttpResponse response = null;
- try {
- response = httpClient.execute(httPost);
- HttpEntity entity = response.getEntity();
- String xmlStr = EntityUtils.toString(entity, "UTF-8");
- logger.info("*******退款(WX Response:" + xmlStr);
- System.out.println("*******退款(WX Response:" + xmlStr);
- return xmlStr;
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- return null;
- } finally {
- try {
- if (response != null) {
- response.close();
- }
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- /**
- * 方法描述:微信查询退款逻辑
- * 创建时间:2017年4月12日 上午11:04:25
- * 作者: xubo
- *
- * @param
- * @return
- */
- public static WechatRefundQueryResult wxRefundquery(String out_trade_no) {
- Map<Object, Object> params = new HashMap<Object, Object>();
- params.put("appid", CommonWxPayPropertiesBuilder.instance().getAppId());//微信分配的公众账号ID(企业号corpid即为此appId)
- params.put("mch_id", CommonWxPayPropertiesBuilder.instance().getMchId());//微信支付分配的商户号
- params.put("nonce_str", CharUtil.getRandomString(16));//随机字符串,不长于32位。推荐随机数生成算法
- params.put("out_trade_no", out_trade_no);//商户侧传给微信的订单号
- //签名前必须要参数全部写在前面
- params.put("sign", arraySign(params, CommonWxPayPropertiesBuilder.instance().getPaySignKey()));//签名
- String mapToXml = MapUtils.convertMap2Xml(params);
- HttpPost httPost = new HttpPost(CommonWxPayPropertiesBuilder.instance().getRefundqueryUrl());
- httPost.addHeader("Connection", "keep-alive");
- httPost.addHeader("Accept", "*/*");
- httPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
- httPost.addHeader("Host", "api.mch.weixin.qq.com");
- httPost.addHeader("X-Requested-With", "XMLHttpRequest");
- httPost.addHeader("Cache-Control", "max-age=0");
- httPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
- httPost.setEntity(new StringEntity(mapToXml, "UTF-8"));
- CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(WechatConfig.getSslcsf()).build();
- CloseableHttpResponse response = null;
- try {
- response = httpClient.execute(httPost);
- HttpEntity entity = response.getEntity();
- String xmlStr = EntityUtils.toString(entity, "UTF-8");
- System.out.println(xmlStr);
- WechatRefundQueryResult result = (WechatRefundQueryResult) XmlUtil.xmlStrToBean(xmlStr, WechatRefundQueryResult.class);
- // Map<String, Object> result = XmlUtil.xmlStrToMap(xmlStr);//.xmlStrToBean(xmlStr, WechatRefundApiResult.class);
- return result;
- //将信息保存到数据库
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- return null;
- } finally {
- try {
- if (response != null) {
- response.close();
- }
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- /**
- * 方法描述:微信查询订单逻辑
- * 创建时间:2018年11月02日 上午11:04:25
- * 作者: huangyaqin
- * @param
- * @return
- */
- public static WechatRefundApiResult wxOrderQuery(String out_trade_no) {
- Map<Object, Object> params = new HashMap<Object, Object>();
- params.put("appid", CommonWxPayPropertiesBuilder.instance().getAppId());//微信分配的公众账号ID(企业号corpid即为此appId)
- params.put("mch_id", CommonWxPayPropertiesBuilder.instance().getMchId());//微信支付分配的商户号
- params.put("nonce_str", CharUtil.getRandomString(16));//随机字符串,不长于32位。推荐随机数生成算法
- params.put("out_trade_no", out_trade_no);//商户侧传给微信的订单号
- //签名前必须要参数全部写在前面
- params.put("sign", arraySign(params, CommonWxPayPropertiesBuilder.instance().getPaySignKey()));//签名
- String mapToXml = MapUtils.convertMap2Xml(params);
- HttpPost httPost = new HttpPost(CommonWxPayPropertiesBuilder.instance().getOrderquery());
- httPost.addHeader("Connection", "keep-alive");
- httPost.addHeader("Accept", "*/*");
- httPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
- httPost.addHeader("Host", "api.mch.weixin.qq.com");
- httPost.addHeader("X-Requested-With", "XMLHttpRequest");
- httPost.addHeader("Cache-Control", "max-age=0");
- httPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
- httPost.setEntity(new StringEntity(mapToXml, "UTF-8"));
- CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(WechatConfig.getSslcsf()).build();
- CloseableHttpResponse response = null;
- try {
- response = httpClient.execute(httPost);
- HttpEntity entity = response.getEntity();
- String xmlStr = EntityUtils.toString(entity, "UTF-8");
- System.out.println("xmlStr : " + xmlStr);
- WechatRefundApiResult result = (WechatRefundApiResult) XmlUtil.xmlStrToBean(xmlStr, WechatRefundApiResult.class);
- return result;
- //将信息保存到数据库
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- return null;
- } finally {
- try {
- if (response != null) {
- response.close();
- }
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- /**
- * 支付交易ID
- */
- public static String getBundleId() {
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
- String tradeno = dateFormat.format(new Date());
- String str = "000000" + (int) (Math.random() * 1000000);
- tradeno = tradeno + str.substring(str.length() - 6);
- return tradeno;
- }
- /**
- * 方法描述:根据签名加密请求参数
- * 创建时间:2017年6月8日 上午11:28:52
- * 作者: xubo
- *
- * @param
- * @return
- */
- public static String arraySign(Map<Object, Object> params, String paySignKey) {
- boolean encode = false;
- Set<Object> keysSet = params.keySet();
- Object[] keys = keysSet.toArray();
- Arrays.sort(keys);
- StringBuffer temp = new StringBuffer();
- boolean first = true;
- for (Object key : keys) {
- if (first) {
- first = false;
- } else {
- temp.append("&");
- }
- temp.append(key).append("=");
- Object value = params.get(key);
- String valueString = "";
- if (null != value) {
- valueString = value.toString();
- }
- if (encode) {
- try {
- temp.append(URLEncoder.encode(valueString, "UTF-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- } else {
- temp.append(valueString);
- }
- }
- temp.append("&key=");
- temp.append(paySignKey);
- System.out.println(temp.toString());
- String packageSign = MD5.getMessageDigest(temp.toString());
- return packageSign;
- }
- /**
- * 请求,只请求一次,不做重试
- *
- * @param url
- * @param data
- * @return
- * @throws Exception
- */
- public static String requestOnce(final String url, String data) {
- BasicHttpClientConnectionManager connManager;
- connManager = new BasicHttpClientConnectionManager(
- RegistryBuilder.<ConnectionSocketFactory>create()
- .register("http", PlainConnectionSocketFactory.getSocketFactory())
- .register("https", SSLConnectionSocketFactory.getSocketFactory())
- .build(),
- null,
- null,
- null
- );
- HttpClient httpClient = HttpClientBuilder.create()
- .setConnectionManager(connManager)
- .build();
- HttpPost httpPost = new HttpPost(url);
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(5000)
- .setConnectTimeout(5000)
- .setConnectionRequestTimeout(10000).build();
- httpPost.setConfig(requestConfig);
- StringEntity postEntity = new StringEntity(data, "UTF-8");
- httpPost.addHeader("Content-Type", "text/xml");
- // httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 " + CommonWxPayPropertiesBuilder.instance().getMchId());
- httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 " + "1517534731");
- httpPost.setEntity(postEntity);
- try {
- HttpResponse httpResponse = httpClient.execute(httpPost);
- HttpEntity httpEntity = httpResponse.getEntity();
- String reusltObj = null;
- reusltObj = EntityUtils.toString(httpEntity, "UTF-8");
- logger.info("请求结果:" + reusltObj);
- return reusltObj;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "";
- }
- /**
- * 请求,只请求一次,不做重试
- *
- * @param url
- * @param params
- * @return
- * @throws Exception
- */
- public static String requestOnceGet(final String url, Map params) {
- StringBuffer strtTotalURL = new StringBuffer(EMPTY);
- BasicHttpClientConnectionManager connManager;
- connManager = new BasicHttpClientConnectionManager(
- RegistryBuilder.<ConnectionSocketFactory>create()
- .register("http", PlainConnectionSocketFactory.getSocketFactory())
- .register("https", SSLConnectionSocketFactory.getSocketFactory())
- .build(),
- null,
- null,
- null
- );
- HttpClient httpClient = HttpClientBuilder.create()
- .setConnectionManager(connManager)
- .build();
- HttpGet httpGet = new HttpGet(url);
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(5000)
- .setConnectTimeout(5000)
- .setConnectionRequestTimeout(10000).build();
- httpGet.setConfig(requestConfig);
- httpGet.addHeader("Content-Type", "text/xml");
- // httpGet.addHeader("User-Agent", "wxpay sdk java v1.0 " + CommonWxPayPropertiesBuilder.instance().getMchId());
- httpGet.addHeader("User-Agent", "wxpay sdk java v1.0 " + "1517534731");
- if (strtTotalURL.indexOf("?") == -1) {
- strtTotalURL.append(url).append("?").append(getUrl(params, "UTF-8"));
- } else {
- strtTotalURL.append(url).append("&").append(getUrl(params, "UTF-8"));
- }
- try {
- HttpResponse httpResponse = httpClient.execute(httpGet);
- HttpEntity httpEntity = httpResponse.getEntity();
- String reusltObj = EntityUtils.toString(httpEntity, "UTF-8");
- logger.info("请求结果:" + reusltObj);
- return reusltObj;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- /**
- * 据Map生成URL字符串
- *
- * @param map Map
- * @param valueEnc URL编码
- * @return URL
- */
- private static String getUrl(Map<String, String> map, String valueEnc) {
- if (null == map || map.keySet().size() == 0) {
- return (EMPTY);
- }
- StringBuffer url = new StringBuffer();
- Set<String> keys = map.keySet();
- for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
- String key = it.next();
- if (map.containsKey(key)) {
- String val = map.get(key);
- String str = val != null ? val : EMPTY;
- try {
- str = URLEncoder.encode(str, valueEnc);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- url.append(key).append("=").append(str).append(URL_PARAM_CONNECT_FLAG);
- }
- }
- String strURL = EMPTY;
- strURL = url.toString();
- if (URL_PARAM_CONNECT_FLAG.equals(EMPTY + strURL.charAt(strURL.length() - 1))) {
- strURL = strURL.substring(0, strURL.length() - 1);
- }
- return (strURL);
- }
- public static void main(String[] args) throws Exception {
- Map<Object, Object> parame = new TreeMap<Object, Object>();
- parame.put("mch_id", ResourceUtil.getConfigByName("\\conf\\wx-mp", "wx.mchId"));//
- String randomStr = CharUtil.getRandomNum(18).toUpperCase();
- parame.put("nonce_str", randomStr);//
- String sign = WechatUtil.arraySign(parame, ResourceUtil.getConfigByName("\\conf\\wx-mp", "wx.paySignKey"));
- parame.put("sign", sign);// 数字签证
- String xml = MapUtils.convertMap2Xml(parame);
- logger.info("xml:" + xml);
- Map<String, Object> resultUn = XmlUtil.xmlStrToMap(WechatUtil.requestOnce("https://apitest.mch.weixin.qq.com/sandboxnew/pay/getsignkey", xml));
- System.out.print(resultUn);
- }
- }
|