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

This commit is contained in:
wincham 2023-06-06 21:32:50 +08:00 committed by GitHub
parent cf0667ef29
commit 7f452406e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 11 deletions

View File

@ -194,6 +194,16 @@ public class EcommerceServiceImpl implements EcommerceService {
return GSON.fromJson(response, FundBalanceResult.class);
}
@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);

View File

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

View File

@ -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;

View File

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