Skip to content

Commit 06c73ca

Browse files
committed
添加发送红包的重载方法,准备替代现有的代码
1 parent d3edb44 commit 06c73ca

5 files changed

Lines changed: 234 additions & 26 deletions

File tree

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public WxCryptUtil(String token, String encodingAesKey,
8585
* @param signKey 加密Key(即 商户Key)
8686
* @return 签名字符串
8787
*/
88-
public static String createSign(Map<String, String> packageParams,
89-
String signKey) {
88+
public static String createSignForPay(Map<String, String> packageParams, String signKey) {
9089
SortedMap<String, String> sortedMap = new TreeMap<>();
9190
sortedMap.putAll(packageParams);
9291

‎weixin-java-mp/pom.xml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<project
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4-
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
55
<modelVersion>4.0.0</modelVersion>
66
<parent>
77
<groupId>com.github.binarywang</groupId>
@@ -54,6 +54,11 @@
5454
<version>2.9.4</version>
5555
<scope>test</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.jooq</groupId>
59+
<artifactId>joor</artifactId>
60+
<version>0.9.6</version>
61+
</dependency>
5762
</dependencies>
5863

5964
<build>

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult;
88
import me.chanjar.weixin.mp.bean.pay.WxMpPayResult;
99
import me.chanjar.weixin.mp.bean.pay.WxRedpackResult;
10+
import me.chanjar.weixin.mp.bean.pay.WxSendRedpackRequest;
1011
import me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult;
1112

1213
/**
@@ -138,5 +139,17 @@ public interface WxMpPayService {
138139
* <partnerKey></partnerKey>商户平台设置的API密钥
139140
*
140141
*/
142+
@Deprecated
141143
WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException;
144+
145+
/**
146+
* 发送微信红包给个人用户
147+
* <pre>
148+
* 文档详见:
149+
* 发送普通红包 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
150+
* 发送裂变红包 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4
151+
* </pre>
152+
*/
153+
WxRedpackResult sendRedpack(WxSendRedpackRequest request) throws WxErrorException;
154+
142155
}

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java‎

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.io.IOException;
4+
import java.lang.reflect.Field;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Map.Entry;
8+
import java.util.SortedMap;
9+
import java.util.TreeMap;
10+
11+
import org.apache.http.Consts;
12+
import org.apache.http.HttpHost;
13+
import org.apache.http.client.config.RequestConfig;
14+
import org.apache.http.client.methods.CloseableHttpResponse;
15+
import org.apache.http.client.methods.HttpPost;
16+
import org.apache.http.entity.StringEntity;
17+
import org.joor.Reflect;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.slf4j.helpers.MessageFormatter;
21+
22+
import com.beust.jcommander.internal.Maps;
323
import com.thoughtworks.xstream.XStream;
24+
import com.thoughtworks.xstream.annotations.XStreamAlias;
25+
426
import me.chanjar.weixin.common.bean.result.WxError;
527
import me.chanjar.weixin.common.exception.WxErrorException;
628
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
@@ -11,22 +33,8 @@
1133
import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult;
1234
import me.chanjar.weixin.mp.bean.pay.WxMpPayResult;
1335
import me.chanjar.weixin.mp.bean.pay.WxRedpackResult;
14-
import me.chanjar.weixin.mp.bean.result.*;
15-
import org.apache.http.Consts;
16-
import org.apache.http.HttpHost;
17-
import org.apache.http.client.config.RequestConfig;
18-
import org.apache.http.client.methods.CloseableHttpResponse;
19-
import org.apache.http.client.methods.HttpPost;
20-
import org.apache.http.entity.StringEntity;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
23-
import org.slf4j.helpers.MessageFormatter;
24-
25-
import java.io.IOException;
26-
import java.util.HashMap;
27-
import java.util.Map;
28-
import java.util.SortedMap;
29-
import java.util.TreeMap;
36+
import me.chanjar.weixin.mp.bean.pay.WxSendRedpackRequest;
37+
import me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult;
3038

3139
/**
3240
* Created by Binary Wang on 2016/7/28.
@@ -77,7 +85,7 @@ public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
7785
packageParams.put("nonce_str", System.currentTimeMillis() + "");
7886
checkParameters(packageParams);
7987

80-
String sign = WxCryptUtil.createSign(packageParams,
88+
String sign = WxCryptUtil.createSignForPay(packageParams,
8189
this.wxMpService.getWxMpConfigStorage().getPartnerKey());
8290
packageParams.put("sign", sign);
8391

@@ -211,7 +219,7 @@ public Map<String, String> getPayInfo(Map<String, String> parameters)
211219
payInfo.put("codeUrl", wxMpPrepayIdResult.getCode_url());
212220
}
213221

214-
String finalSign = WxCryptUtil.createSign(payInfo,
222+
String finalSign = WxCryptUtil.createSignForPay(payInfo,
215223
this.wxMpService.getWxMpConfigStorage().getPartnerKey());
216224
payInfo.put("paySign", finalSign);
217225
return payInfo;
@@ -238,7 +246,7 @@ public WxMpPayResult getJSSDKPayResult(String transactionId,
238246
}
239247

240248
packageParams.put("nonce_str", nonce_str);
241-
packageParams.put("sign", WxCryptUtil.createSign(packageParams,
249+
packageParams.put("sign", WxCryptUtil.createSignForPay(packageParams,
242250
this.wxMpService.getWxMpConfigStorage().getPartnerKey()));
243251

244252
StringBuilder request = new StringBuilder("<xml>");
@@ -295,7 +303,7 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
295303
refundParams.put("nonce_str", System.currentTimeMillis() + "");
296304
refundParams.put("op_user_id",
297305
this.wxMpService.getWxMpConfigStorage().getPartnerId());
298-
String sign = WxCryptUtil.createSign(refundParams,
306+
String sign = WxCryptUtil.createSignForPay(refundParams,
299307
this.wxMpService.getWxMpConfigStorage().getPartnerKey());
300308
refundParams.put("sign", sign);
301309

@@ -354,11 +362,12 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
354362
@Override
355363
public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm,
356364
String signature) {
357-
return signature.equals(WxCryptUtil.createSign(kvm,
365+
return signature.equals(WxCryptUtil.createSignForPay(kvm,
358366
this.wxMpService.getWxMpConfigStorage().getPartnerKey()));
359367
}
360368

361369
@Override
370+
@Deprecated
362371
public WxRedpackResult sendRedpack(Map<String, String> parameters)
363372
throws WxErrorException {
364373
SortedMap<String, String> packageParams = new TreeMap<>(parameters);
@@ -368,7 +377,7 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
368377
this.wxMpService.getWxMpConfigStorage().getPartnerId());
369378
packageParams.put("nonce_str", System.currentTimeMillis() + "");
370379

371-
String sign = WxCryptUtil.createSign(packageParams,
380+
String sign = WxCryptUtil.createSignForPay(packageParams,
372381
this.wxMpService.getWxMpConfigStorage().getPartnerKey());
373382
packageParams.put("sign", sign);
374383

@@ -409,4 +418,49 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
409418
}
410419
}
411420

421+
@Override
422+
public WxRedpackResult sendRedpack(WxSendRedpackRequest request) throws WxErrorException {
423+
XStream xstream = XStreamInitializer.getInstance();
424+
xstream.processAnnotations(WxRedpackResult.class);
425+
426+
request.setWxAppid(this.wxMpService.getWxMpConfigStorage().getAppId());
427+
request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId());
428+
request.setNonceStr(System.currentTimeMillis() + "");
429+
430+
String sign = WxCryptUtil.createSignForPay(xmlBean2Map(request),
431+
this.wxMpService.getWxMpConfigStorage().getPartnerKey());
432+
request.setSign(sign);
433+
434+
String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
435+
if (request.getAmtType() != null) {
436+
//裂变红包
437+
url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";
438+
}
439+
440+
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
441+
return (WxRedpackResult) xstream.fromXML(responseContent);
442+
}
443+
444+
private Map<String, String> xmlBean2Map(Object bean) {
445+
Map<String, String> result = Maps.newHashMap();
446+
for (Entry<String, Reflect> entry : Reflect.on(bean).fields().entrySet()) {
447+
Reflect reflect = entry.getValue();
448+
if (reflect.get() == null) {
449+
continue;
450+
}
451+
452+
try {
453+
Field field = WxSendRedpackRequest.class.getDeclaredField(entry.getKey());
454+
if (field.isAnnotationPresent(XStreamAlias.class)) {
455+
result.put(reflect.get().toString(), field.getAnnotation(XStreamAlias.class).value());
456+
}
457+
} catch (NoSuchFieldException | SecurityException e) {
458+
e.printStackTrace();
459+
}
460+
461+
}
462+
463+
return result;
464+
}
465+
412466
}

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/WxSendRedpackRequest.java‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,141 @@ public class WxSendRedpackRequest {
156156
*/
157157
@XStreamAlias("consume_mch_id")
158158
private String consumeMchId;
159+
160+
public String getMchBillno() {
161+
return mchBillno;
162+
}
163+
164+
public void setMchBillno(String mchBillno) {
165+
this.mchBillno = mchBillno;
166+
}
167+
168+
public String getSendName() {
169+
return sendName;
170+
}
171+
172+
public void setSendName(String sendName) {
173+
this.sendName = sendName;
174+
}
175+
176+
public String getReOpenid() {
177+
return reOpenid;
178+
}
179+
180+
public void setReOpenid(String reOpenid) {
181+
this.reOpenid = reOpenid;
182+
}
183+
184+
public Integer getTotalAmount() {
185+
return totalAmount;
186+
}
187+
188+
public void setTotalAmount(Integer totalAmount) {
189+
this.totalAmount = totalAmount;
190+
}
191+
192+
public Integer getTotalNum() {
193+
return totalNum;
194+
}
195+
196+
public void setTotalNum(Integer totalNum) {
197+
this.totalNum = totalNum;
198+
}
199+
200+
public String getAmtType() {
201+
return amtType;
202+
}
203+
204+
public void setAmtType(String amtType) {
205+
this.amtType = amtType;
206+
}
207+
208+
public String getWishing() {
209+
return wishing;
210+
}
211+
212+
public void setWishing(String wishing) {
213+
this.wishing = wishing;
214+
}
215+
216+
public String getClientIp() {
217+
return clientIp;
218+
}
219+
220+
public void setClientIp(String clientIp) {
221+
this.clientIp = clientIp;
222+
}
223+
224+
public String getActName() {
225+
return actName;
226+
}
227+
228+
public void setActName(String actName) {
229+
this.actName = actName;
230+
}
231+
232+
public String getRemark() {
233+
return remark;
234+
}
235+
236+
public void setRemark(String remark) {
237+
this.remark = remark;
238+
}
239+
240+
public String getWxAppid() {
241+
return wxAppid;
242+
}
243+
244+
public void setWxAppid(String wxAppid) {
245+
this.wxAppid = wxAppid;
246+
}
247+
248+
public String getMchId() {
249+
return mchId;
250+
}
251+
252+
public void setMchId(String mchId) {
253+
this.mchId = mchId;
254+
}
255+
256+
public String getNonceStr() {
257+
return nonceStr;
258+
}
259+
260+
public void setNonceStr(String nonceStr) {
261+
this.nonceStr = nonceStr;
262+
}
263+
264+
public String getSign() {
265+
return sign;
266+
}
267+
268+
public void setSign(String sign) {
269+
this.sign = sign;
270+
}
271+
272+
public String getSceneId() {
273+
return sceneId;
274+
}
275+
276+
public void setSceneId(String sceneId) {
277+
this.sceneId = sceneId;
278+
}
279+
280+
public String getRiskInfo() {
281+
return riskInfo;
282+
}
283+
284+
public void setRiskInfo(String riskInfo) {
285+
this.riskInfo = riskInfo;
286+
}
287+
288+
public String getConsumeMchId() {
289+
return consumeMchId;
290+
}
291+
292+
public void setConsumeMchId(String consumeMchId) {
293+
this.consumeMchId = consumeMchId;
294+
}
295+
159296
}

0 commit comments

Comments
 (0)