From 8891b68e7986d6ec53aa52fb60985dd9272cd66a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ader1y=20=E6=A0=88=E7=83=9F?= Date: Wed, 18 Dec 2024 11:37:37 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20#3443=20=E3=80=90=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E3=80=91=E5=88=9D=E5=A7=8B=E5=8C=96V3?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=97=B6=E5=8F=96=E6=B6=88=E5=AF=B9=E7=A7=81?= =?UTF-8?q?=E9=92=A5=E7=9A=84=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binarywang/wxpay/config/WxPayConfig.java | 5 ---- .../config/CustomizedWxPayConfigTest.java | 9 ++++--- .../wxpay/config/WxPayConfigTest.java | 24 ------------------- .../testbase/CustomizedApiTestModule.java | 20 +++++++++++++--- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java index a8ad909b3..293c52eac 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java @@ -14,7 +14,6 @@ import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.Certificate; import java.security.cert.X509Certificate; -import java.util.Base64; import java.util.Optional; import javax.net.ssl.SSLContext; import lombok.Data; @@ -295,10 +294,6 @@ public class WxPayConfig { } try { if (merchantPrivateKey == null) { - if (StringUtils.isNotBlank(this.getPrivateKeyString())) { - this.setPrivateKeyString(Base64.getEncoder().encodeToString(this.getPrivateKeyString().getBytes())); - } - try (InputStream keyInputStream = this.loadConfigInputStream(this.getPrivateKeyString(), this.getPrivateKeyPath(), this.privateKeyContent, "privateKeyPath")) { merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/CustomizedWxPayConfigTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/CustomizedWxPayConfigTest.java index a42026e3e..3b2bdfeaa 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/CustomizedWxPayConfigTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/CustomizedWxPayConfigTest.java @@ -1,5 +1,9 @@ package com.github.binarywang.wxpay.config; +import static org.testng.Assert.assertEquals; + +import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result; +import com.github.binarywang.wxpay.constant.WxPayErrorCode; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.testbase.CustomizedApiTestModule; @@ -30,10 +34,9 @@ public class CustomizedWxPayConfigTest { public void testCustomizerV3HttpClient() { try { - wxPayService.queryOrderV3("a", null); + WxPayOrderQueryV3Result result = wxPayService.queryOrderV3("a", null); } catch (WxPayException e) { - // ignore - e.printStackTrace(); + assertEquals(e.getErrCode(), WxPayErrorCode.RefundQuery.PARAM_ERROR); } } } diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java index 72750e01c..46bc23aac 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java @@ -1,16 +1,7 @@ package com.github.binarywang.wxpay.config; -import com.github.binarywang.wxpay.exception.WxPayException; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.bouncycastle.pqc.jcajce.provider.util.KeyUtil; import org.testng.annotations.Test; -import java.security.KeyPair; -import java.security.KeyPairGenerator; -import java.security.SecureRandom; -import java.security.Security; -import java.util.Base64; - /** *
  *  Created by BinaryWang on 2017/6/18.
@@ -54,19 +45,4 @@ public class WxPayConfigTest {
     payConfig.initSSLContext();
   }
 
-
-  @Test
-  public void testInitApiV3HttpClient() throws Exception {
-    Security.addProvider(new BouncyCastleProvider());
-    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA","BC");
-    keyPairGenerator.initialize(2048,new SecureRandom());
-    KeyPair keyPair = keyPairGenerator.genKeyPair();
-    byte[] encoded = keyPair.getPrivate().getEncoded();
-    // 模拟用户配置
-    String privateKeyString = Base64.getEncoder().encodeToString(encoded);
-    payConfig.setPrivateKeyString(privateKeyString);
-    payConfig.setApiV3Key("Test");
-    payConfig.initApiV3HttpClient();
-  }
-
 }
diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/CustomizedApiTestModule.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/CustomizedApiTestModule.java
index a0cc399ea..484227e34 100644
--- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/CustomizedApiTestModule.java
+++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/CustomizedApiTestModule.java
@@ -6,15 +6,14 @@ import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
 import com.google.inject.Binder;
 import com.google.inject.Module;
 import com.thoughtworks.xstream.XStream;
+import java.io.*;
+import java.nio.charset.StandardCharsets;
 import me.chanjar.weixin.common.error.WxRuntimeException;
 import me.chanjar.weixin.common.util.xml.XStreamInitializer;
 import org.apache.http.HttpRequestInterceptor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.IOException;
-import java.io.InputStream;
-
 /**
  * The type Api test module.
  */
@@ -39,7 +38,22 @@ public class CustomizedApiTestModule implements Module {
       config.setHttpClientBuilderCustomizer((builder) -> {
         builder.addInterceptorLast((HttpRequestInterceptor) (r, c) -> System.out.println("--------> HttpRequestInterceptor ..."));
       });
+      try (FileInputStream fis = new FileInputStream(config.getPrivateKeyPath());
+           InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
+           BufferedReader reader = new BufferedReader(isr)) {
 
+        StringBuilder stringBuilder = new StringBuilder();
+        String line;
+        while ((line = reader.readLine()) != null) {
+          stringBuilder.append(line);
+          stringBuilder.append(System.lineSeparator());
+        }
+
+        String fileContent = stringBuilder.toString();
+        config.setPrivateKeyString(fileContent);
+        System.out.println(fileContent);
+
+      }
       WxPayService wxService = new WxPayServiceImpl();
       wxService.setConfig(config);