Wechat download statement

Keywords: xml Java PHP Database

I've come into contact with some relatively new things recently. Record them

First post the official API address of wechat and feel the charm of wechat...

https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6

After learning some notes about wechat download bill, we will officially start

 

The following is the specific implementation:

Paste the main method first, and other tool classes will have specific instructions:

	public static void main(String[] args) throws Exception {
//		downloadBill();
	}  

    public static void downloadBill() throws Exception {
        //The MyConfig tool class here is some of its own encapsulated configurations. To facilitate your own use, you can skip this step
		MyConfig myConfig = new MyConfig();  
		SortedMap<Object,Object> parameters =new TreeMap<Object,Object>();
        //The value value in this can be configured according to the wechat merchant parameters
		parameters.put("appid", myConfig.getAppID());//appid
		parameters.put("mch_id", myConfig.getMchID());//Merchant No
		parameters.put("nonce_str", CreateNoncestr());//Random string
		parameters.put("bill_date", "20180125");//Statement date
		parameters.put("bill_type", "ALL");//Statement type
        //Call the method here to generate the signature required by wechat (this is a pit. When making a wechat refund, I was stuck for a day because the signature did not meet the specification)
		String sign = createSign("utf-8", parameters);//autograph
		parameters.put("sign", sign);

        //The getRequestXml method here is a tool method to transfer xml, which I will post below
		String reuqestXml = getRequestXml(parameters); 
        /**
          * CommonUtil here is an https general tool class, which I will post below
          * download_bill_url It is the download address of wechat official statement
          * download_bill_url = "https://api.mch.weixin.qq.com/pay/downloadbill"
          */
		String result=CommonUtil.httpsRequest(myConfig.download_bill_url, "POST", reuqestXml);
		//The generation time of wechat statement is after 9:00 of the next day, so when the query date is the same day, the error message indicates that the date is invalid
        if(result.startsWith("<xml>")){
            System.out.println(result);
            System.out.println("No order");
        }else {  
            String tradeMsg =  result.substring(result.indexOf("`"));
        	String tradeInfo = tradeMsg.substring(0, tradeMsg.indexOf("total")).replace("`", "");// Remove summary data and '`'
           String tradeInfo =  tradeMsg.substring(0,tradeMsg.indexOf("total"));
           String tradeTotalMsg =tradeMsg.substring(tradeMsg.indexOf("total"));
           String tradeTotalInfo =tradeTotalMsg.substring(tradeTotalMsg.indexOf("`"));
          
           
           System.out.println("result----->"+result);
           System.out.println("tradeMsg----->"+tradeMsg);
           System.out.println("tradeInfo----->"+tradeInfo);
           System.out.println("tradeTotalMsg----->"+tradeTotalMsg);
           System.out.println("tradeTotalInfo----->"+tradeTotalInfo);
           
           //The last column is the rate, which ends with% as the cutting target to facilitate the operation and storage of the database
           String[] tradeArray = tradeInfo.split("%");
           List<Map<String, Object>> resultList = new ArrayList<>();
           
           for(int i = 0;i < tradeArray.length;i++) {
        	   String[] tradeDetailArray = tradeArray[i].split(",");
        	   Map<String, Object> map = new HashMap<>();
        	   
               /**
                 * Here I take several fields that are more useful to me
                 * For detailed fields, you can refer to here or see the print results of the console
                 * https://www.cnblogs.com/mgqy/articles/7657483.html
                 */
        	   map.put("readeTime", tradeDetailArray[0]);//Transaction time
        	   map.put("appid", tradeDetailArray[1]);//The official account ID
        	   map.put("mchId", tradeDetailArray[2]);//Merchant No
        	   map.put("transactionId", tradeDetailArray[5]);//Wechat Order No
        	   map.put("outTradeNo", tradeDetailArray[6]);//Merchant order number
        	   map.put("openid", tradeDetailArray[7]);//User ID (openid)
        	   map.put("tradeState", tradeDetailArray[9]);// Transaction status
        	   map.put("tradeBank", tradeDetailArray[10]);// paying bank
        	   map.put("totalFee", tradeDetailArray[12]);//Total amount
        	   map.put("refundFee", tradeDetailArray[16]);//refund amount 
        	   map.put("tradeName", tradeDetailArray[20]);// Commodity name
        	   map.put("poundage", tradeDetailArray[22]);//Service Charge
        	   resultList.add(map);
           }
           System.out.println("resultList--->"+resultList);
       }
	}

