Skip to content

Commit e16e0e9

Browse files
authored
🎨 #3548【微信支付】修复公钥模式下V3接口未设置Wechatpay-Serial请求头导致的验签失败
1 parent 89280ab commit e16e0e9

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ public class WxPayConfig {
227227
*/
228228
private Verifier verifier;
229229

230+
/**
231+
* 是否将全部v3接口的请求都添加Wechatpay-Serial请求头,默认不添加
232+
*/
233+
private boolean strictlyNeedWechatPaySerial = false;
234+
230235
/**
231236
* 返回所设置的微信支付接口请求地址域名.
232237
*

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public WxPayRefundResult refundV2(WxPayRefundRequest request) throws WxPayExcept
251251
@Override
252252
public WxPayRefundV3Result refundV3(WxPayRefundV3Request request) throws WxPayException {
253253
String url = String.format("%s/v3/refund/domestic/refunds", this.getPayBaseUrl());
254-
String response = this.postV3(url, GSON.toJson(request));
254+
String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
255255
return GSON.fromJson(response, WxPayRefundV3Result.class);
256256
}
257257

@@ -294,21 +294,21 @@ public WxPayRefundQueryResult refundQueryV2(WxPayRefundQueryRequest request) thr
294294
@Override
295295
public WxPayRefundQueryV3Result refundQueryV3(String outRefundNo) throws WxPayException {
296296
String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), outRefundNo);
297-
String response = this.getV3(url);
297+
String response = this.getV3WithWechatPaySerial(url);
298298
return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
299299
}
300300

301301
@Override
302302
public WxPayRefundQueryV3Result refundQueryV3(WxPayRefundQueryV3Request request) throws WxPayException {
303303
String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), request.getOutRefundNo());
304-
String response = this.getV3(url);
304+
String response = this.getV3WithWechatPaySerial(url);
305305
return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
306306
}
307307

308308
@Override
309309
public WxPayRefundQueryV3Result refundPartnerQueryV3(WxPayRefundQueryV3Request request) throws WxPayException {
310310
String url = String.format("%s/v3/refund/domestic/refunds/%s?sub_mchid=%s", this.getPayBaseUrl(), request.getOutRefundNo(), request.getSubMchid());
311-
String response = this.getV3(url);
311+
String response = this.getV3WithWechatPaySerial(url);
312312
return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
313313
}
314314

@@ -523,7 +523,7 @@ public WxPayOrderQueryV3Result queryOrderV3(WxPayOrderQueryV3Request request) th
523523
url = String.format("%s/v3/pay/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId());
524524
}
525525
String query = String.format("?mchid=%s", request.getMchid());
526-
String response = this.getV3(url + query);
526+
String response = this.getV3WithWechatPaySerial(url + query);
527527
return GSON.fromJson(response, WxPayOrderQueryV3Result.class);
528528
}
529529

@@ -548,14 +548,14 @@ public WxPayPartnerOrderQueryV3Result queryPartnerOrderV3(WxPayPartnerOrderQuery
548548
url = String.format("%s/v3/pay/partner/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId());
549549
}
550550
String query = String.format("?sp_mchid=%s&sub_mchid=%s", request.getSpMchId(), request.getSubMchId());
551-
String response = this.getV3(url + query);
551+
String response = this.getV3WithWechatPaySerial(url + query);
552552
return GSON.fromJson(response, WxPayPartnerOrderQueryV3Result.class);
553553
}
554554

555555
@Override
556556
public CombineQueryResult queryCombine(String combineOutTradeNo) throws WxPayException {
557557
String url = String.format("%s/v3/combine-transactions/out-trade-no/%s", this.getPayBaseUrl(), combineOutTradeNo);
558-
String response = this.getV3(url);
558+
String response = this.getV3WithWechatPaySerial(url);
559559
return GSON.fromJson(response, CombineQueryResult.class);
560560
}
561561

@@ -609,7 +609,7 @@ public void closeOrderV3(WxPayOrderCloseV3Request request) throws WxPayException
609609
request.setMchid(this.getConfig().getMchId());
610610
}
611611
String url = String.format("%s/v3/pay/transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getOutTradeNo());
612-
this.postV3(url, GSON.toJson(request));
612+
this.postV3WithWechatpaySerial(url, GSON.toJson(request));
613613
}
614614

615615
@Override
@@ -621,13 +621,13 @@ public void closePartnerOrderV3(WxPayPartnerOrderCloseV3Request request) throws
621621
request.setSubMchId(this.getConfig().getSubMchId());
622622
}
623623
String url = String.format("%s/v3/pay/partner/transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getOutTradeNo());
624-
this.postV3(url, GSON.toJson(request));
624+
this.postV3WithWechatpaySerial(url, GSON.toJson(request));
625625
}
626626

627627
@Override
628628
public void closeCombine(CombineCloseRequest request) throws WxPayException {
629629
String url = String.format("%s/v3/combine-transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getCombineOutTradeNo());
630-
this.postV3(url, GSON.toJson(request));
630+
this.postV3WithWechatpaySerial(url, GSON.toJson(request));
631631
}
632632

633633
@Override
@@ -771,7 +771,7 @@ public WxPayUnifiedOrderV3Result unifiedPartnerOrderV3(TradeTypeEnum tradeType,
771771
}
772772

773773
String url = this.getPayBaseUrl() + tradeType.getBasePartnerUrl();
774-
String response = this.postV3(url, GSON.toJson(request));
774+
String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
775775
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
776776
}
777777

@@ -788,7 +788,7 @@ public WxPayUnifiedOrderV3Result unifiedOrderV3(TradeTypeEnum tradeType, WxPayUn
788788
}
789789

790790
String url = this.getPayBaseUrl() + tradeType.getPartnerUrl();
791-
String response = this.postV3(url, GSON.toJson(request));
791+
String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
792792
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
793793
}
794794

@@ -801,7 +801,7 @@ public CombineTransactionsResult combine(TradeTypeEnum tradeType, CombineTransac
801801
request.setCombineMchid(this.getConfig().getMchId());
802802
}
803803
String url = this.getPayBaseUrl() + tradeType.getCombineUrl();
804-
String response = this.postV3(url, GSON.toJson(request));
804+
String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
805805
return GSON.fromJson(response, CombineTransactionsResult.class);
806806
}
807807

@@ -1114,7 +1114,7 @@ public WxPayApplyBillV3Result applyTradeBill(WxPayApplyTradeBillV3Request reques
11141114
} else {
11151115
url = String.format("%s/v3/bill/tradebill?bill_date=%s&bill_type=%s&tar_type=%s", this.getPayBaseUrl(), request.getBillDate(), request.getBillType(), request.getTarType());
11161116
}
1117-
String response = this.getV3(url);
1117+
String response = this.getV3WithWechatPaySerial(url);
11181118
return GSON.fromJson(response, WxPayApplyBillV3Result.class);
11191119
}
11201120

@@ -1126,7 +1126,7 @@ public WxPayApplyBillV3Result applyFundFlowBill(WxPayApplyFundFlowBillV3Request
11261126
} else {
11271127
url = String.format("%s/v3/bill/fundflowbill?bill_date=%s&account_type=%s&tar_type=%s", this.getPayBaseUrl(), request.getBillDate(), request.getAccountType(), request.getTarType());
11281128
}
1129-
String response = this.getV3(url);
1129+
String response = this.getV3WithWechatPaySerial(url);
11301130
return GSON.fromJson(response, WxPayApplyBillV3Result.class);
11311131
}
11321132

@@ -1155,7 +1155,7 @@ public WxPayCodepayResult codepay(WxPayCodepayRequest request) throws WxPayExcep
11551155
request.setMchid(this.getConfig().getMchId());
11561156
}
11571157
String url = String.format("%s/v3/pay/transactions/codepay", this.getPayBaseUrl());
1158-
String body = this.postV3(url, GSON.toJson(request));
1158+
String body = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
11591159
return GSON.fromJson(body, WxPayCodepayResult.class);
11601160
}
11611161

@@ -1181,7 +1181,7 @@ public WxPayOrderReverseV3Result reverseOrderV3(WxPayOrderReverseV3Request reque
11811181
}
11821182
// 拼接参数请求路径并发送
11831183
String url = String.format("%s/v3/pay/transactions/out-trade-no/%s/reverse", this.getPayBaseUrl(), request.getOutTradeNo());
1184-
String response = this.postV3(url, GSON.toJson(request));
1184+
String response = this.postV3WithWechatpaySerial(url, GSON.toJson(request));
11851185
return GSON.fromJson(response, WxPayOrderReverseV3Result.class);
11861186
}
11871187

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayExc
243243

244244
@Override
245245
public String getV3(String url) throws WxPayException {
246+
if (this.getConfig().isStrictlyNeedWechatPaySerial()) {
247+
return getV3WithWechatPaySerial(url);
248+
}
246249
HttpGet httpGet = new HttpGet(url);
247250
httpGet.addHeader(ACCEPT, APPLICATION_JSON);
248251
httpGet.addHeader(CONTENT_TYPE, APPLICATION_JSON);

0 commit comments

Comments
 (0)