Skip to content

Commit 8891b68

Browse files
zhanyan-Ader1ybinarywang
authored andcommitted
🐛 #3443 【微信支付】初始化V3请求时取消对私钥的加密
1 parent 47471a6 commit 8891b68

File tree

4 files changed

+23
-35
lines changed

4 files changed

+23
-35
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.security.PublicKey;
1515
import java.security.cert.Certificate;
1616
import java.security.cert.X509Certificate;
17-
import java.util.Base64;
1817
import java.util.Optional;
1918
import javax.net.ssl.SSLContext;
2019
import lombok.Data;
@@ -295,10 +294,6 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
295294
}
296295
try {
297296
if (merchantPrivateKey == null) {
298-
if (StringUtils.isNotBlank(this.getPrivateKeyString())) {
299-
this.setPrivateKeyString(Base64.getEncoder().encodeToString(this.getPrivateKeyString().getBytes()));
300-
}
301-
302297
try (InputStream keyInputStream = this.loadConfigInputStream(this.getPrivateKeyString(), this.getPrivateKeyPath(),
303298
this.privateKeyContent, "privateKeyPath")) {
304299
merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/CustomizedWxPayConfigTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.github.binarywang.wxpay.config;
22

3+
import static org.testng.Assert.assertEquals;
4+
5+
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result;
6+
import com.github.binarywang.wxpay.constant.WxPayErrorCode;
37
import com.github.binarywang.wxpay.exception.WxPayException;
48
import com.github.binarywang.wxpay.service.WxPayService;
59
import com.github.binarywang.wxpay.testbase.CustomizedApiTestModule;
@@ -30,10 +34,9 @@ public void testCustomizerHttpClient() {
3034

3135
public void testCustomizerV3HttpClient() {
3236
try {
33-
wxPayService.queryOrderV3("a", null);
37+
WxPayOrderQueryV3Result result = wxPayService.queryOrderV3("a", null);
3438
} catch (WxPayException e) {
35-
// ignore
36-
e.printStackTrace();
39+
assertEquals(e.getErrCode(), WxPayErrorCode.RefundQuery.PARAM_ERROR);
3740
}
3841
}
3942
}

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
package com.github.binarywang.wxpay.config;
22

3-
import com.github.binarywang.wxpay.exception.WxPayException;
4-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
5-
import org.bouncycastle.pqc.jcajce.provider.util.KeyUtil;
63
import org.testng.annotations.Test;
74

8-
import java.security.KeyPair;
9-
import java.security.KeyPairGenerator;
10-
import java.security.SecureRandom;
11-
import java.security.Security;
12-
import java.util.Base64;
13-
145
/**
156
* <pre>
167
* Created by BinaryWang on 2017/6/18.
@@ -54,19 +45,4 @@ public void testInitSSLContext_base64() throws Exception {
5445
payConfig.initSSLContext();
5546
}
5647

57-
58-
@Test
59-
public void testInitApiV3HttpClient() throws Exception {
60-
Security.addProvider(new BouncyCastleProvider());
61-
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA","BC");
62-
keyPairGenerator.initialize(2048,new SecureRandom());
63-
KeyPair keyPair = keyPairGenerator.genKeyPair();
64-
byte[] encoded = keyPair.getPrivate().getEncoded();
65-
// 模拟用户配置
66-
String privateKeyString = Base64.getEncoder().encodeToString(encoded);
67-
payConfig.setPrivateKeyString(privateKeyString);
68-
payConfig.setApiV3Key("Test");
69-
payConfig.initApiV3HttpClient();
70-
}
71-
7248
}

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/CustomizedApiTestModule.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
import com.google.inject.Binder;
77
import com.google.inject.Module;
88
import com.thoughtworks.xstream.XStream;
9+
import java.io.*;
10+
import java.nio.charset.StandardCharsets;
911
import me.chanjar.weixin.common.error.WxRuntimeException;
1012
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
1113
import org.apache.http.HttpRequestInterceptor;
1214
import org.slf4j.Logger;
1315
import org.slf4j.LoggerFactory;
1416

15-
import java.io.IOException;
16-
import java.io.InputStream;
17-
1817
/**
1918
* The type Api test module.
2019
*/
@@ -39,7 +38,22 @@ public void configure(Binder binder) {
3938
config.setHttpClientBuilderCustomizer((builder) -> {
4039
builder.addInterceptorLast((HttpRequestInterceptor) (r, c) -> System.out.println("--------> HttpRequestInterceptor ..."));
4140
});
41+
try (FileInputStream fis = new FileInputStream(config.getPrivateKeyPath());
42+
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
43+
BufferedReader reader = new BufferedReader(isr)) {
44+
45+
StringBuilder stringBuilder = new StringBuilder();
46+
String line;
47+
while ((line = reader.readLine()) != null) {
48+
stringBuilder.append(line);
49+
stringBuilder.append(System.lineSeparator());
50+
}
4251

52+
String fileContent = stringBuilder.toString();
53+
config.setPrivateKeyString(fileContent);
54+
System.out.println(fileContent);
55+
56+
}
4357
WxPayService wxService = new WxPayServiceImpl();
4458
wxService.setConfig(config);
4559

0 commit comments

Comments
 (0)