Here are some tool classes and methods used:

1. Random string generation method

	/**
	 * Random string
	 * @return
	 */
    public static String CreateNoncestr() {  
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
        String res = "";  
        for (int i = 0; i < 16; i++) {  
            Random rd = new Random();  
            res += chars.charAt(rd.nextInt(chars.length() - 1));  
        }  
        return res;  
     } 

 

2. Write signature method

    /**
     * Write signature
     * @param charSet
     * @param parameters
     * @return
     * @throws Exception 
     */
     public static String createSign(String charSet,SortedMap<Object,Object> parameters) throws Exception{  
    	MyConfig myconfig = new MyConfig();
        StringBuffer sb = new StringBuffer();  
        Set es = parameters.entrySet();  
        Iterator it = es.iterator();  
        while(it.hasNext()) {  
            Map.Entry entry = (Map.Entry)it.next();  
            String k = (String)entry.getKey();  
            Object v = entry.getValue();  
            if(null != v && !"".equals(v)  
                    && !"sign".equals(k) && !"key".equals(k)) {  
                sb.append(k + "=" + v + "&");  
            }  
        }  
        sb.append("key=" + myconfig.getKey());  
//        String sign = MD5Util.MD5Encode(sb.toString(), charSet).toUpperCase();  
        String sign = MD5Util.MD5Encode(sb.toString(), charSet).toUpperCase();  
//        String sign = MD5.MD5Encode(sb.toString(), charSet).toUpperCase();  
        return sign;  
      }  

 

3. Transfer to xml format

    /**
      * Convert to xml format
      * @param parameters
      * @return
      */
     public static String getRequestXml(SortedMap<Object,Object> parameters){  
         StringBuffer sb = new StringBuffer();  
         sb.append("<xml>");  
         Set es = parameters.entrySet();  
         Iterator it = es.iterator();  
         while(it.hasNext()) {  
           Map.Entry entry = (Map.Entry)it.next();  
           String k = (String)entry.getKey();  
           String v = (String)entry.getValue();  
           if ("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)||"sign".equalsIgnoreCase(k)) {  
               sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");  
           }else {  
               sb.append("<"+k+">"+v+"</"+k+">");  
           }  
         }  
         sb.append("</xml>");  
         return sb.toString();  
     }  

 

4. https general tool class


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import org.apache.log4j.Logger;

import net.sf.json.JSONObject;

/**
 * https General tools
 */
public class CommonUtil {
    
    private static Logger log = Logger.getLogger(CommonUtil.class);
    public static JSONObject httpsRequestToJsonObject(String requestUrl, String requestMethod, String outputStr) {
        JSONObject jsonObject = null;
        try {
             String buffer = httpsRequest(requestUrl, requestMethod, outputStr);
            jsonObject = JSONObject.fromObject(buffer.toString());
        } catch (Exception e) {
            log.error("https Request exception:"+e.getMessage());
        }
        return jsonObject;
    }
    
    
    public static String httpsRequest(String requestUrl, String requestMethod, String outputStr){
        try {
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // Set request mode (GET/POST)
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            // Write data to output stream when outputStr is not null
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // Pay attention to the coding format
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            // Read return content from input stream
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            // Release resources
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            return buffer.toString();
        } catch (ConnectException ce) {
            System.out.println("Connection timeout:{}");
        } catch (Exception e) {
            System.out.println("https Request exception:{}");
        }
        return null;
    }
 }

Here, wechat download statement function is realized

Please forgive and give your comments on the bad code. 937017870@qq.com

Posted by agravayne on Wed, 29 Apr 2020 23:16:20 -0700