Authored by tanling

test

... ... @@ -25,6 +25,11 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependency>
<!-- 测试用 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.yoho.pay;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
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.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClient {
private Logger logger = LoggerFactory.getLogger(getClass());
protected static final int connectionRequestTimeout = 6000;
protected static final int connectionTimeout = 3000;
protected static final int socketTimeout = 2000;
//最大总数
private final int maxTotal = 30;
//默认并发数
private final int defaultMaxPerRoute = 10;
protected CloseableHttpClient httpClient;
public void init() throws Exception{
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
return true;
}
}).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if
// you don't want to weaken
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened
// "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create()
.register("http",
PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
connMgr.setMaxTotal(maxTotal);
connMgr.setDefaultMaxPerRoute(defaultMaxPerRoute);
b.setConnectionManager(connMgr);
//request config
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout)
.build();
b.setDefaultRequestConfig(requestConfig);
// finally, build the HttpClient;
// -- done!
httpClient = b.build();
}
public void destroy() {
if(httpClient != null)
httpClient.getConnectionManager().shutdown();
}
/**
* Get请求
* @param url
* @return
* @throws Exception
*/
public String get(String url) throws Exception {
HttpGet httpget = new HttpGet(url);
return sendHttpRequest(httpget);
}
/**
* Post请求
* @param url
* @param body
* @return
* @throws Exception
*/
public String post(String url, String body) throws Exception {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setEntity(new StringEntity(body, "UTF-8"));
return sendHttpRequest(httpPost);
}
/**
* 提交form-data
* @param url
* @param formDataMap
* @return
* @throws Exception
*/
public String postFormData(String url, Map<String, String> formDataMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
//httpPost.addHeader("Content-Type", "multipart/form-data;charset=UTF-8");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
if(formDataMap != null) {
for(Map.Entry<String, String> data : formDataMap.entrySet()) {
nvps.add(new BasicNameValuePair(data.getKey(), data.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
return sendHttpRequest(httpPost);
}
/**
* http发送请求
* @param httpRequest
* @return
* @throws Exception
*/
private String sendHttpRequest(HttpUriRequest httpRequest) throws Exception {
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpRequest);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.error("http response code: {}, request: {}", statusCode, httpRequest.getURI());
throw new RuntimeException("http response status: " + statusCode);
}
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
finally {
if (response != null) {
try {
response.close();
} catch (Exception e) {
logger.error("pay httpclient resp close failed: {}", e.getMessage());
}
}
if(httpRequest != null) {
try {
httpRequest.abort();
} catch (Exception e) {
logger.error("pay httpclient req abort failed: {}", e.getMessage());
}
}
}
}
}
... ...
package com.yoho.pay;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.yoho.core.common.utils.DateUtil;
import com.yoho.core.common.utils.MD5;
import org.junit.Test;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class PayTest {
public static final String url = "https://qr-test2.chinaums.com/netpay-route-server/api/";
/**
* 预支付=下单接口 支付宝
*/
@Test
public void testPrepay() throws Exception {
JSONObject data = new JSONObject();
data.put("msgSrc","WWW.TEST.COM");
// 支付宝
data.put("msgType", "trade.precreate"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder
data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
// 支付宝- 31945075157
data.put("merOrderId", "31945075157");
data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付
data.put("tid","00000001"); //终端号
data.put("instMid", "APPDEFAULT"); // 机构商户号
data.put("totalAmount", 1);
data.put("sign", getSign(data));
HttpClient httpClient = new HttpClient();
httpClient.init();
String result = httpClient.post(url, JSON.toJSONString(data));
System.out.println(result);
}
/**
* 预支付=下单接口 微信
*/
@Test
public void testPrepayWx() throws Exception {
JSONObject data = new JSONObject();
data.put("msgSrc","WWW.TEST.COM");
// 支付宝
data.put("msgType", "wx.unifiedOrder"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder
data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
// 支付宝- 31945075157
data.put("merOrderId", "3194"+"24253748565");
data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付
data.put("tid","00000001"); //终端号
data.put("instMid", "APPDEFAULT"); // 机构商户号
data.put("totalAmount", 1);
data.put("tradeType","APP");
data.put("sign", getSign(data));
HttpClient httpClient = new HttpClient();
httpClient.init();
String result = httpClient.post(url, JSON.toJSONString(data));
System.out.println(result);
}
/**
* 预支付=下单接口 微信
*/
@Test
public void testPrepayQmf() throws Exception {
JSONObject data = new JSONObject();
data.put("msgSrc","WWW.TEST.COM");
// 支付宝
data.put("msgType", "uac.appOrder"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder
data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
// 支付宝- 31945075157
data.put("merOrderId", "3194"+"24253748567");
data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付
data.put("tid","00000001"); //终端号
data.put("instMid", "APPDEFAULT"); // 机构商户号
data.put("totalAmount", 1);
data.put("tradeType","APP");
data.put("sign", getSign(data));
HttpClient httpClient = new HttpClient();
httpClient.init();
String result = httpClient.post(url, JSON.toJSONString(data));
System.out.println(result);
}
public static String getSign(JSONObject data){
List<String> list = Lists.newArrayList();
for (String str: data.keySet()){
list.add(str+"="+data.getString(str));
}
Collections.sort(list);
String param = list.stream().collect(Collectors.joining("&"));
String sign = MD5.md5(param+"fcAmtnx7MwismjWNhNKdHC44mNXtnEQeJkRrhKJwyrW2ysRR");
return sign;
}
/**
* 订单查询接口
*/
@Test
public void testQuary(){
JSONObject data = new JSONObject();
data.put("msgSrc","WWW.TEST.COM");
// 支付宝
data.put("msgType", "query"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder
data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
data.put("merOrderId", "24455075157");
data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付
data.put("tid","00000001"); //终端号
data.put("instMid", "APPDEFAULT"); // 机构商户号
data.put("sign", getSign(data));
}
/**
* 订单退款
*/
@Test
public void testRefund(){
JSONObject data = new JSONObject();
data.put("msgSrc","WWW.TEST.COM");
// 支付宝
data.put("msgType", "refund"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder
data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
data.put("merOrderId", "24455075157");
data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付
data.put("tid","00000001"); //终端号
data.put("instMid", "APPDEFAULT"); // 机构商户号
data.put("refundAmount",1);
data.put("sign", getSign(data));
}
/**
* 订单关闭
*/
@Test
public void testClose(){
JSONObject data = new JSONObject();
data.put("msgSrc","WWW.TEST.COM");
// 支付宝
data.put("msgType", "close"); // 微信:wx.unifiedOrder, 支付宝:trade.precreate, 全民付:qmf.order, 银联云闪付:uac.appOrder
data.put("requestTimestamp", DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss"));
data.put("merOrderId", "24455075157");
data.put("mid", "898310148160568"); //商户号 仅用于支付宝和云闪付
data.put("tid","00000001"); //终端号
data.put("instMid", "APPDEFAULT"); // 机构商户号
data.put("refundAmount",1);
data.put("sign", getSign(data));
}
// 回调接口
}
... ...