您好,登錄后才能下訂單哦!
本文為大家分享了java微信公眾號企業付款的開發代碼,供大家參考,具體內容如下
詳情參照微信開發者文檔 企業付款文檔
java代碼 定義所傳遞的參數
@RequestMapping(value = "zhifu", method = RequestMethod.GET) public @ResponseBody String getWeixinOpenid(String code, HttpServletRequest request) { // 訂單號 自定義 生成32位uuid String partner_trade_no = UUIDGenerator.getUUID(); // 隨機數 String nonce_str = UUIDGenerator.getUUID(); // 轉賬金額(分為單位)1-200 int jine = 100; // 企業付款信息 String desc = "轉賬"; // ip地址 String spbill_create_ip = "xx.xx.xx"; // re_user_name String re_user_name = "xx"; String check_name = CheckName.NO_CHECK.toString(); String zfpath = "D:/apiclient_cert.p12"; try { // 獲取openid String openid = WeChatUtil.getByOpenid(appid, secret, code); // 付款 boolean flag = WeChatUtil.enterprisePayment(openid, appid, mchid, nonce_str, partner_trade_no, re_user_name, jine, desc, spbill_create_ip, check_name, key, zfpath); // 成功 if (flag) { return "SUCCESS"; } } catch (Exception e) { System.err.println(e.getStackTrace()); } return "FAIL"; }
獲取關注本公眾號用戶唯一標示 獲取openid
java代碼 獲取openid 靜態方法
/** * 獲取openid * * @description * @param appid * @param secret * @param code * @return * @author shaomiao */ public static String getByOpenid(String appid, String secret, String code) { String url = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code"; String jsonstring = WeChatUtil.getJsonString(url); JSONObject json1 = JSONObject.parseObject(jsonstring); String openid = json1.get("openid").toString(); return openid; }
企業付款的調用公共方法
java代碼
post提交 xml參數
解析回調的xml
/** * 企業付款 * * @description * @param openid * @param appid * @param mchid 商戶id * @param nonce_str * @param partner_trade_no * @param re_user_name * @param jine * @param desc * @param spbill_create_ip * @param check_name * @return * @author Jobs * @throws IOException * @throws ClientProtocolException */ public static boolean enterprisePayment(String openid, String appid, String mchid, String nonce_str, String partner_trade_no, String re_user_name, int jine, String desc, String spbill_create_ip, String check_name, String key, String zfpath) throws Exception { boolean getSuccess = true; if (null != openid) { // zf Map<String, String> params_map = new LinkedHashMap<String, String>(); StringBuffer param = new StringBuffer(); // appid param.append("mch_appid=" + appid); // 商戶id param.append("&mchid=" + mchid); // 隨機字符串 // param.append("&nonce_str=" // + ZifwUtil.string2MD5(new Date().getTime() + "")); param.append("&nonce_str=" + nonce_str); // 訂單號自定義 param.append("&partner_trade_no=" + partner_trade_no); param.append("&openid=" + openid); // 校驗用戶姓名選項 /** * NO_CHECK:不校驗真實姓名 * FORCE_CHECK:強校驗真實姓名(未實名認證的用戶會校驗失敗,無法轉賬) * OPTION_CHECK:針對已實名認證的用戶才校驗真實姓名(未實名認證用戶不校驗,可以轉賬成功) */ param.append("&check_name=" + check_name); // 收款用戶姓名 param.append("&re_user_name=" + re_user_name); // 金額 param.append("&amount=" + jine); // 企業付款描述信息 param.append("&desc=" + desc); // Ip地址 param.append("&spbill_create_ip=" + spbill_create_ip); String[] params = param.toString().split("&"); Arrays.sort(params); param = new StringBuffer(); for (String p : params) { String[] value = p.split("="); params_map.put(value[0], value[1]); param.append(value[0] + "=" + value[1] + "&"); } // 簽名最后 String sign = ZifwUtil.string2MD5(param.toString() + "key=" + key) .toUpperCase(); params_map.put("sign", sign); String reqStr = ZifwUtil.toXml(params_map); // ZHENGSHU CloseableHttpClient httpclient = certificateValidation(zfpath, mchid); HttpPost httppost = new HttpPost( "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"); StringEntity myEntity = new StringEntity(reqStr, "UTF-8"); httppost.setEntity(myEntity); System.out.println("executing request" + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); InputStreamReader reader = new InputStreamReader( resEntity.getContent(), "UTF-8"); char[] buff = new char[1024]; int length = 0; StringBuffer strhuxml = new StringBuffer(); while ((length = reader.read(buff)) != -1) { strhuxml.append(new String(buff, 0, length)); System.out.println(new String(buff, 0, length)); } // httpclient.close(); httpclient.getConnectionManager().shutdown(); // String ret = ZifwUtil.post( // "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", // reqStr); // 解析傳過來的xml Document document = DocumentHelper.parseText(strhuxml.toString()); // 得到xml根元素 Element root = document.getRootElement(); // 得到根元素的所有子節點 List<Element> elementList = root.elements(); String errors = ""; for (Element e : elementList) { // result_code業務 if ("return_code".equals(e.getName()) && !"SUCCESS".equals(e.getText())) { getSuccess = false; } if ("result_code".equals(e.getName()) && !"SUCCESS".equals(e.getText())) { getSuccess = false; } } } return getSuccess; }
微信簽名驗證證書
驗證證書公共方法
/** * 驗證證書公共方法 * * @description * @param zfpath 證書的路徑 * @param mchid 商戶id * @return * @throws Exception * @author Jobs */ // shanghuid // 驗證證書 @SuppressWarnings("deprecation") public static CloseableHttpClient certificateValidation(String zfpath, String mchid) throws Exception { // 指定讀取證書格式為PKCS12 KeyStore keyStore = KeyStore.getInstance("PKCS12"); // 證書地址 FileInputStream instream = new FileInputStream(new File(zfpath)); try { keyStore.load(instream, mchid.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom() .loadKeyMaterial(keyStore, mchid.toCharArray()).build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom() .setSSLSocketFactory(sslsf).build(); return httpclient; }
微信公共方法 字符串轉xml
/** * 微信支付拼接xml * * @param params * @return */ public static String toXml(Map<String, String> params) { String xml = "<xml>"; for (String key : params.keySet()) { if ("body".equals(key) || "attach".equals(key) || "sign".equals(key)) { xml += "<" + key + "><![CDATA[" + params.get(key) + "]]></" + key + ">"; } else { xml += "<" + key + ">" + params.get(key) + "</" + key + ">"; } } xml += "</xml>"; return xml; }
微信公共方法 字符串MD5
加密
用來加密簽名
/*** * MD5加碼 生成32位md5碼 */ public static String string2MD5(String inStr) { StringBuffer buf = new StringBuffer(); try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(inStr.getBytes("utf-8")); byte b[] = md.digest(); int i; for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } } catch (Exception e) { e.printStackTrace(); } return buf.toString(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。