From 691423f4edae7354979d786a9be9945b74d0127d Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期三, 10 十一月 2021 12:01:02 +0800 Subject: [PATCH] 修改接收body数据方法 --- 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