xiaoyong931011
2021-06-22 38609ee05255ce5c36f308fe8d595555a86f9ba3
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.xzx.gc.common.utils.payutil;
 
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import javax.net.ssl.SSLContext;
import java.io.*;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
@Component
public class WxUtilAdmin {
 
    //@Value("#{ssl.certificate}")
    String sslCertificate;
 
 
    /**
     * 微信小程序现金红包支付
     * @param param
     * @return
     * @throws Exception
     */
    @Transactional
    public Map<String, String> sendWxXcxPay(String url, String param) throws Exception {
        return reqPost(url, param);
    }
 
    Map<String, String> reqPost(String url,String xmlParam) throws Exception{
        KeyStore keyStore  = KeyStore.getInstance("PKCS12");
        //读取商户证书(我下载下来的证书保存到D盘,根据自己实际情况填写)
        try(FileInputStream instream = new FileInputStream(new File(sslCertificate+"apiclient_cert.p12"))) {
            keyStore.load(instream, "1546567611".toCharArray());
        }
 
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, "1546567611".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();
        HttpPost httpPost = new HttpPost(url);
 
        httpPost.setEntity(new StringEntity(xmlParam));
        try(CloseableHttpResponse response1 = httpclient.execute(httpPost) ) {
            int status = response1.getStatusLine().getStatusCode();
            if(status>=200&&status<400){
                System.out.println(response1.getStatusLine());
                HttpEntity entity1 = response1.getEntity();
                String jsonStr = EntityUtils.toString(entity1, "UTF-8");
                Map<String, Object> dataMap = new HashMap<String, Object>();
                System.out.println("返回数据:"+jsonStr);
                /*if(jsonStr.indexOf("FAIL")!=-1){
                    return prepay_id;
                }*/
                // jsonStr=new String(jsonStr.getBytes("UTF-8"), "GBK");
                Map map = doXMLParse(jsonStr);
                return map;
            }
            return null;
        }
    }
 
 
    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
     * @param strxml
     * @return
     * @throws Exception
     * @throws IOException
     */
    static Map doXMLParse(String strxml) throws Exception {
        if(null == strxml || "".equals(strxml)) {
            return null;
        }
 
        Map m = new HashMap();
        InputStream in = new ByteArrayInputStream(strxml.getBytes());
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List list = root.getChildren();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            String v = "";
            List children = e.getChildren();
            if(children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                // v = getChildrenText(children);
            }
 
            m.put(k, v);
        }
        //关闭流
        in.close();
        return m;
    }
 
 
 
}