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 7a14ea152..d22a37496 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 @@ -227,6 +227,11 @@ public class WxPayConfig { */ private Verifier verifier; + /** + * 是否将全部v3接口的请求都添加Wechatpay-Serial请求头,默认不添加 + */ + private boolean strictlyNeedWechatPaySerial = false; + /** * 返回所设置的微信支付接口请求地址域名. * diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java index 05d1f8c22..530609a7d 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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 @@ -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 @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } 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 d8ba95971..f57ff13d4 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 @@ -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);