Skip to content

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

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

Merged
merged 3 commits into from
Apr 15, 2025
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 @@ -227,6 +227,11 @@ public class WxPayConfig {
*/
private Verifier verifier;

/**
* 是否将全部v3接口的请求都添加Wechatpay-Serial请求头,默认不添加
*/
private boolean strictlyNeedWechatPaySerial = false;

/**
* 返回所设置的微信支付接口请求地址域名.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public WxPayRefundResult refundV2(WxPayRefundRequest request) throws WxPayExcept
@Override
public WxPayRefundV3Result refundV3(WxPayRefundV3Request request) throws WxPayException {
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);
}

Expand Down Expand Up @@ -294,21 +294,21 @@ public WxPayRefundQueryResult refundQueryV2(WxPayRefundQueryRequest request) thr
@Override
public WxPayRefundQueryV3Result refundQueryV3(String outRefundNo) throws WxPayException {
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);
}

@Override
public WxPayRefundQueryV3Result refundQueryV3(WxPayRefundQueryV3Request request) throws WxPayException {
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);
}

@Override
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 response = this.getV3(url);
String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
}

Expand Down Expand Up @@ -523,7 +523,7 @@ public WxPayOrderQueryV3Result queryOrderV3(WxPayOrderQueryV3Request request) th
url = String.format("%s/v3/pay/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId());
}
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);
}

Expand All @@ -548,14 +548,14 @@ public WxPayPartnerOrderQueryV3Result queryPartnerOrderV3(WxPayPartnerOrderQuery
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 response = this.getV3(url + query);
String response = this.getV3WithWechatPaySerial(url + query);
return GSON.fromJson(response, WxPayPartnerOrderQueryV3Result.class);
}

@Override
public CombineQueryResult queryCombine(String combineOutTradeNo) throws WxPayException {
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);
}

Expand Down Expand Up @@ -609,7 +609,7 @@ public void closeOrderV3(WxPayOrderCloseV3Request request) throws WxPayException
request.setMchid(this.getConfig().getMchId());
}
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
Expand All @@ -621,13 +621,13 @@ public void closePartnerOrderV3(WxPayPartnerOrderCloseV3Request request) throws
request.setSubMchId(this.getConfig().getSubMchId());
}
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
public void closeCombine(CombineCloseRequest request) throws WxPayException {
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
Expand Down Expand Up @@ -771,7 +771,7 @@ public WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(TradeTypeEnum tradeType,
}

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);
}

Expand All @@ -788,7 +788,7 @@ public WxPayUnifiedOrderV3Result unifiedOrderV3(TradeTypeEnum tradeType, WxPayUn
}

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);
}

Expand All @@ -801,7 +801,7 @@ public CombineTransactionsResult combine(TradeTypeEnum tradeType, CombineTransac
request.setCombineMchid(this.getConfig().getMchId());
}
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);
}

Expand Down Expand Up @@ -1114,7 +1114,7 @@ public WxPayApplyBillV3Result applyTradeBill(WxPayApplyTradeBillV3Request reques
} 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());
}
String response = this.getV3(url);
String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayApplyBillV3Result.class);
}

Expand All @@ -1126,7 +1126,7 @@ public WxPayApplyBillV3Result applyFundFlowBill(WxPayApplyFundFlowBillV3Request
} 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());
}
String response = this.getV3(url);
String response = this.getV3WithWechatPaySerial(url);
return GSON.fromJson(response, WxPayApplyBillV3Result.class);
}

Expand Down Expand Up @@ -1155,7 +1155,7 @@ public WxPayCodepayResult codepay(WxPayCodepayRequest request) throws WxPayExcep
request.setMchid(this.getConfig().getMchId());
}
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);
}

Expand All @@ -1181,7 +1181,7 @@ public WxPayOrderReverseV3Result reverseOrderV3(WxPayOrderReverseV3Request reque
}
// 拼接参数请求路径并发送
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayExc

@Override
public String getV3(String url) throws WxPayException {
if (this.getConfig().isStrictlyNeedWechatPaySerial()) {
return getV3WithWechatPaySerial(url);
}
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader(ACCEPT, APPLICATION_JSON);
httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON);
Expand Down