浏览代码

:art: 微信支付v3接口电子回单下载问题修复

wincham 1 年之前
父节点
当前提交
7f452406e8

+ 10 - 0
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java

@@ -195,6 +195,16 @@ public class EcommerceServiceImpl implements EcommerceService {
   }
 
   @Override
+  public FundBalanceResult subNowBalance(String subMchid, SpAccountTypeEnum accountType) throws WxPayException {
+    String url = String.format("%s/v3/ecommerce/fund/balance/%s", this.payService.getPayBaseUrl(), subMchid);
+    if (Objects.nonNull(accountType)) {
+      url += "?account_type=" + accountType.getValue();
+    }
+    String response = this.payService.getV3(url);
+    return GSON.fromJson(response, FundBalanceResult.class);
+  }
+
+  @Override
   public FundBalanceResult subDayEndBalance(String subMchid, String date) throws WxPayException {
     String url = String.format("%s/v3/ecommerce/fund/enddaybalance/%s?date=%s", this.payService.getPayBaseUrl(), subMchid, date);
     String response = this.payService.getV3(url);

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

@@ -2,13 +2,12 @@ package com.github.binarywang.wxpay.service.impl;
 
 import com.github.binarywang.wxpay.bean.WxPayApiData;
 import com.github.binarywang.wxpay.exception.WxPayException;
+import com.github.binarywang.wxpay.v3.WxPayV3DownloadHttpGet;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import me.chanjar.weixin.common.util.json.GsonParser;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpStatus;
+import org.apache.http.*;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.CredentialsProvider;
@@ -28,6 +27,7 @@ import javax.net.ssl.SSLContext;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Base64;
+import java.util.Objects;
 
 /**
  * <pre>
@@ -257,15 +257,20 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
   @Override
   public InputStream downloadV3(String url) throws WxPayException {
     CloseableHttpClient httpClient = this.createApiV3HttpClient();
-    HttpGet httpGet = new HttpGet(url);
+    HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
     httpGet.addHeader("Accept", ContentType.WILDCARD.getMimeType());
     try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
       //v3已经改为通过状态码判断200 204 成功
       int statusCode = response.getStatusLine().getStatusCode();
-      if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
+      Header contentType = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
+      boolean isJsonContentType = Objects.nonNull(contentType) &&
+        ContentType.APPLICATION_JSON.getMimeType().equals(ContentType.parse(String.valueOf(contentType.getValue())).getMimeType());
+      if ((HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode)
+          && !isJsonContentType) {
         this.log.info("\n【请求地址】:{}\n", url);
         return response.getEntity().getContent();
       } else {
+        //response里的header有content-type=json说明返回了错误信息
         //有错误提示信息返回
         String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
         JsonObject jsonObject = GsonParser.parse(responseString);

+ 6 - 6
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/SignatureExec.java

@@ -1,16 +1,12 @@
 package com.github.binarywang.wxpay.v3;
 
-import java.io.IOException;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
 import org.apache.http.StatusLine;
 import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 import org.apache.http.client.methods.HttpExecutionAware;
 import org.apache.http.client.methods.HttpRequestWrapper;
-import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.client.methods.RequestBuilder;
 import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.conn.routing.HttpRoute;
 import org.apache.http.entity.BufferedHttpEntity;
@@ -18,6 +14,8 @@ import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.impl.execchain.ClientExecChain;
 import org.apache.http.util.EntityUtils;
 
+import java.io.IOException;
+
 public class SignatureExec implements ClientExecChain {
   final ClientExecChain mainExec;
   final Credentials credentials;
@@ -81,8 +79,10 @@ public class SignatureExec implements ClientExecChain {
     StatusLine statusLine = response.getStatusLine();
     if (statusLine.getStatusCode() >= 200 && statusLine.getStatusCode() < 300) {
       convertToRepeatableResponseEntity(response);
-      if (!validator.validate(response)) {
-        throw new HttpException("应答的微信支付签名验证失败");
+      if (!(request.getOriginal() instanceof WxPayV3DownloadHttpGet)) {
+        if (!validator.validate(response)) {
+          throw new HttpException("应答的微信支付签名验证失败");
+        }
       }
     }
     return response;

+ 21 - 0
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3DownloadHttpGet.java

@@ -0,0 +1,21 @@
+package com.github.binarywang.wxpay.v3;
+
+
+import org.apache.http.client.methods.HttpGet;
+
+import java.net.URI;
+
+public class WxPayV3DownloadHttpGet extends HttpGet {
+
+
+  public WxPayV3DownloadHttpGet() {
+  }
+
+  public WxPayV3DownloadHttpGet(URI uri) {
+    super(uri);
+  }
+
+  public WxPayV3DownloadHttpGet(String uri) {
+    super(uri);
+  }
+}