mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-07-15 05:13:16 +08:00
抽取获取代理对象的方法到接口中,方便微信支付调用,并优化部分代码
This commit is contained in:
parent
1ceb4bf707
commit
d88a8d3d83
@ -5,6 +5,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
|
|||||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||||
import me.chanjar.weixin.mp.bean.*;
|
import me.chanjar.weixin.mp.bean.*;
|
||||||
import me.chanjar.weixin.mp.bean.result.*;
|
import me.chanjar.weixin.mp.bean.result.*;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信API的Service
|
* 微信API的Service
|
||||||
@ -228,6 +229,11 @@ public interface WxMpService {
|
|||||||
*/
|
*/
|
||||||
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
|
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取代理对象
|
||||||
|
*/
|
||||||
|
HttpHost getHttpProxy();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入 {@link WxMpConfigStorage} 的实现
|
* 注入 {@link WxMpConfigStorage} 的实现
|
||||||
*/
|
*/
|
||||||
|
@ -14,6 +14,8 @@ import me.chanjar.weixin.mp.bean.pay.result.*;
|
|||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.http.Consts;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
|
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
|
||||||
@ -27,6 +29,7 @@ import org.apache.http.util.EntityUtils;
|
|||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -199,7 +202,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
|
|
||||||
String url = PAY_BASE_URL + "/pay/orderquery";
|
String url = PAY_BASE_URL + "/pay/orderquery";
|
||||||
|
|
||||||
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
|
String responseContent = this.executeRequest(url, xstream.toXML(request));
|
||||||
WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
|
WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
|
||||||
result.composeCoupons(responseContent);
|
result.composeCoupons(responseContent);
|
||||||
if ("FAIL".equals(result.getResultCode())) {
|
if ("FAIL".equals(result.getResultCode())) {
|
||||||
@ -233,7 +236,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
|
|
||||||
String url = PAY_BASE_URL + "/pay/closeorder";
|
String url = PAY_BASE_URL + "/pay/closeorder";
|
||||||
|
|
||||||
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
|
String responseContent = this.executeRequest(url, xstream.toXML(request));
|
||||||
WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent);
|
WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent);
|
||||||
if ("FAIL".equals(result.getResultCode())) {
|
if ("FAIL".equals(result.getResultCode())) {
|
||||||
throw new WxErrorException(WxError.newBuilder()
|
throw new WxErrorException(WxError.newBuilder()
|
||||||
@ -263,7 +266,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
|
|
||||||
String url = PAY_BASE_URL + "/pay/unifiedorder";
|
String url = PAY_BASE_URL + "/pay/unifiedorder";
|
||||||
|
|
||||||
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
|
String responseContent = this.executeRequest(url, xstream.toXML(request));
|
||||||
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
|
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
|
||||||
.fromXML(responseContent);
|
.fromXML(responseContent);
|
||||||
if ("FAIL".equals(result.getResultCode())) {
|
if ("FAIL".equals(result.getResultCode())) {
|
||||||
@ -377,7 +380,27 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String executeRequest( String url, String requestStr) throws WxErrorException {
|
||||||
|
HttpPost httpPost = new HttpPost(url);
|
||||||
|
if (this.wxMpService.getHttpProxy() != null) {
|
||||||
|
httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
try (CloseableHttpClient httpclient = HttpClients.custom().build()) {
|
||||||
|
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
|
||||||
|
|
||||||
|
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
|
||||||
|
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
|
||||||
|
}finally {
|
||||||
|
httpPost.releaseConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String executeRequestWithKeyFile( String url, File keyFile, String requestStr, String mchId) throws WxErrorException {
|
private String executeRequestWithKeyFile( String url, File keyFile, String requestStr, String mchId) throws WxErrorException {
|
||||||
|
|
||||||
try (FileInputStream inputStream = new FileInputStream(keyFile)) {
|
try (FileInputStream inputStream = new FileInputStream(keyFile)) {
|
||||||
KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
||||||
keyStore.load(inputStream, mchId.toCharArray());
|
keyStore.load(inputStream, mchId.toCharArray());
|
||||||
@ -386,13 +409,18 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
|||||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
|
||||||
new DefaultHostnameVerifier());
|
new DefaultHostnameVerifier());
|
||||||
|
|
||||||
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
|
HttpPost httpPost = new HttpPost(url);
|
||||||
HttpPost httpPost = new HttpPost(url);
|
if (this.wxMpService.getHttpProxy() != null) {
|
||||||
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
|
httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
|
||||||
|
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
|
||||||
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
|
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
|
||||||
return EntityUtils.toString(response.getEntity());
|
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
|
||||||
}
|
}
|
||||||
|
}finally {
|
||||||
|
httpPost.releaseConnection();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
|
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
|
||||||
|
@ -425,6 +425,7 @@ public class WxMpServiceImpl implements WxMpService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public HttpHost getHttpProxy() {
|
public HttpHost getHttpProxy() {
|
||||||
return this.httpProxy;
|
return this.httpProxy;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user