@@ -1,15 +1,30 @@ | |||
# Weixin Java Tools <a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=078f7a153d243853e24cf2b542e7a6ccbf2a592bc138080f84d11297f736ec46"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="weixin-java-tools" title="weixin-java-tools"></a> | |||
## 微信公众号/企业号开发Java SDK | |||
## [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badge/) ![Maven Central](https://img.shields.io/maven-central/v/com.github.binarywang/weixin-java-parent.svg) [![Build Status](https://travis-ci.org/binarywang/weixin-java-tools.svg?branch=develop)](https://travis-ci.org/binarywang/weixin-java-tools) [![Code Climate](https://codeclimate.com/github/binarywang/weixin-java-tools.png)](https://codeclimate.com/github/binarywang/weixin-java-tools) | |||
# Weixin Java Tools 微信公众号/企业号开发Java SDK | |||
## <a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=078f7a153d243853e24cf2b542e7a6ccbf2a592bc138080f84d11297f736ec46"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="weixin-java-tools" title="weixin-java-tools"></a> [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badge/) ![Maven Central](https://img.shields.io/maven-central/v/com.github.binarywang/weixin-java-parent.svg) [![Build Status](https://travis-ci.org/binarywang/weixin-java-tools.svg?branch=develop)](https://travis-ci.org/binarywang/weixin-java-tools) | |||
### 声明:本项目基于chanjarster/weixin-java-tools,由于原作者长期没有维护,故单独维护和发布,且发布到maven上的groupId也会不同。 | |||
#### 最新更新:1.3.5版发布!!! on 2016-06-30 | |||
--- | |||
#### 本项目主要存放在github上,地址为 : | |||
* https://github.com/wechat-group/weixin-java-tools | |||
* ===========但同时会在其他几个网站同步更新,地址分别是: | |||
* https://bitbucket.org/binarywang/weixin-java-tools | |||
* http://git.oschina.net/binary/weixin-java-tools | |||
* https://git.coding.net/binarywang/weixin-java-tools.git | |||
### 详细开发文档请看 [wiki](https://github.com/chanjarster/weixin-java-tools/wiki)。 | |||
## 目前可参考的Demo项目: | |||
* https://github.com/wechat-group/weixin-java-tools-springmvc | |||
* https://github.com/wechat-group/weixin-mp-demo | |||
* =========以下为备份仓库,会保持跟主仓库同步 | |||
* http://git.oschina.net/binary/weixin-mp-demo | |||
* https://bitbucket.org/binarywang/weixin-mp-demo | |||
=========== | |||
## 使用开发交流工具: | |||
## 开发交流工具: | |||
* QQ群:343954419 <a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=078f7a153d243853e24cf2b542e7a6ccbf2a592bc138080f84d11297f736ec46"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="weixin-java-tools" title="weixin-java-tools"></a> | |||
* 注意:为保证入群成员质量,请申请入群时认真输入Github帐号ID,即你的github主页地址https://github.com/XXXX 中最后的部分XXXX的内容,谢谢~ | |||
* 微信群: 因二维码有时间限制,如有想加入微信群的,请入QQ群后咨询获取最新入群二维码。 | |||
@@ -7,6 +7,6 @@ import me.chanjar.weixin.common.exception.WxErrorException; | |||
*/ | |||
public interface WxErrorExceptionHandler { | |||
public void handle(WxErrorException e); | |||
void handle(WxErrorException e); | |||
} |
@@ -22,6 +22,6 @@ public interface WxMessageDuplicateChecker { | |||
* @param messageId messageId需要根据上面讲的方式构造 | |||
* @return 如果是重复消息,返回true,否则返回false | |||
*/ | |||
public boolean isDuplicate(String messageId); | |||
boolean isDuplicate(String messageId); | |||
} |
@@ -87,11 +87,7 @@ public class WxMessageInMemoryDuplicateChecker implements WxMessageDuplicateChec | |||
} | |||
checkBackgroundProcessStarted(); | |||
Long timestamp = msgId2Timestamp.putIfAbsent(messageId, System.currentTimeMillis()); | |||
if (timestamp == null) { | |||
// 第一次接收到这个消息 | |||
return false; | |||
} | |||
return true; | |||
return timestamp != null; | |||
} | |||
@@ -1,5 +1,8 @@ | |||
package me.chanjar.weixin.common.bean; | |||
import org.apache.commons.lang3.builder.ToStringBuilder; | |||
import org.apache.commons.lang3.builder.ToStringStyle; | |||
import java.io.Serializable; | |||
/** | |||
@@ -10,95 +13,100 @@ import java.io.Serializable; | |||
*/ | |||
public class WxCardApiSignature implements Serializable { | |||
private static final long serialVersionUID = 158176707226975979L; | |||
private static final long serialVersionUID = 158176707226975979L; | |||
private String appId; | |||
private String appId; | |||
private String cardId; | |||
private String cardId; | |||
private String cardType; | |||
private String cardType; | |||
private String locationId; | |||
private String locationId; | |||
private String code; | |||
private String code; | |||
private String openId; | |||
private String openId; | |||
private Long timestamp; | |||
private Long timestamp; | |||
private String nonceStr; | |||
private String nonceStr; | |||
private String signature; | |||
private String signature; | |||
public String getAppId() { | |||
return appId; | |||
} | |||
@Override | |||
public String toString() { | |||
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
} | |||
public void setAppId(String appId) { | |||
this.appId = appId; | |||
} | |||
public String getAppId() { | |||
return appId; | |||
} | |||
public void setAppId(String appId) { | |||
this.appId = appId; | |||
} | |||
public String getCardId() { | |||
return cardId; | |||
} | |||
public String getCardId() { | |||
return cardId; | |||
} | |||
public void setCardId(String cardId) { | |||
this.cardId = cardId; | |||
} | |||
public void setCardId(String cardId) { | |||
this.cardId = cardId; | |||
} | |||
public String getCardType() { | |||
return cardType; | |||
} | |||
public String getCardType() { | |||
return cardType; | |||
} | |||
public void setCardType(String cardType) { | |||
this.cardType = cardType; | |||
} | |||
public void setCardType(String cardType) { | |||
this.cardType = cardType; | |||
} | |||
public String getLocationId() { | |||
return locationId; | |||
} | |||
public String getLocationId() { | |||
return locationId; | |||
} | |||
public void setLocationId(String locationId) { | |||
this.locationId = locationId; | |||
} | |||
public String getCode() { | |||
return code; | |||
} | |||
return code; | |||
} | |||
public void setCode(String code) { | |||
this.code = code; | |||
} | |||
public void setCode(String code) { | |||
this.code = code; | |||
} | |||
public String getOpenId() { | |||
return openId; | |||
} | |||
public String getOpenId() { | |||
return openId; | |||
} | |||
public void setOpenId(String openId) { | |||
this.openId = openId; | |||
} | |||
public void setOpenId(String openId) { | |||
this.openId = openId; | |||
} | |||
public Long getTimestamp() { | |||
return timestamp; | |||
} | |||
public Long getTimestamp() { | |||
return timestamp; | |||
} | |||
public void setTimestamp(Long timestamp) { | |||
this.timestamp = timestamp; | |||
} | |||
public void setTimestamp(Long timestamp) { | |||
this.timestamp = timestamp; | |||
} | |||
public String getNonceStr() { | |||
return nonceStr; | |||
} | |||
public String getNonceStr() { | |||
return nonceStr; | |||
} | |||
public void setNonceStr(String nonceStr) { | |||
this.nonceStr = nonceStr; | |||
} | |||
public void setNonceStr(String nonceStr) { | |||
this.nonceStr = nonceStr; | |||
} | |||
public String getSignature() { | |||
return signature; | |||
} | |||
public String getSignature() { | |||
return signature; | |||
} | |||
public void setSignature(String signature) { | |||
this.signature = signature; | |||
} | |||
public void setSignature(String signature) { | |||
this.signature = signature; | |||
} | |||
} |
@@ -13,7 +13,7 @@ public interface InternalSession { | |||
* | |||
* @param isValid The new value for the <code>isValid</code> flag | |||
*/ | |||
public void setValid(boolean isValid); | |||
void setValid(boolean isValid); | |||
/** | |||
* Return the <code>isValid</code> flag for this session. | |||
@@ -28,14 +28,14 @@ public interface InternalSessionManager { | |||
* @exception IllegalStateException if a new session cannot be | |||
* instantiated for any reason | |||
*/ | |||
public InternalSession createSession(String sessionId); | |||
InternalSession createSession(String sessionId); | |||
/** | |||
* Remove this Session from the active Sessions for this Manager. | |||
* | |||
* @param session Session to be removed | |||
*/ | |||
public void remove(InternalSession session); | |||
void remove(InternalSession session); | |||
/** | |||
* Remove this Session from the active Sessions for this Manager. | |||
@@ -43,7 +43,7 @@ public interface InternalSessionManager { | |||
* @param session Session to be removed | |||
* @param update Should the expiration statistics be updated | |||
*/ | |||
public void remove(InternalSession session, boolean update); | |||
void remove(InternalSession session, boolean update); | |||
/** | |||
* Add this Session to the set of active Sessions for this Manager. | |||
@@ -71,7 +71,7 @@ public interface InternalSessionManager { | |||
/** | |||
* Implements the Manager interface, direct call to processExpires | |||
*/ | |||
public void backgroundProcess(); | |||
void backgroundProcess(); | |||
/** | |||
* Set the default maximum inactive interval (in seconds) | |||
@@ -323,9 +323,8 @@ public class StandardSession implements WxSession, InternalSession { | |||
if (!attributes.equals(session.attributes)) return false; | |||
if (!facade.equals(session.facade)) return false; | |||
if (!id.equals(session.id)) return false; | |||
if (!manager.equals(session.manager)) return false; | |||
return manager.equals(session.manager); | |||
return true; | |||
} | |||
@Override | |||
@@ -4,14 +4,14 @@ import java.util.Enumeration; | |||
public interface WxSession { | |||
public Object getAttribute(String name); | |||
Object getAttribute(String name); | |||
public Enumeration<String> getAttributeNames(); | |||
Enumeration<String> getAttributeNames(); | |||
public void setAttribute(String name, Object value); | |||
void setAttribute(String name, Object value); | |||
public void removeAttribute(String name); | |||
void removeAttribute(String name); | |||
public void invalidate(); | |||
void invalidate(); | |||
} |
@@ -5,12 +5,12 @@ public interface WxSessionManager { | |||
/** | |||
* 获取某个sessionId对应的session,如果sessionId没有对应的session,则新建一个并返回。 | |||
*/ | |||
public WxSession getSession(String sessionId); | |||
WxSession getSession(String sessionId); | |||
/** | |||
* 获取某个sessionId对应的session,如果sessionId没有对应的session,若create为true则新建一个,否则返回null。 | |||
*/ | |||
public WxSession getSession(String sessionId, boolean create); | |||
WxSession getSession(String sessionId, boolean create); | |||
} |
@@ -14,7 +14,7 @@ public class InputStreamResponseHandler implements ResponseHandler<InputStream> | |||
public static final ResponseHandler<InputStream> INSTANCE = new InputStreamResponseHandler(); | |||
public InputStream handleResponse(final HttpResponse response) throws HttpResponseException, IOException { | |||
public InputStream handleResponse(final HttpResponse response) throws IOException { | |||
final StatusLine statusLine = response.getStatusLine(); | |||
final HttpEntity entity = response.getEntity(); | |||
if (statusLine.getStatusCode() >= 300) { | |||
@@ -39,7 +39,7 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin | |||
@Override | |||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException { | |||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException { | |||
if (queryParam != null) { | |||
if (uri.indexOf('?') == -1) { | |||
uri += '?'; | |||
@@ -25,7 +25,7 @@ import java.io.IOException; | |||
public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> { | |||
@Override | |||
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, ClientProtocolException, IOException { | |||
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
@@ -26,6 +26,6 @@ public interface RequestExecutor<T, E> { | |||
* @throws ClientProtocolException | |||
* @throws IOException | |||
*/ | |||
public T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, ClientProtocolException, IOException; | |||
T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, IOException; | |||
} |
@@ -19,7 +19,7 @@ import java.io.IOException; | |||
public class SimpleGetRequestExecutor implements RequestExecutor<String, String> { | |||
@Override | |||
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException { | |||
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException { | |||
if (queryParam != null) { | |||
if (uri.indexOf('?') == -1) { | |||
uri += '?'; | |||
@@ -21,7 +21,7 @@ import java.io.IOException; | |||
public class SimplePostRequestExecutor implements RequestExecutor<String, String> { | |||
@Override | |||
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException { | |||
public String execute(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(); | |||
@@ -19,7 +19,7 @@ public class Utf8ResponseHandler implements ResponseHandler<String> { | |||
public static final ResponseHandler<String> INSTANCE = new Utf8ResponseHandler(); | |||
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException { | |||
public String handleResponse(final HttpResponse response) throws IOException { | |||
final StatusLine statusLine = response.getStatusLine(); | |||
final HttpEntity entity = response.getEntity(); | |||
if (statusLine.getStatusCode() >= 300) { | |||
@@ -91,7 +91,7 @@ public class GsonHelper { | |||
public static boolean getAsPrimitiveBool(JsonElement element) { | |||
Boolean r = getAsBoolean(element); | |||
return r == null ? false : r.booleanValue(); | |||
return r != null && r.booleanValue(); | |||
} | |||
public static Double getAsDouble(JsonElement element) { | |||
@@ -211,10 +211,7 @@ public class StringManager { | |||
@Override | |||
protected boolean removeEldestEntry( | |||
Map.Entry<Locale,StringManager> eldest) { | |||
if (size() > (LOCALE_CACHE_SIZE - 1)) { | |||
return true; | |||
} | |||
return false; | |||
return size() > (LOCALE_CACHE_SIZE - 1); | |||
} | |||
}; | |||
managers.put(packageName, map); | |||
@@ -12,61 +12,61 @@ import java.io.File; | |||
*/ | |||
public interface WxCpConfigStorage { | |||
public String getAccessToken(); | |||
String getAccessToken(); | |||
public boolean isAccessTokenExpired(); | |||
boolean isAccessTokenExpired(); | |||
/** | |||
* 强制将access token过期掉 | |||
*/ | |||
public void expireAccessToken(); | |||
void expireAccessToken(); | |||
public void updateAccessToken(WxAccessToken accessToken); | |||
void updateAccessToken(WxAccessToken accessToken); | |||
public void updateAccessToken(String accessToken, int expiresIn); | |||
void updateAccessToken(String accessToken, int expiresIn); | |||
public String getJsapiTicket(); | |||
String getJsapiTicket(); | |||
public boolean isJsapiTicketExpired(); | |||
boolean isJsapiTicketExpired(); | |||
/** | |||
* 强制将jsapi ticket过期掉 | |||
*/ | |||
public void expireJsapiTicket(); | |||
void expireJsapiTicket(); | |||
/** | |||
* 应该是线程安全的 | |||
* @param jsapiTicket | |||
*/ | |||
public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds); | |||
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds); | |||
public String getCorpId(); | |||
String getCorpId(); | |||
public String getCorpSecret(); | |||
String getCorpSecret(); | |||
public String getAgentId(); | |||
String getAgentId(); | |||
public String getToken(); | |||
String getToken(); | |||
public String getAesKey(); | |||
String getAesKey(); | |||
public long getExpiresTime(); | |||
long getExpiresTime(); | |||
public String getOauth2redirectUri(); | |||
String getOauth2redirectUri(); | |||
public String getHttp_proxy_host(); | |||
String getHttp_proxy_host(); | |||
public int getHttp_proxy_port(); | |||
int getHttp_proxy_port(); | |||
public String getHttp_proxy_username(); | |||
String getHttp_proxy_username(); | |||
public String getHttp_proxy_password(); | |||
String getHttp_proxy_password(); | |||
public File getTmpDirFile(); | |||
File getTmpDirFile(); | |||
/** | |||
* http client builder | |||
* @return ApacheHttpClientBuilder | |||
*/ | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
} |
@@ -22,9 +22,9 @@ public interface WxCpMessageHandler { | |||
* @param sessionManager | |||
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null | |||
*/ | |||
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} |
@@ -22,9 +22,9 @@ public interface WxCpMessageInterceptor { | |||
* @param sessionManager | |||
* @return true代表OK,false代表不OK | |||
*/ | |||
public boolean intercept(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
boolean intercept(WxCpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxCpService wxCpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} |
@@ -10,6 +10,6 @@ public interface WxCpMessageMatcher { | |||
/** | |||
* 消息是否匹配某种模式 | |||
*/ | |||
public boolean match(WxCpXmlMessage message); | |||
boolean match(WxCpXmlMessage message); | |||
} |
@@ -210,10 +210,7 @@ public class WxCpMessageRouter { | |||
messageId = String.valueOf(wxMessage.getMsgId()); | |||
} | |||
if (messageDuplicateChecker.isDuplicate(messageId)) { | |||
return true; | |||
} | |||
return false; | |||
return messageDuplicateChecker.isDuplicate(messageId); | |||
} | |||
@@ -70,7 +70,7 @@ public interface WxCpService { | |||
* @see #getJsapiTicket(boolean) | |||
* @throws WxErrorException | |||
*/ | |||
public String getJsapiTicket() throws WxErrorException; | |||
String getJsapiTicket() throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -82,7 +82,7 @@ public interface WxCpService { | |||
* @param forceRefresh 强制刷新 | |||
* @throws WxErrorException | |||
*/ | |||
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException; | |||
String getJsapiTicket(boolean forceRefresh) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -92,7 +92,7 @@ public interface WxCpService { | |||
* </pre> | |||
* @param url url | |||
*/ | |||
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException; | |||
WxJsapiSignature createJsapiSignature(String url) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -49,7 +49,6 @@ public class WxCpMessageRouterTest { | |||
} | |||
}).handler(new WxEchoCpMessageHandler(sb, "matcher")).end() | |||
.rule().async(async).handler(new WxEchoCpMessageHandler(sb, "ALL")).end(); | |||
; | |||
} | |||
@Test(dataProvider="messages-1") | |||
@@ -0,0 +1,115 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import me.chanjar.weixin.common.bean.WxCardApiSignature; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | |||
/** | |||
* 卡券相关接口 | |||
* Created by Binary Wang on 2016/7/27. | |||
* @author binarywang(https://github.com/binarywang) | |||
*/ | |||
public interface WxMpCardService { | |||
/** | |||
* 获得卡券api_ticket,不强制刷新卡券api_ticket | |||
* | |||
* @return 卡券api_ticket | |||
* @throws WxErrorException | |||
* @see #getCardApiTicket(boolean) | |||
*/ | |||
String getCardApiTicket() throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获得卡券api_ticket | |||
* 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94.9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param forceRefresh 强制刷新 | |||
* @return 卡券api_ticket | |||
* @throws WxErrorException | |||
*/ | |||
String getCardApiTicket(boolean forceRefresh) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 创建调用卡券api时所需要的签名 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD | |||
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94 | |||
* .9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param optionalSignParam 参与签名的参数数组。 | |||
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id | |||
* </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空 | |||
* @return 卡券Api签名对象 | |||
*/ | |||
WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws | |||
WxErrorException; | |||
/** | |||
* 卡券Code解码 | |||
* | |||
* @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得 | |||
* @return 解密后的Code | |||
*/ | |||
String decryptCardCode(String encryptCode) throws WxErrorException; | |||
/** | |||
* 卡券Code查询 | |||
* | |||
* @param cardId 卡券ID代表一类卡券 | |||
* @param code 单张卡券的唯一标准 | |||
* @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同 | |||
* @return WxMpCardResult对象 | |||
*/ | |||
WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume) | |||
throws WxErrorException; | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
*/ | |||
String consumeCardCode(String code) throws WxErrorException; | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @param cardId 当自定义Code卡券时需要传入card_id | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
*/ | |||
String consumeCardCode(String code, String cardId) throws WxErrorException; | |||
/** | |||
* 卡券Mark接口。 | |||
* 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住), | |||
* 才能进一步调用核销接口,否则报错。 | |||
* | |||
* @param code 卡券的code码 | |||
* @param cardId 卡券的ID | |||
* @param openId 用券用户的openid | |||
* @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用 | |||
*/ | |||
void markCardCode(String code, String cardId, String openId, boolean isMark) throws | |||
WxErrorException; | |||
/** | |||
* 查看卡券详情接口 | |||
* 详见 https://mp.weixin.qq.com/wiki/14/8dd77aeaee85f922db5f8aa6386d385e.html#.E6.9F.A5.E7.9C.8B.E5.8D.A1.E5.88.B8.E8.AF.A6.E6.83.85 | |||
* | |||
* @param cardId 卡券的ID | |||
* @return 返回的卡券详情JSON字符串 | |||
* <br> [注] 由于返回的JSON格式过于复杂,难以定义其对应格式的Bean并且难以维护,因此只返回String格式的JSON串。 | |||
* <br> 可由 com.google.gson.JsonParser#parse 等方法直接取JSON串中的某个字段。 | |||
*/ | |||
String getCardDetail(String cardId) throws WxErrorException; | |||
} |
@@ -13,89 +13,89 @@ import java.io.File; | |||
*/ | |||
public interface WxMpConfigStorage { | |||
public String getAccessToken(); | |||
String getAccessToken(); | |||
public boolean isAccessTokenExpired(); | |||
boolean isAccessTokenExpired(); | |||
/** | |||
* 强制将access token过期掉 | |||
*/ | |||
public void expireAccessToken(); | |||
void expireAccessToken(); | |||
/** | |||
* 应该是线程安全的 | |||
* @param accessToken | |||
*/ | |||
public void updateAccessToken(WxAccessToken accessToken); | |||
void updateAccessToken(WxAccessToken accessToken); | |||
/** | |||
* 应该是线程安全的 | |||
* @param accessToken | |||
* @param expiresIn | |||
*/ | |||
public void updateAccessToken(String accessToken, int expiresIn); | |||
void updateAccessToken(String accessToken, int expiresIn); | |||
public String getJsapiTicket(); | |||
String getJsapiTicket(); | |||
public boolean isJsapiTicketExpired(); | |||
boolean isJsapiTicketExpired(); | |||
/** | |||
* 强制将jsapi ticket过期掉 | |||
*/ | |||
public void expireJsapiTicket(); | |||
void expireJsapiTicket(); | |||
/** | |||
* 应该是线程安全的 | |||
* @param jsapiTicket | |||
*/ | |||
public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds); | |||
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds); | |||
public String getCardApiTicket(); | |||
String getCardApiTicket(); | |||
public boolean isCardApiTicketExpired(); | |||
boolean isCardApiTicketExpired(); | |||
/** | |||
* 强制将卡券api ticket过期掉 | |||
*/ | |||
public void expireCardApiTicket(); | |||
void expireCardApiTicket(); | |||
/** | |||
* 应该是线程安全的 | |||
* @param cardApiTicket | |||
*/ | |||
public void updateCardApiTicket(String cardApiTicket, int expiresInSeconds); | |||
void updateCardApiTicket(String cardApiTicket, int expiresInSeconds); | |||
public String getAppId(); | |||
String getAppId(); | |||
public String getSecret(); | |||
String getSecret(); | |||
public String getPartnerId(); | |||
String getPartnerId(); | |||
public String getPartnerKey(); | |||
String getPartnerKey(); | |||
public String getToken(); | |||
String getToken(); | |||
public String getAesKey(); | |||
String getAesKey(); | |||
public long getExpiresTime(); | |||
long getExpiresTime(); | |||
public String getOauth2redirectUri(); | |||
String getOauth2redirectUri(); | |||
public String getHttp_proxy_host(); | |||
String getHttp_proxy_host(); | |||
public int getHttp_proxy_port(); | |||
int getHttp_proxy_port(); | |||
public String getHttp_proxy_username(); | |||
String getHttp_proxy_username(); | |||
public String getHttp_proxy_password(); | |||
String getHttp_proxy_password(); | |||
public File getTmpDirFile(); | |||
File getTmpDirFile(); | |||
public SSLContext getSSLContext(); | |||
SSLContext getSSLContext(); | |||
/** | |||
* http client builder | |||
* @return ApacheHttpClientBuilder | |||
*/ | |||
public ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
ApacheHttpClientBuilder getApacheHttpClientBuilder(); | |||
} |
@@ -22,7 +22,7 @@ public interface WxMpGroupService { | |||
* | |||
* @param name 分组名字(30个字符以内) | |||
*/ | |||
public WxMpGroup groupCreate(String name) throws WxErrorException; | |||
WxMpGroup groupCreate(String name) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -30,7 +30,7 @@ public interface WxMpGroupService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口 | |||
* </pre> | |||
*/ | |||
public List<WxMpGroup> groupGet() throws WxErrorException; | |||
List<WxMpGroup> groupGet() throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -40,7 +40,7 @@ public interface WxMpGroupService { | |||
* | |||
* @param openid 微信用户的openid | |||
*/ | |||
public long userGetGroup(String openid) throws WxErrorException; | |||
long userGetGroup(String openid) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -52,7 +52,7 @@ public interface WxMpGroupService { | |||
* | |||
* @param group 要更新的group,group的id,name必须设置 | |||
*/ | |||
public void groupUpdate(WxMpGroup group) throws WxErrorException; | |||
void groupUpdate(WxMpGroup group) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -65,5 +65,5 @@ public interface WxMpGroupService { | |||
* @param openid 用户openid | |||
* @param to_groupid 移动到的分组id | |||
*/ | |||
public void userUpdateGroup(String openid, long to_groupid) throws WxErrorException; | |||
void userUpdateGroup(String openid, long to_groupid) throws WxErrorException; | |||
} |
@@ -25,7 +25,7 @@ public interface WxMpMaterialService { | |||
* @throws WxErrorException | |||
* @see #mediaUpload(String, String, InputStream) | |||
*/ | |||
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException; | |||
WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -39,7 +39,7 @@ public interface WxMpMaterialService { | |||
* @return 保存到本地的临时文件 | |||
* @throws WxErrorException | |||
*/ | |||
public File mediaDownload(String media_id) throws WxErrorException; | |||
File mediaDownload(String media_id) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -51,7 +51,7 @@ public interface WxMpMaterialService { | |||
* @return WxMediaImgUploadResult 返回图片url | |||
* @throws WxErrorException | |||
*/ | |||
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException; | |||
WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -72,7 +72,7 @@ public interface WxMpMaterialService { | |||
* @param inputStream 输入流 | |||
* @throws WxErrorException | |||
*/ | |||
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException; | |||
WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException; | |||
/** | |||
* <pre> | |||
@@ -90,7 +90,7 @@ public interface WxMpMaterialService { | |||
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
* @param material 上传的素材, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterial} | |||
*/ | |||
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException; | |||
WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -101,7 +101,7 @@ public interface WxMpMaterialService { | |||
* | |||
* @param news 上传的图文消息, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialNews} | |||
*/ | |||
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException; | |||
WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -112,7 +112,7 @@ public interface WxMpMaterialService { | |||
* | |||
* @param media_id 永久素材的id | |||
*/ | |||
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException; | |||
InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -123,7 +123,7 @@ public interface WxMpMaterialService { | |||
* | |||
* @param media_id 永久素材的id | |||
*/ | |||
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException; | |||
WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -134,7 +134,7 @@ public interface WxMpMaterialService { | |||
* | |||
* @param media_id 永久素材的id | |||
*/ | |||
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException; | |||
WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -145,7 +145,7 @@ public interface WxMpMaterialService { | |||
* | |||
* @param wxMpMaterialArticleUpdate 用来更新图文素材的bean, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate} | |||
*/ | |||
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException; | |||
boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -156,7 +156,7 @@ public interface WxMpMaterialService { | |||
* | |||
* @param media_id 永久素材的id | |||
*/ | |||
public boolean materialDelete(String media_id) throws WxErrorException; | |||
boolean materialDelete(String media_id) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -165,7 +165,7 @@ public interface WxMpMaterialService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html | |||
* </pre> | |||
*/ | |||
public WxMpMaterialCountResult materialCount() throws WxErrorException; | |||
WxMpMaterialCountResult materialCount() throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -177,7 +177,7 @@ public interface WxMpMaterialService { | |||
* @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回 | |||
* @param count 返回素材的数量,取值在1到20之间 | |||
*/ | |||
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException; | |||
WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -190,6 +190,6 @@ public interface WxMpMaterialService { | |||
* @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回 | |||
* @param count 返回素材的数量,取值在1到20之间 | |||
*/ | |||
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException; | |||
WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException; | |||
} |
@@ -18,7 +18,7 @@ public interface WxMpMenuService { | |||
* 详情请见:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html | |||
* </pre> | |||
*/ | |||
public void menuCreate(WxMenu menu) throws WxErrorException; | |||
void menuCreate(WxMenu menu) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -26,7 +26,7 @@ public interface WxMpMenuService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口 | |||
* </pre> | |||
*/ | |||
public void menuDelete() throws WxErrorException; | |||
void menuDelete() throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -36,7 +36,7 @@ public interface WxMpMenuService { | |||
* | |||
* @param menuid | |||
*/ | |||
public void menuDelete(String menuid) throws WxErrorException; | |||
void menuDelete(String menuid) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -44,7 +44,7 @@ public interface WxMpMenuService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口 | |||
* </pre> | |||
*/ | |||
public WxMenu menuGet() throws WxErrorException; | |||
WxMenu menuGet() throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -54,6 +54,6 @@ public interface WxMpMenuService { | |||
* | |||
* @param userid 可以是粉丝的OpenID,也可以是粉丝的微信号。 | |||
*/ | |||
public WxMenu menuTryMatch(String userid) throws WxErrorException; | |||
WxMenu menuTryMatch(String userid) throws WxErrorException; | |||
} |
@@ -21,9 +21,9 @@ public interface WxMpMessageHandler { | |||
* @param sessionManager | |||
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null | |||
*/ | |||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxMpService wxMpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxMpService wxMpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} |
@@ -22,9 +22,9 @@ public interface WxMpMessageInterceptor { | |||
* @param sessionManager | |||
* @return true代表OK,false代表不OK | |||
*/ | |||
public boolean intercept(WxMpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxMpService wxMpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
boolean intercept(WxMpXmlMessage wxMessage, | |||
Map<String, Object> context, | |||
WxMpService wxMpService, | |||
WxSessionManager sessionManager) throws WxErrorException; | |||
} |
@@ -11,6 +11,6 @@ public interface WxMpMessageMatcher { | |||
* 消息是否匹配某种模式 | |||
* @param message | |||
*/ | |||
public boolean match(WxMpXmlMessage message); | |||
boolean match(WxMpXmlMessage message); | |||
} |
@@ -209,10 +209,7 @@ public class WxMpMessageRouter { | |||
messageId.append(wxMessage.getMsgId()); | |||
} | |||
if (messageDuplicateChecker.isDuplicate(messageId.toString())) { | |||
return true; | |||
} | |||
return false; | |||
return messageDuplicateChecker.isDuplicate(messageId.toString()); | |||
} | |||
@@ -0,0 +1,138 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.mp.bean.result.*; | |||
import java.util.Map; | |||
/** | |||
* 微信支付相关接口 | |||
* Created by Binary Wang on 2016/7/28. | |||
* @author binarywang (https://github.com/binarywang) | |||
*/ | |||
public interface WxMpPayService { | |||
/** | |||
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | |||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | |||
* | |||
* @param openId 支付人openId | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl); | |||
/** | |||
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | |||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | |||
* | |||
* @param parameters All required/optional parameters for weixin payment | |||
*/ | |||
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters); | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* | |||
* @param parameters the required or optional parameters | |||
*/ | |||
Map<String, String> getPayInfo(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装NATIVE发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* tradeType 交易类型 NATIVE (其他交易类型JSAPI,APP,WAP) | |||
* | |||
* @param productId 商户商品ID | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
Map<String, String> getNativePayInfo(String productId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException; | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装JSAPI发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* tradeType 交易类型 JSAPI(其他交易类型NATIVE,APP,WAP) | |||
* | |||
* @param openId 支付人openId | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
Map<String, String> getJsapiPayInfo(String openId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException; | |||
/** | |||
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 | |||
* | |||
*/ | |||
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo); | |||
/** | |||
* 读取支付结果通知 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7 | |||
* | |||
*/ | |||
WxMpPayCallback getJSSDKCallbackData(String xmlData); | |||
/** | |||
* 微信支付-申请退款 | |||
* 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 | |||
* | |||
* @param parameters 需要传入的退款参数的Map。以下几项为参数的必须项:<br/> | |||
* <li/> transaction_id | |||
* <li/> out_trade_no (仅在上述transaction_id为空时是必须项) | |||
* <li/> out_refund_no | |||
* <li/> total_fee | |||
* <li/> refund_fee | |||
* @return 退款操作结果 | |||
*/ | |||
WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 计算Map键值对是否和签名相符, | |||
* 按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的 格式(即 key1=value1&key2=value2...)拼接成字符串 | |||
* </pre> | |||
* | |||
*/ | |||
boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature); | |||
/** | |||
* 发送微信红包给个人用户 | |||
* <p> | |||
* 需要传入的必填参数如下: | |||
* mch_billno//商户订单号 | |||
* send_name//商户名称 | |||
* re_openid//用户openid | |||
* total_amount//红包总额 | |||
* total_num//红包发放总人数 | |||
* wishing//红包祝福语 | |||
* client_ip//服务器Ip地址 | |||
* act_name//活动名称 | |||
* remark //备注 | |||
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5 | |||
* <p> | |||
* 使用现金红包功能需要在xml配置文件中额外设置: | |||
* <partnerId></partnerId>微信商户平台ID | |||
* <partnerKey></partnerKey>商户平台设置的API密钥 | |||
* | |||
*/ | |||
WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException; | |||
} |
@@ -21,7 +21,7 @@ public interface WxMpQrcodeService { | |||
* @param scene_id 参数。 | |||
* @param expire_seconds 过期秒数,默认60秒,最小60秒,最大1800秒 | |||
*/ | |||
public WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException; | |||
WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -31,7 +31,7 @@ public interface WxMpQrcodeService { | |||
* | |||
* @param scene_id 参数。永久二维码时最大值为100000(目前参数只支持1--100000) | |||
*/ | |||
public WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException; | |||
WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -41,7 +41,7 @@ public interface WxMpQrcodeService { | |||
* | |||
* @param scene_str 参数。字符串类型长度现在为1到64 | |||
*/ | |||
public WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException; | |||
WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -51,7 +51,7 @@ public interface WxMpQrcodeService { | |||
* | |||
* @param ticket 二维码ticket | |||
*/ | |||
public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException; | |||
File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -62,7 +62,7 @@ public interface WxMpQrcodeService { | |||
* @param ticket 二维码ticket | |||
* @param needShortUrl 是否需要压缩的二维码地址 | |||
*/ | |||
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException; | |||
String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -72,6 +72,6 @@ public interface WxMpQrcodeService { | |||
* | |||
* @param ticket 二维码ticket | |||
*/ | |||
public String qrCodePictureUrl(String ticket) throws WxErrorException; | |||
String qrCodePictureUrl(String ticket) throws WxErrorException; | |||
} |
@@ -1,6 +1,5 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import me.chanjar.weixin.common.bean.WxCardApiSignature; | |||
import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
@@ -8,14 +7,13 @@ import me.chanjar.weixin.mp.bean.*; | |||
import me.chanjar.weixin.mp.bean.result.*; | |||
import java.text.SimpleDateFormat; | |||
import java.util.Map; | |||
/** | |||
* 微信API的Service | |||
*/ | |||
public interface WxMpService { | |||
public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); | |||
SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); | |||
/** | |||
* <pre> | |||
@@ -23,14 +21,14 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性 | |||
* </pre> | |||
*/ | |||
public boolean checkSignature(String timestamp, String nonce, String signature); | |||
boolean checkSignature(String timestamp, String nonce, String signature); | |||
/** | |||
* 获取access_token, 不强制刷新access_token | |||
* | |||
* @see #getAccessToken(boolean) | |||
*/ | |||
public String getAccessToken() throws WxErrorException; | |||
String getAccessToken() throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -46,14 +44,14 @@ public interface WxMpService { | |||
* | |||
* @param forceRefresh 强制刷新 | |||
*/ | |||
public String getAccessToken(boolean forceRefresh) throws WxErrorException; | |||
String getAccessToken(boolean forceRefresh) throws WxErrorException; | |||
/** | |||
* 获得jsapi_ticket,不强制刷新jsapi_ticket | |||
* | |||
* @see #getJsapiTicket(boolean) | |||
*/ | |||
public String getJsapiTicket() throws WxErrorException; | |||
String getJsapiTicket() throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -65,7 +63,7 @@ public interface WxMpService { | |||
* | |||
* @param forceRefresh 强制刷新 | |||
*/ | |||
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException; | |||
String getJsapiTicket(boolean forceRefresh) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -74,7 +72,7 @@ public interface WxMpService { | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
*/ | |||
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException; | |||
WxJsapiSignature createJsapiSignature(String url) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -82,7 +80,7 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=发送客服消息 | |||
* </pre> | |||
*/ | |||
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException; | |||
void customMessageSend(WxMpCustomMessage message) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -91,12 +89,10 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=高级群发接口 | |||
* </pre> | |||
* | |||
* @param news | |||
* @throws WxErrorException | |||
* @see #massGroupMessageSend(me.chanjar.weixin.mp.bean.WxMpMassGroupMessage) | |||
* @see #massOpenIdsMessageSend(me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage) | |||
*/ | |||
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException; | |||
WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -107,7 +103,7 @@ public interface WxMpService { | |||
* @see #massGroupMessageSend(me.chanjar.weixin.mp.bean.WxMpMassGroupMessage) | |||
* @see #massOpenIdsMessageSend(me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage) | |||
*/ | |||
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException; | |||
WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -117,7 +113,7 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=高级群发接口 | |||
* </pre> | |||
*/ | |||
public WxMpMassSendResult massGroupMessageSend(WxMpMassGroupMessage message) throws WxErrorException; | |||
WxMpMassSendResult massGroupMessageSend(WxMpMassGroupMessage message) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -127,7 +123,7 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=高级群发接口 | |||
* </pre> | |||
*/ | |||
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException; | |||
WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -135,9 +131,8 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=长链接转短链接接口 | |||
* </pre> | |||
* | |||
* @param long_url | |||
*/ | |||
public String shortUrl(String long_url) throws WxErrorException; | |||
String shortUrl(String long_url) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -145,11 +140,9 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=模板消息接口 | |||
* </pre> | |||
* | |||
* @param templateMessage | |||
* @return msgid | |||
* @throws WxErrorException | |||
*/ | |||
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException; | |||
String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -165,11 +158,9 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息 | |||
* </pre> | |||
* | |||
* @param scope | |||
* @param state | |||
* @return url | |||
*/ | |||
public String oauth2buildAuthorizationUrl(String scope, String state); | |||
String oauth2buildAuthorizationUrl(String scope, String state); | |||
/** | |||
* <pre> | |||
@@ -178,11 +169,9 @@ public interface WxMpService { | |||
* </pre> | |||
* | |||
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode | |||
* @param scope | |||
* @param state | |||
* @return url | |||
*/ | |||
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state); | |||
String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state); | |||
/** | |||
* <pre> | |||
@@ -190,33 +179,31 @@ public interface WxMpService { | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息 | |||
* </pre> | |||
*/ | |||
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException; | |||
WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 刷新oauth2的access token | |||
* </pre> | |||
*/ | |||
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException; | |||
WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以 | |||
* </pre> | |||
* | |||
* @param oAuth2AccessToken | |||
* @param lang zh_CN, zh_TW, en | |||
*/ | |||
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException; | |||
WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 验证oauth2的access token是否有效 | |||
* </pre> | |||
* | |||
* @param oAuth2AccessToken | |||
*/ | |||
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken); | |||
boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken); | |||
/** | |||
* <pre> | |||
@@ -243,12 +230,12 @@ public interface WxMpService { | |||
* 可以参考,{@link me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor}的实现方法 | |||
* </pre> | |||
*/ | |||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; | |||
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; | |||
/** | |||
* 注入 {@link WxMpConfigStorage} 的实现 | |||
*/ | |||
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider); | |||
void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider); | |||
/** | |||
* <pre> | |||
@@ -266,256 +253,15 @@ public interface WxMpService { | |||
*/ | |||
void setMaxRetryTimes(int maxRetryTimes); | |||
/** | |||
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | |||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | |||
* | |||
* @param openId 支付人openId | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl); | |||
/** | |||
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | |||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | |||
* | |||
* @param parameters All required/optional parameters for weixin payment | |||
* @throws IllegalArgumentException | |||
*/ | |||
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters); | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* | |||
* @param parameters the required or optional parameters | |||
*/ | |||
Map<String, String> getPayInfo(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装NATIVE发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* tradeType 交易类型 NATIVE (其他交易类型JSAPI,APP,WAP) | |||
* | |||
* @param productId 商户商品ID | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
Map<String, String> getNativePayInfo(String productId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException; | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装JSAPI发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* tradeType 交易类型 JSAPI(其他交易类型NATIVE,APP,WAP) | |||
* | |||
* @param openId 支付人openId | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
Map<String, String> getJsapiPayInfo(String openId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException; | |||
/** | |||
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 | |||
* | |||
* @param transactionId | |||
* @param outTradeNo | |||
*/ | |||
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo); | |||
/** | |||
* 读取支付结果通知 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7 | |||
* | |||
* @param xmlData | |||
*/ | |||
WxMpPayCallback getJSSDKCallbackData(String xmlData); | |||
/** | |||
* 微信支付-申请退款 | |||
* 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 | |||
* | |||
* @param parameters 需要传入的退款参数的Map。以下几项为参数的必须项:<br/> | |||
* <li/> transaction_id | |||
* <li/> out_trade_no (仅在上述transaction_id为空时是必须项) | |||
* <li/> out_refund_no | |||
* <li/> total_fee | |||
* <li/> refund_fee | |||
* @return 退款操作结果 | |||
* @throws WxErrorException | |||
*/ | |||
public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 计算Map键值对是否和签名相符, | |||
* 按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的 格式(即 key1=value1&key2=value2...)拼接成字符串 | |||
* </pre> | |||
* | |||
* @param kvm | |||
* @param signature | |||
*/ | |||
public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature); | |||
/** | |||
* 发送微信红包给个人用户 | |||
* <p> | |||
* 需要传入的必填参数如下: | |||
* mch_billno//商户订单号 | |||
* send_name//商户名称 | |||
* re_openid//用户openid | |||
* total_amount//红包总额 | |||
* total_num//红包发放总人数 | |||
* wishing//红包祝福语 | |||
* client_ip//服务器Ip地址 | |||
* act_name//活动名称 | |||
* remark //备注 | |||
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5 | |||
* <p> | |||
* 使用现金红包功能需要在xml配置文件中额外设置: | |||
* <partnerId></partnerId>微信商户平台ID | |||
* <partnerKey></partnerKey>商户平台设置的API密钥 | |||
* | |||
* @param parameters | |||
*/ | |||
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* 获得卡券api_ticket,不强制刷新卡券api_ticket | |||
* | |||
* @return 卡券api_ticket | |||
* @throws WxErrorException | |||
* @see #getCardApiTicket(boolean) | |||
*/ | |||
public String getCardApiTicket() throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获得卡券api_ticket | |||
* 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94.9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param forceRefresh 强制刷新 | |||
* @return 卡券api_ticket | |||
* @throws WxErrorException | |||
*/ | |||
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 创建调用卡券api时所需要的签名 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD | |||
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94 | |||
* .9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param optionalSignParam 参与签名的参数数组。 | |||
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id | |||
* </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空 | |||
* @return 卡券Api签名对象 | |||
*/ | |||
public WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws | |||
WxErrorException; | |||
/** | |||
* 卡券Code解码 | |||
* | |||
* @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得 | |||
* @return 解密后的Code | |||
* @throws WxErrorException | |||
*/ | |||
public String decryptCardCode(String encryptCode) throws WxErrorException; | |||
/** | |||
* 卡券Code查询 | |||
* | |||
* @param cardId 卡券ID代表一类卡券 | |||
* @param code 单张卡券的唯一标准 | |||
* @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同 | |||
* @return WxMpCardResult对象 | |||
* @throws WxErrorException | |||
*/ | |||
public WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume) | |||
throws WxErrorException; | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
* @throws WxErrorException | |||
*/ | |||
public String consumeCardCode(String code) throws WxErrorException; | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @param cardId 当自定义Code卡券时需要传入card_id | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
* @throws WxErrorException | |||
*/ | |||
public String consumeCardCode(String code, String cardId) throws WxErrorException; | |||
/** | |||
* 卡券Mark接口。 | |||
* 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住), | |||
* 才能进一步调用核销接口,否则报错。 | |||
* | |||
* @param code 卡券的code码 | |||
* @param cardId 卡券的ID | |||
* @param openId 用券用户的openid | |||
* @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用 | |||
* @throws WxErrorException | |||
*/ | |||
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws | |||
WxErrorException; | |||
/** | |||
* 查看卡券详情接口 | |||
* 详见 https://mp.weixin.qq.com/wiki/14/8dd77aeaee85f922db5f8aa6386d385e.html#.E6.9F.A5.E7.9C.8B.E5.8D.A1.E5.88.B8.E8.AF.A6.E6.83.85 | |||
* | |||
* @param cardId 卡券的ID | |||
* @return 返回的卡券详情JSON字符串 | |||
* <br> [注] 由于返回的JSON格式过于复杂,难以定义其对应格式的Bean并且难以维护,因此只返回String格式的JSON串。 | |||
* <br> 可由 com.google.gson.JsonParser#parse 等方法直接取JSON串中的某个字段。 | |||
* @throws WxErrorException | |||
*/ | |||
public String getCardDetail(String cardId) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 预览接口 | |||
* 详情请见:http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E9.A2.84.E8.A7.88.E6.8E.A5.E5.8F.A3.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91 | |||
* </pre> | |||
* | |||
* @param wxMpMassPreviewMessage | |||
* @return wxMpMassSendResult | |||
* @throws WxErrorException | |||
*/ | |||
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | |||
WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | |||
/** | |||
* <pre> | |||
@@ -524,9 +270,7 @@ public interface WxMpService { | |||
* 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A | |||
* </pre> | |||
* | |||
* @param wxMpIndustry | |||
* @return JsonObject | |||
* @throws WxErrorException | |||
*/ | |||
String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException; | |||
@@ -537,7 +281,6 @@ public interface WxMpService { | |||
* </pre> | |||
* | |||
* @return wxMpIndustry | |||
* @throws WxErrorException | |||
*/ | |||
WxMpIndustry getIndustry() throws WxErrorException; | |||
@@ -589,4 +332,18 @@ public interface WxMpService { | |||
* @return WxMpQrcodeService | |||
*/ | |||
WxMpQrcodeService getQrcodeService(); | |||
/** | |||
* 返回卡券相关接口的方法实现类,以方便调用个其各种接口 | |||
* | |||
* @return WxMpCardService | |||
*/ | |||
WxMpCardService getCardService(); | |||
/** | |||
* 返回微信支付相关接口的方法实现类,以方便调用个其各种接口 | |||
* | |||
* @return WxMpPayService | |||
*/ | |||
WxMpPayService getPayService(); | |||
} |
@@ -25,7 +25,7 @@ public interface WxMpUserService { | |||
* @param openid 用户openid | |||
* @param remark 备注名 | |||
*/ | |||
public void userUpdateRemark(String openid, String remark) throws WxErrorException; | |||
void userUpdateRemark(String openid, String remark) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -36,7 +36,7 @@ public interface WxMpUserService { | |||
* @param openid 用户openid | |||
* @param lang 语言,zh_CN 简体(默认),zh_TW 繁体,en 英语 | |||
*/ | |||
public WxMpUser userInfo(String openid, String lang) throws WxErrorException; | |||
WxMpUser userInfo(String openid, String lang) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -46,7 +46,7 @@ public interface WxMpUserService { | |||
* | |||
* @param next_openid 可选,第一个拉取的OPENID,null为从头开始拉取 | |||
*/ | |||
public WxMpUserList userList(String next_openid) throws WxErrorException; | |||
WxMpUserList userList(String next_openid) throws WxErrorException; | |||
/** | |||
* <pre> | |||
@@ -0,0 +1,248 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParser; | |||
import com.google.gson.JsonPrimitive; | |||
import com.google.gson.internal.Streams; | |||
import com.google.gson.reflect.TypeToken; | |||
import com.google.gson.stream.JsonReader; | |||
import me.chanjar.weixin.common.bean.WxCardApiSignature; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.util.RandomUtils; | |||
import me.chanjar.weixin.common.util.crypto.SHA1; | |||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||
import me.chanjar.weixin.mp.api.WxMpCardService; | |||
import me.chanjar.weixin.mp.api.WxMpService; | |||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.io.StringReader; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.Arrays; | |||
/** | |||
* Created by Binary Wang on 2016/7/27. | |||
*/ | |||
public class WxMpCardServiceImpl implements WxMpCardService { | |||
private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class); | |||
/** | |||
* 全局的是否正在刷新卡券api_ticket的锁 | |||
*/ | |||
private final Object globalCardApiTicketRefreshLock = new Object(); | |||
private WxMpService wxMpService; | |||
WxMpCardServiceImpl(WxMpService wxMpService) { | |||
this.wxMpService = wxMpService; | |||
} | |||
/** | |||
* 获得卡券api_ticket,不强制刷新卡券api_ticket | |||
* | |||
* @return 卡券api_ticket | |||
* @see #getCardApiTicket(boolean) | |||
*/ | |||
@Override | |||
public String getCardApiTicket() throws WxErrorException { | |||
return getCardApiTicket(false); | |||
} | |||
/** | |||
* <pre> | |||
* 获得卡券api_ticket | |||
* 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD | |||
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94 | |||
* .9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param forceRefresh 强制刷新 | |||
* @return 卡券api_ticket | |||
*/ | |||
@Override | |||
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { | |||
if (forceRefresh) { | |||
this.wxMpService.getWxMpConfigStorage().expireCardApiTicket(); | |||
} | |||
if (this.wxMpService.getWxMpConfigStorage().isCardApiTicketExpired()) { | |||
synchronized (this.globalCardApiTicketRefreshLock) { | |||
if (this.wxMpService.getWxMpConfigStorage().isCardApiTicketExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card"; | |||
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); | |||
this.wxMpService.getWxMpConfigStorage().updateCardApiTicket(cardApiTicket, expiresInSeconds); | |||
} | |||
} | |||
} | |||
return this.wxMpService.getWxMpConfigStorage().getCardApiTicket(); | |||
} | |||
/** | |||
* <pre> | |||
* 创建调用卡券api时所需要的签名 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD | |||
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94 | |||
* .9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param optionalSignParam 参与签名的参数数组。 | |||
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id | |||
* </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空 | |||
* @return 卡券Api签名对象 | |||
*/ | |||
@Override | |||
public WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws | |||
WxErrorException { | |||
long timestamp = System.currentTimeMillis() / 1000; | |||
String nonceStr = RandomUtils.getRandomStr(); | |||
String cardApiTicket = getCardApiTicket(false); | |||
String[] signParam = Arrays.copyOf(optionalSignParam, optionalSignParam.length + 3); | |||
signParam[optionalSignParam.length] = String.valueOf(timestamp); | |||
signParam[optionalSignParam.length + 1] = nonceStr; | |||
signParam[optionalSignParam.length + 2] = cardApiTicket; | |||
try { | |||
String signature = SHA1.gen(signParam); | |||
WxCardApiSignature cardApiSignature = new WxCardApiSignature(); | |||
cardApiSignature.setTimestamp(timestamp); | |||
cardApiSignature.setNonceStr(nonceStr); | |||
cardApiSignature.setSignature(signature); | |||
return cardApiSignature; | |||
} catch (NoSuchAlgorithmException e) { | |||
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build()); | |||
} | |||
} | |||
/** | |||
* 卡券Code解码 | |||
* | |||
* @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得 | |||
* @return 解密后的Code | |||
*/ | |||
@Override | |||
public String decryptCardCode(String encryptCode) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/decrypt"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("encrypt_code", encryptCode); | |||
String responseContent = this.wxMpService.post(url, param.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code"); | |||
return jsonPrimitive.getAsString(); | |||
} | |||
/** | |||
* 卡券Code查询 | |||
* | |||
* @param cardId 卡券ID代表一类卡券 | |||
* @param code 单张卡券的唯一标准 | |||
* @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同 | |||
* @return WxMpCardResult对象 | |||
*/ | |||
@Override | |||
public WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/get"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("card_id", cardId); | |||
param.addProperty("code", code); | |||
param.addProperty("check_consume", checkConsume); | |||
String responseContent = this.wxMpService.post(url, param.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, | |||
new TypeToken<WxMpCardResult>() { | |||
}.getType()); | |||
} | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
*/ | |||
@Override | |||
public String consumeCardCode(String code) throws WxErrorException { | |||
return consumeCardCode(code, null); | |||
} | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @param cardId 当自定义Code卡券时需要传入card_id | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
*/ | |||
@Override | |||
public String consumeCardCode(String code, String cardId) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/consume"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("code", code); | |||
if (cardId != null && !"".equals(cardId)) { | |||
param.addProperty("card_id", cardId); | |||
} | |||
return this.wxMpService.post(url, param.toString()); | |||
} | |||
/** | |||
* 卡券Mark接口。 | |||
* 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住), | |||
* 才能进一步调用核销接口,否则报错。 | |||
* | |||
* @param code 卡券的code码 | |||
* @param cardId 卡券的ID | |||
* @param openId 用券用户的openid | |||
* @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用 | |||
*/ | |||
@Override | |||
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws | |||
WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/mark"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("code", code); | |||
param.addProperty("card_id", cardId); | |||
param.addProperty("openid", openId); | |||
param.addProperty("is_mark", isMark); | |||
String responseContent = this.wxMpService.post(url, param.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, | |||
new TypeToken<WxMpCardResult>() { }.getType()); | |||
if (!cardResult.getErrorCode().equals("0")) { | |||
this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg()); | |||
} | |||
} | |||
@Override | |||
public String getCardDetail(String cardId) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/get"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("card_id", cardId); | |||
String responseContent = this.wxMpService.post(url, param.toString()); | |||
// 判断返回值 | |||
JsonObject json = (new JsonParser()).parse(responseContent).getAsJsonObject(); | |||
String errcode = json.get("errcode").getAsString(); | |||
if (!"0".equals(errcode)) { | |||
String errmsg = json.get("errmsg").getAsString(); | |||
WxError error = new WxError(); | |||
error.setErrorCode(Integer.valueOf(errcode)); | |||
error.setErrorMsg(errmsg); | |||
throw new WxErrorException(error); | |||
} | |||
return responseContent; | |||
} | |||
} |
@@ -0,0 +1,335 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import com.thoughtworks.xstream.XStream; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.util.crypto.WxCryptUtil; | |||
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | |||
import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
import me.chanjar.weixin.mp.api.WxMpPayService; | |||
import me.chanjar.weixin.mp.bean.result.*; | |||
import org.apache.http.Consts; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.client.config.RequestConfig; | |||
import org.apache.http.client.methods.CloseableHttpResponse; | |||
import org.apache.http.client.methods.HttpPost; | |||
import org.apache.http.entity.StringEntity; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.slf4j.helpers.MessageFormatter; | |||
import java.io.IOException; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.SortedMap; | |||
import java.util.TreeMap; | |||
/** | |||
* Created by Binary Wang on 2016/7/28. | |||
* @author binarywang (https://github.com/binarywang) | |||
*/ | |||
public class WxMpPayServiceImpl implements WxMpPayService { | |||
private final Logger log = LoggerFactory.getLogger(WxMpPayServiceImpl.class); | |||
private HttpHost httpProxy; | |||
private WxMpServiceImpl wxMpService; | |||
public WxMpPayServiceImpl(WxMpServiceImpl wxMpService) { | |||
this.wxMpService = wxMpService; | |||
this.httpProxy = wxMpService.getHttpProxy(); | |||
} | |||
@Override | |||
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) { | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
packageParams.put("mch_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", tradeType); | |||
packageParams.put("openid", openId); | |||
return getPrepayId(packageParams); | |||
} | |||
@Override | |||
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
final SortedMap<String, String> packageParams = new TreeMap<>(parameters); | |||
packageParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
packageParams.put("mch_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
packageParams.put("nonce_str", nonce_str); | |||
checkParameters(packageParams); | |||
String sign = WxCryptUtil.createSign(packageParams, this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
packageParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Map.Entry<String, String> para : packageParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = this.wxMpService.getHttpclient().execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", WxMpPrepayIdResult.class); | |||
return (WxMpPrepayIdResult) xstream.fromXML(responseContent); | |||
} catch (IOException e) { | |||
throw new RuntimeException("Failed to get prepay id due to IO exception.", e); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
private final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid", "mch_id", "body", "out_trade_no", | |||
"total_fee", "spbill_create_ip", "notify_url","trade_type"}; | |||
private void checkParameters(Map<String, String> parameters) { | |||
for (String para : this.REQUIRED_ORDER_PARAMETERS) { | |||
if (!parameters.containsKey(para)) | |||
throw new IllegalArgumentException("Reqiured argument '" + para + "' is missing."); | |||
} | |||
if ("JSAPI".equals(parameters.get("trade_type")) && !parameters.containsKey("openid")) | |||
throw new IllegalArgumentException("Reqiured argument 'openid' is missing when trade_type is 'JSAPI'."); | |||
if ("NATIVE".equals(parameters.get("trade_type")) && !parameters.containsKey("product_id")) | |||
throw new IllegalArgumentException("Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'."); | |||
} | |||
@Override | |||
public Map<String, String> getJsapiPayInfo(String openId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException { | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
packageParams.put("mch_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", "JSAPI"); | |||
packageParams.put("openid", openId); | |||
return getPayInfo(packageParams); | |||
} | |||
@Override | |||
public Map<String, String> getNativePayInfo(String productId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException{ | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
packageParams.put("mch_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", "NATIVE"); | |||
packageParams.put("product_id", productId); | |||
return getPayInfo(packageParams); | |||
} | |||
@Override | |||
public Map<String, String> getPayInfo(Map<String, String> parameters) throws WxErrorException { | |||
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters); | |||
if (!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getReturn_code()) | |||
||!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getResult_code())) { | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("return_code:" + wxMpPrepayIdResult.getReturn_code() + | |||
";return_msg:" + wxMpPrepayIdResult.getReturn_msg() + | |||
";result_code:" + wxMpPrepayIdResult.getResult_code() + | |||
";err_code" + wxMpPrepayIdResult.getErr_code() + | |||
";err_code_des" + wxMpPrepayIdResult.getErr_code_des()); | |||
throw new WxErrorException(error); | |||
} | |||
String prepayId = wxMpPrepayIdResult.getPrepay_id(); | |||
if (prepayId == null || prepayId.equals("")) { | |||
throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", wxMpPrepayIdResult.getErr_code(), wxMpPrepayIdResult.getErr_code_des())); | |||
} | |||
Map<String, String> payInfo = new HashMap<>(); | |||
payInfo.put("appId", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符 | |||
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000)); | |||
payInfo.put("nonceStr", System.currentTimeMillis() + ""); | |||
payInfo.put("package", "prepay_id=" + prepayId); | |||
payInfo.put("signType", "MD5"); | |||
if("NATIVE".equals(parameters.get("trade_type"))){ | |||
payInfo.put("codeUrl", wxMpPrepayIdResult.getCode_url()); | |||
} | |||
String finalSign = WxCryptUtil.createSign(payInfo, this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
payInfo.put("paySign", finalSign); | |||
return payInfo; | |||
} | |||
@Override | |||
public WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
SortedMap<String, String> packageParams = new TreeMap<>(); | |||
packageParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
packageParams.put("mch_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
if (transactionId != null && !"".equals(transactionId.trim())) | |||
packageParams.put("transaction_id", transactionId); | |||
else if (outTradeNo != null && !"".equals(outTradeNo.trim())) | |||
packageParams.put("out_trade_no", outTradeNo); | |||
else | |||
throw new IllegalArgumentException("Either 'transactionId' or 'outTradeNo' must be given."); | |||
packageParams.put("nonce_str", nonce_str); | |||
packageParams.put("sign", WxCryptUtil.createSign(packageParams, this.wxMpService.getWxMpConfigStorage().getPartnerKey())); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Map.Entry<String, String> para : packageParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = this.wxMpService.getHttpclient().execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", WxMpPayResult.class); | |||
return (WxMpPayResult) xstream.fromXML(responseContent); | |||
} catch (IOException e) { | |||
throw new RuntimeException("Failed to query order due to IO exception.", e); | |||
} | |||
} | |||
@Override | |||
public WxMpPayCallback getJSSDKCallbackData(String xmlData) { | |||
try { | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", WxMpPayCallback.class); | |||
return (WxMpPayCallback) xstream.fromXML(xmlData); | |||
} catch (Exception e){ | |||
e.printStackTrace(); | |||
} | |||
return new WxMpPayCallback(); | |||
} | |||
@Override | |||
public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException { | |||
SortedMap<String, String> refundParams = new TreeMap<>(parameters); | |||
refundParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
refundParams.put("mch_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
refundParams.put("nonce_str", System.currentTimeMillis() + ""); | |||
refundParams.put("op_user_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
String sign = WxCryptUtil.createSign(refundParams, this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
refundParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Map.Entry<String, String> para : refundParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = this.wxMpService.getHttpclient().execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.processAnnotations(WxMpPayRefundResult.class); | |||
WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream.fromXML(responseContent); | |||
if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode()) | |||
||!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) { | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode() + | |||
";return_msg:" + wxMpPayRefundResult.getReturnMsg() + | |||
";result_code:" + wxMpPayRefundResult.getResultCode() + | |||
";err_code" + wxMpPayRefundResult.getErrCode() + | |||
";err_code_des" + wxMpPayRefundResult.getErrCodeDes()); | |||
throw new WxErrorException(error); | |||
} | |||
return wxMpPayRefundResult; | |||
} catch (IOException e) { | |||
this.log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", | |||
request.toString()).getMessage(), e); | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("incorrect response."); | |||
throw new WxErrorException(error); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@Override | |||
public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature) { | |||
return signature.equals(WxCryptUtil.createSign(kvm, this.wxMpService.getWxMpConfigStorage().getPartnerKey())); | |||
} | |||
@Override | |||
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
SortedMap<String, String> packageParams = new TreeMap<>(parameters); | |||
packageParams.put("wxappid", this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
packageParams.put("mch_id", this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
packageParams.put("nonce_str", nonce_str); | |||
String sign = WxCryptUtil.createSign(packageParams, this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
packageParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Map.Entry<String, String> para : packageParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = this.wxMpService.getHttpclient().execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.processAnnotations(WxRedpackResult.class); | |||
return (WxRedpackResult) xstream.fromXML(responseContent); | |||
} catch (IOException e) { | |||
this.log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e); | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
throw new WxErrorException(error); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
} |
@@ -1,12 +1,11 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import com.google.gson.*; | |||
import com.google.gson.JsonArray; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.internal.Streams; | |||
import com.google.gson.reflect.TypeToken; | |||
import com.google.gson.stream.JsonReader; | |||
import com.thoughtworks.xstream.XStream; | |||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||
import me.chanjar.weixin.common.bean.WxCardApiSignature; | |||
import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
@@ -14,33 +13,23 @@ import me.chanjar.weixin.common.session.StandardSessionManager; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.common.util.RandomUtils; | |||
import me.chanjar.weixin.common.util.crypto.SHA1; | |||
import me.chanjar.weixin.common.util.crypto.WxCryptUtil; | |||
import me.chanjar.weixin.common.util.http.*; | |||
import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
import me.chanjar.weixin.mp.api.*; | |||
import me.chanjar.weixin.mp.bean.*; | |||
import me.chanjar.weixin.mp.bean.result.*; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import org.apache.http.Consts; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.config.RequestConfig; | |||
import org.apache.http.client.methods.CloseableHttpResponse; | |||
import org.apache.http.client.methods.HttpGet; | |||
import org.apache.http.client.methods.HttpPost; | |||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
import org.apache.http.entity.StringEntity; | |||
import org.apache.http.impl.client.BasicResponseHandler; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.slf4j.helpers.MessageFormatter; | |||
import java.io.IOException; | |||
import java.io.StringReader; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.*; | |||
import java.util.Map.Entry; | |||
public class WxMpServiceImpl implements WxMpService { | |||
@@ -49,35 +38,38 @@ public class WxMpServiceImpl implements WxMpService { | |||
/** | |||
* 全局的是否正在刷新access token的锁 | |||
*/ | |||
protected final Object globalAccessTokenRefreshLock = new Object(); | |||
private final Object globalAccessTokenRefreshLock = new Object(); | |||
/** | |||
* 全局的是否正在刷新jsapi_ticket的锁 | |||
*/ | |||
protected final Object globalJsapiTicketRefreshLock = new Object(); | |||
private final Object globalJsapiTicketRefreshLock = new Object(); | |||
/** | |||
* 全局的是否正在刷新卡券api_ticket的锁 | |||
*/ | |||
protected final Object globalCardApiTicketRefreshLock = new Object(); | |||
protected WxMpConfigStorage wxMpConfigStorage; | |||
private WxMpConfigStorage wxMpConfigStorage; | |||
protected WxMpKefuService kefuService = new WxMpKefuServiceImpl(this); | |||
private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this); | |||
private WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this); | |||
private WxMpMenuService menuService = new WxMpMenuServiceImpl(this); | |||
protected WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this); | |||
private WxMpUserService userService = new WxMpUserServiceImpl(this); | |||
protected WxMpMenuService menuService = new WxMpMenuServiceImpl(this); | |||
private WxMpGroupService groupService = new WxMpGroupServiceImpl(this); | |||
protected WxMpUserService userService = new WxMpUserServiceImpl(this); | |||
private WxMpQrcodeService qrCodeService = new WxMpQrcodeServiceImpl(this); | |||
protected WxMpGroupService groupService = new WxMpGroupServiceImpl(this); | |||
private WxMpCardService cardService = new WxMpCardServiceImpl(this); | |||
protected WxMpQrcodeService qrCodeService = new WxMpQrcodeServiceImpl(this); | |||
private WxMpPayService payService = new WxMpPayServiceImpl(this); | |||
protected CloseableHttpClient httpClient; | |||
private CloseableHttpClient httpClient; | |||
protected HttpHost httpProxy; | |||
private HttpHost httpProxy; | |||
public HttpHost getHttpProxy() { | |||
return this.httpProxy; | |||
} | |||
private int retrySleepMillis = 1000; | |||
@@ -107,10 +99,9 @@ public class WxMpServiceImpl implements WxMpService { | |||
if (this.wxMpConfigStorage.isAccessTokenExpired()) { | |||
synchronized (this.globalAccessTokenRefreshLock) { | |||
if (this.wxMpConfigStorage.isAccessTokenExpired()) { | |||
String url = new StringBuffer() | |||
.append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential") | |||
.append("&appid=").append(this.wxMpConfigStorage.getAppId()) | |||
.append("&secret=").append(this.wxMpConfigStorage.getSecret()).toString(); | |||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + | |||
"&appid=" + this.wxMpConfigStorage.getAppId() + | |||
"&secret=" + this.wxMpConfigStorage.getSecret(); | |||
try { | |||
HttpGet httpGet = new HttpGet(url); | |||
if (this.httpProxy != null) { | |||
@@ -128,8 +119,6 @@ public class WxMpServiceImpl implements WxMpService { | |||
}finally { | |||
httpGet.releaseConnection(); | |||
} | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} | |||
@@ -149,6 +138,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
if (forceRefresh) { | |||
this.wxMpConfigStorage.expireJsapiTicket(); | |||
} | |||
if (this.wxMpConfigStorage.isJsapiTicketExpired()) { | |||
synchronized (this.globalJsapiTicketRefreshLock) { | |||
if (this.wxMpConfigStorage.isJsapiTicketExpired()) { | |||
@@ -259,7 +249,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) { | |||
StringBuffer url = new StringBuffer(); | |||
StringBuilder url = new StringBuilder(); | |||
url.append("https://open.weixin.qq.com/connect/oauth2/authorize?"); | |||
url.append("appid=").append(this.wxMpConfigStorage.getAppId()); | |||
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI)); | |||
@@ -274,7 +264,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { | |||
StringBuffer url = new StringBuffer(); | |||
StringBuilder url = new StringBuilder(); | |||
url.append("https://api.weixin.qq.com/sns/oauth2/access_token?"); | |||
url.append("appid=").append(this.wxMpConfigStorage.getAppId()); | |||
url.append("&secret=").append(this.wxMpConfigStorage.getSecret()); | |||
@@ -285,8 +275,6 @@ public class WxMpServiceImpl implements WxMpService { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
return WxMpOAuth2AccessToken.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} | |||
@@ -294,7 +282,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { | |||
StringBuffer url = new StringBuffer(); | |||
StringBuilder url = new StringBuilder(); | |||
url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?"); | |||
url.append("appid=").append(this.wxMpConfigStorage.getAppId()); | |||
url.append("&grant_type=refresh_token"); | |||
@@ -304,8 +292,6 @@ public class WxMpServiceImpl implements WxMpService { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
return WxMpOAuth2AccessToken.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} | |||
@@ -313,7 +299,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException { | |||
StringBuffer url = new StringBuffer(); | |||
StringBuilder url = new StringBuilder(); | |||
url.append("https://api.weixin.qq.com/sns/userinfo?"); | |||
url.append("access_token=").append(oAuth2AccessToken.getAccessToken()); | |||
url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | |||
@@ -327,8 +313,6 @@ public class WxMpServiceImpl implements WxMpService { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
return WxMpUser.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} | |||
@@ -336,7 +320,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) { | |||
StringBuffer url = new StringBuffer(); | |||
StringBuilder url = new StringBuilder(); | |||
url.append("https://api.weixin.qq.com/sns/auth?"); | |||
url.append("access_token=").append(oAuth2AccessToken.getAccessToken()); | |||
url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | |||
@@ -344,8 +328,6 @@ public class WxMpServiceImpl implements WxMpService { | |||
try { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} catch (WxErrorException e) { | |||
@@ -435,14 +417,12 @@ public class WxMpServiceImpl implements WxMpService { | |||
throw new WxErrorException(error); | |||
} | |||
return null; | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} | |||
} | |||
protected CloseableHttpClient getHttpclient() { | |||
public CloseableHttpClient getHttpclient() { | |||
return this.httpClient; | |||
} | |||
@@ -487,513 +467,6 @@ public class WxMpServiceImpl implements WxMpService { | |||
this.maxRetryTimes = maxRetryTimes; | |||
} | |||
@Override | |||
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) { | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", tradeType); | |||
packageParams.put("openid", openId); | |||
return getPrepayId(packageParams); | |||
} | |||
@Override | |||
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
final SortedMap<String, String> packageParams = new TreeMap<>(parameters); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("nonce_str", nonce_str); | |||
checkParameters(packageParams); | |||
String sign = WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey()); | |||
packageParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Entry<String, String> para : packageParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = getHttpclient().execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", WxMpPrepayIdResult.class); | |||
WxMpPrepayIdResult wxMpPrepayIdResult = (WxMpPrepayIdResult) xstream.fromXML(responseContent); | |||
return wxMpPrepayIdResult; | |||
} catch (IOException e) { | |||
throw new RuntimeException("Failed to get prepay id due to IO exception.", e); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid", "mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip", "notify_url", | |||
"trade_type", }; | |||
private void checkParameters(Map<String, String> parameters) { | |||
for (String para : this.REQUIRED_ORDER_PARAMETERS) { | |||
if (!parameters.containsKey(para)) | |||
throw new IllegalArgumentException("Reqiured argument '" + para + "' is missing."); | |||
} | |||
if ("JSAPI".equals(parameters.get("trade_type")) && !parameters.containsKey("openid")) | |||
throw new IllegalArgumentException("Reqiured argument 'openid' is missing when trade_type is 'JSAPI'."); | |||
if ("NATIVE".equals(parameters.get("trade_type")) && !parameters.containsKey("product_id")) | |||
throw new IllegalArgumentException("Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'."); | |||
} | |||
@Override | |||
public Map<String, String> getJsapiPayInfo(String openId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException{ | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", "JSAPI"); | |||
packageParams.put("openid", openId); | |||
return getPayInfo(packageParams); | |||
} | |||
@Override | |||
public Map<String, String> getNativePayInfo(String productId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException{ | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", "NATIVE"); | |||
packageParams.put("product_id", productId); | |||
return getPayInfo(packageParams); | |||
} | |||
@Override | |||
public Map<String, String> getPayInfo(Map<String, String> parameters) throws WxErrorException { | |||
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters); | |||
if (!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getReturn_code()) | |||
||!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getResult_code())) { | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("return_code:" + wxMpPrepayIdResult.getReturn_code() + | |||
";return_msg:" + wxMpPrepayIdResult.getReturn_msg() + | |||
";result_code:" + wxMpPrepayIdResult.getResult_code() + | |||
";err_code" + wxMpPrepayIdResult.getErr_code() + | |||
";err_code_des" + wxMpPrepayIdResult.getErr_code_des()); | |||
throw new WxErrorException(error); | |||
} | |||
String prepayId = wxMpPrepayIdResult.getPrepay_id(); | |||
if (prepayId == null || prepayId.equals("")) { | |||
throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", wxMpPrepayIdResult.getErr_code(), wxMpPrepayIdResult.getErr_code_des())); | |||
} | |||
Map<String, String> payInfo = new HashMap<>(); | |||
payInfo.put("appId", this.wxMpConfigStorage.getAppId()); | |||
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符 | |||
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000)); | |||
payInfo.put("nonceStr", System.currentTimeMillis() + ""); | |||
payInfo.put("package", "prepay_id=" + prepayId); | |||
payInfo.put("signType", "MD5"); | |||
if("NATIVE".equals(parameters.get("trade_type"))){ | |||
payInfo.put("codeUrl", wxMpPrepayIdResult.getCode_url()); | |||
} | |||
String finalSign = WxCryptUtil.createSign(payInfo, this.wxMpConfigStorage.getPartnerKey()); | |||
payInfo.put("paySign", finalSign); | |||
return payInfo; | |||
} | |||
@Override | |||
public WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
SortedMap<String, String> packageParams = new TreeMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
if (transactionId != null && !"".equals(transactionId.trim())) | |||
packageParams.put("transaction_id", transactionId); | |||
else if (outTradeNo != null && !"".equals(outTradeNo.trim())) | |||
packageParams.put("out_trade_no", outTradeNo); | |||
else | |||
throw new IllegalArgumentException("Either 'transactionId' or 'outTradeNo' must be given."); | |||
packageParams.put("nonce_str", nonce_str); | |||
packageParams.put("sign", WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey())); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Entry<String, String> para : packageParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = this.httpClient.execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", WxMpPayResult.class); | |||
WxMpPayResult wxMpPayResult = (WxMpPayResult) xstream.fromXML(responseContent); | |||
return wxMpPayResult; | |||
} catch (IOException e) { | |||
throw new RuntimeException("Failed to query order due to IO exception.", e); | |||
} | |||
} | |||
@Override | |||
public WxMpPayCallback getJSSDKCallbackData(String xmlData) { | |||
try { | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", WxMpPayCallback.class); | |||
WxMpPayCallback wxMpCallback = (WxMpPayCallback) xstream.fromXML(xmlData); | |||
return wxMpCallback; | |||
} catch (Exception e){ | |||
e.printStackTrace(); | |||
} | |||
return new WxMpPayCallback(); | |||
} | |||
@Override | |||
public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException { | |||
SortedMap<String, String> refundParams = new TreeMap<>(parameters); | |||
refundParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
refundParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
refundParams.put("nonce_str", System.currentTimeMillis() + ""); | |||
refundParams.put("op_user_id", this.wxMpConfigStorage.getPartnerId()); | |||
String sign = WxCryptUtil.createSign(refundParams, this.wxMpConfigStorage.getPartnerKey()); | |||
refundParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Entry<String, String> para : refundParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try( | |||
CloseableHttpResponse response = getHttpclient().execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.processAnnotations(WxMpPayRefundResult.class); | |||
WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream.fromXML(responseContent); | |||
if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode()) | |||
||!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) { | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode() + | |||
";return_msg:" + wxMpPayRefundResult.getReturnMsg() + | |||
";result_code:" + wxMpPayRefundResult.getResultCode() + | |||
";err_code" + wxMpPayRefundResult.getErrCode() + | |||
";err_code_des" + wxMpPayRefundResult.getErrCodeDes()); | |||
throw new WxErrorException(error); | |||
} | |||
return wxMpPayRefundResult; | |||
} catch (IOException e) { | |||
this.log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", request.toString()).getMessage(), e); | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("incorrect response."); | |||
throw new WxErrorException(error); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@Override | |||
public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature) { | |||
return signature.equals(WxCryptUtil.createSign(kvm, this.wxMpConfigStorage.getPartnerKey())); | |||
} | |||
@Override | |||
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
SortedMap<String, String> packageParams = new TreeMap<>(parameters); | |||
packageParams.put("wxappid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("nonce_str", nonce_str); | |||
String sign = WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey()); | |||
packageParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Entry<String, String> para : packageParams.entrySet()) { | |||
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | |||
} | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = getHttpclient().execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.processAnnotations(WxRedpackResult.class); | |||
WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(responseContent); | |||
return wxMpRedpackResult; | |||
} catch (IOException e) { | |||
this.log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e); | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
throw new WxErrorException(error); | |||
}finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
/** | |||
* 获得卡券api_ticket,不强制刷新卡券api_ticket | |||
* | |||
* @return 卡券api_ticket | |||
* @throws WxErrorException | |||
* @see #getCardApiTicket(boolean) | |||
*/ | |||
@Override | |||
public String getCardApiTicket() throws WxErrorException { | |||
return getCardApiTicket(false); | |||
} | |||
/** | |||
* <pre> | |||
* 获得卡券api_ticket | |||
* 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD | |||
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94 | |||
* .9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param forceRefresh 强制刷新 | |||
* @return 卡券api_ticket | |||
* @throws WxErrorException | |||
*/ | |||
@Override | |||
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { | |||
if (forceRefresh) { | |||
this.wxMpConfigStorage.expireCardApiTicket(); | |||
} | |||
if (this.wxMpConfigStorage.isCardApiTicketExpired()) { | |||
synchronized (this.globalCardApiTicketRefreshLock) { | |||
if (this.wxMpConfigStorage.isCardApiTicketExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card"; | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); | |||
this.wxMpConfigStorage.updateCardApiTicket(cardApiTicket, expiresInSeconds); | |||
} | |||
} | |||
} | |||
return this.wxMpConfigStorage.getCardApiTicket(); | |||
} | |||
/** | |||
* <pre> | |||
* 创建调用卡券api时所需要的签名 | |||
* | |||
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD | |||
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94 | |||
* .9F.E6.88.90.E7.AE.97.E6.B3.95 | |||
* </pre> | |||
* | |||
* @param optionalSignParam 参与签名的参数数组。 | |||
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id | |||
* </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空 | |||
* @return 卡券Api签名对象 | |||
*/ | |||
@Override | |||
public WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws | |||
WxErrorException { | |||
long timestamp = System.currentTimeMillis() / 1000; | |||
String nonceStr = RandomUtils.getRandomStr(); | |||
String cardApiTicket = getCardApiTicket(false); | |||
String[] signParam = Arrays.copyOf(optionalSignParam, optionalSignParam.length + 3); | |||
signParam[optionalSignParam.length] = String.valueOf(timestamp); | |||
signParam[optionalSignParam.length + 1] = nonceStr; | |||
signParam[optionalSignParam.length + 2] = cardApiTicket; | |||
try { | |||
String signature = SHA1.gen(signParam); | |||
WxCardApiSignature cardApiSignature = new WxCardApiSignature(); | |||
cardApiSignature.setTimestamp(timestamp); | |||
cardApiSignature.setNonceStr(nonceStr); | |||
cardApiSignature.setSignature(signature); | |||
return cardApiSignature; | |||
} catch (NoSuchAlgorithmException e) { | |||
throw new RuntimeException(e); | |||
} | |||
} | |||
/** | |||
* 卡券Code解码 | |||
* | |||
* @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得 | |||
* @return 解密后的Code | |||
* @throws WxErrorException | |||
*/ | |||
@Override | |||
public String decryptCardCode(String encryptCode) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/decrypt"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("encrypt_code", encryptCode); | |||
String responseContent = post(url, param.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code"); | |||
return jsonPrimitive.getAsString(); | |||
} | |||
/** | |||
* 卡券Code查询 | |||
* | |||
* @param cardId 卡券ID代表一类卡券 | |||
* @param code 单张卡券的唯一标准 | |||
* @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同 | |||
* @return WxMpCardResult对象 | |||
* @throws WxErrorException | |||
*/ | |||
@Override | |||
public WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/get"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("card_id", cardId); | |||
param.addProperty("code", code); | |||
param.addProperty("check_consume", checkConsume); | |||
String responseContent = post(url, param.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, | |||
new TypeToken<WxMpCardResult>() { | |||
}.getType()); | |||
} | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
* @throws WxErrorException | |||
*/ | |||
@Override | |||
public String consumeCardCode(String code) throws WxErrorException { | |||
return consumeCardCode(code, null); | |||
} | |||
/** | |||
* 卡券Code核销。核销失败会抛出异常 | |||
* | |||
* @param code 单张卡券的唯一标准 | |||
* @param cardId 当自定义Code卡券时需要传入card_id | |||
* @return 调用返回的JSON字符串。 | |||
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。 | |||
* @throws WxErrorException | |||
*/ | |||
@Override | |||
public String consumeCardCode(String code, String cardId) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/consume"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("code", code); | |||
if (cardId != null && !"".equals(cardId)) { | |||
param.addProperty("card_id", cardId); | |||
} | |||
String responseContent = post(url, param.toString()); | |||
return responseContent; | |||
} | |||
/** | |||
* 卡券Mark接口。 | |||
* 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住), | |||
* 才能进一步调用核销接口,否则报错。 | |||
* | |||
* @param code 卡券的code码 | |||
* @param cardId 卡券的ID | |||
* @param openId 用券用户的openid | |||
* @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用 | |||
* @throws WxErrorException | |||
*/ | |||
@Override | |||
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws | |||
WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/code/mark"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("code", code); | |||
param.addProperty("card_id", cardId); | |||
param.addProperty("openid", openId); | |||
param.addProperty("is_mark", isMark); | |||
String responseContent = post(url, param.toString()); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, | |||
new TypeToken<WxMpCardResult>() { }.getType()); | |||
if (!cardResult.getErrorCode().equals("0")) { | |||
this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg()); | |||
} | |||
} | |||
@Override | |||
public String getCardDetail(String cardId) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/card/get"; | |||
JsonObject param = new JsonObject(); | |||
param.addProperty("card_id", cardId); | |||
String responseContent = post(url, param.toString()); | |||
// 判断返回值 | |||
JsonObject json = (new JsonParser()).parse(responseContent).getAsJsonObject(); | |||
String errcode = json.get("errcode").getAsString(); | |||
if (!"0".equals(errcode)) { | |||
String errmsg = json.get("errmsg").getAsString(); | |||
WxError error = new WxError(); | |||
error.setErrorCode(Integer.valueOf(errcode)); | |||
error.setErrorMsg(errmsg); | |||
throw new WxErrorException(error); | |||
} | |||
return responseContent; | |||
} | |||
@Override | |||
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview"; | |||
@@ -1048,4 +521,14 @@ public class WxMpServiceImpl implements WxMpService { | |||
return this.qrCodeService; | |||
} | |||
@Override | |||
public WxMpCardService getCardService() { | |||
return this.cardService; | |||
} | |||
@Override | |||
public WxMpPayService getPayService() { | |||
return this.payService; | |||
} | |||
} |
@@ -1,6 +1,8 @@ | |||
package me.chanjar.weixin.mp.bean.result; | |||
import me.chanjar.weixin.mp.bean.WxMpCard; | |||
import org.apache.commons.lang3.builder.ToStringBuilder; | |||
import org.apache.commons.lang3.builder.ToStringStyle; | |||
import java.io.Serializable; | |||
@@ -58,14 +60,7 @@ public class WxMpCardResult implements Serializable { | |||
@Override | |||
public String toString() { | |||
return "WxMpCardResult{" + | |||
"errorCode='" + errorCode + '\'' + | |||
", errorMsg='" + errorMsg + '\'' + | |||
", openId='" + openId + '\'' + | |||
", card=" + card + | |||
", userCardStatus='" + userCardStatus + '\'' + | |||
", canConsume=" + canConsume + | |||
'}'; | |||
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
} | |||
public String getUserCardStatus() { | |||
@@ -24,7 +24,7 @@ public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, S | |||
super(); | |||
} | |||
public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { | |||
public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
@@ -25,7 +25,7 @@ public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMate | |||
super(); | |||
} | |||
public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { | |||
public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
@@ -23,7 +23,7 @@ import java.util.Map; | |||
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> { | |||
@Override | |||
public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException { | |||
public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build(); | |||
@@ -24,7 +24,7 @@ public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMat | |||
super(); | |||
} | |||
public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { | |||
public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
@@ -34,7 +34,7 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec | |||
this.tmpDirFile = tmpDirFile; | |||
} | |||
public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException { | |||
public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
@@ -24,7 +24,7 @@ import java.io.IOException; | |||
*/ | |||
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> { | |||
@Override | |||
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, ClientProtocolException, IOException { | |||
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
@@ -31,7 +31,7 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi | |||
@Override | |||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException { | |||
WxMpQrCodeTicket ticket) throws WxErrorException, IOException { | |||
if (ticket != null) { | |||
if (uri.indexOf('?') == -1) { | |||
uri += '?'; | |||
@@ -18,7 +18,7 @@ public class WxLongTimeJsonSerializer extends JsonSerializer<Long> { | |||
@Override | |||
public void serialize(Long value, JsonGenerator gen, | |||
SerializerProvider serializers) | |||
throws IOException, JsonProcessingException { | |||
throws IOException { | |||
gen.writeString(DF.format(value * 1000)); | |||
} | |||
} |
@@ -21,7 +21,7 @@ public class WxMpUserGsonAdapter implements JsonDeserializer<WxMpUser> { | |||
WxMpUser wxMpUser = new WxMpUser(); | |||
Integer subscribe = GsonHelper.getInteger(o, "subscribe"); | |||
if (subscribe != null) { | |||
wxMpUser.setSubscribe(new Integer(0).equals(subscribe) ? false : true); | |||
wxMpUser.setSubscribe(!new Integer(0).equals(subscribe)); | |||
} | |||
wxMpUser.setCity(GsonHelper.getString(o, "city")); | |||
wxMpUser.setCountry(GsonHelper.getString(o, "country")); | |||
@@ -49,7 +49,6 @@ public class WxMpMessageRouterTest { | |||
} | |||
}).handler(new WxEchoMpMessageHandler(sb, "matcher")).end() | |||
.rule().async(async).handler(new WxEchoMpMessageHandler(sb, "ALL")).end(); | |||
; | |||
} | |||
@Test(dataProvider="messages-1") | |||
@@ -0,0 +1,93 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.bean.WxCardApiSignature; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
import static org.testng.AssertJUnit.assertNotNull; | |||
/** | |||
* 测试代码仅供参考,未做严格测试,因原接口作者并未提供单元测试代码 | |||
* Created by Binary Wang on 2016/7/27. | |||
* @author binarywang (https://github.com/binarywang) | |||
*/ | |||
@Test | |||
@Guice(modules = ApiTestModule.class) | |||
public class WxMpCardServiceImplTest { | |||
@Inject | |||
protected WxMpServiceImpl wxService; | |||
private String cardId = "123"; | |||
private String code = "good"; | |||
private String openid = "abc"; | |||
@Test | |||
public void testGetCardApiTicket() throws Exception { | |||
String cardApiTicket = this.wxService.getCardService().getCardApiTicket(); | |||
assertNotNull(cardApiTicket); | |||
System.out.println(cardApiTicket); | |||
} | |||
@Test | |||
public void testGetCardApiTicketWithParam() throws Exception { | |||
String cardApiTicket = this.wxService.getCardService().getCardApiTicket(true); | |||
assertNotNull(cardApiTicket); | |||
System.out.println(cardApiTicket); | |||
} | |||
@Test | |||
public void testCreateCardApiSignature() throws Exception { | |||
//app_id, card_id, card_type, code, openid, location_id | |||
String[] param = {"123", cardId, "", code, openid, ""}; | |||
WxCardApiSignature cardApiSignature = this.wxService.getCardService().createCardApiSignature(param); | |||
assertNotNull(cardApiSignature); | |||
System.out.println(cardApiSignature); | |||
} | |||
@Test | |||
public void testDecryptCardCode() throws Exception { | |||
String encryptCode = "pd0vTUHSHc9tMUCL2gXABcUmINm6yxqJh0y9Phsy63E="; | |||
String cardCode = this.wxService.getCardService().decryptCardCode(encryptCode); | |||
assertNotNull(cardCode); | |||
System.out.println(cardCode); | |||
} | |||
@Test | |||
public void testQueryCardCode() throws Exception { | |||
WxMpCardResult wxMpCardResult = this.wxService.getCardService().queryCardCode(cardId, code, false); | |||
assertNotNull(wxMpCardResult); | |||
System.out.println(wxMpCardResult); | |||
} | |||
@Test | |||
public void testConsumeCardCode() throws Exception { | |||
String result = this.wxService.getCardService().consumeCardCode(code); | |||
assertNotNull(result); | |||
System.out.println(result); | |||
} | |||
@Test | |||
public void testConsumeCardCodeWithCardId() throws Exception { | |||
String result = this.wxService.getCardService().consumeCardCode(code, cardId); | |||
assertNotNull(result); | |||
System.out.println(result); | |||
} | |||
@Test | |||
public void testMarkCardCode() throws Exception { | |||
this.wxService.getCardService().markCardCode(code, cardId, openid, true); | |||
System.out.println("done"); | |||
} | |||
@Test | |||
public void testGetCardDetail() throws Exception { | |||
String result = this.wxService.getCardService().getCardDetail(cardId); | |||
assertNotNull(result); | |||
System.out.println(result); | |||
} | |||
} |
@@ -0,0 +1,60 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import org.testng.annotations.Test; | |||
/** | |||
* Created by Binary Wang on 2016/7/28. | |||
* @author binarywang (https://github.com/binarywang) | |||
*/ | |||
public class WxMpPayServiceImplTest { | |||
@Test | |||
public void testGetPrepayId() throws Exception { | |||
} | |||
@Test | |||
public void testGetPrepayId1() throws Exception { | |||
} | |||
@Test | |||
public void testGetJsapiPayInfo() throws Exception { | |||
} | |||
@Test | |||
public void testGetNativePayInfo() throws Exception { | |||
} | |||
@Test | |||
public void testGetPayInfo() throws Exception { | |||
} | |||
@Test | |||
public void testGetJSSDKPayResult() throws Exception { | |||
} | |||
@Test | |||
public void testGetJSSDKCallbackData() throws Exception { | |||
} | |||
@Test | |||
public void testRefundPay() throws Exception { | |||
} | |||
@Test | |||
public void testCheckJSSDKCallbackDataSignature() throws Exception { | |||
} | |||
@Test | |||
public void testSendRedpack() throws Exception { | |||
} | |||
} |