🎨 #3548【微信支付】修复公钥模式下V3接口未设置Wechatpay-Serial请求头导致的验签失败

This commit is contained in:
HerveyHall 2025-04-15 15:14:12 +08:00 committed by GitHub
parent 89280abd00
commit e16e0e9373
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 17 deletions

View File

@ -227,6 +227,11 @@ public class WxPayConfig {
*/ */
private Verifier verifier; private Verifier verifier;
/**
* 是否将全部v3接口的请求都添加Wechatpay-Serial请求头默认不添加
*/
private boolean strictlyNeedWechatPaySerial = false;
/** /**
* 返回所设置的微信支付接口请求地址域名. * 返回所设置的微信支付接口请求地址域名.
* *

View File

@ -251,7 +251,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
@Override @Override
public WxPayRefundV3Result refundV3(WxPayRefundV3Request request) throws WxPayException { public WxPayRefundV3Result refundV3(WxPayRefundV3Request request) throws WxPayException {
String url = String.format("%s/v3/refund/domestic/refunds", this.getPayBaseUrl()); String url = String.format("%s/v3/refund/domestic/refunds", this.getPayBaseUrl());
String response = this.postV3(url, GSON.toJson(request)); String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(response, WxPayRefundV3Result.class); return GSON.fromJson(response, WxPayRefundV3Result.class);
} }
@ -294,21 +294,21 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
@Override @Override
public WxPayRefundQueryV3Result refundQueryV3(String outRefundNo) throws WxPayException { public WxPayRefundQueryV3Result refundQueryV3(String outRefundNo) throws WxPayException {
String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), outRefundNo); String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), outRefundNo);
String response = this.getV3(url); String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayRefundQueryV3Result.class); return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
} }
@Override @Override
public WxPayRefundQueryV3Result refundQueryV3(WxPayRefundQueryV3Request request) throws WxPayException { public WxPayRefundQueryV3Result refundQueryV3(WxPayRefundQueryV3Request request) throws WxPayException {
String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), request.getOutRefundNo()); String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), request.getOutRefundNo());
String response = this.getV3(url); String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayRefundQueryV3Result.class); return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
} }
@Override @Override
public WxPayRefundQueryV3Result refundPartnerQueryV3(WxPayRefundQueryV3Request request) throws WxPayException { public WxPayRefundQueryV3Result refundPartnerQueryV3(WxPayRefundQueryV3Request request) throws WxPayException {
String url = String.format("%s/v3/refund/domestic/refunds/%s?sub_mchid=%s", this.getPayBaseUrl(), request.getOutRefundNo(), request.getSubMchid()); String url = String.format("%s/v3/refund/domestic/refunds/%s?sub_mchid=%s", this.getPayBaseUrl(), request.getOutRefundNo(), request.getSubMchid());
String response = this.getV3(url); String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayRefundQueryV3Result.class); return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
} }
@ -523,7 +523,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
url = String.format("%s/v3/pay/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId()); url = String.format("%s/v3/pay/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId());
} }
String query = String.format("?mchid=%s", request.getMchid()); String query = String.format("?mchid=%s", request.getMchid());
String response = this.getV3(url + query); String response = this.getV3WithWechatPaySerial(url + query);
return GSON.fromJson(response, WxPayOrderQueryV3Result.class); return GSON.fromJson(response, WxPayOrderQueryV3Result.class);
} }
@ -548,14 +548,14 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
url = String.format("%s/v3/pay/partner/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId()); url = String.format("%s/v3/pay/partner/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId());
} }
String query = String.format("?sp_mchid=%s&sub_mchid=%s", request.getSpMchId(), request.getSubMchId()); String query = String.format("?sp_mchid=%s&sub_mchid=%s", request.getSpMchId(), request.getSubMchId());
String response = this.getV3(url + query); String response = this.getV3WithWechatPaySerial(url + query);
return GSON.fromJson(response, WxPayPartnerOrderQueryV3Result.class); return GSON.fromJson(response, WxPayPartnerOrderQueryV3Result.class);
} }
@Override @Override
public CombineQueryResult queryCombine(String combineOutTradeNo) throws WxPayException { public CombineQueryResult queryCombine(String combineOutTradeNo) throws WxPayException {
String url = String.format("%s/v3/combine-transactions/out-trade-no/%s", this.getPayBaseUrl(), combineOutTradeNo); String url = String.format("%s/v3/combine-transactions/out-trade-no/%s", this.getPayBaseUrl(), combineOutTradeNo);
String response = this.getV3(url); String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, CombineQueryResult.class); return GSON.fromJson(response, CombineQueryResult.class);
} }
@ -609,7 +609,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
request.setMchid(this.getConfig().getMchId()); request.setMchid(this.getConfig().getMchId());
} }
String url = String.format("%s/v3/pay/transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getOutTradeNo()); String url = String.format("%s/v3/pay/transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getOutTradeNo());
this.postV3(url, GSON.toJson(request)); this.postV3WithWechatpaySerial(url, GSON.toJson(request));
} }
@Override @Override
@ -621,13 +621,13 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
request.setSubMchId(this.getConfig().getSubMchId()); request.setSubMchId(this.getConfig().getSubMchId());
} }
String url = String.format("%s/v3/pay/partner/transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getOutTradeNo()); String url = String.format("%s/v3/pay/partner/transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getOutTradeNo());
this.postV3(url, GSON.toJson(request)); this.postV3WithWechatpaySerial(url, GSON.toJson(request));
} }
@Override @Override
public void closeCombine(CombineCloseRequest request) throws WxPayException { public void closeCombine(CombineCloseRequest request) throws WxPayException {
String url = String.format("%s/v3/combine-transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getCombineOutTradeNo()); String url = String.format("%s/v3/combine-transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getCombineOutTradeNo());
this.postV3(url, GSON.toJson(request)); this.postV3WithWechatpaySerial(url, GSON.toJson(request));
} }
@Override @Override
@ -771,7 +771,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
} }
String url = this.getPayBaseUrl() + tradeType.getBasePartnerUrl(); String url = this.getPayBaseUrl() + tradeType.getBasePartnerUrl();
String response = this.postV3(url, GSON.toJson(request)); String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class); return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
} }
@ -788,7 +788,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
} }
String url = this.getPayBaseUrl() + tradeType.getPartnerUrl(); String url = this.getPayBaseUrl() + tradeType.getPartnerUrl();
String response = this.postV3(url, GSON.toJson(request)); String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class); return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
} }
@ -801,7 +801,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
request.setCombineMchid(this.getConfig().getMchId()); request.setCombineMchid(this.getConfig().getMchId());
} }
String url = this.getPayBaseUrl() + tradeType.getCombineUrl(); String url = this.getPayBaseUrl() + tradeType.getCombineUrl();
String response = this.postV3(url, GSON.toJson(request)); String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(response, CombineTransactionsResult.class); return GSON.fromJson(response, CombineTransactionsResult.class);
} }
@ -1114,7 +1114,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
} else { } else {
url = String.format("%s/v3/bill/tradebill?bill_date=%s&bill_type=%s&tar_type=%s", this.getPayBaseUrl(), request.getBillDate(), request.getBillType(), request.getTarType()); url = String.format("%s/v3/bill/tradebill?bill_date=%s&bill_type=%s&tar_type=%s", this.getPayBaseUrl(), request.getBillDate(), request.getBillType(), request.getTarType());
} }
String response = this.getV3(url); String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayApplyBillV3Result.class); return GSON.fromJson(response, WxPayApplyBillV3Result.class);
} }
@ -1126,7 +1126,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
} else { } else {
url = String.format("%s/v3/bill/fundflowbill?bill_date=%s&account_type=%s&tar_type=%s", this.getPayBaseUrl(), request.getBillDate(), request.getAccountType(), request.getTarType()); url = String.format("%s/v3/bill/fundflowbill?bill_date=%s&account_type=%s&tar_type=%s", this.getPayBaseUrl(), request.getBillDate(), request.getAccountType(), request.getTarType());
} }
String response = this.getV3(url); String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayApplyBillV3Result.class); return GSON.fromJson(response, WxPayApplyBillV3Result.class);
} }
@ -1155,7 +1155,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
request.setMchid(this.getConfig().getMchId()); request.setMchid(this.getConfig().getMchId());
} }
String url = String.format("%s/v3/pay/transactions/codepay", this.getPayBaseUrl()); String url = String.format("%s/v3/pay/transactions/codepay", this.getPayBaseUrl());
String body = this.postV3(url, GSON.toJson(request)); String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(body, WxPayCodepayResult.class); return GSON.fromJson(body, WxPayCodepayResult.class);
} }
@ -1181,7 +1181,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
} }
// 拼接参数请求路径并发送 // 拼接参数请求路径并发送
String url = String.format("%s/v3/pay/transactions/out-trade-no/%s/reverse", this.getPayBaseUrl(), request.getOutTradeNo()); String url = String.format("%s/v3/pay/transactions/out-trade-no/%s/reverse", this.getPayBaseUrl(), request.getOutTradeNo());
String response = this.postV3(url, GSON.toJson(request)); String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
return GSON.fromJson(response, WxPayOrderReverseV3Result.class); return GSON.fromJson(response, WxPayOrderReverseV3Result.class);
} }

View File

@ -243,6 +243,9 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
@Override @Override
public String getV3(String url) throws WxPayException { public String getV3(String url) throws WxPayException {
if (this.getConfig().isStrictlyNeedWechatPaySerial()) {
return getV3WithWechatPaySerial(url);
}
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);