@@ -53,7 +53,6 @@ | |||
<jedis.version>2.9.0</jedis.version> | |||
<gson.version>2.7</gson.version> | |||
<guava.version>19.0</guava.version> | |||
<joor.version>0.9.6</joor.version> | |||
<commons-lang3.version>3.4</commons-lang3.version> | |||
<commons-io.version>2.5</commons-io.version> | |||
<commons-codec.version>1.10</commons-codec.version> | |||
@@ -113,10 +112,6 @@ | |||
<groupId>com.google.guava</groupId> | |||
<artifactId>guava</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.jooq</groupId> | |||
<artifactId>joor</artifactId> | |||
</dependency> | |||
</dependencies> | |||
<dependencyManagement> | |||
@@ -162,11 +157,6 @@ | |||
<artifactId>guava</artifactId> | |||
<version>${guava.version}</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.jooq</groupId> | |||
<artifactId>joor</artifactId> | |||
<version>${joor.version}</version> | |||
</dependency> | |||
</dependencies> | |||
</dependencyManagement> | |||
@@ -0,0 +1,82 @@ | |||
package me.chanjar.weixin.common.util; | |||
import com.google.common.collect.Lists; | |||
import com.google.common.collect.Maps; | |||
import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
import me.chanjar.weixin.common.annotation.Required; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import java.lang.reflect.Field; | |||
import java.util.List; | |||
import java.util.Map; | |||
/** | |||
* <pre> | |||
* bean操作的一些工具类 | |||
* Created by Binary Wang on 2016-10-21. | |||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
* </pre> | |||
*/ | |||
public class BeanUtils { | |||
/** | |||
* 检查bean里标记为@Required的field是否为空,为空则抛异常 | |||
* @param bean 要检查的bean对象 | |||
* @throws WxErrorException | |||
*/ | |||
public static void checkRequiredFields(Object bean) throws WxErrorException { | |||
List<String> nullFields = Lists.newArrayList(); | |||
for (Field field : bean.getClass().getDeclaredFields()) { | |||
try { | |||
boolean isAccessible = field.isAccessible(); | |||
field.setAccessible(true); | |||
if (field.isAnnotationPresent(Required.class) | |||
&& field.get(bean) == null) { | |||
nullFields.add(field.getName()); | |||
} | |||
field.setAccessible(isAccessible); | |||
} catch (SecurityException | IllegalArgumentException | |||
| IllegalAccessException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
if (!nullFields.isEmpty()) { | |||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("必填字段 " + nullFields + " 必须提供值").build()); | |||
} | |||
} | |||
/** | |||
* 将bean按照@XStreamAlias标识的字符串内容生成以之为key的map对象 | |||
* @param bean 包含@XStreamAlias的xml bean对象 | |||
* @return map对象 | |||
*/ | |||
public static Map<String, String> xmlBean2Map(Object bean) { | |||
Map<String, String> result = Maps.newHashMap(); | |||
for (Field field : bean.getClass().getDeclaredFields()) { | |||
try { | |||
boolean isAccessible = field.isAccessible(); | |||
field.setAccessible(true); | |||
if (field.get(bean) == null) { | |||
field.setAccessible(isAccessible); | |||
continue; | |||
} | |||
if (field.isAnnotationPresent(XStreamAlias.class)) { | |||
result.put(field.getAnnotation(XStreamAlias.class).value(), | |||
field.get(bean).toString()); | |||
} | |||
field.setAccessible(isAccessible); | |||
} catch (SecurityException | IllegalArgumentException | |||
| IllegalAccessException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
return result; | |||
} | |||
} |
@@ -1,12 +1,9 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import com.google.common.collect.Lists; | |||
import com.google.common.collect.Maps; | |||
import com.thoughtworks.xstream.XStream; | |||
import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
import me.chanjar.weixin.common.annotation.Required; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.util.BeanUtils; | |||
import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
import me.chanjar.weixin.mp.api.WxMpPayService; | |||
import me.chanjar.weixin.mp.api.WxMpService; | |||
@@ -23,15 +20,12 @@ import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.apache.http.impl.client.HttpClients; | |||
import org.apache.http.ssl.SSLContexts; | |||
import org.apache.http.util.EntityUtils; | |||
import org.joor.Reflect; | |||
import javax.net.ssl.SSLContext; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
import java.lang.reflect.Field; | |||
import java.security.KeyStore; | |||
import java.util.*; | |||
import java.util.Map.Entry; | |||
/** | |||
* Created by Binary Wang on 2016/7/28. | |||
@@ -116,7 +110,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
request.setMchId(partnerId); | |||
request.setNonceStr( System.currentTimeMillis() + ""); | |||
request.setOpUserId(partnerId); | |||
String sign = this.createSign(this.xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
String sign = this.createSign(BeanUtils.xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
request.setSign(sign); | |||
String url = PAY_BASE_URL + "/secapi/pay/refund"; | |||
@@ -138,8 +132,8 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
return wxMpPayRefundResult; | |||
} | |||
private void checkParameters(WxMpPayRefundRequest request) { | |||
checkNotNullParams(request); | |||
private void checkParameters(WxMpPayRefundRequest request) throws WxErrorException { | |||
BeanUtils.checkRequiredFields(request); | |||
if (StringUtils.isNotBlank(request.getRefundAccount())) { | |||
if(!ArrayUtils.contains(REFUND_ACCOUNT, request.getRefundAccount())){ | |||
@@ -171,7 +165,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
request.setMchId(mchId); | |||
request.setNonceStr(System.currentTimeMillis() + ""); | |||
String sign = this.createSign(this.xmlBean2Map(request), | |||
String sign = this.createSign(BeanUtils.xmlBean2Map(request), | |||
this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
request.setSign(sign); | |||
@@ -194,29 +188,6 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
return redpackResult; | |||
} | |||
private Map<String, String> xmlBean2Map(Object bean) { | |||
Map<String, String> result = Maps.newHashMap(); | |||
for (Entry<String, Reflect> entry : Reflect.on(bean).fields().entrySet()) { | |||
Reflect reflect = entry.getValue(); | |||
if (reflect.get() == null) { | |||
continue; | |||
} | |||
try { | |||
Field field = bean.getClass().getDeclaredField(entry.getKey()); | |||
if (field.isAnnotationPresent(XStreamAlias.class)) { | |||
result.put(field.getAnnotation(XStreamAlias.class).value(), | |||
reflect.get().toString()); | |||
} | |||
} catch (NoSuchFieldException | SecurityException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
return result; | |||
} | |||
/** | |||
* 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3) | |||
* @param packageParams 原始参数 | |||
@@ -253,7 +224,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
request.setNonceStr(System.currentTimeMillis() + ""); | |||
String sign = this.createSign(this.xmlBean2Map(request), | |||
String sign = this.createSign(BeanUtils.xmlBean2Map(request), | |||
this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
request.setSign(sign); | |||
@@ -271,8 +242,8 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
return result; | |||
} | |||
private void checkParameters(WxUnifiedOrderRequest request) { | |||
checkNotNullParams(request); | |||
private void checkParameters(WxUnifiedOrderRequest request) throws WxErrorException { | |||
BeanUtils.checkRequiredFields(request); | |||
if (! ArrayUtils.contains(TRADE_TYPES, request.getTradeType())) { | |||
throw new IllegalArgumentException("trade_type目前必须为" + Arrays.toString(TRADE_TYPES) + "其中之一"); | |||
@@ -287,27 +258,6 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
} | |||
} | |||
private void checkNotNullParams(Object request) { | |||
List<String> nullFields = Lists.newArrayList(); | |||
for (Entry<String, Reflect> entry : Reflect.on(request).fields() | |||
.entrySet()) { | |||
Reflect reflect = entry.getValue(); | |||
try { | |||
Field field = request.getClass().getDeclaredField(entry.getKey()); | |||
if (field.isAnnotationPresent(Required.class) | |||
&& reflect.get() == null) { | |||
nullFields.add(entry.getKey()); | |||
} | |||
} catch (NoSuchFieldException | SecurityException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
if (!nullFields.isEmpty()) { | |||
throw new IllegalArgumentException("必填字段[" + nullFields + "]必须提供值"); | |||
} | |||
} | |||
@Override | |||
public Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxErrorException { | |||
WxUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request); | |||
@@ -345,7 +295,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
@Override | |||
public WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErrorException { | |||
checkNotNullParams(request); | |||
BeanUtils.checkRequiredFields(request); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.processAnnotations(WxEntPayRequest.class); | |||
@@ -355,7 +305,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
request.setNonceStr(System.currentTimeMillis() + ""); | |||
String sign = this.createSign(xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
String sign = this.createSign(BeanUtils.xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
request.setSign(sign); | |||
String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers"; | |||
@@ -380,7 +330,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
request.setNonceStr(System.currentTimeMillis() + ""); | |||
String sign = this.createSign(xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
String sign = this.createSign(BeanUtils.xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
request.setSign(sign); | |||
String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo"; | |||
@@ -1,23 +1,19 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import com.google.common.collect.Lists; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParser; | |||
import com.google.gson.reflect.TypeToken; | |||
import me.chanjar.weixin.common.annotation.Required; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.util.BeanUtils; | |||
import me.chanjar.weixin.mp.api.WxMpService; | |||
import me.chanjar.weixin.mp.api.WxMpStoreService; | |||
import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo; | |||
import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo; | |||
import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import org.joor.Reflect; | |||
import java.lang.reflect.Field; | |||
import java.util.List; | |||
import java.util.Map.Entry; | |||
/** | |||
* Created by Binary Wang on 2016/9/26. | |||
@@ -35,7 +31,7 @@ public class WxMpStoreServiceImpl implements WxMpStoreService { | |||
@Override | |||
public void add(WxMpStoreBaseInfo request) throws WxErrorException { | |||
checkParameters(request); | |||
BeanUtils.checkRequiredFields(request); | |||
String url = API_BASE_URL + "/addpoi"; | |||
String response = this.wxMpService.post(url, request.toJson()); | |||
@@ -71,28 +67,6 @@ public class WxMpStoreServiceImpl implements WxMpStoreService { | |||
} | |||
} | |||
private void checkParameters(WxMpStoreBaseInfo request) { | |||
List<String> nullFields = Lists.newArrayList(); | |||
for (Entry<String, Reflect> entry : Reflect.on(request).fields() | |||
.entrySet()) { | |||
Reflect reflect = entry.getValue(); | |||
try { | |||
Field field = request.getClass().getDeclaredField(entry.getKey()); | |||
if (field.isAnnotationPresent(Required.class) | |||
&& reflect.get() == null) { | |||
nullFields.add(entry.getKey()); | |||
} | |||
} catch (NoSuchFieldException | SecurityException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
if (!nullFields.isEmpty()) { | |||
throw new IllegalArgumentException("必填字段[" + nullFields + "]必须提供值"); | |||
} | |||
} | |||
@Override | |||
public WxMpStoreListResult list(int begin, int limit) | |||
throws WxErrorException { | |||
@@ -30,6 +30,7 @@ public class WxMpStoreServiceImplTest { | |||
* @throws WxErrorException | |||
*/ | |||
public void testAdd() throws WxErrorException { | |||
this.wxMpService.getStoreService().add(WxMpStoreBaseInfo.builder().build()); | |||
this.wxMpService.getStoreService() | |||
.add(WxMpStoreBaseInfo.builder().businessName("haha").branchName("abc") | |||
.province("aaa").district("aaa").telephone("122").address("abc").categories(new String[] { "美食,江浙菜" }) | |||
@@ -1,33 +0,0 @@ | |||
package me.chanjar.weixin.mp.bean.pay; | |||
import java.lang.reflect.Field; | |||
import java.util.Map.Entry; | |||
import org.joor.Reflect; | |||
import org.testng.annotations.Test; | |||
import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
@Test | |||
public class WxSendRedpackRequestTest { | |||
public void test() throws NoSuchFieldException, SecurityException { | |||
WxSendRedpackRequest request = new WxSendRedpackRequest(); | |||
request.setMchBillno("123"); | |||
request.setActName("ab"); | |||
for (Entry<String, Reflect> entry : Reflect.on(request).fields().entrySet()) { | |||
Reflect reflect = entry.getValue(); | |||
if (reflect.get() == null) { | |||
continue; | |||
} | |||
Field field = WxSendRedpackRequest.class.getDeclaredField(entry.getKey()); | |||
if (field.isAnnotationPresent(XStreamAlias.class)) { | |||
System.err.println(reflect.get() + " = " + field.getAnnotation(XStreamAlias.class).value()); | |||
} | |||
} | |||
} | |||
} |