admin
2021-01-25 2ba431be9c12a79783e0f9ef249292b7fa95f2a1
utils/src/main/java/org/yeshi/utils/wx/WXPayUtil.java
@@ -1,11 +1,15 @@
package org.yeshi.utils.wx;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.DocumentException;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
@@ -22,6 +26,21 @@
 *
 */
public class WXPayUtil {
   private static String post(String url, String entity) {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(url);
      method.setRequestBody(entity);
      try {
         client.executeMethod(method);
         return method.getResponseBodyAsString();
      } catch (HttpException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return "";
   }
   /**
    * 付款到零钱
@@ -153,16 +172,16 @@
      if (params == null)
         throw new WXPlaceOrderParamsException(1, "请传入下单参数");
      if (params.getInfo() == null)
      if (params.getApp() == null)
         throw new WXPlaceOrderParamsException(2, "请传入下单应用信息");
      if (StringUtil.isNullOrEmpty(params.getInfo().getAppId()))
      if (StringUtil.isNullOrEmpty(params.getApp().getAppId()))
         throw new WXPlaceOrderParamsException(201, "请传入下单应用信息-appId");
      if (StringUtil.isNullOrEmpty(params.getInfo().getAppSecret()))
      if (StringUtil.isNullOrEmpty(params.getApp().getAppSecret()))
         throw new WXPlaceOrderParamsException(202, "请传入下单应用信息-appSecret");
      if (StringUtil.isNullOrEmpty(params.getInfo().getMchId()))
      if (StringUtil.isNullOrEmpty(params.getApp().getMchId()))
         throw new WXPlaceOrderParamsException(203, "请传入下单应用信息-mchId");
      if (StringUtil.isNullOrEmpty(params.getInfo().getMchKey()))
      if (StringUtil.isNullOrEmpty(params.getApp().getMchKey()))
         throw new WXPlaceOrderParamsException(204, "请传入下单应用信息-mchKey");
      if (StringUtil.isNullOrEmpty(params.getBody()))
         throw new WXPlaceOrderParamsException(3, "请传入body");
@@ -186,8 +205,8 @@
      // throw new WXPlaceOrderParamsException(9, "请传入openId");
      Map<String, String> map = new HashMap<String, String>();
      map.put("appid", params.getInfo().getAppId());
      map.put("mch_id", params.getInfo().getMchId());
      map.put("appid", params.getApp().getAppId());
      map.put("mch_id", params.getApp().getMchId());
      map.put("nonce_str", StringUtil.getRandomCode(32));
      map.put("body", params.getBody());
      map.put("out_trade_no", params.getOrderNo());
@@ -197,14 +216,14 @@
      map.put("trade_type", params.getTradeType());
      if (!StringUtil.isNullOrEmpty(params.getOpenId()))
         map.put("openid", params.getOpenId());
      map.put("sign", WXUtil.getSignMD5(map, params.getInfo().getMchKey()));
      map.put("sign", WXUtil.getSignMD5(map, params.getApp().getMchKey()));
      String entity = WXUtil.loadWXMessage(map);
      String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder", entity);
      try {
         System.out.println("统一下单结果:" + new String(result.getBytes("GBK"), "UTF-8"));
      } catch (UnsupportedEncodingException e) {
      } catch (Exception e) {
         e.printStackTrace();
      }
      Map<String, String> resultMap = WXUtil.parseXML(result);
@@ -314,12 +333,12 @@
      map.put("nonce_str", StringUtil.getRandomCode(32));
      map.put("out_refund_no", refundOrderNo);
      map.put("sign", WXUtil.getSignMD5(map, app.getMchKey()));
      String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/refundquery", WXUtil.loadWXMessage(map));
      String result = post("https://api.mch.weixin.qq.com/pay/refundquery", WXUtil.loadWXMessage(map));
      System.out.println("订单查询结果:" + result);
      Map<String, String> resultMap = null;
      try {
         resultMap = WXUtil.parseXML(result);
      } catch (Exception e) {
      } catch (Throwable e) {
         try {
            result = new String(result.getBytes("GBK"), "UTF-8");
         } catch (UnsupportedEncodingException e1) {