From db287aac9eb5032b1158b405a9b4807301467ed3 Mon Sep 17 00:00:00 2001
From: zhouxiang <zhouxiang>
Date: 星期四, 24 三月 2022 17:48:03 +0800
Subject: [PATCH] 添加redis获取整个hashKey数据和单个hashKey数据方法

---
 src/main/java/com/hx/mp/util/WXPayUtil.java |  204 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 184 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/hx/mp/util/WXPayUtil.java b/src/main/java/com/hx/mp/util/WXPayUtil.java
index 79aea02..f363886 100644
--- a/src/main/java/com/hx/mp/util/WXPayUtil.java
+++ b/src/main/java/com/hx/mp/util/WXPayUtil.java
@@ -2,7 +2,9 @@
 
 import com.hx.exception.ServiceException;
 import com.hx.util.SimpleTool;
+import com.hx.util.StringUtils;
 import net.sf.json.JSONObject;
+import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
@@ -249,6 +251,154 @@
         return returnObj;
     }
 
+    /**缁熶竴鏀粯锛堝垎璐︼級
+     * @param request 鏂规硶鑾峰彇
+     * @param appId  灏忕▼搴忓彿
+     * @param partner  鍟嗘埛鍙�
+     * @param key  绉橀挜
+     * @param notifyUrl  鍥炶皟閾炬帴
+     * @param out_trade_no  璁㈠崟鍙�
+     * @param body 鍟嗗搧鎻忚堪
+     * @param total_fee 鏀粯閲戦
+     * @param openid 鐢ㄦ埛openId
+     * @param attach 闄勫甫鏁版嵁鍖�
+     * @param notifyUrl 鍥炶皟閫氱煡鍦板潃
+     * @param trade_type 浜ゆ槗绫诲瀷
+     * @param profit_sharing 鏄惁鍒嗚处锛歂涓嶏紝Y鏄�
+     * @return JSON  status = "SUC"涓烘垚鍔�
+     */
+    public static JSONObject unifiedPay(HttpServletRequest request,String appId,String partner,String key,String notifyUrl,String out_trade_no, String body, String total_fee, String openid,
+                                        String attach,String trade_type,String profit_sharing) throws Exception {
+
+        if (!SimpleTool.checkNotNull(notifyUrl)) {
+            throw new ServiceException("鏀粯鍔熻兘鏁呴殰锛�");
+        }
+
+        // 鍒涘缓鏌ヨ璇锋眰瀵硅薄
+        RequestHandler reqHandler = new RequestHandler(null, null);
+        // 閫氫俊瀵硅薄
+        TenpayHttpClient httpClient = new TenpayHttpClient();
+        // 搴旂瓟瀵硅薄
+        ClientResponseHandler resHandler = new ClientResponseHandler();
+
+        // -----------------------------
+        // 璁剧疆璇锋眰鍙傛暟
+        // -----------------------------
+        // reqHandler.init();
+        reqHandler.setKey(key);
+        reqHandler.setGateUrl(FIRST_ORDER_URL);// 璇锋眰URL
+
+        // -----------------------------
+        // 璁剧疆鎺ュ彛鍙傛暟(sign鍚庡彴鑷姩鐢熸垚)
+        // -----------------------------
+
+        reqHandler.setParameter("appid", appId); // 鍏紬鍙�/灏忕▼搴�
+        reqHandler.setParameter("mch_id", partner); // 鍟嗘埛鍙�
+        reqHandler.setParameter("nonce_str", SimpleTool.getUUIDName().substring(0, 30));// 闅忔満涔辩爜
+        reqHandler.setParameter("body", body);// 鍟嗗搧鎻忚堪
+        reqHandler.setParameter("out_trade_no", out_trade_no);// 鍟嗘埛璁㈠崟鍙�
+        reqHandler.setParameter("total_fee", total_fee);// 鎬婚噾棰�
+        reqHandler.setParameter("spbill_create_ip", "8.8.8.8");// 缁堢IP
+        reqHandler.setParameter("notify_url",notifyUrl);// 閫氱煡鍦板潃
+        reqHandler.setParameter("trade_type", trade_type);// 浜ゆ槗绫诲瀷
+        //JSAPI锛孨ATIVE锛孉PP
+        reqHandler.setParameter("openid", openid);// openId
+        reqHandler.setParameter("attach", attach);// 闄勫甫鏁版嵁鍖�
+        reqHandler.setParameter("profit_sharing", profit_sharing);// 闄勫甫鏁版嵁鍖�
+
+        // -----------------------------
+        // 璁剧疆閫氫俊鍙傛暟
+        // -----------------------------
+        // 璁剧疆璇锋眰杩斿洖鐨勭瓑寰呮椂闂�
+        httpClient.setTimeOut(5);
+
+        // 璁剧疆ca璇佷功
+        // httpClient.setCaInfo(new File(CA_PATH));
+
+        // 璁剧疆涓汉(鍟嗘埛)璇佷功
+        // httpClient.setCertInfo(new File(CERT_PATH), CERT_PWD);
+
+        // 璁剧疆鍙戦�佺被鍨婸OST
+        httpClient.setMethod("POST");
+
+        // 璁剧疆璇锋眰鍐呭(鐢熸垚sign)
+        String requestUrl = reqHandler.getRequestURL();// 缁勬嫾https://www.baidu.com?a=x&b=xx
+
+        httpClient.setReqContent(requestUrl);// https://www.baidu.com?a=x&b=xx
+        String rescontent = "null";
+
+        httpClient.setRequestHandler(reqHandler);// 鎶婂鐞嗗璞★紝鍍忔槸鍙傛暟鍚勭涓滆タ閮借缃繘鍘绘柟渚胯幏鍙�(quan)
+
+        // 杩斿洖鍑哄幓鐨勫璞�(鐘舵�侊紝閿欒鍘熷洜锛岃鎿嶄綔鐩稿叧淇℃伅(鍙傛暟,杩斿洖鍊�))
+        JSONObject returnObj = new JSONObject();
+
+        // 鍚庡彴璋冪敤
+        if (httpClient.call()) {
+            System.out.println("缁熶竴涓嬪崟,鎴愬姛cll浜�::");
+
+            // 璁剧疆缁撴灉鍙傛暟
+            rescontent = httpClient.getResContent();
+            System.out.println("缁熶竴涓嬪崟杩斿洖缁撴灉:" + rescontent);
+            resHandler.setContent(rescontent);// 瑙f瀽xml
+            resHandler.setKey(key);
+
+            // 鑾峰彇杩斿洖鍙傛暟
+            String return_code = resHandler.getParameter("return_code");
+            String return_msg = resHandler.getParameter("return_msg");
+
+            // 鍒ゆ柇绛惧悕鍙婄粨鏋�
+            if (resHandler.isTenpaySign() && "SUCCESS".equals(return_code)) {
+                String prepay_id = resHandler.getParameter("prepay_id");// 棰勬敮浠樹氦鏄撲細璇濇爣璇�
+                String code_url = resHandler.getParameter("code_url");// 浜岀淮鐮侀摼鎺�
+
+                String result_code = resHandler.getParameter("result_code");// 涓氬姟缁撴灉
+                String appid = resHandler.getParameter("appid");// 鍏紬璐﹀彿ID
+                String mch_id = resHandler.getParameter("mch_id");// 鍟嗘埛鍙�
+                String nonce_str = resHandler.getParameter("nonce_str");// 闅忔満鐮�
+                String sign = resHandler.getParameter("sign");// 绛惧悕
+
+                if (result_code.equals("SUCCESS")) {
+                    returnObj.put("status", "suc");
+                    returnObj.put("sign", sign);
+                    returnObj.put("nonce_str", nonce_str);
+                    returnObj.put("mch_id", mch_id);
+                    returnObj.put("appid", appid);
+                    returnObj.put("prepay_id", prepay_id);
+                    returnObj.put("code_url", code_url);
+                    returnObj.put("out_trade_no", out_trade_no);
+                } else {
+                    String errMsg = "[ERROR]result_code:" + resHandler.getParameter("result_code") + " err_code:"
+                            + resHandler.getParameter("err_code") + "err_code_des:"
+                            + resHandler.getParameter("err_code_des");
+
+                    // 閿欒鏃讹紝杩斿洖缁撴灉鏈鍚嶏紝璁板綍retcode銆乺etmsg鐪嬪け璐ヨ鎯呫��
+                    returnObj.put("status", "ERROR-C");
+                    returnObj.put("errMsg", errMsg);
+                }
+            } else {
+                String errMsg = "return_code:" + return_code + "err_code:" + resHandler.getParameter("err_code")
+                        + " return_msg:" + return_msg;
+                // 閿欒鏃讹紝杩斿洖缁撴灉鏈鍚嶏紝璁板綍retcode銆乺etmsg鐪嬪け璐ヨ鎯呫��
+                returnObj.put("status", "ERROR-B");
+                returnObj.put("errMsg", errMsg);
+            }
+        } else {
+            // 鏈夊彲鑳藉洜涓虹綉缁滃師鍥狅紝璇锋眰宸茬粡澶勭悊锛屼絾鏈敹鍒板簲绛斻��
+            returnObj.put("status", "ERROR-A");
+            returnObj.put("errMsg", httpClient.getResponseCode() + ":" + httpClient.getErrInfo());
+        }
+
+        // 鑾峰彇debug淇℃伅,寤鸿鎶婅姹傘�佸簲绛斿唴瀹广�乨ebug淇℃伅锛岄�氫俊杩斿洖鐮佸啓鍏ユ棩蹇楋紝鏂逛究瀹氫綅闂
+        String detail = "http res:" + httpClient.getResponseCode() + "," + httpClient.getErrInfo() + ";" + "req url:"
+                + requestUrl + ";" + ";" + "req debug:" + reqHandler.getDebugInfo() + ";" + "res content:" + rescontent
+                + ";" + "res debug:" + resHandler.getDebugInfo() + ";";
+
+        returnObj.put("detail", detail);
+
+
+        return returnObj;
+    }
+
     /**澶勭悊淇℃伅
      */
     public static JSONObject paymentData(JSONObject payObj,String key){
@@ -321,37 +471,47 @@
              try {
                  StringEntity se = new StringEntity(xml);
                  httppost.setEntity(se);  
-                 System.out.println("executing request" + httppost.getRequestLine());  
+
                  CloseableHttpResponse responseEntry = httpclient.execute(httppost);
                  try {  
                      HttpEntity entity = responseEntry.getEntity();
-                     System.out.println(responseEntry.getStatusLine());  
                      if (entity != null) {
+
                          SAXReader saxReader = new SAXReader();
                          Document document = saxReader.read(entity.getContent());
                          Element rootElt = document.getRootElement();
-                         String resultCode = rootElt.elementText("result_code");  
-                         JSONObject result = new JSONObject();  
+                         String returnCode = rootElt.elementText("return_code");
+                         JSONObject result = new JSONObject();
 
-                         if(resultCode.equals("SUCCESS")){  
-                             result.put("weixinPayUrl", rootElt.elementText("code_url"));
-                             result.put("prepayId", rootElt.elementText("prepay_id"));  
-                             result.put("msg","success");  
-                             
-                         	String refund_id = rootElt.elementText("refund_id");//寰俊閫�娆惧崟鍙�
-             				String r_out_refund_no=rootElt.elementText("out_refund_no");
-                         	String errMsg = "鍟嗘埛鍙�"+r_out_refund_no+"鐨勯��娆炬祦姘村彿鏄細"+refund_id;
-                         	result.put("status", "SUCCESS");
-                         	result.put("errMsg", errMsg);
-                         	result.put("refund_id", refund_id);
+                         if(returnCode.equals("SUCCESS")){
+                             String resultCode = rootElt.elementText("result_code");
+                             if(resultCode.equals("SUCCESS")) {
+                                 result.put("weixinPayUrl", rootElt.elementText("code_url"));
+                                 result.put("prepayId", rootElt.elementText("prepay_id"));
+                                 result.put("msg", "success");
+
+                                 String refund_id = rootElt.elementText("refund_id");//寰俊閫�娆惧崟鍙�
+                                 String r_out_refund_no = rootElt.elementText("out_refund_no");
+                                 String errMsg = "鍟嗘埛鍙�" + r_out_refund_no + "鐨勯��娆炬祦姘村彿鏄細" + refund_id;
+                                 result.put("status", "SUCCESS");
+                                 result.put("errMsg", errMsg);
+                                 result.put("refund_id", refund_id);
+                             }else{
+                                 String errMsg = "[ERROR]result_code:" + rootElt.elementText("result_code")+
+                                         " err_code:" + rootElt.elementText("err_code");
+
+                                 //閿欒鏃讹紝杩斿洖缁撴灉鏈鍚嶏紝璁板綍retcode銆乺etmsg鐪嬪け璐ヨ鎯呫��
+                                 result.put("errMsg", errMsg);
+                                 result.put("status","false");
+                                 result.put("msg",rootElt.elementText("err_code_des"));
+                             }
                          }else{  
-                         	String errMsg = "[ERROR]result_code:" + rootElt.elementText("result_code")+
-         							" err_code:" + rootElt.elementText("err_code");
+                         	String errMsg = "[ERROR]return_code:" + rootElt.elementText("return_code");
          					
          					//閿欒鏃讹紝杩斿洖缁撴灉鏈鍚嶏紝璁板綍retcode銆乺etmsg鐪嬪け璐ヨ鎯呫��
                          	result.put("errMsg", errMsg);
                          	result.put("status","false");
-                         	result.put("msg",rootElt.elementText("err_code_des"));
+                         	result.put("msg",rootElt.elementText("return_msg"));
                          }  
                          return result;  
                      }  
@@ -395,8 +555,12 @@
             parameters.put("appid",appId);
             parameters.put("mch_id",partner);
             parameters.put("nonce_str", nonceStr);
-            parameters.put("out_trade_no", out_trade_no);
-            parameters.put("transaction_id", transaction_id);
+            if(!StringUtils.isEmpty(out_trade_no)) {
+                parameters.put("out_trade_no", out_trade_no);
+            }
+            if(!StringUtils.isEmpty(transaction_id)) {
+                parameters.put("transaction_id", transaction_id);
+            }
             parameters.put("out_refund_no", out_refund_no);
             parameters.put("fee_type", "CNY");
             parameters.put("total_fee", total_fee);//鎬婚噾棰�

--
Gitblit v1.8.0