Skip to content

Commit bd5f22b

Browse files
authored
Merge pull request #85 from Eric-Lee-Handyman/master
代码可读性优化 & 语法简化
2 parents 3d2cf15 + 0e1d54c commit bd5f22b

23 files changed

+1038
-955
lines changed

.editorconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs
2+
# https://editorconfig.org
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 2
9+
indent_style = space
10+
insert_final_newline = true
11+
max_line_length = 100
12+
tab_width = 2
13+
trim_trailing_whitespace = true
14+
continuation_indent_size = 4
15+
16+
[*.java]
17+
indent_size = 4
18+
max_line_length = 120
19+
tab_width = 4
20+
continuation_indent_size = 8
21+
22+
[{*.gant,*.gradle,*.groovy,*.gson,*.gy}]
23+
indent_size = 4
24+
tab_width = 4
25+
continuation_indent_size = 8
26+
27+
[{*.markdown,*.md}]
28+
indent_size = 4
29+
tab_width = 4
30+
continuation_indent_size = 8

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# wechatpay-apache-httpclient
1+
# wechatpay-apache-httpclient
22

33
## 概览
44

@@ -54,7 +54,7 @@ import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
5454
//...
5555
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
5656
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
57-
.withWechatpay(wechatpayCertificates);
57+
.withWechatPay(wechatpayCertificates);
5858
// ... 接下来,你仍然可以通过builder设置各种参数,来配置你的HttpClient
5959

6060
// 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签
@@ -113,7 +113,7 @@ rootNode.putObject("payer")
113113
.put("openid", "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o");
114114

115115
objectMapper.writeValue(bos, rootNode);
116-
116+
117117
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
118118
CloseableHttpResponse response = httpClient.execute(httpPost);
119119

@@ -148,7 +148,7 @@ ObjectNode rootNode = objectMapper.createObjectNode();
148148
rootNode.put("mchid","1900009191");
149149

150150
objectMapper.writeValue(bos, rootNode);
151-
151+
152152
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
153153
CloseableHttpResponse response = httpClient.execute(httpPost);
154154

@@ -173,7 +173,7 @@ Credentials credentials = new WechatPay2Credentials(merchantId, new Signer() {
173173
});
174174
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
175175
.withCredentials(credentials)
176-
.withWechatpay(wechatpayCertificates);
176+
.withWechatPay(wechatpayCertificates);
177177
```
178178

179179
## 自动更新证书功能
@@ -250,7 +250,7 @@ String filePath = "/your/home/hellokitty.png";
250250
URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload");
251251
File file = new File(filePath);
252252

253-
try (FileInputStream ins1 = new FileInputStream(file)) {
253+
try (FileInputStream ins1 = new FileInputStream(file)) {
254254
String sha256 = DigestUtils.sha256Hex(ins1);
255255
try (InputStream ins2 = new FileInputStream(file)) {
256256
HttpPost request = new WechatPayUploadHttpPost.Builder(uri)
@@ -261,7 +261,7 @@ try (FileInputStream ins1 = new FileInputStream(file)) {
261261
}
262262
```
263263

