這幾天看了下之前寫的有關微信支付的博客,看的人還是挺多的,看了下留言不知道是因為博客寫的不夠細還是什么情況,大多都找我要源碼,我覺得吧程序員還是需要有這么一個思考的過程,因此沒直接給源碼,俗話說“授人以魚不如授人以漁”。因此希望看文章的同時也花一點時間自己親自敲一敲代碼。好了廢話不多說這次來分享微信現金紅包接口的使用。
下面是微信開發文檔對現金紅包的介紹:
現金紅包,是微信支付商戶平臺提供的營銷工具之一,上線以來深受廣大商戶與用戶的喜愛。商戶可以通過本平臺向微信支付用戶發放現金紅包。用戶領取紅包后,資金到達用戶微信支付零錢賬戶,和零錢包的其他資金有一樣的使用出口;若用戶未領取,資金將會在24小時后退回商戶的微信支付賬戶中。
產品意義
微信支付現金紅包因資金的承載方式為現金,一直以來深受用戶的青睞,近年來的春晚中,現金紅包都扮演著重要的角色;在日常運營中也為商戶的營銷活動帶來熱烈的反響。總的來說,現金紅包在包括但不僅限于以下場景中發揮著重要意義:
- ◆ 為企業拉取新用戶、鞏固老用戶關系、提升用戶活躍度
- ◆ 結合巧妙的創意點子,輔以紅包點綴,打造火爆的活動,提升企業與品牌知名度
- ◆ 結合企業運營活動,以紅包作為獎品,使你的抽獎、滿送等營銷活動更便利進行
- ◆ 同時,除了營銷之外,現金紅包在企業日常的運營中也扮演著重要角色。如:為員工返福利、為供應商返利、會員積分/虛擬等級兌現等等
綜上所述微信現金紅包是一種營銷工具,可以通過關注公眾號、注冊等給用戶發放增加用戶粘性。這次著重從程序開發方面分享我的心得體會
一 使用微信現金紅包功能需具備的條件
1 擁有微信商戶平臺且秘鑰證書齊全
2 商戶平太需要有足夠的余額可供使用(不夠可以從商戶平臺使用財付通充值)
3 有微信支付開發基礎更佳
二 開發的重點和難點
1 微信簽名算法
2 httpclient以及證書的使用
3 微信文檔的閱讀(https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3)
如果有微信h5支付或掃碼支付的童鞋看這一部分的文檔可以說是小菜一碟,理解起來不費吹灰之力,同時只要掌握httpclient的知識就萬事俱備了
三 直接擼代碼
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
|
public static void sendRedPack(String mch_billno,String openId,String send_name,String total_fee,String total_num,String wishing,String act_name,String remark,String ip) throws Exception{ String non=PayCommonUtil.CreateNoncestr(); SortedMap<Object, Object> p = new TreeMap<Object, Object>(); p.put( "nonce_str" , non); p.put( "mch_billno" , mch_billno); p.put( "mch_id" , ConfigUtil.MCH_ID); p.put( "wxappid" , ConfigUtil.APPID); p.put( "re_openid" , openId); p.put( "total_amount" , total_fee); p.put( "total_num" , "1" ); p.put( "client_ip" , "127.0.0.1" ); p.put( "act_name" ,act_name); p.put( "send_name" , send_name); p.put( "wishing" , wishing); p.put( "remark" ,remark); String sign = PayCommonUtil.createSign( "UTF-8" , p); System.out.println(sign); p.put( "sign" , sign); String reuqestXml = PayCommonUtil.getRequestXml(p); KeyStore keyStore = KeyStore.getInstance( "PKCS12" ); FileInputStream instream = new FileInputStream( new File(ConfigUtil.CERT_PATH)); try { keyStore.load(instream, ConfigUtil.MCH_ID.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, ConfigUtil.MCH_ID.toCharArray()).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null , SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom() .setSSLSocketFactory(sslsf).build(); try { HttpPost httpPost = new HttpPost( "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" );// 退款接口 httpPost.addHeader( "Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" ); System.out.println( "executing request" + httpPost.getRequestLine()); //請求的xml需轉碼為iso8859-1編碼,否則易出現簽名錯誤或紅包上的文字顯示有誤 StringEntity reqEntity = new StringEntity( new String(reuqestXml.getBytes(), "ISO8859-1" )); // 設置類型 httpPost.setEntity(reqEntity); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); System.out.println( "----------------------------------------" ); System.out.println(response.getStatusLine()); if (entity != null ) { System.out.println( "Response content length: " + entity.getContentLength()); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(entity.getContent(), "UTF-8" )); String text; while ((text = bufferedReader.readLine()) != null ) { System.out.println(text); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } } |
需要注意的地方是下面這里:
//請求的xml需轉碼為iso8859-1編碼,否則易出現簽名錯誤或紅包上的文字顯示有誤
1
|
StringEntity reqEntity = new StringEntity( new String(reuqestXml.getBytes(), "ISO8859-1" )); |
這個地方可以說把我弄得差點崩潰了各種試,各種調試還是抱著試一試的心態加上去就OK了,這個可能是因為httpclient和原生的HttpsConnection在數據傳輸上的不同吧。這里沒做過多的研究。
調用這個方法就更簡單了直接像下面這樣
1
2
3
4
5
6
7
8
9
|
public static void main(String args[]){ try { sendRedPack( "12828839012016101420" , "接收者的openid" , "xxx" , "100" , "1" , "恭喜發財,年年有余" , "新年紅包" , "新年紅包還不快搶" , "127.0.0.1" ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
紅包發送后打印的信息如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
TTP/1.1 200 OK Response content length: 567 <xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[發放成功]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <err_code><![CDATA[SUCCESS]]></err_code> <err_code_des><![CDATA[發放成功]]></err_code_des> <mch_billno><![CDATA[12828839012016101421]]></mch_billno> <mch_id><![CDATA[1282883901]]></mch_id> <wxappid><![CDATA[xxxxx]]></wxappid> <re_openid><![CDATA[xxxx]]></re_openid> <total_amount>100</total_amount> <send_listid><![CDATA[1000041701201610143000090813093]]></send_listid> </xml> |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。