🎨 #3553【微信支付】v3请求统一加上Wechatpay-Serial请求头

This commit is contained in:
SynchPj 2025-04-22 11:08:50 +08:00 committed by GitHub
parent ddfbee25e1
commit cbb3b24577
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 4 deletions

View File

@ -300,13 +300,13 @@ public class WxPayConfig {
this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase(); this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase();
} }
try { try {
if (merchantPrivateKey == null) { if (merchantPrivateKey == null && StringUtils.isNotBlank(this.getPrivateKeyPath())) {
try (InputStream keyInputStream = this.loadConfigInputStream(this.getPrivateKeyString(), this.getPrivateKeyPath(), try (InputStream keyInputStream = this.loadConfigInputStream(this.getPrivateKeyString(), this.getPrivateKeyPath(),
this.privateKeyContent, "privateKeyPath")) { this.privateKeyContent, "privateKeyPath")) {
merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); 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(), try (InputStream certInputStream = this.loadConfigInputStream(this.getPrivateCertString(), this.getPrivateCertPath(),
this.privateCertContent, "privateCertPath")) { this.privateCertContent, "privateCertPath")) {
certificate = PemUtils.loadCertificate(certInputStream); certificate = PemUtils.loadCertificate(certInputStream);

View File

@ -44,6 +44,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
private static final String ACCEPT = "Accept"; private static final String ACCEPT = "Accept";
private static final String CONTENT_TYPE = "Content-Type"; private static final String CONTENT_TYPE = "Content-Type";
private static final String APPLICATION_JSON = "application/json"; private static final String APPLICATION_JSON = "application/json";
private static final String WECHATPAY_SERIAL = "Wechatpay-Serial";
@Override @Override
public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException { 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(ACCEPT, APPLICATION_JSON);
httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON); httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON);
String serialNumber = getWechatpaySerial(getConfig()); String serialNumber = getWechatpaySerial(getConfig());
httpPost.addHeader("Wechatpay-Serial", serialNumber); httpPost.addHeader(WECHATPAY_SERIAL, serialNumber);
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
//v3已经改为通过状态码判断200 204 成功 //v3已经改为通过状态码判断200 204 成功
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
@ -133,6 +134,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
public String patchV3(String url, String requestStr) throws WxPayException { public String patchV3(String url, String requestStr) throws WxPayException {
CloseableHttpClient httpClient = this.createApiV3HttpClient(); CloseableHttpClient httpClient = this.createApiV3HttpClient();
HttpPatch httpPatch = new HttpPatch(url); HttpPatch httpPatch = new HttpPatch(url);
String serialNumber = getWechatpaySerial(getConfig());
httpPatch.addHeader(WECHATPAY_SERIAL, serialNumber);
httpPatch.setEntity(this.createEntry(requestStr)); httpPatch.setEntity(this.createEntry(requestStr));
httpPatch.setConfig(RequestConfig.custom() httpPatch.setConfig(RequestConfig.custom()
@ -204,6 +207,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
@Override @Override
public String postV3(String url, HttpPost httpPost) throws WxPayException { public String postV3(String url, HttpPost httpPost) throws WxPayException {
String serialNumber = getWechatpaySerial(getConfig());
httpPost.addHeader(WECHATPAY_SERIAL, serialNumber);
return this.requestV3(url, httpPost); return this.requestV3(url, httpPost);
} }
@ -249,6 +254,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
httpGet.addHeader(ACCEPT, APPLICATION_JSON); httpGet.addHeader(ACCEPT, APPLICATION_JSON);
httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON); httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON);
String serialNumber = getWechatpaySerial(getConfig());
httpGet.addHeader(WECHATPAY_SERIAL, serialNumber);
return this.requestV3(url, httpGet); return this.requestV3(url, httpGet);
} }
@ -258,7 +265,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
httpGet.addHeader(ACCEPT, APPLICATION_JSON); httpGet.addHeader(ACCEPT, APPLICATION_JSON);
httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON); httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON);
String serialNumber = getWechatpaySerial(getConfig()); String serialNumber = getWechatpaySerial(getConfig());
httpGet.addHeader("Wechatpay-Serial", serialNumber); httpGet.addHeader(WECHATPAY_SERIAL, serialNumber);
return this.requestV3(url, httpGet); return this.requestV3(url, httpGet);
} }
@ -267,6 +274,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
CloseableHttpClient httpClient = this.createApiV3HttpClient(); CloseableHttpClient httpClient = this.createApiV3HttpClient();
HttpGet httpGet = new WxPayV3DownloadHttpGet(url); HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
httpGet.addHeader(ACCEPT, ContentType.WILDCARD.getMimeType()); httpGet.addHeader(ACCEPT, ContentType.WILDCARD.getMimeType());
String serialNumber = getWechatpaySerial(getConfig());
httpGet.addHeader(WECHATPAY_SERIAL, serialNumber);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) { try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
//v3已经改为通过状态码判断200 204 成功 //v3已经改为通过状态码判断200 204 成功
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
@ -298,6 +307,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
httpPut.setEntity(entity); httpPut.setEntity(entity);
httpPut.addHeader(ACCEPT, APPLICATION_JSON); httpPut.addHeader(ACCEPT, APPLICATION_JSON);
httpPut.addHeader(CONTENT_TYPE, APPLICATION_JSON); httpPut.addHeader(CONTENT_TYPE, APPLICATION_JSON);
String serialNumber = getWechatpaySerial(getConfig());
httpPut.addHeader(WECHATPAY_SERIAL, serialNumber);
return requestV3(url, httpPut); return requestV3(url, httpPut);
} }
@ -306,6 +317,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
HttpDelete httpDelete = new HttpDelete(url); HttpDelete httpDelete = new HttpDelete(url);
httpDelete.addHeader(ACCEPT, APPLICATION_JSON); httpDelete.addHeader(ACCEPT, APPLICATION_JSON);
httpDelete.addHeader(CONTENT_TYPE, APPLICATION_JSON); httpDelete.addHeader(CONTENT_TYPE, APPLICATION_JSON);
String serialNumber = getWechatpaySerial(getConfig());
httpDelete.addHeader(WECHATPAY_SERIAL, serialNumber);
return requestV3(url, httpDelete); return requestV3(url, httpDelete);
} }