微信紅包的使用已經(jīng)很廣泛,本篇文章介紹了微信發(fā)紅包的實(shí)例,需要有認(rèn)證的公眾號(hào),且開通了微信支付,商戶平臺(tái)且開通了現(xiàn)金紅包的權(quán)限即可。
https://pay.weixin.qq.com商戶登陸地址。選擇查看營(yíng)銷中心的現(xiàn)金紅包
https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_1 現(xiàn)金紅包的官網(wǎng)文檔說(shuō)明
先看幾個(gè)圖 簡(jiǎn)單的測(cè)試。前提需要你去商戶平臺(tái)先充值。不支持預(yù)支付。本文只是總結(jié)微信現(xiàn)金紅包接口的調(diào)用與實(shí)現(xiàn)。具體要根據(jù)自己的業(yè)務(wù)去實(shí)現(xiàn)如何調(diào)用該接口。
https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3 文檔中普通紅包有關(guān)于所有的講解。 調(diào)用必須有商戶平臺(tái)的證書。
需要的參數(shù)也都有列出。根據(jù)自己需求來(lái)決定。
1.java封裝一個(gè)紅包對(duì)象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/** * 紅包對(duì)象 * @author 小帥帥丶 * @date 2016-8-17上午11:12:19 * @開源中國(guó) http://my.oschina.net/xshuai */ public class RedPack implements Serializable{ private String sign; //根據(jù)屬性生成的驗(yàn)證 private String mch_billno; //訂單號(hào) private String mch_id; //商戶號(hào) private String wxappid; // 微信appid private String send_name; // 商戶名稱 private String re_openid; // 用戶openid private String total_amount; // 付款金額 private String total_num; //紅包接收人數(shù) 現(xiàn)金紅包只能是 1 private String wishing; // 紅包祝福語(yǔ) private String client_ip; // 調(diào)用接口機(jī)器的IP private String act_name; // 活動(dòng)名稱 private String remark; // 備注 private String nonce_str; // 隨機(jī)字符串 //set get省略 } |
2.需要用的工具類 createBillNo是生成商戶訂單號(hào) 官網(wǎng)文檔要求如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/** * 紅包工具類 * @author 小帥帥丶 * @date 2016-8-17上午11:12:19 * @開源中國(guó) http://my.oschina.net/xshuai */ public class RedPackUtil { /** * 生成商戶訂單號(hào) * @param mch_id 商戶號(hào) * @param userId 該用戶的userID * @return */ public static String createBillNo(){ //組成: mch_id+yyyymmdd+10位一天內(nèi)不能重復(fù)的數(shù)字 //10位一天內(nèi)不能重復(fù)的數(shù)字實(shí)現(xiàn)方法如下: //因?yàn)槊總€(gè)用戶綁定了userId,他們的userId不同,加上隨機(jī)生成的(10-length(userId))可保證這10位數(shù)字不一樣 Date dt= new Date(); SimpleDateFormat df = new SimpleDateFormat( "yyyymmdd" ); String nowTime= df.format(dt); int length = 10 ; return WXConstants.MCH_ID + nowTime + getRandomNum(length); } /** * 生成特定位數(shù)的隨機(jī)數(shù)字 * @param length * @return */ public static String getRandomNum( int length) { String val = "" ; Random random = new Random(); for ( int i = 0 ; i < length; i++) { val += String.valueOf(random.nextInt( 10 )); } return val; } } |
3.前面工作很簡(jiǎn)單需要的證書和商戶號(hào)有。且商戶平臺(tái)有金額即可測(cè)試現(xiàn)金紅包接口
1
2
3
4
|
RedPack pack = new RedPack( null //第一次為空, RedPackUtil.createBillNo()//商戶訂單號(hào), "你自己的商戶號(hào)" , "公眾號(hào)的appid" , "名稱" , "要發(fā)送用戶的openid" , "發(fā)送金額 單位是分 例如100 則是1元RMB" , "只能是1" , "9" , "127.0.0.1" , "活動(dòng)名稱" , "備注" , "隨機(jī)字符串" ); |
測(cè)試中除了sign為空。其他都可以填充。現(xiàn)在我們生成sign簽名;根據(jù)pack對(duì)象中的參數(shù)去生成sign;
具體簽名算法https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3 官網(wǎng)給出的地址
https://pay.weixin.qq.com/wiki/tools/signverify/可以在這個(gè)測(cè)試頁(yè)面進(jìn)行對(duì)比看加密后是否一致。
1
2
3
4
5
|
String signs = Signature.getSign(pack); //生成的signset到pack對(duì)象中 pack.setSign(signs); //將對(duì)象轉(zhuǎn)為xml格式 微信要求xml格式 String xml = XmlUtil.objToXml(pack,RedPack. class , "xml" ); |
4.發(fā)送紅包
1
2
|
RedPackService service = new RedPacService(); String result = service.redpackOrder(xml); //請(qǐng)求返回的數(shù)據(jù)是否成功 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
public class RedPackService{ /** * 紅包接口地址 */ private final static String REDP_ORDER_PATH= "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" ; /** * 紅包 * 需要證書 * @param paramXml * @return */ public static String redpackOrder(String paramXml){ try { WXBaseService service= new WXBaseService(REDP_ORDER_PATH); return service.sendPost(paramXml); } catch (Exception e) { log.error(e.toString()); } return null ; } } /** * 通過(guò)Https往API post xml數(shù)據(jù) * * @param url API地址 * @param xmlObj 要提交的XML數(shù)據(jù)對(duì)象 * @return API回包的實(shí)際數(shù)據(jù) * @throws IOException * @throws KeyStoreException * @throws UnrecoverableKeyException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public String sendPost(String url, String postDataXML) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException { if (!hasInit) { init(); } String result = null ; HttpPost httpPost = new HttpPost(url); //解決XStream對(duì)出現(xiàn)雙下劃線的bug // XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_"))); //將要提交給API的數(shù)據(jù)對(duì)象轉(zhuǎn)換成XML格式數(shù)據(jù)Post給API // String postDataXML = xStreamForRequestPostData.toXML(xmlObj); Util.log( "API,POST過(guò)去的數(shù)據(jù)是:" ); Util.log(postDataXML); //得指明使用UTF-8編碼,否則到API服務(wù)器XML的中文不能被成功識(shí)別 StringEntity postEntity = new StringEntity(postDataXML, "UTF-8" ); httpPost.addHeader( "Content-Type" , "text/xml" ); httpPost.setEntity(postEntity); //設(shè)置請(qǐng)求器的配置 httpPost.setConfig(requestConfig); Util.log( "executing request" + httpPost.getRequestLine()); try { HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity, "UTF-8" ); } catch (ConnectionPoolTimeoutException e) { log.e( "http get throw ConnectionPoolTimeoutException(wait time out)" ); } catch (ConnectTimeoutException e) { log.e( "http get throw ConnectTimeoutException" ); } catch (SocketTimeoutException e) { log.e( "http get throw SocketTimeoutException" ); } catch (Exception e) { log.e( "http get throw Exception" ); } finally { httpPost.abort(); } return result; } |
5.返回的xml看是否成功 由于只充值了1元 前幾天已經(jīng)測(cè)試發(fā)送 所以返回如下信息
1
2
3
4
5
6
7
8
9
10
11
12
|
< xml > < return_code > <![CDATA[SUCCESS]]> </ return_code > < return_msg > <![CDATA[帳號(hào)余額不足,請(qǐng)到商戶平臺(tái)充值后再重試]]> </ return_msg > < result_code > <![CDATA[FAIL]]> </ result_code > < err_code > <![CDATA[NOTENOUGH]]> </ err_code > < err_code_des > <![CDATA[帳號(hào)余額不足,請(qǐng)到商戶平臺(tái)充值后再重試]]> </ err_code_des > < mch_billno > <![CDATA[1371729102201629220149762756]]> </ mch_billno > < mch_id > <![CDATA[這里是商戶號(hào)為了保密刪除了]]> </ mch_id > < wxappid > <![CDATA[微信公眾號(hào)appid]]> </ wxappid > < re_openid > <![CDATA[od5qQw8E_LbiAW9sZzuD-2xHtmvx這個(gè)是用戶的openid]]> </ re_openid > < total_amount >100</ total_amount > </ xml > |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。