From cbb3b24577b5d2754f96d563dfa8901fca66bc23 Mon Sep 17 00:00:00 2001 From: SynchPj <46849861+SynchPj@users.noreply.github.com> Date: Tue, 22 Apr 2025 11:08:50 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#3553=E3=80=90=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E3=80=91v3=E8=AF=B7=E6=B1=82=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E5=8A=A0=E4=B8=8AWechatpay-Serial=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binarywang/wxpay/config/WxPayConfig.java | 4 ++-- .../impl/WxPayServiceApacheHttpImpl.java | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 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 2cfec2bc1..75db10a07 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 @@ -300,13 +300,13 @@ public class WxPayConfig { this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase(); } try { - if (merchantPrivateKey == null) { + if (merchantPrivateKey == null && StringUtils.isNotBlank(this.getPrivateKeyPath())) { try (InputStream keyInputStream = this.loadConfigInputStream(this.getPrivateKeyString(), this.getPrivateKeyPath(), this.privateKeyContent, "privateKeyPath")) { merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); } } - if (certificate == null && StringUtils.isBlank(this.getCertSerialNo())) { + if (certificate == null && StringUtils.isBlank(this.getCertSerialNo()) && StringUtils.isNotBlank(this.getPrivateCertPath())) { try (InputStream certInputStream = this.loadConfigInputStream(this.getPrivateCertString(), this.getPrivateCertPath(), this.privateCertContent, "privateCertPath")) { certificate = PemUtils.loadCertificate(certInputStream); diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java index f57ff13d4..f273fe1f9 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java @@ -44,6 +44,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { private static final String ACCEPT = "Accept"; private static final String CONTENT_TYPE = "Content-Type"; private static final String APPLICATION_JSON = "application/json"; + private static final String WECHATPAY_SERIAL = "Wechatpay-Serial"; @Override public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException { @@ -101,7 +102,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { httpPost.addHeader(ACCEPT, APPLICATION_JSON); httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON); String serialNumber = getWechatpaySerial(getConfig()); - httpPost.addHeader("Wechatpay-Serial", serialNumber); + httpPost.addHeader(WECHATPAY_SERIAL, serialNumber); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { //v3已经改为通过状态码判断200 204 成功 int statusCode = response.getStatusLine().getStatusCode(); @@ -133,6 +134,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { public String patchV3(String url, String requestStr) throws WxPayException { CloseableHttpClient httpClient = this.createApiV3HttpClient(); HttpPatch httpPatch = new HttpPatch(url); + String serialNumber = getWechatpaySerial(getConfig()); + httpPatch.addHeader(WECHATPAY_SERIAL, serialNumber); httpPatch.setEntity(this.createEntry(requestStr)); httpPatch.setConfig(RequestConfig.custom() @@ -204,6 +207,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { @Override public String postV3(String url, HttpPost httpPost) throws WxPayException { + String serialNumber = getWechatpaySerial(getConfig()); + httpPost.addHeader(WECHATPAY_SERIAL, serialNumber); return this.requestV3(url, httpPost); } @@ -249,6 +254,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { HttpGet httpGet = new HttpGet(url); httpGet.addHeader(ACCEPT, APPLICATION_JSON); httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON); + String serialNumber = getWechatpaySerial(getConfig()); + httpGet.addHeader(WECHATPAY_SERIAL, serialNumber); return this.requestV3(url, httpGet); } @@ -258,7 +265,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { httpGet.addHeader(ACCEPT, APPLICATION_JSON); httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON); String serialNumber = getWechatpaySerial(getConfig()); - httpGet.addHeader("Wechatpay-Serial", serialNumber); + httpGet.addHeader(WECHATPAY_SERIAL, serialNumber); return this.requestV3(url, httpGet); } @@ -267,6 +274,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { CloseableHttpClient httpClient = this.createApiV3HttpClient(); HttpGet httpGet = new WxPayV3DownloadHttpGet(url); httpGet.addHeader(ACCEPT, ContentType.WILDCARD.getMimeType()); + String serialNumber = getWechatpaySerial(getConfig()); + httpGet.addHeader(WECHATPAY_SERIAL, serialNumber); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { //v3已经改为通过状态码判断200 204 成功 int statusCode = response.getStatusLine().getStatusCode(); @@ -298,6 +307,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { httpPut.setEntity(entity); httpPut.addHeader(ACCEPT, APPLICATION_JSON); httpPut.addHeader(CONTENT_TYPE, APPLICATION_JSON); + String serialNumber = getWechatpaySerial(getConfig()); + httpPut.addHeader(WECHATPAY_SERIAL, serialNumber); return requestV3(url, httpPut); } @@ -306,6 +317,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { HttpDelete httpDelete = new HttpDelete(url); httpDelete.addHeader(ACCEPT, APPLICATION_JSON); httpDelete.addHeader(CONTENT_TYPE, APPLICATION_JSON); + String serialNumber = getWechatpaySerial(getConfig()); + httpDelete.addHeader(WECHATPAY_SERIAL, serialNumber); return requestV3(url, httpDelete); }