Skip to content

Commit

Permalink
根据优化建议修改 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
lianup committed Jul 19, 2022
1 parent 95534e0 commit b016518
Show file tree
Hide file tree
Showing 106 changed files with 213 additions and 195 deletions.
4 changes: 2 additions & 2 deletions .code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ source:
# 提供产品代码库中工具或框架自动生成的且在代码库中的代码,没有可为空。以便后续代码统计环节进行排除等特殊处理。
auto_generate_source:
# 自动生成代码文件的正则表达式,若无统一标识格式,可以指定具体目录,样例可参考test_source举例
filepath_regex: [ ".*/service/.*" ]
filepath_regex: [ ".*/service/.*"]

# 提供产品代码库中直接以源码形式存在的第三方代码目录或代码文件名的正则表达。
# 此处备注的第三方代码在后续统计代码量环节会被排除,若代码库中不存在需要排除的第三方代码,该项配置标识可为空
third_party_source:
#第三方代码文件的正则表达式,若无统一标识格式,可以指定具体目录,样例可参考test_source举例
filepath_regex: [ "/vendor/.*" ]
filepath_regex: [ "/vendor/.*", ".*/buildSrc/.*" ]
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


# Gradle files
.gradle/*
.gradle
build/*
out/*
*/build/*
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ package com.wechat.pay.java.service;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.core.cipher.AeadAesCipher;
import com.wechat.pay.java.service.certificate.CertificateService;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
Expand All @@ -69,8 +68,7 @@ public class QuickStart {
public static void downloadCertificate() {
CertificateService certificateService = buildCertificateService();
List<X509Certificate> certificates =
certificateService.downloadCertificate(
new AeadAesCipher(apiV3Key.getBytes(StandardCharsets.UTF_8)));
certificateService.downloadCertificate(apiV3Key.getBytes(StandardCharsets.UTF_8));
}
/**
* 构建证书下载服务
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/com/wechat/pay/java/core/RSAConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public RSAConfig build() {
requireNonNull(merchantSerialNumber);
requireNonNull(merchantId);
requireNonNull(wechatPayCertificates);
if (wechatPayCertificates.isEmpty()) {
throw new IllegalArgumentException(
"Build RSAConfig, wechatPayCertificates is empty.Please "
+ "call wechatPayCertificates() or wechatPayCertificatesFromPath() method.");
}
X509Certificate latestCertificate = null;
// 获取最近可用的微信支付平台证书
for (X509Certificate x509Certificate : wechatPayCertificates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public String getCertificateSerialNumber() {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class ServiceException extends WechatPayException {
public ServiceException(HttpRequest httpRequest, int httpStatusCode, String responseBody) {
super(
String.format(
"Wrong HttpStatusCode[%d]\nhttResponseBody[%.1024s]\tHttpRequest[%s]",
"Wrong HttpStatusCode[%d]%nhttResponseBody[%.1024s]\tHttpRequest[%s]",
httpStatusCode, responseBody, httpRequest));
this.httpRequest = httpRequest;
this.httpStatusCode = httpStatusCode;
this.responseBody = responseBody;
if (responseBody != null) {
JsonObject jsonObject = GsonUtil.createGson().fromJson(responseBody, JsonObject.class);
JsonObject jsonObject = GsonUtil.getGson().fromJson(responseBody, JsonObject.class);
JsonElement code = jsonObject.get("code");
JsonElement message = jsonObject.get("message");
this.errorCode = code == null ? null : code.getAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String getContentType() {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public RequestBody getBody() {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public T getServiceResponse() {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}

public static class Builder<T> {
Expand Down Expand Up @@ -89,7 +89,7 @@ public HttpResponse<T> build() {
}
ResponseBody body = new JsonResponseBody.Builder().body(originalResponse.getBody()).build();
T serviceResponse =
GsonUtil.createGson().fromJson(originalResponse.getBody(), serviceResponseType);
GsonUtil.getGson().fromJson(originalResponse.getBody(), serviceResponseType);
return new HttpResponse<>(
originalResponse.getRequest(), originalResponse.getHeaders(), body, serviceResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public String getContentType() {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public String getContentType() {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}

public static class Builder {
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/java/com/wechat/pay/java/core/http/UrlEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wechat.pay.java.core.http;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class UrlEncoder {

/**
* 对参数进行url编码
*
* @param string 待编码的字符串
* @return 编码后的字符串
*/
public static String urlEncode(String string) {
try {
return URLEncoder.encode(string, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
public final class OkHttpClientAdapter extends AbstractHttpClient {

private static final Logger logger = LoggerFactory.getLogger(OkHttpClientAdapter.class);

private static final String META_NAME = "meta";
private static final String FILE_NAME = "file";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ public void setPlaintext(String plaintext) {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/** 通知解析器 */
public class NotificationParser {

private final Gson gson = GsonUtil.createGson();
private final Gson gson = GsonUtil.getGson();
private final Map<String, Verifier> verifiers = new HashMap<>();
private final Map<String, AeadCipher> ciphers = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String getSignType() {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void setOriginalType(String originalType) {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}

public String getNonce() {
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/com/wechat/pay/java/core/util/GsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class GsonUtil {

private GsonUtil() {}

private static final GsonBuilder gsonBuilder;
private static final Gson gson;

static {
gsonBuilder =
gson =
new GsonBuilder()
.disableHtmlEscaping()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
Expand Down Expand Up @@ -44,10 +44,11 @@ public boolean shouldSkipField(FieldAttributes fieldAttributes) {
public boolean shouldSkipClass(Class<?> aClass) {
return false;
}
});
})
.create();
}

public static Gson createGson() {
return gsonBuilder.create();
public static Gson getGson() {
return gson;
}
}
4 changes: 3 additions & 1 deletion core/src/main/java/com/wechat/pay/java/core/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public static String toString(InputStream inputStream) throws IOException {
* @throws IOException 读取字节失败、关闭流失败等
*/
public static String loadStringFromPath(String path) throws IOException {
return toString(Files.newInputStream(Paths.get(path)));
try (InputStream inputStream = Files.newInputStream(Paths.get(path))) {
return toString(inputStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
Expand Down Expand Up @@ -112,7 +113,7 @@ public static X509Certificate loadX509FromPath(String certificatePath) {
public static X509Certificate loadX509FromString(String certificateString) {
X509Certificate certificate = null;
try (ByteArrayInputStream inputStream =
new ByteArrayInputStream(certificateString.getBytes())) {
new ByteArrayInputStream(certificateString.getBytes(StandardCharsets.UTF_8))) {
certificate = loadX509FromStream(inputStream);
} catch (IOException e) {
logger.warn("Get certificate from certificate string,inputStream close failed.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public <T> boolean validate(HttpHeaders responseHeaders, String body) {
}

@Test
public void testExecuteSendPostRequestWithJsonBody() {
public void testExecuteSendPostReqWithJsonBody() {
Credential executeSendPostCredential =
new Credential() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public void setOutTradeNo(String outTradeNo) {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public void setRequestId(String requestId) {

@Override
public String toString() {
return GsonUtil.createGson().toJson(this);
return GsonUtil.getGson().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class NotificationParserTest {
public static void init() throws IOException {

notificationJson = IOUtil.loadStringFromPath(NOTIFICATION_JSON_PATH);
notification = GsonUtil.createGson().fromJson(notificationJson, Notification.class);
notification = GsonUtil.getGson().fromJson(notificationJson, Notification.class);
verifyAlgorithm = notification.getResource().getAlgorithm();
verifiyNonce = notification.getResource().getNonce();

Expand Down Expand Up @@ -99,6 +99,6 @@ public void testParse() {
.build();

Object decryptObject = parser.parse(requestParam, Object.class);
Assert.assertEquals(DECRYPT_OBJECT_STRING, GsonUtil.createGson().toJson(decryptObject));
Assert.assertEquals(DECRYPT_OBJECT_STRING, GsonUtil.getGson().toJson(decryptObject));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testCreateGson() {
+ outTradeNo
+ "\"\n"
+ "}";
Gson gson = GsonUtil.createGson();
Gson gson = GsonUtil.getGson();
TestServiceRequest getTestServiceRequest =
gson.fromJson(testServiceRequestJson, TestServiceRequest.class);
TestServiceRequest createTestServiceRequest = new TestServiceRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.core.cipher.AeadAesCipher;
import com.wechat.pay.java.service.certificate.CertificateService;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
Expand All @@ -29,8 +28,7 @@ public static void main(String[] args) {
public static void downloadCertificate() {
CertificateService certificateService = buildCertificateService();
List<X509Certificate> certificates =
certificateService.downloadCertificate(
new AeadAesCipher(apiV3Key.getBytes(StandardCharsets.UTF_8)));
certificateService.downloadCertificate(apiV3Key.getBytes(StandardCharsets.UTF_8));
}
/**
* 构建证书下载服务
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.wechat.pay.java.service.certificate;

import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.core.cipher.AeadAesCipher;
import com.wechat.pay.java.core.cipher.AeadCipher;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.List;
Expand Down Expand Up @@ -31,13 +29,11 @@ public static void main(String[] args) {
// 初始化证书服务
service = new CertificateService(rsaConfig);
// 设置商户apiV3密钥,apiV3密钥用于解密下载证书
AeadCipher aeadCipher = new AeadAesCipher(apiV3Key.getBytes(StandardCharsets.UTF_8));
// ... 调用接口
}

/** 下载证书 */
public static List<X509Certificate> downloadCertificate() {
AeadCipher aeadCipher = new AeadAesCipher(apiV3Key.getBytes(StandardCharsets.UTF_8));
return service.downloadCertificate(aeadCipher);
return service.downloadCertificate(apiV3Key.getBytes(StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,23 @@ public static void main(String[] args) {
service = new AppService(config);
// ... 调用接口
}

/** 关闭订单 */
public static void closeOrder() {

CloseOrderRequest request = new CloseOrderRequest();
service.closeOrder(request);
}

/** APP支付下单 */
public static PrepayResponse prepay() {
PrepayRequest request = new PrepayRequest();
return service.prepay(request);
}

/** 微信支付订单号查询订单 */
public static Transaction queryOrderById() {

QueryOrderByIdRequest request = new QueryOrderByIdRequest();
return service.queryOrderById(request);
}

/** 商户订单号查询订单 */
public static Transaction queryOrderByOutTradeNo() {

Expand Down
Loading

0 comments on commit b016518

Please sign in to comment.