Browse Source

#629 修复WxPayOrderNotifyResult解析xml报错问题

master
Binary Wang 6 years ago
parent
commit
a10007a68e
2 changed files with 21 additions and 17 deletions
  1. +17
    -13
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/converter/WxPayOrderNotifyResultConverter.java
  2. +4
    -4
      weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java

+ 17
- 13
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/converter/WxPayOrderNotifyResultConverter.java View File

@@ -3,6 +3,7 @@ package com.github.binarywang.wxpay.converter;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyCoupon;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.converters.MarshallingContext;
@@ -21,6 +22,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @author aimilin
*/
public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter {

public WxPayOrderNotifyResultConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
@@ -72,26 +76,26 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter
fields.addAll(Arrays.asList(obj.getClass().getSuperclass().getDeclaredFields()));
Map<String, Field> fieldMap = getFieldMap(fields);

List<WxPayOrderNotifyCoupon> coupons = new ArrayList<>(10);
Map<Integer, WxPayOrderNotifyCoupon> coupons = Maps.newTreeMap();
while (reader.hasMoreChildren()) {
reader.moveDown();
if (fieldMap.containsKey(reader.getNodeName())) {
Field field = fieldMap.get(reader.getNodeName());
setFieldValue(context, obj, field);
this.setFieldValue(context, obj, field);
} else if (StringUtils.startsWith(reader.getNodeName(), "coupon_id_")) {
String id = (String) context.convertAnother(obj, String.class);
getIndex(coupons, reader.getNodeName()).setCouponId(id);
this.getElement(coupons, reader.getNodeName()).setCouponId(id);
} else if (StringUtils.startsWith(reader.getNodeName(), "coupon_type_")) {
String type = (String) context.convertAnother(obj, String.class);
getIndex(coupons, reader.getNodeName()).setCouponType(type);
this.getElement(coupons, reader.getNodeName()).setCouponType(type);
} else if (StringUtils.startsWith(reader.getNodeName(), "coupon_fee_")) {
Integer fee = (Integer) context.convertAnother(obj, Integer.class);
getIndex(coupons, reader.getNodeName()).setCouponFee(fee);
this.getElement(coupons, reader.getNodeName()).setCouponFee(fee);
}
reader.moveUp();
}

obj.setCouponList(coupons);
obj.setCouponList(Lists.newArrayList(coupons.values()));
return obj;
}

@@ -102,12 +106,12 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), obj.getClass());
pd.getWriteMethod().invoke(obj, val);
}
} catch (Exception e) {
} catch (Exception ignored) {
}
}

private Map<String, Field> getFieldMap(List<Field> fields) {
Map<String, Field> fieldMap = Maps.uniqueIndex(fields, new Function<Field, String>() {
return Maps.uniqueIndex(fields, new Function<Field, String>() {
@Override
public String apply(Field field) {
if (field.isAnnotationPresent(XStreamAlias.class)) {
@@ -116,14 +120,14 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter
return field.getName();
}
});
return fieldMap;
}

private WxPayOrderNotifyCoupon getIndex(List<WxPayOrderNotifyCoupon> coupons, String nodeName) {
Integer index = Integer.valueOf(StringUtils.substring(nodeName, nodeName.lastIndexOf("_") + 1));
if (index >= coupons.size() || coupons.get(index) == null) {
coupons.add(index, new WxPayOrderNotifyCoupon());
private WxPayOrderNotifyCoupon getElement(Map<Integer, WxPayOrderNotifyCoupon> coupons, String nodeName) {
Integer index = Integer.valueOf(StringUtils.substringAfterLast(nodeName, "_"));
if (coupons.get(index) == null) {
coupons.put(index, new WxPayOrderNotifyCoupon());
}

return coupons.get(index);
}
}

+ 4
- 4
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java View File

@@ -12,7 +12,7 @@ import org.testng.annotations.*;
*/
public class WxPayOrderNotifyResultTest {
@Test
public void testFromXML() throws Exception {
public void testFromXML() {
String xmlString = "<xml>\n" +
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
" <attach><![CDATA[支付测试]]></attach>\n" +
@@ -32,12 +32,12 @@ public class WxPayOrderNotifyResultTest {
" <trade_type><![CDATA[JSAPI]]></trade_type>\n" +
" <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>\n" +
" <coupon_count>2</coupon_count>\n" +
" <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" +
" <coupon_id_0>10000</coupon_id_0>\n" +
" <coupon_fee_0>100</coupon_fee_0>\n" +
" <coupon_type_1><![CDATA[NO_CASH]]></coupon_type_1>\n" +
" <coupon_id_1>10001</coupon_id_1>\n" +
" <coupon_fee_1>200</coupon_fee_1>\n" +
" <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" +
" <coupon_id_0>10000</coupon_id_0>\n" +
" <coupon_fee_0>100</coupon_fee_0>\n" +
"</xml>";

WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlString);


Loading…
Cancel
Save