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;
|
}
|
|
|
|
}
|