微信下載對賬單

調用微信對賬單接口

public static void main(String[] args) throws Exception {
		// 隨機數
		String str = WXPayUtil.generateNonceStr();
		// data
		Map<String, String> data = new LinkedHashMap<>();
		// Map<String, String> data1 = new LinkedHashMap<>();
		data.put("appid", APPID);
		data.put("bill_date", "20191111");
		data.put("bill_type", "ALL");
		data.put("mch_id", MCH_ID);
		data.put("nonce_str", str);
	 	data.put("tar_type", "GZIP");
		String sign = WXPayUtil.generateSignature(data, API_KEY);
		data.put("sign", sign);
		System.out.println(sign);
		System.out.println(WXPayUtil.mapToXml(data));
		String result = SendPost.post(WXPayUtil.mapToXml(data), 6000, 6000);
//		System.out.println("result"+WXPayUtil.xmlToMap(result));

		System.out.println(result);
		if (result.startsWith("<xml>") || result.contains("error_code")) {
			System.out.println("無數據或者日期錯誤");

		} else {

			// 獲取表頭以外的其他內容:交易記錄、彙總表頭、彙總數據
			String otherContent = result.substring(result.indexOf("`"));
			System.out.println("otherContent" + otherContent);

			// 獲取交易記錄
			String records = otherContent.substring(0, otherContent.indexOf("總"));
			String[] total = records.split("\n");

			for (int i = 0; i < total.length; i++) {
				String[] order = total[i].replace("`", "").split(",");
				System.out.println("order" + order[5]);
				.....
			}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

隨機數

/**
	 * 獲取隨機字符串 Nonce Str
	 *
	 * @return String 隨機字符串
	 */
	public static String generateNonceStr() {
		char[] nonceChars = new char[32];
		for (int index = 0; index < nonceChars.length; ++index) {
			nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
		}
		return new String(nonceChars);
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

生成sign簽名

/**
	 * 生成簽名
	 *
	 * @param data 待簽名數據
	 * @param key  API密鑰
	 * @return 簽名
	 */
	public static String generateSignature(final Map<String, String> data, String key) throws Exception {
		return generateSignature(data, key, SignType.MD5);
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

簽名邏輯

/**
	 * 生成簽名. 注意,若含有sign_type字段,必須和signType參數保持一致。
	 *
	 * @param data     待簽名數據
	 * @param key      API密鑰
	 * @param signType 簽名方式
	 * @return 簽名
	 */
	public static String generateSignature(final Map<String, String> data, String key, SignType signType)
			throws Exception {
		Set<String> keySet = data.keySet();
		String[] keyArray = keySet.toArray(new String[keySet.size()]);
		Arrays.sort(keyArray);
		StringBuilder sb = new StringBuilder();
		for (String k : keyArray) {
			if (k.equals(WXPayConstants.FIELD_SIGN)) {
				continue;
			}
			if (data.get(k).trim().length() > 0) // 參數值爲空,則不參與簽名
				sb.append(k).append("=").append(data.get(k).trim()).append("&");
		}
		sb.append("key=").append(key);
		if (SignType.MD5.equals(signType)) {
			return MD5(sb.toString()).toUpperCase();
		} else if (SignType.HMACSHA256.equals(signType)) {
			return HMACSHA256(sb.toString(), key);
		} else {
			throw new Exception(String.format("Invalid sign_type: %s", signType));
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

將map轉化爲xml

/**
	 * 將Map轉換爲XML格式的字符串
	 *
	 * @param data Map類型數據
	 * @return XML格式的字符串
	 * @throws Exception
	 */
	public static String mapToXml(Map<String, String> data) throws Exception {
		org.w3c.dom.Document document = WXPayXmlUtil.newDocument();
		org.w3c.dom.Element root = document.createElement("xml");
		document.appendChild(root);
		for (String key : data.keySet()) {
			String value = data.get(key);
			if (value == null) {
				value = "";
			}
			value = value.trim();
			org.w3c.dom.Element filed = document.createElement(key);
			filed.appendChild(document.createTextNode(value));
			root.appendChild(filed);
		}
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		DOMSource source = new DOMSource(document);
		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		StringWriter writer = new StringWriter();
		StreamResult result = new StreamResult(writer);
		transformer.transform(source, result);
		String output = writer.getBuffer().toString(); // .replaceAll("\n|\r", "");
		try {
			writer.close();
		} catch (Exception ex) {
		}
		return output;
	}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

http post請求

/**
	 * post 請求
	 * 
	 * @return
	 * @throws IOException
	 * @throws ClientProtocolException
	 * 
	 * 
	 * 
	 */
	public static String post(String data, int readTimeoutMs, int connectTimeoutMs)
			throws ClientProtocolException, IOException {
		BasicHttpClientConnectionManager connManager = null;
		HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();

		String url = "https://api.mch.weixin.qq.com/pay/downloadbill"; // 路徑
		HttpPost httpPost = new HttpPost(url);
		// 是指連接時間
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs)
				.setConnectTimeout(connectTimeoutMs).build();
		httpPost.setConfig(requestConfig);

		StringEntity postEntity = new StringEntity(data, "UTF-8");
		httpPost.addHeader("Content-Type", "text/xml;charset=utf-8");
		httpPost.addHeader("Accept-Encoding", "gzip,deflate");
		// httpPost.addHeader("User-Agent", USER_AGENT + " " +
		// config.getMchID());//10000100
		httpPost.addHeader("User-Agent", USER_AGENT + " " + 1490714122);
		httpPost.setEntity(postEntity);

		HttpResponse httpResponse = httpClient.execute(httpPost);
		// HttpEntity httpEntity = httpResponse.getEntity();
		System.out.println(httpResponse);
		Header[] headers = httpResponse.getHeaders("Content-Type");
		boolean isGzip = false;
		for (Header h : headers) {
			if (h.getValue().equals("application/x-gzip")) {
				// 返回頭中含有gzip
				isGzip = true;
			}
		}
		if (isGzip) {
			// 需要進行gzip解壓處理
			return EntityUtils.toString(new GzipDecompressingEntity(httpResponse.getEntity()), "UTF-8");
		} else {
			return EntityUtils.toString(httpResponse.getEntity());
		}

		// return EntityUtils.toString(httpEntity, "UTF-8");
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章