Merge branch 'develop' into develop

This commit is contained in:
crskyp 2017-04-27 19:22:57 +08:00 committed by GitHub
commit 44cbf65f81
28 changed files with 111 additions and 24 deletions

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.6.0</version>
<version>2.7.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>WeiXin Java Tools - Parent</name>
<description>微信公众号、企业号上级POM</description>

View File

@ -52,7 +52,7 @@
---------------------------------
## Maven & Gradle 最新正式版本
* 微信支付(暂时为测试版本)
* 微信支付:
maven
```xml

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.6.0</version>
<version>2.7.0-SNAPSHOT</version>
</parent>
<artifactId>weixin-java-common</artifactId>
@ -39,6 +39,11 @@
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-http</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
<build>

View File

@ -10,6 +10,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import okhttp3.*;
@ -111,7 +112,7 @@ public class MediaDownloadRequestExecutor extends AbstractRequestExecutor<File,
}
}
String fileName = getFileName(response);
String fileName = getFileNameApache(response);
if (StringUtils.isBlank(fileName)) {
return null;
}
@ -128,7 +129,6 @@ public class MediaDownloadRequestExecutor extends AbstractRequestExecutor<File,
/**
* jodd-http实现方式
*
* @param provider
* @param proxyInfo
* @param uri
@ -237,7 +237,23 @@ public class MediaDownloadRequestExecutor extends AbstractRequestExecutor<File,
if (m.matches()) {
return m.group(1);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
request.withConnectionProvider(provider);
HttpResponse response = request.send();
String contentType = response.header("Content-Type");
if (contentType != null && contentType.startsWith("application/json")) {
// application/json; encoding=utf-8 下载媒体文件出错
throw new WxErrorException(WxError.fromJson(response.bodyText()));
}
String fileName = getFileNameJodd(response);
if (StringUtils.isBlank(fileName)) {
return null;
}
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
String[] nameAndExt = fileName.split("\\.");
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
}
}

View File

@ -8,6 +8,7 @@ import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import okhttp3.*;

View File

@ -3,6 +3,7 @@ package me.chanjar.weixin.common.util.http;
import jodd.http.HttpConnectionProvider;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import okhttp3.ConnectionPool;
@ -63,5 +64,4 @@ public interface RequestExecutor<T, E> {
*/
T executeOkhttp(ConnectionPool pool, final OkhttpProxyInfo proxyInfo, String uri, E data) throws WxErrorException, IOException;
}

View File

@ -7,6 +7,7 @@ import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import okhttp3.*;
@ -28,6 +29,7 @@ import java.util.concurrent.TimeUnit;
*/
public class SimpleGetRequestExecutor extends AbstractRequestExecutor<String, String> {
/**
* apache-http实现方式
*
@ -98,6 +100,7 @@ public class SimpleGetRequestExecutor extends AbstractRequestExecutor<String, St
return responseContent;
}
/**
* okHttp实现方式
*
@ -146,5 +149,4 @@ public class SimpleGetRequestExecutor extends AbstractRequestExecutor<String, St
return responseContent;
}
}

View File

@ -7,6 +7,7 @@ import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import okhttp3.*;
@ -40,6 +41,7 @@ public class SimplePostRequestExecutor extends AbstractRequestExecutor<String, S
* @throws IOException
*/
public String executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.6.0</version>
<version>2.7.0-SNAPSHOT</version>
</parent>
<artifactId>weixin-java-cp</artifactId>

View File

@ -1,6 +1,5 @@
package me.chanjar.weixin.cp.api.impl.apache;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
@ -8,6 +7,7 @@ import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.cp.api.impl.AbstractWxCpService;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -73,6 +73,7 @@ public class WxCpServiceImpl extends AbstractWxCpService<CloseableHttpClient, Ht
}
@Override
public void initHttp() {
ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage
.getApacheHttpClientBuilder();
@ -91,4 +92,5 @@ public class WxCpServiceImpl extends AbstractWxCpService<CloseableHttpClient, Ht
this.httpClient = apacheHttpClientBuilder.build();
}
}

View File

@ -19,6 +19,7 @@ public class WxCpServiceImpl extends AbstractWxCpService<HttpConnectionProvider,
@Override
public ProxyInfo getRequestHttpProxy() {
return httpProxy;
}
@Override
@ -56,10 +57,12 @@ public class WxCpServiceImpl extends AbstractWxCpService<HttpConnectionProvider,
@Override
public void initHttp() {
if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
httpProxy = new ProxyInfo(ProxyInfo.ProxyType.HTTP, configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort(), configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword());
}
httpClient = JoddHttp.httpConnectionProvider;
}
}

View File

@ -61,6 +61,10 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
messageJson.add("voice", voice);
}
if (StringUtils.isNotBlank(message.getSafe())) {
messageJson.addProperty("safe", message.getSafe());
}
if (WxConsts.CUSTOM_MSG_VIDEO.equals(message.getMsgType())) {
JsonObject video = new JsonObject();
video.addProperty("media_id", message.getMediaId());

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.6.0</version>
<version>2.7.0-SNAPSHOT</version>
</parent>
<artifactId>weixin-java-mp</artifactId>
<name>WeiXin Java Tools - MP</name>

View File

@ -350,4 +350,5 @@ public interface WxMpService {
*/
void initHttp();
}

View File

@ -1,5 +1,6 @@
package me.chanjar.weixin.mp.api.impl.apache;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
@ -71,11 +72,13 @@ public class WxMpServiceImpl extends AbstractWxMpService<CloseableHttpClient,Htt
+ this.getWxMpConfigStorage().getSecret();
try {
HttpGet httpGet = new HttpGet(url);
if (this.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
httpGet.setConfig(config);
}
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
String resultContent = new BasicResponseHandler().handleResponse(response);
WxError error = WxError.fromJson(resultContent);
if (error.getErrorCode() != 0) {
@ -96,4 +99,5 @@ public class WxMpServiceImpl extends AbstractWxMpService<CloseableHttpClient,Htt
}
return this.getWxMpConfigStorage().getAccessToken();
}
}

View File

@ -1,6 +1,5 @@
package me.chanjar.weixin.mp.api.impl.jodd;
import jodd.http.*;
import jodd.http.net.SocketHttpConnectionProvider;
import me.chanjar.weixin.common.bean.WxAccessToken;
@ -30,6 +29,7 @@ public class WxMpServiceImpl extends AbstractWxMpService<HttpConnectionProvider,
@Override
public void initHttp() {
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
@ -40,7 +40,6 @@ public class WxMpServiceImpl extends AbstractWxMpService<HttpConnectionProvider,
}
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
@ -57,9 +56,11 @@ public class WxMpServiceImpl extends AbstractWxMpService<HttpConnectionProvider,
+ this.getWxMpConfigStorage().getSecret();
HttpRequest request = HttpRequest.get(url);
if (this.getRequestHttpProxy() != null) {
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
provider.useProxy(getRequestHttpProxy());
request.withConnectionProvider(provider);
}
HttpResponse response = request.send();
@ -77,4 +78,5 @@ public class WxMpServiceImpl extends AbstractWxMpService<HttpConnectionProvider,
}
return this.getWxMpConfigStorage().getAccessToken();
}
}

View File

@ -10,7 +10,9 @@ import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import okhttp3.*;
@ -57,6 +59,7 @@ public class MaterialDeleteRequestExecutor extends AbstractRequestExecutor<Boole
}
}
@Override
public Boolean executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
@ -108,5 +111,4 @@ public class MaterialDeleteRequestExecutor extends AbstractRequestExecutor<Boole
}
}
}

View File

@ -10,7 +10,9 @@ import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
@ -57,6 +59,7 @@ public class MaterialNewsInfoRequestExecutor extends AbstractRequestExecutor<WxM
}
}
@Override
public WxMpMaterialNews executeJodd(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
@ -102,6 +105,7 @@ public class MaterialNewsInfoRequestExecutor extends AbstractRequestExecutor<WxM
Response response = client.newCall(request).execute();
String responseContent = response.body().toString();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);

View File

@ -10,7 +10,9 @@ import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;

View File

@ -10,7 +10,9 @@ import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;

View File

@ -6,6 +6,7 @@ import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
@ -95,6 +96,7 @@ public class MaterialVoiceAndImageDownloadRequestExecutor extends AbstractReques
Response response = client.newCall(request).execute();
try (InputStream inputStream = new ByteArrayInputStream(response.body().bytes())) {
// 下载媒体文件出错
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");

View File

@ -4,7 +4,9 @@ import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.MimeTypes;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
@ -13,7 +15,9 @@ import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import okhttp3.*;
@ -38,7 +42,6 @@ import java.util.UUID;
*/
public class MediaImgUploadRequestExecutor extends AbstractRequestExecutor<WxMediaImgUploadResult, File> {
@Override
public WxMediaImgUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File data) throws WxErrorException, IOException {
if (data == null) {
@ -99,6 +102,7 @@ public class MediaImgUploadRequestExecutor extends AbstractRequestExecutor<WxMed
@Override
public WxMediaImgUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
File data) throws WxErrorException, IOException {
if (data == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
}

View File

@ -8,11 +8,13 @@ import jodd.util.MimeTypes;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.AbstractRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import okhttp3.*;
@ -101,6 +103,7 @@ public class QrCodeRequestExecutor extends AbstractRequestExecutor<File, WxMpQrC
@Override
public File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';

View File

@ -4,6 +4,8 @@ import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

View File

@ -7,6 +7,8 @@ import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.locks.ReentrantLock;

View File

@ -6,6 +6,8 @@ import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>weixin-java-parent</artifactId>
<groupId>com.github.binarywang</groupId>
<version>2.6.0</version>
<version>2.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -188,17 +188,37 @@ public class WxPayServiceImpl implements WxPayService {
}
Map<String, String> payInfo = new HashMap<>();
payInfo.put("appId", getConfig().getAppId());
// 支付签名时间戳注意微信jssdk中的所有使用timestamp字段均为小写
// 但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
if ("NATIVE".equals(request.getTradeType())) {
payInfo.put("codeUrl", unifiedOrderResult.getCodeURL());
} else if ("APP".equals(request.getTradeType())) {
// APP支付绑定的是微信开放平台上的账号APPID为开放平台上绑定APP后发放的参数
String appId = getConfig().getAppId();
Map<String, String> configMap = new HashMap<>();
// 此map用于参与调起sdk支付的二次签名,格式全小写timestamp只能是10位,格式固定切勿修改
String partnerid = getConfig().getMchId();
configMap.put("prepayid", prepayId);
configMap.put("partnerid", partnerid);
configMap.put("package", "Sign=WXPay");
configMap.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
configMap.put("noncestr", String.valueOf(System.currentTimeMillis()));
configMap.put("appid", appId);
// 此map用于客户端与微信服务器交互
payInfo.put("paySign", SignUtils.createSign(payInfo, this.getConfig().getMchKey()));
payInfo.put("prepayId", prepayId);
payInfo.put("partnerId", partnerid);
payInfo.put("appId", appId);
payInfo.put("packageValue", "Sign=WXPay");
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
payInfo.put("nonceStr", String.valueOf(System.currentTimeMillis()));
} else if ("JSAPI".equals(request.getTradeType())) {
payInfo.put("appId", unifiedOrderResult.getAppid());
// 支付签名时间戳注意微信jssdk中的所有使用timestamp字段均为小写但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
payInfo.put("nonceStr", String.valueOf(System.currentTimeMillis()));
payInfo.put("package", "prepay_id=" + prepayId);
payInfo.put("signType", "MD5");
if ("NATIVE".equals(request.getTradeType())) {
payInfo.put("codeUrl", unifiedOrderResult.getCodeURL());
}
payInfo.put("paySign", SignUtils.createSign(payInfo, this.getConfig().getMchKey()));
}
return payInfo;
}