View Javadoc
1   package com.foxinmy.weixin4j.pay.payment.mch;
2   
3   import com.alibaba.fastjson.annotation.JSONField;
4   import com.foxinmy.weixin4j.pay.type.mch.RedpacketSceneType;
5   import com.foxinmy.weixin4j.util.DateUtil;
6   
7   import javax.xml.bind.annotation.XmlAccessType;
8   import javax.xml.bind.annotation.XmlAccessorType;
9   import javax.xml.bind.annotation.XmlElement;
10  import javax.xml.bind.annotation.XmlRootElement;
11  
12  /**
13   * 红包
14   *
15   * @className Redpacket
16   * @author jinyu(foxinmy@gmail.com)
17   * @date 2015年3月28日
18   * @since JDK 1.6
19   * @see <a
20   *      href="https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_1">普通红包</a>
21   * @see <a
22   *      href="https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=16_1">裂变红包</a>
23   */
24  @XmlRootElement
25  @XmlAccessorType(XmlAccessType.FIELD)
26  public class Redpacket extends MerchantResult {
27  
28  	private static final long serialVersionUID = -7021352305575714281L;
29  
30  	/**
31  	 * 商户订单号(每个订单号必须唯一) 组成: mch_id+yyyymmdd+10位一天内不能重复的数字。
32  	 */
33  	@XmlElement(name = "mch_billno")
34  	@JSONField(name = "mch_billno")
35  	private String outTradeNo;
36  	/**
37  	 * 接受收红包的用户的openid 必填
38  	 */
39  	@XmlElement(name = "re_openid")
40  	@JSONField(name = "re_openid")
41  	private String openId;
42  	/**
43  	 * 红包发送者名称 必填
44  	 */
45  	@XmlElement(name = "send_name")
46  	@JSONField(name = "send_name")
47  	private String sendName;
48  	/**
49  	 * 付款金额,单位分
50  	 */
51  	@XmlElement(name = "total_amount")
52  	@JSONField(name = "total_amount")
53  	private int totalAmount;
54  	/**
55  	 * 红包发放总人数
56  	 */
57  	@XmlElement(name = "total_num")
58  	@JSONField(name = "total_num")
59  	private int totalNum;
60  	/**
61  	 * 红包金额设置方式(裂变红包) ALL_RAND—全部随机,商户指定总金额和红包发放总人数,由微信支付随机计算出各红包金额
62  	 */
63  	@XmlElement(name = "amt_type")
64  	@JSONField(name = "amt_type")
65  	private String amtType;
66  	/**
67  	 * 红包祝福语
68  	 */
69  	private String wishing;
70  	/**
71  	 * ip地址
72  	 */
73  	@XmlElement(name = "client_ip")
74  	@JSONField(name = "client_ip")
75  	private String clientIp;
76  	/**
77  	 * 活动名称
78  	 */
79  	@XmlElement(name = "act_name")
80  	@JSONField(name = "act_name")
81  	private String actName;
82  	/**
83  	 * 备注
84  	 */
85  	private String remark;
86  
87  	/**
88  	 * 服务商模式下触达用户时的appid(可填服务商自己的appid或子商户的appid),服务商模式下必填,
89  	 * 服务商模式下填入的子商户appid必须在微信支付商户平台中先录入,否则会校验不过。 非必须
90  	 */
91  	@XmlElement(name = "msgappid")
92  	@JSONField(name = "msgappid")
93  	private String msgAppId;
94  	/**
95  	 * 扣钱方mchid,常规模式下无效,服务商模式下选填,服务商模式下不填默认扣子商户的钱.非必须
96  	 */
97  	@XmlElement(name = "consume_mch_id")
98  	@JSONField(name = "consume_mch_id")
99  	private String consumeMchId;
100 	/**
101 	 * 发放红包使用场景,红包金额大于200时必传
102 	 */
103 	@XmlElement(name = "scene_id")
104 	@JSONField(name = "scene_id")
105 	private RedpacketSceneType sceneType;
106 	/**
107 	 * 活动信息
108 	 */
109 	@XmlElement(name = "risk_info")
110 	@JSONField(name = "risk_info")
111 	private String risk;
112 
113 	protected Redpacket() {
114 		// jaxb required
115 	}
116 
117 	/**
118 	 * 红包
119 	 *
120 	 * @param outTradeNo
121 	 *            商户侧一天内不可重复的订单号 接口根据商户订单号支持重入 如出现超时可再调用 必填
122 	 * @param openId
123 	 *            接受收红包的用户的openid 必填
124 	 * @param sendName
125 	 *            红包发送者名称 必填
126 	 * @param totalAmount
127 	 *            付款金额 <font color="red">单位为元,自动格式化为分</font> 必填
128 	 * @param totalNum
129 	 *            红包发放总人数 大于1视为裂变红包 必填
130 	 * @param wishing
131 	 *            红包祝福语 必填
132 	 * @param clientIp
133 	 *            Ip地址 必填
134 	 * @param actName
135 	 *            活动名称 必填
136 	 * @param remark
137 	 *            备注 必填
138 	 */
139 	public Redpacket(String outTradeNo, String openId, String sendName,
140 			double totalAmount, int totalNum, String wishing, String clientIp,
141 			String actName, String remark) {
142 		this.outTradeNo = outTradeNo;
143 		this.openId = openId;
144 		this.sendName = sendName;
145 		this.totalAmount = DateUtil.formatYuan2Fen(totalAmount);
146 		this.totalNum = totalNum;
147 		this.wishing = wishing;
148 		this.clientIp = clientIp;
149 		this.actName = actName;
150 		this.remark = remark;
151 		this.amtType = totalNum > 1 ? "ALL_RAND" : null;
152 	}
153 
154 	/**
155 	 * 批量发送时可能需要
156 	 * 
157 	 * @param outTradeNo
158 	 *            订单号
159 	 * @param openId
160 	 *            用户ID
161 	 * @return 红包实体
162 	 */
163 	public Redpacket copy(String outTradeNo, String openId) {
164 		Redpacket readpacket = new Redpacket(outTradeNo, openId, sendName,
165 				totalAmount, totalNum, wishing, clientIp, actName, remark);
166 		readpacket.setMsgAppId(msgAppId);
167 		readpacket.setConsumeMchId(consumeMchId);
168 		readpacket.setSceneType(sceneType);
169 		readpacket.setRisk(risk);
170 		return readpacket;
171 	}
172 
173 	public String getOutTradeNo() {
174 		return outTradeNo;
175 	}
176 
177 	public void setOutTradeNo(String outTradeNo) {
178 		this.outTradeNo = outTradeNo;
179 	}
180 
181 	public String getOpenId() {
182 		return openId;
183 	}
184 
185 	public void setOpenId(String openId) {
186 		this.openId = openId;
187 	}
188 
189 	public String getSendName() {
190 		return sendName;
191 	}
192 
193 	public void setSendName(String sendName) {
194 		this.sendName = sendName;
195 	}
196 
197 	public int getTotalAmount() {
198 		return totalAmount;
199 	}
200 
201 	public void setTotalAmount(int totalAmount) {
202 		this.totalAmount = totalAmount;
203 	}
204 
205 	/**
206 	 * <font color="red">调用接口获取单位为分,get方法转换为元方便使用</font>
207 	 * 
208 	 * @return 元单位
209 	 */
210 	@JSONField(serialize = false)
211 	public double getFormatTotalAmount() {
212 		return totalAmount / 100d;
213 	}
214 
215 	public int getTotalNum() {
216 		return totalNum;
217 	}
218 
219 	public void setTotalNum(int totalNum) {
220 		this.totalNum = totalNum;
221 	}
222 
223 	public String getWishing() {
224 		return wishing;
225 	}
226 
227 	public void setWishing(String wishing) {
228 		this.wishing = wishing;
229 	}
230 
231 	public String getAmtType() {
232 		return amtType;
233 	}
234 
235 	public void setAmtType(String amtType) {
236 		this.amtType = amtType;
237 	}
238 
239 	public String getClientIp() {
240 		return clientIp;
241 	}
242 
243 	public void setClientIp(String clientIp) {
244 		this.clientIp = clientIp;
245 	}
246 
247 	public String getActName() {
248 		return actName;
249 	}
250 
251 	public void setActName(String actName) {
252 		this.actName = actName;
253 	}
254 
255 	public String getRemark() {
256 		return remark;
257 	}
258 
259 	public void setRemark(String remark) {
260 		this.remark = remark;
261 	}
262 
263 	public String getMsgAppId() {
264 		return msgAppId;
265 	}
266 
267 	public void setMsgAppId(String msgAppId) {
268 		this.msgAppId = msgAppId;
269 	}
270 
271 	public String getConsumeMchId() {
272 		return consumeMchId;
273 	}
274 
275 	public void setConsumeMchId(String consumeMchId) {
276 		this.consumeMchId = consumeMchId;
277 	}
278 
279 	public RedpacketSceneType getSceneType() {
280 		return sceneType;
281 	}
282 
283 	public void setSceneType(RedpacketSceneType sceneType) {
284 		this.sceneType = sceneType;
285 	}
286 
287 	public String getRisk() {
288 		return risk;
289 	}
290 
291 	public void setRisk(String risk) {
292 		this.risk = risk;
293 	}
294 
295 	public void setRisk(RedpacketRisk risk) {
296 		this.risk = risk.toContent();
297 	}
298 
299 	@Override
300 	public String toString() {
301 		return "Redpacket [outTradeNo=" + outTradeNo + ", openId=" + openId
302 				+ ", sendName=" + sendName + ", totalAmount=" + totalAmount
303 				+ ", totalNum=" + totalNum + ", amtType=" + amtType
304 				+ ", wishing=" + wishing + ", clientIp=" + clientIp
305 				+ ", actName=" + actName + ", remark=" + remark + ", msgAppId="
306 				+ msgAppId + ", consumeMchId=" + consumeMchId + ", sceneType="
307 				+ sceneType + ", risk=" + risk + ", " + super.toString() + "]";
308 	}
309 }