From 0344dc66783353faff118a0ea91f7d2aa07bbd4a Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期六, 02 四月 2022 18:47:44 +0800 Subject: [PATCH] 优化加密初始化方法 --- src/main/java/com/hx/mp/util/WXPayUtil.java | 148 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 148 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/mp/util/WXPayUtil.java b/src/main/java/com/hx/mp/util/WXPayUtil.java index 3006e70..f363886 100644 --- a/src/main/java/com/hx/mp/util/WXPayUtil.java +++ b/src/main/java/com/hx/mp/util/WXPayUtil.java @@ -251,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){ -- Gitblit v1.8.0