264-
[AutoUpdateVerifierTest.uploadImageTest](/src/test/java/com/wechat/pay/contrib/apache/httpclient/AutoUpdateVerifierTest.java#L86)是一个更完整的示例。
264+
[AutoUpdateVerifierTest.uploadImageTest](/src/test/java/com/wechat/pay/contrib/apache/httpclient/AutoUpdateVerifierTest.java#90)是一个更完整的示例。
265265

266266
## 常见问题
267267

@@ -281,7 +281,7 @@ PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
281281

282282
### 如何下载平台证书?
283283

284-
使用`WechatPayHttpClientBuilder`需要调用`withWechatpay`设置[微信支付平台证书](https://wechatpay-api.gitbook.io/wechatpay-api-v3/ren-zheng/zheng-shu#ping-tai-zheng-shu),而平台证书又只能通过调用[获取平台证书接口](https://wechatpay-api.gitbook.io/wechatpay-api-v3/jie-kou-wen-dang/ping-tai-zheng-shu#huo-qu-ping-tai-zheng-shu-lie-biao)下载。为了解开"死循环",你可以在第一次下载平台证书时,按照下述方法临时"跳过”应答签名的验证。
284+
使用`WechatPayHttpClientBuilder`需要调用`withWechatPay`设置[微信支付平台证书](https://wechatpay-api.gitbook.io/wechatpay-api-v3/ren-zheng/zheng-shu#ping-tai-zheng-shu),而平台证书又只能通过调用[获取平台证书接口](https://wechatpay-api.gitbook.io/wechatpay-api-v3/jie-kou-wen-dang/ping-tai-zheng-shu#huo-qu-ping-tai-zheng-shu-lie-biao)下载。为了解开"死循环",你可以在第一次下载平台证书时,按照下述方法临时"跳过”应答签名的验证。
285285

286286
```java
287287
CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()

build.gradle

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ publishing {
5757
licenses {
5858
license {
5959
name = 'The Apache Software License, Version 2.0'
60-
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
60+
url = 'https://www.apache.org/licenses/LICENSE-2.0.txt'
6161
}
6262
}
6363
developers {
@@ -91,8 +91,3 @@ publishing {
9191
signing {
9292
sign publishing.publications.maven
9393
}
94-
95-
96-
97-
98-

settings.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
rootProject.name = 'wechatpay-apache-httpclient'
2-
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
package com.wechat.pay.contrib.apache.httpclient;
2-
3-
import java.io.IOException;
4-
import org.apache.http.client.methods.HttpRequestWrapper;
5-
6-
public interface Credentials {
7-
8-
String getSchema();
9-
10-
String getToken(HttpRequestWrapper request) throws IOException;
11-
}
1+
package com.wechat.pay.contrib.apache.httpclient;
2+
3+
import java.io.IOException;
4+
import org.apache.http.client.methods.HttpRequestWrapper;
5+
6+
/**
7+
* @author xy-peng
8+
*/
9+
public interface Credentials {
10+
11+
String getSchema();
12+
13+
String getToken(HttpRequestWrapper request) throws IOException;
14+
15+
}
Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.wechat.pay.contrib.apache.httpclient;
22

3+
import static org.apache.http.HttpHeaders.AUTHORIZATION;
4+
import static org.apache.http.HttpStatus.SC_MULTIPLE_CHOICES;
5+
import static org.apache.http.HttpStatus.SC_OK;
6+
7+
import java.io.IOException;
38
import org.apache.http.HttpEntity;
49
import org.apache.http.HttpEntityEnclosingRequest;
510
import org.apache.http.HttpException;
@@ -12,67 +17,69 @@
1217
import org.apache.http.entity.BufferedHttpEntity;
1318
import org.apache.http.impl.execchain.ClientExecChain;
1419

15-
import java.io.IOException;
16-
20+
/**
21+
* @author xy-peng
22+
*/
1723
public class SignatureExec implements ClientExecChain {
18-
final ClientExecChain mainExec;
19-
final Credentials credentials;
20-
final Validator validator;
2124

22-
SignatureExec(Credentials credentials, Validator validator, ClientExecChain mainExec) {
23-
this.credentials = credentials;
24-
this.validator = validator;
25-
this.mainExec = mainExec;
26-
}
25+
private static final String WECHAT_PAY_HOST_NAME_SUFFIX = ".mch.weixin.qq.com";
26+
private final ClientExecChain mainExec;
27+
private final Credentials credentials;
28+
private final Validator validator;
2729

28-
protected void convertToRepeatableResponseEntity(CloseableHttpResponse response)
29-
throws IOException {
30-
HttpEntity entity = response.getEntity();
31-
if (entity != null) {
32-
response.setEntity(new BufferedHttpEntity(entity));
30+
protected SignatureExec(Credentials credentials, Validator validator, ClientExecChain mainExec) {
31+
this.credentials = credentials;
32+
this.validator = validator;
33+
this.mainExec = mainExec;
3334
}
34-
}
3535

36-
protected void convertToRepeatableRequestEntity(HttpRequestWrapper request) throws IOException {
37-
if (request instanceof HttpEntityEnclosingRequest) {
38-
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
39-
if (entity != null) {
40-
((HttpEntityEnclosingRequest) request).setEntity(new BufferedHttpEntity(entity));
41-
}
36+
protected void convertToRepeatableResponseEntity(CloseableHttpResponse response) throws IOException {
37+
HttpEntity entity = response.getEntity();
38+
if (entity != null) {
39+
response.setEntity(new BufferedHttpEntity(entity));
40+
}
4241
}
43-
}
4442

45-
@Override
46-
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
47-
HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException {
48-
if (request.getTarget().getHostName().endsWith(".mch.weixin.qq.com")) {
49-
return executeWithSignature(route, request, context, execAware);
50-
} else {
51-
return mainExec.execute(route, request, context, execAware);
43+
protected void convertToRepeatableRequestEntity(HttpRequestWrapper request) throws IOException {
44+
if (request instanceof HttpEntityEnclosingRequest) {
45+
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
46+
if (entity != null) {
47+
((HttpEntityEnclosingRequest) request).setEntity(new BufferedHttpEntity(entity));
48+
}
49+
}
5250
}
53-
}
5451

55-
private CloseableHttpResponse executeWithSignature(HttpRoute route, HttpRequestWrapper request,
56-
HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException {
57-
// 上传类不需要消耗两次故不做转换
58-
if (!(request.getOriginal() instanceof WechatPayUploadHttpPost)) {
59-
convertToRepeatableRequestEntity(request);
52+
@Override
53+
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext context,
54+
HttpExecutionAware execAware) throws IOException, HttpException {
55+
if (request.getTarget().getHostName().endsWith(WECHAT_PAY_HOST_NAME_SUFFIX)) {
56+
return executeWithSignature(route, request, context, execAware);
57+
} else {
58+
return mainExec.execute(route, request, context, execAware);
59+
}
6060
}
61-
// 添加认证信息
62-
request.addHeader("Authorization",
63-
credentials.getSchema() + " " + credentials.getToken(request));
6461

65-
// 执行
66-
CloseableHttpResponse response = mainExec.execute(route, request, context, execAware);
62+
private CloseableHttpResponse executeWithSignature(HttpRoute route, HttpRequestWrapper request,
63+
HttpClientContext context,
64+
HttpExecutionAware execAware) throws IOException, HttpException {
65+
// 上传类不需要消耗两次故不做转换
66+
if (!(request.getOriginal() instanceof WechatPayUploadHttpPost)) {
67+
convertToRepeatableRequestEntity(request);
68+
}
69+
// 添加认证信息
70+
request.addHeader(AUTHORIZATION, credentials.getSchema() + " " + credentials.getToken(request));
71+
72+
// 执行
73+
CloseableHttpResponse response = mainExec.execute(route, request, context, execAware);
6774

68-
// 对成功应答验签
69-
StatusLine statusLine = response.getStatusLine();
70-
if (statusLine.getStatusCode() >= 200 && statusLine.getStatusCode() < 300) {
71-
convertToRepeatableResponseEntity(response);
72-
if (!validator.validate(response)) {
73-
throw new HttpException("应答的微信支付签名验证失败");
74-
}
75+
// 对成功应答验签
76+
StatusLine statusLine = response.getStatusLine();
77+
if (statusLine.getStatusCode() >= SC_OK && statusLine.getStatusCode() < SC_MULTIPLE_CHOICES) {
78+
convertToRepeatableResponseEntity(response);
79+
if (!validator.validate(response)) {
80+
throw new HttpException("应答的微信支付签名验证失败");
81+
}
82+
}
83+
return response;
7584
}
76-
return response;
77-
}
7885
}

src/main/java/com/wechat/pay/contrib/apache/httpclient/Validator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import java.io.IOException;
44
import org.apache.http.client.methods.CloseableHttpResponse;
55

6+
/**
7+
* @author xy-peng
8+
*/
69
public interface Validator {
7-
boolean validate(CloseableHttpResponse response) throws IOException;
10+
11+
boolean validate(CloseableHttpResponse response) throws IOException;
12+
813
}

0 commit comments

Comments
 (0)