Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 【微信支付】服务商模式-兼容公钥模式下请求头序列号 #3498

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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 @@ -320,16 +320,7 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
//构造Http Proxy正向代理
WxPayHttpProxy wxPayHttpProxy = getWxPayHttpProxy();

Verifier certificatesVerifier;
if (publicKey == null) {
certificatesVerifier =
new AutoUpdateCertificatesVerifier(
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
this.getApiV3Key().getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime(),
this.getPayBaseUrl(), wxPayHttpProxy);
} else {
certificatesVerifier = new PublicCertificateVerifier(publicKey, publicKeyId);
}
Verifier certificatesVerifier = getVerifier(merchantPrivateKey, wxPayHttpProxy, publicKey);

WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
Expand All @@ -355,6 +346,19 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
}
}

private Verifier getVerifier(PrivateKey merchantPrivateKey, WxPayHttpProxy wxPayHttpProxy, PublicKey publicKey) {
Verifier certificatesVerifier = new AutoUpdateCertificatesVerifier(
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
this.getApiV3Key().getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime(),
this.getPayBaseUrl(), wxPayHttpProxy);
if (publicKey != null) {
Verifier publicCertificatesVerifier = new PublicCertificateVerifier(publicKey, publicKeyId);
publicCertificatesVerifier.setOtherVerifier(certificatesVerifier);
certificatesVerifier = publicCertificatesVerifier;
}
return certificatesVerifier;
}

/**
* 初始化一个WxPayHttpProxy对象
*
Expand Down Expand Up @@ -382,7 +386,7 @@ private InputStream loadConfigInputStream(String configString, String configPath
if (configContent != null) {
return new ByteArrayInputStream(configContent);
}

if (StringUtils.isNotEmpty(configString)) {
configContent = Base64.getDecoder().decode(configString);
return new ByteArrayInputStream(configContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public String postV3(String url, String requestStr) throws WxPayException {
HttpPost httpPost = this.createHttpPost(url, requestStr);
httpPost.addHeader(ACCEPT, APPLICATION_JSON);
httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON);
String serialNumber = getWechatpaySerial(getConfig());
httpPost.addHeader("Wechatpay-Serial", serialNumber);
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
//v3已经改为通过状态码判断200 204 成功
int statusCode = response.getStatusLine().getStatusCode();
Expand Down Expand Up @@ -387,10 +389,9 @@ private WxPayException convertException(JsonObject jsonObject) {
* @return
*/
private String getWechatpaySerial(WxPayConfig wxPayConfig) {
String serialNumber = wxPayConfig.getVerifier().getValidCertificate().getSerialNumber().toString(16).toUpperCase();
if (StringUtils.isNotBlank(wxPayConfig.getPublicKeyId())) {
serialNumber = wxPayConfig.getPublicKeyId();
return wxPayConfig.getPublicKeyId();
}
return serialNumber;
return wxPayConfig.getVerifier().getValidCertificate().getSerialNumber().toString(16).toUpperCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ public class PublicCertificateVerifier implements Verifier{

private final PublicKey publicKey;

private Verifier certificateVerifier;

private final X509PublicCertificate publicCertificate;

public PublicCertificateVerifier(PublicKey publicKey, String publicId) {
this.publicKey = publicKey;
this.publicCertificate = new X509PublicCertificate(publicKey, publicId);
}

public void setOtherVerifier(Verifier verifier) {
this.certificateVerifier = verifier;
}

@Override
public boolean verify(String serialNumber, byte[] message, String signature) {
if (!serialNumber.contains("PUB_KEY_ID")) {
return this.certificateVerifier.verify(serialNumber, message, signature);
}
try {
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initVerify(publicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface Verifier {


X509Certificate getValidCertificate();

default void setOtherVerifier(Verifier verifier) {};
}