mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-07 23:24:39 +08:00
🎨 添加 Apache HttpComponents Client 5.x 为可选的 http client
Some checks failed
Publish to Maven Central / build-and-publish (push) Has been cancelled
Some checks failed
Publish to Maven Central / build-and-publish (push) Has been cancelled
This commit is contained in:
@@ -34,6 +34,11 @@
|
||||
<artifactId>jodd-util</artifactId>
|
||||
<version>6.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
@@ -18,7 +18,7 @@ import java.util.List;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorCouponCodeRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>* 字段名:批次号
|
||||
|
@@ -18,7 +18,7 @@ import java.util.List;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorCouponCodeResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>* 字段名:批次号
|
||||
@@ -130,8 +130,8 @@ public class BusiFavorCouponCodeResult implements Serializable {
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public static class FailCode {
|
||||
public static final float serialVersionUID = 1L;
|
||||
public static class FailCode implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>* 字段名:上传失败的券code
|
||||
|
@@ -4,6 +4,8 @@ import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* H5发券请求对象
|
||||
* <pre>
|
||||
@@ -14,8 +16,8 @@ import lombok.NoArgsConstructor;
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorCouponsUrlRequest {
|
||||
public static final float serialVersionUID = 1L;
|
||||
public class BusiFavorCouponsUrlRequest implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -4,6 +4,8 @@ import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 核销用户券请求对象
|
||||
* <pre>
|
||||
@@ -14,8 +16,8 @@ import lombok.NoArgsConstructor;
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorCouponsUseRequest {
|
||||
public static final float serialVersionUID = 1L;
|
||||
public class BusiFavorCouponsUseRequest implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -17,7 +17,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorQueryOneUserCouponsRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>* 字段名:用户标识
|
||||
|
@@ -20,7 +20,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorQueryOneUserCouponsResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>* 字段名:批次归属商户号
|
||||
|
@@ -17,7 +17,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorQueryUserCouponsRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>* 字段名:用户标识
|
||||
|
@@ -18,7 +18,7 @@ import java.util.List;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BusiFavorQueryUserCouponsResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>* 字段名:+结果集
|
||||
|
@@ -17,7 +17,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class AvailableWeek implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -51,7 +51,7 @@ public class AvailableWeek implements Serializable {
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public static class AvailableDayTimeItem implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -18,7 +18,7 @@ import java.util.List;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class CouponAvailableTime implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -18,7 +18,7 @@ import java.io.Serializable;
|
||||
@NoArgsConstructor
|
||||
public class IrregularyAvaliableTime implements Serializable {
|
||||
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -23,7 +23,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BatchDetailsRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:微信支付批次单号
|
||||
|
@@ -25,7 +25,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BatchDetailsResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
@@ -18,7 +18,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BatchNumberRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -20,7 +20,7 @@ import java.util.List;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BatchNumberResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
@@ -20,7 +20,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class BillReceiptResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
@@ -21,7 +21,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class DownloadRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -20,7 +20,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class ElectronicReceiptsRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:受理类型
|
||||
|
@@ -20,7 +20,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class ElectronicReceiptsResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:受理类型
|
||||
|
@@ -20,7 +20,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class MerchantBatchRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:商家批次单号
|
||||
|
@@ -19,7 +19,7 @@ import java.util.List;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class PartnerTransferRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -18,7 +18,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class PartnerTransferResult implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:商家批次单号
|
||||
|
@@ -18,7 +18,7 @@ import java.io.Serializable;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class ReceiptBillRequest implements Serializable {
|
||||
public static final float serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:商家批次单号
|
||||
|
@@ -39,7 +39,6 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("rawtypes")
|
||||
public boolean canConvert(Class type) {
|
||||
return type.equals(WxPayOrderNotifyResult.class);
|
||||
}
|
||||
|
@@ -64,7 +64,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
||||
|
||||
private static final Gson GSON = new GsonBuilder().create();
|
||||
|
||||
static ThreadLocal<WxPayApiData> wxApiData = new ThreadLocal<>();
|
||||
static final ThreadLocal<WxPayApiData> wxApiData = new ThreadLocal<>();
|
||||
|
||||
|
||||
@Setter
|
||||
|
@@ -0,0 +1,367 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.WxPayApiData;
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.github.binarywang.wxpay.v3.WxPayV3DownloadHttpGet;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.util.http.apache.ByteArrayResponseHandler;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.*;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.*;
|
||||
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 微信支付请求实现类,apache httpconponents 实现.
|
||||
*
|
||||
* @author altusea
|
||||
*/
|
||||
@Slf4j
|
||||
public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl {
|
||||
|
||||
private static final String ACCEPT = "Accept";
|
||||
private static final String CONTENT_TYPE = "Content-Type";
|
||||
private static final String APPLICATION_JSON = "application/json";
|
||||
private static final String WECHAT_PAY_SERIAL = "Wechatpay-Serial";
|
||||
|
||||
@Override
|
||||
public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
|
||||
try {
|
||||
HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey);
|
||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
|
||||
final byte[] bytes = httpClient.execute(httpPost, ByteArrayResponseHandler.INSTANCE);
|
||||
final String responseData = Base64.getEncoder().encodeToString(bytes);
|
||||
this.logRequestAndResponse(url, requestStr, responseData);
|
||||
wxApiData.set(new WxPayApiData(url, requestStr, responseData, null));
|
||||
return bytes;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.logError(url, requestStr, e);
|
||||
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
|
||||
throw new WxPayException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String post(String url, String requestStr, boolean useKey) throws WxPayException {
|
||||
try {
|
||||
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
|
||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
this.logRequestAndResponse(url, requestStr, responseString);
|
||||
if (this.getConfig().isIfSaveApiData()) {
|
||||
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
|
||||
}
|
||||
return responseString;
|
||||
}
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.logError(url, requestStr, e);
|
||||
if (this.getConfig().isIfSaveApiData()) {
|
||||
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
|
||||
}
|
||||
throw new WxPayException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String postV3(String url, String requestStr) throws WxPayException {
|
||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||
this.configureRequest(httpPost);
|
||||
return this.requestV3(url, requestStr, httpPost);
|
||||
}
|
||||
|
||||
private String requestV3(String url, String requestStr, HttpRequestBase httpRequestBase) throws WxPayException {
|
||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpRequestBase)) {
|
||||
//v3已经改为通过状态码判断200 204 成功
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
//post方法有可能会没有返回值的情况
|
||||
String responseString = null;
|
||||
if (response.getEntity() != null) {
|
||||
responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
|
||||
this.logRequestAndResponse(url, requestStr, responseString);
|
||||
return responseString;
|
||||
}
|
||||
|
||||
//有错误提示信息返回
|
||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||
throw convertException(jsonObject);
|
||||
} catch (Exception e) {
|
||||
this.logError(url, requestStr, e);
|
||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||
} finally {
|
||||
httpRequestBase.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String patchV3(String url, String requestStr) throws WxPayException {
|
||||
HttpPatch httpPatch = new HttpPatch(url);
|
||||
httpPatch.setEntity(createEntry(requestStr));
|
||||
return this.requestV3(url, requestStr, httpPatch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException {
|
||||
HttpPost httpPost = this.createHttpPost(url, requestStr);
|
||||
this.configureRequest(httpPost);
|
||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
//v3已经改为通过状态码判断200 204 成功
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String responseString = "{}";
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
responseString = EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
|
||||
this.logRequestAndResponse(url, requestStr, responseString);
|
||||
return responseString;
|
||||
}
|
||||
|
||||
//有错误提示信息返回
|
||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||
throw convertException(jsonObject);
|
||||
} catch (Exception e) {
|
||||
this.logError(url, requestStr, e);
|
||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String postV3(String url, HttpPost httpPost) throws WxPayException {
|
||||
return this.requestV3(url, httpPost);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayException {
|
||||
this.configureRequest(httpRequest);
|
||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
|
||||
//v3已经改为通过状态码判断200 204 成功
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
//post方法有可能会没有返回值的情况
|
||||
String responseString = null;
|
||||
if (response.getEntity() != null) {
|
||||
responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
|
||||
log.info("\n【请求地址】:{}\n【响应数据】:{}", url, responseString);
|
||||
return responseString;
|
||||
}
|
||||
|
||||
//有错误提示信息返回
|
||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||
throw convertException(jsonObject);
|
||||
} catch (Exception e) {
|
||||
log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||
} finally {
|
||||
httpRequest.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getV3(String url) throws WxPayException {
|
||||
if (this.getConfig().isStrictlyNeedWechatPaySerial()) {
|
||||
return getV3WithWechatPaySerial(url);
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
return this.requestV3(url, httpGet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getV3WithWechatPaySerial(String url) throws WxPayException {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
return this.requestV3(url, httpGet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream downloadV3(String url) throws WxPayException {
|
||||
HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
|
||||
this.configureRequest(httpGet);
|
||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
//v3已经改为通过状态码判断200 204 成功
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
Header contentType = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
|
||||
boolean isJsonContentType = Objects.nonNull(contentType) && ContentType.APPLICATION_JSON.getMimeType()
|
||||
.equals(ContentType.parse(String.valueOf(contentType.getValue())).getMimeType());
|
||||
if ((HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) && !isJsonContentType) {
|
||||
log.info("\n【请求地址】:{}\n", url);
|
||||
return response.getEntity().getContent();
|
||||
}
|
||||
|
||||
//response里的header有content-type=json说明返回了错误信息
|
||||
//有错误提示信息返回
|
||||
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||
throw convertException(jsonObject);
|
||||
} catch (Exception e) {
|
||||
log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
||||
throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String putV3(String url, String requestStr) throws WxPayException {
|
||||
HttpPut httpPut = new HttpPut(url);
|
||||
StringEntity entity = createEntry(requestStr);
|
||||
httpPut.setEntity(entity);
|
||||
return requestV3(url, httpPut);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String deleteV3(String url) throws WxPayException {
|
||||
HttpDelete httpDelete = new HttpDelete(url);
|
||||
return requestV3(url, httpDelete);
|
||||
}
|
||||
|
||||
private void configureRequest(HttpRequestBase request) {
|
||||
String serialNumber = getWechatPaySerial(getConfig());
|
||||
String method = request.getMethod();
|
||||
request.addHeader(ACCEPT, APPLICATION_JSON);
|
||||
if (!method.equals("POST")) {
|
||||
request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
|
||||
}
|
||||
request.addHeader(WECHAT_PAY_SERIAL, serialNumber);
|
||||
|
||||
request.setConfig(RequestConfig.custom()
|
||||
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())
|
||||
.setConnectTimeout(this.getConfig().getHttpConnectionTimeout())
|
||||
.setSocketTimeout(this.getConfig().getHttpTimeout())
|
||||
.build());
|
||||
}
|
||||
|
||||
private CloseableHttpClient createApiV3HttpClient() throws WxPayException {
|
||||
CloseableHttpClient apiV3HttpClient = this.getConfig().getApiV3HttpClient();
|
||||
if (null == apiV3HttpClient) {
|
||||
return this.getConfig().initApiV3HttpClient();
|
||||
}
|
||||
return apiV3HttpClient;
|
||||
}
|
||||
|
||||
private static StringEntity createEntry(String requestStr) {
|
||||
return new StringEntity(requestStr, ContentType.create(APPLICATION_JSON, StandardCharsets.UTF_8));
|
||||
//return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||
}
|
||||
|
||||
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
|
||||
HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
||||
if (useKey) {
|
||||
this.initSSLContext(httpClientBuilder);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
|
||||
if (StringUtils.isEmpty(this.getConfig().getHttpProxyUsername())) {
|
||||
this.getConfig().setHttpProxyUsername("whatever");
|
||||
}
|
||||
|
||||
// 使用代理服务器 需要用户认证的代理服务器
|
||||
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
provider.setCredentials(new AuthScope(this.getConfig().getHttpProxyHost(),
|
||||
this.getConfig().getHttpProxyPort()),
|
||||
new UsernamePasswordCredentials(this.getConfig().getHttpProxyUsername(),
|
||||
this.getConfig().getHttpProxyPassword()));
|
||||
httpClientBuilder.setDefaultCredentialsProvider(provider)
|
||||
.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
|
||||
}
|
||||
|
||||
// 提供自定义httpClientBuilder的能力
|
||||
Optional.ofNullable(getConfig().getHttpClientBuilderCustomizer()).ifPresent(e -> {
|
||||
e.customize(httpClientBuilder);
|
||||
});
|
||||
|
||||
return httpClientBuilder;
|
||||
}
|
||||
|
||||
private HttpPost createHttpPost(String url, String requestStr) {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setEntity(createEntry(requestStr));
|
||||
|
||||
httpPost.setConfig(RequestConfig.custom()
|
||||
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())
|
||||
.setConnectTimeout(this.getConfig().getHttpConnectionTimeout())
|
||||
.setSocketTimeout(this.getConfig().getHttpTimeout())
|
||||
.build());
|
||||
|
||||
return httpPost;
|
||||
}
|
||||
|
||||
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
|
||||
SSLContext sslContext = this.getConfig().getSslContext();
|
||||
if (null == sslContext) {
|
||||
sslContext = this.getConfig().initSSLContext();
|
||||
}
|
||||
|
||||
httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext,
|
||||
new DefaultHostnameVerifier()));
|
||||
}
|
||||
|
||||
private WxPayException convertException(JsonObject jsonObject) {
|
||||
//TODO 这里考虑使用新的适用于V3的异常
|
||||
JsonElement codeElement = jsonObject.get("code");
|
||||
String code = codeElement == null ? null : codeElement.getAsString();
|
||||
String message = jsonObject.get("message").getAsString();
|
||||
WxPayException wxPayException = new WxPayException(message);
|
||||
wxPayException.setErrCode(code);
|
||||
wxPayException.setErrCodeDes(message);
|
||||
return wxPayException;
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容微信支付公钥模式
|
||||
*/
|
||||
private String getWechatPaySerial(WxPayConfig wxPayConfig) {
|
||||
if (StringUtils.isNotBlank(wxPayConfig.getPublicKeyId())) {
|
||||
return wxPayConfig.getPublicKeyId();
|
||||
}
|
||||
|
||||
return wxPayConfig.getVerifier().getValidCertificate().getSerialNumber().toString(16).toUpperCase();
|
||||
}
|
||||
|
||||
private void logRequestAndResponse(String url, String requestStr, String responseStr) {
|
||||
log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseStr);
|
||||
}
|
||||
|
||||
private void logError(String url, String requestStr, Exception e) {
|
||||
log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
||||
}
|
||||
}
|
@@ -92,8 +92,7 @@ public class SignUtils {
|
||||
for (String key : new TreeMap<>(params).keySet()) {
|
||||
String value = params.get(key);
|
||||
boolean shouldSign = false;
|
||||
if (StringUtils.isNotEmpty(value) && !ArrayUtils.contains(ignoredParams, key)
|
||||
&& !NO_SIGN_PARAMS.contains(key)) {
|
||||
if (StringUtils.isNotEmpty(value) && !ArrayUtils.contains(ignoredParams, key) && !NO_SIGN_PARAMS.contains(key)) {
|
||||
shouldSign = true;
|
||||
}
|
||||
|
||||
|
@@ -24,7 +24,7 @@ public class ZipUtils {
|
||||
resultFile.createNewFile();
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(resultFile);
|
||||
GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(file));) {
|
||||
GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(file))) {
|
||||
IOUtils.copy(gzis, fos);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user