Skip to content

Commit cbf7811

Browse files
committed
完成订单查询结果对象中coupon的组装逻辑代码,并加入单元测试
1 parent c47b8ec commit cbf7811

File tree

2 files changed

+146
-76
lines changed

2 files changed

+146
-76
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayOrderQueryResult.java

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.collect.Lists;
44
import com.thoughtworks.xstream.annotations.XStreamAlias;
5+
import io.restassured.path.xml.XmlPath;
56

67
import java.util.List;
78

@@ -17,6 +18,7 @@
1718
* <li>示例值
1819
* <li>描述
1920
* </pre>
21+
*
2022
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
2123
*/
2224
@XStreamAlias("xml")
@@ -179,75 +181,6 @@ public class WxPayOrderQueryResult extends WxPayBaseResult {
179181
private Integer couponCount;
180182

181183
private List<Coupon> coupons;
182-
183-
public static class Coupon {
184-
/**
185-
* <pre>代金券类型
186-
* coupon_type_$n
187-
* 否
188-
* String
189-
* CASH
190-
* <li>CASH--充值代金券
191-
* <li>NO_CASH---非充值代金券
192-
* 订单使用代金券时有返回(取值:CASH、NO_CASH)。$n为下标,从0开始编号,举例:coupon_type_$0
193-
* </pre>
194-
*/
195-
private String couponType;
196-
197-
/**
198-
* <pre>代金券ID
199-
* coupon_id_$n
200-
* 否
201-
* String(20)
202-
* 10000
203-
* 代金券ID, $n为下标,从0开始编号
204-
* </pre>
205-
*/
206-
private String couponId;
207-
208-
/**
209-
* <pre>单个代金券支付金额
210-
* coupon_fee_$n
211-
* 否
212-
* Int
213-
* 100
214-
* 单个代金券支付金额, $n为下标,从0开始编号
215-
* </pre>
216-
*/
217-
private Integer couponFee;
218-
219-
public Coupon(String couponType, String couponId, Integer couponFee) {
220-
this.couponType = couponType;
221-
this.couponId = couponId;
222-
this.couponFee = couponFee;
223-
}
224-
225-
public String getCouponType() {
226-
return this.couponType;
227-
}
228-
229-
public void setCouponType(String couponType) {
230-
this.couponType = couponType;
231-
}
232-
233-
public String getCouponId() {
234-
return this.couponId;
235-
}
236-
237-
public void setCouponId(String couponId) {
238-
this.couponId = couponId;
239-
}
240-
241-
public Integer getCouponFee() {
242-
return this.couponFee;
243-
}
244-
245-
public void setCouponFee(Integer couponFee) {
246-
this.couponFee = couponFee;
247-
}
248-
249-
}
250-
251184
/**
252185
* <pre>微信支付订单号
253186
* transaction_id
@@ -259,7 +192,6 @@ public void setCouponFee(Integer couponFee) {
259192
*/
260193
@XStreamAlias("transaction_id")
261194
private String transactionId;
262-
263195
/**
264196
* <pre>商户订单号
265197
* out_trade_no
@@ -271,7 +203,6 @@ public void setCouponFee(Integer couponFee) {
271203
*/
272204
@XStreamAlias("out_trade_no")
273205
private String outTradeNo;
274-
275206
/**
276207
* <pre>附加数据
277208
* attach
@@ -283,7 +214,6 @@ public void setCouponFee(Integer couponFee) {
283214
*/
284215
@XStreamAlias("attach")
285216
private String attach;
286-
287217
/**
288218
* <pre>支付完成时间
289219
* time_end
@@ -295,7 +225,6 @@ public void setCouponFee(Integer couponFee) {
295225
*/
296226
@XStreamAlias("time_end")
297227
private String timeEnd;
298-
299228
/**
300229
* <pre>交易状态描述
301230
* trade_state_desc
@@ -460,10 +389,86 @@ public void setTradeStateDesc(String tradeStateDesc) {
460389
this.tradeStateDesc = tradeStateDesc;
461390
}
462391

463-
public void composeCoupons(String xmlString){
464-
if(this.couponCount != null && this.couponCount > 0 ){
392+
/**
393+
* 通过xml组装coupons属性内容
394+
*/
395+
public void composeCoupons() {
396+
if (this.couponCount != null && this.couponCount > 0) {
465397
this.coupons = Lists.newArrayList();
466-
//TODO 暂时待实现
398+
XmlPath xmlPath = new XmlPath(this.getXmlString());
399+
for (int i = 0; i < this.couponCount; i++){
400+
this.coupons.add(new Coupon(this.getXmlValueIfExists(xmlPath, "xml.coupon_type_" + i, String.class),
401+
this.getXmlValueIfExists(xmlPath, "xml.coupon_id_" + i, String.class),
402+
this.getXmlValueIfExists(xmlPath, "xml.coupon_fee_" + i, Integer.class)));
403+
}
404+
}
405+
}
406+
407+
public static class Coupon {
408+
/**
409+
* <pre>代金券类型
410+
* coupon_type_$n
411+
* 否
412+
* String
413+
* CASH
414+
* <li>CASH--充值代金券
415+
* <li>NO_CASH---非充值代金券
416+
* 订单使用代金券时有返回(取值:CASH、NO_CASH)。$n为下标,从0开始编号,举例:coupon_type_$0
417+
* </pre>
418+
*/
419+
private String couponType;
420+
421+
/**
422+
* <pre>代金券ID
423+
* coupon_id_$n
424+
* 否
425+
* String(20)
426+
* 10000
427+
* 代金券ID, $n为下标,从0开始编号
428+
* </pre>
429+
*/
430+
private String couponId;
431+
432+
/**
433+
* <pre>单个代金券支付金额
434+
* coupon_fee_$n
435+
* 否
436+
* Int
437+
* 100
438+
* 单个代金券支付金额, $n为下标,从0开始编号
439+
* </pre>
440+
*/
441+
private Integer couponFee;
442+
443+
public Coupon(String couponType, String couponId, Integer couponFee) {
444+
this.couponType = couponType;
445+
this.couponId = couponId;
446+
this.couponFee = couponFee;
447+
}
448+
449+
public String getCouponType() {
450+
return this.couponType;
451+
}
452+
453+
public void setCouponType(String couponType) {
454+
this.couponType = couponType;
467455
}
456+
457+
public String getCouponId() {
458+
return this.couponId;
459+
}
460+
461+
public void setCouponId(String couponId) {
462+
this.couponId = couponId;
463+
}
464+
465+
public Integer getCouponFee() {
466+
return this.couponFee;
467+
}
468+
469+
public void setCouponFee(Integer couponFee) {
470+
this.couponFee = couponFee;
471+
}
472+
468473
}
469474
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package me.chanjar.weixin.mp.bean.pay.result;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
/**
7+
* <pre>
8+
* Created by Binary Wang on 2017-01-04.
9+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
10+
* </pre>
11+
*/
12+
public class WxPayOrderQueryResultTest {
13+
@Test
14+
public void testComposeCoupons() throws Exception {
15+
/**
16+
* xml样例字符串来自于官方文档,并稍加改造加入了coupon相关的数据便于测试
17+
*/
18+
String xmlString = "<xml>\n" +
19+
" <return_code><![CDATA[SUCCESS]]></return_code>\n" +
20+
" <return_msg><![CDATA[OK]]></return_msg>\n" +
21+
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
22+
" <mch_id><![CDATA[10000100]]></mch_id>\n" +
23+
" <device_info><![CDATA[1000]]></device_info>\n" +
24+
" <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>\n" +
25+
" <sign><![CDATA[BDF0099C15FF7BC6B1585FBB110AB635]]></sign>\n" +
26+
" <result_code><![CDATA[SUCCESS]]></result_code>\n" +
27+
" <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid>\n" +
28+
" <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
29+
" <trade_type><![CDATA[MICROPAY]]></trade_type>\n" +
30+
" <bank_type><![CDATA[CCB_DEBIT]]></bank_type>\n" +
31+
" <total_fee>1</total_fee>\n" +
32+
" <fee_type><![CDATA[CNY]]></fee_type>\n" +
33+
" <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>\n" +
34+
" <out_trade_no><![CDATA[1415757673]]></out_trade_no>\n" +
35+
" <attach><![CDATA[订单额外描述]]></attach>\n" +
36+
" <time_end><![CDATA[20141111170043]]></time_end>\n" +
37+
" <trade_state><![CDATA[SUCCESS]]></trade_state>\n" +
38+
" <coupon_count>2</coupon_count>\n" +
39+
" <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" +
40+
" <coupon_id_0>10000</coupon_id_0>\n" +
41+
" <coupon_fee_0>100</coupon_fee_0>\n" +
42+
" <coupon_type_1><![CDATA[NO_CASH]]></coupon_type_1>\n" +
43+
" <coupon_id_1>10001</coupon_id_1>\n" +
44+
" <coupon_fee_1>200</coupon_fee_1>\n" +
45+
"</xml>";
46+
47+
WxPayOrderQueryResult orderQueryResult = WxPayOrderQueryResult.fromXML(xmlString, WxPayOrderQueryResult.class);
48+
orderQueryResult.composeCoupons();
49+
50+
Assert.assertEquals(orderQueryResult.getCouponCount().intValue(), 2);
51+
Assert.assertNotNull(orderQueryResult.getCoupons());
52+
Assert.assertEquals(orderQueryResult.getCoupons().size(), 2);
53+
54+
Assert.assertEquals(orderQueryResult.getCoupons().get(0).getCouponFee().intValue(), 100);
55+
Assert.assertEquals(orderQueryResult.getCoupons().get(1).getCouponFee().intValue(), 200);
56+
57+
Assert.assertEquals(orderQueryResult.getCoupons().get(0).getCouponType(), "CASH");
58+
Assert.assertEquals(orderQueryResult.getCoupons().get(1).getCouponType(), "NO_CASH");
59+
60+
Assert.assertEquals(orderQueryResult.getCoupons().get(0).getCouponId(), "10000");
61+
Assert.assertEquals(orderQueryResult.getCoupons().get(1).getCouponId(), "10001");
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)