1 package com.foxinmy.weixin4j.pay.api;
2
3 import com.alibaba.fastjson.TypeReference;
4 import com.foxinmy.weixin4j.exception.WeixinException;
5 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
6 import com.foxinmy.weixin4j.pay.model.WeixinPayAccount;
7 import com.foxinmy.weixin4j.pay.payment.coupon.CouponDetail;
8 import com.foxinmy.weixin4j.pay.payment.coupon.CouponResult;
9 import com.foxinmy.weixin4j.pay.payment.coupon.CouponStock;
10 import com.foxinmy.weixin4j.util.StringUtil;
11 import com.foxinmy.weixin4j.xml.XmlStream;
12
13 import java.util.Map;
14
15
16
17
18
19
20
21
22
23
24
25 public class CouponApi extends MchApi {
26
27 public CouponApi(WeixinPayAccount weixinAccount) {
28 super(weixinAccount);
29 }
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public CouponResult sendCoupon(String couponStockId, String partnerTradeNo,
49 String openId, String opUserId) throws WeixinException {
50 Map<String, String> map = createBaseRequestMap(null);
51 map.put("coupon_stock_id", couponStockId);
52 map.put("partner_trade_no", partnerTradeNo);
53 map.put("openid", openId);
54
55 map.put("openid_count", "1");
56
57 if (StringUtil.isBlank(opUserId)) {
58 opUserId = weixinAccount.getMchId();
59 }
60 map.put("op_user_id", opUserId);
61 map.put("version", "1.0");
62 map.put("type", "XML");
63 map.put("sign", weixinSignature.sign(map));
64 String param = XmlStream.map2xml(map);
65 WeixinResponse response = getWeixinSSLExecutor().post(
66 getRequestUri("coupon_send_uri"), param);
67 return response.getAsObject(new TypeReference<CouponResult>() {
68 });
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82 public CouponStock queryCouponStock(String couponStockId)
83 throws WeixinException {
84 Map<String, String> map = createBaseRequestMap(null);
85 map.put("coupon_stock_id", couponStockId);
86 map.put("sign", weixinSignature.sign(map));
87 String param = XmlStream.map2xml(map);
88 WeixinResponse response = weixinExecutor.post(
89 getRequestUri("couponstock_query_uri"), param);
90 return response.getAsObject(new TypeReference<CouponStock>() {
91 });
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public CouponDetail queryCouponDetail(String openId, String couponId,
110 String stockId) throws WeixinException {
111 Map<String, String> map = createBaseRequestMap(null);
112 map.put("openid", openId);
113 map.put("coupon_id", couponId);
114 map.put("stock_id", stockId);
115 map.put("sign", weixinSignature.sign(map));
116 String param = XmlStream.map2xml(map);
117 WeixinResponse response = weixinExecutor.post(
118 getRequestUri("coupondetail_query_uri"), param);
119 return response.getAsObject(new TypeReference<CouponDetail>() {
120 });
121 }
122 }