Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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;
Expand Down Expand Up @@ -295,10 +294,6 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
}
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,10 +34,9 @@ public void testCustomizerHttpClient() {

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

/**
* <pre>
* Created by BinaryWang on 2017/6/18.
Expand Down Expand Up @@ -54,19 +45,4 @@ public void testInitSSLContext_base64() throws Exception {
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
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.
*/
Expand All @@ -39,7 +38,22 @@ public void configure(Binder binder) {
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);

Expand Down