這幾天看了下之前寫(xiě)的有關(guān)微信支付的博客,看的人還是挺多的,看了下留言不知道是因?yàn)椴┛蛯?xiě)的不夠細(xì)還是什么情況,大多都找我要源碼,我覺(jué)得吧程序員還是需要有這么一個(gè)思考的過(guò)程,因此沒(méi)直接給源碼,俗話(huà)說(shuō)“授人以魚(yú)不如授人以漁”。因此希望看文章的同時(shí)也花一點(diǎn)時(shí)間自己親自敲一敲代碼。好了廢話(huà)不多說(shuō)這次來(lái)分享微信現(xiàn)金紅包接口的使用。
下面是微信開(kāi)發(fā)文檔對(duì)現(xiàn)金紅包的介紹:
現(xiàn)金紅包,是微信支付商戶(hù)平臺(tái)提供的營(yíng)銷(xiāo)工具之一,上線(xiàn)以來(lái)深受廣大商戶(hù)與用戶(hù)的喜愛(ài)。商戶(hù)可以通過(guò)本平臺(tái)向微信支付用戶(hù)發(fā)放現(xiàn)金紅包。用戶(hù)領(lǐng)取紅包后,資金到達(dá)用戶(hù)微信支付零錢(qián)賬戶(hù),和零錢(qián)包的其他資金有一樣的使用出口;若用戶(hù)未領(lǐng)取,資金將會(huì)在24小時(shí)后退回商戶(hù)的微信支付賬戶(hù)中。
產(chǎn)品意義
微信支付現(xiàn)金紅包因資金的承載方式為現(xiàn)金,一直以來(lái)深受用戶(hù)的青睞,近年來(lái)的春晚中,現(xiàn)金紅包都扮演著重要的角色;在日常運(yùn)營(yíng)中也為商戶(hù)的營(yíng)銷(xiāo)活動(dòng)帶來(lái)熱烈的反響。總的來(lái)說(shuō),現(xiàn)金紅包在包括但不僅限于以下場(chǎng)景中發(fā)揮著重要意義:
- ◆ 為企業(yè)拉取新用戶(hù)、鞏固老用戶(hù)關(guān)系、提升用戶(hù)活躍度
- ◆ 結(jié)合巧妙的創(chuàng)意點(diǎn)子,輔以紅包點(diǎn)綴,打造火爆的活動(dòng),提升企業(yè)與品牌知名度
- ◆ 結(jié)合企業(yè)運(yùn)營(yíng)活動(dòng),以紅包作為獎(jiǎng)品,使你的抽獎(jiǎng)、滿(mǎn)送等營(yíng)銷(xiāo)活動(dòng)更便利進(jìn)行
- ◆ 同時(shí),除了營(yíng)銷(xiāo)之外,現(xiàn)金紅包在企業(yè)日常的運(yùn)營(yíng)中也扮演著重要角色。如:為員工返福利、為供應(yīng)商返利、會(huì)員積分/虛擬等級(jí)兌現(xiàn)等等
綜上所述微信現(xiàn)金紅包是一種營(yíng)銷(xiāo)工具,可以通過(guò)關(guān)注公眾號(hào)、注冊(cè)等給用戶(hù)發(fā)放增加用戶(hù)粘性。這次著重從程序開(kāi)發(fā)方面分享我的心得體會(huì)
一 使用微信現(xiàn)金紅包功能需具備的條件
1 擁有微信商戶(hù)平臺(tái)且秘鑰證書(shū)齊全
2 商戶(hù)平太需要有足夠的余額可供使用(不夠可以從商戶(hù)平臺(tái)使用財(cái)付通充值)
3 有微信支付開(kāi)發(fā)基礎(chǔ)更佳
二 開(kāi)發(fā)的重點(diǎn)和難點(diǎn)
1 微信簽名算法
2 httpclient以及證書(shū)的使用
3 微信文檔的閱讀(https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3)
如果有微信h5支付或掃碼支付的童鞋看這一部分的文檔可以說(shuō)是小菜一碟,理解起來(lái)不費(fèi)吹灰之力,同時(shí)只要掌握httpclient的知識(shí)就萬(wàn)事俱備了
三 直接擼代碼
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()); //請(qǐng)求的xml需轉(zhuǎn)碼為iso8859-1編碼,否則易出現(xiàn)簽名錯(cuò)誤或紅包上的文字顯示有誤 StringEntity reqEntity = new StringEntity( new String(reuqestXml.getBytes(), "ISO8859-1" )); // 設(shè)置類(lèi)型 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(); } } |
需要注意的地方是下面這里:
//請(qǐng)求的xml需轉(zhuǎn)碼為iso8859-1編碼,否則易出現(xiàn)簽名錯(cuò)誤或紅包上的文字顯示有誤
1
|
StringEntity reqEntity = new StringEntity( new String(reuqestXml.getBytes(), "ISO8859-1" )); |
這個(gè)地方可以說(shuō)把我弄得差點(diǎn)崩潰了各種試,各種調(diào)試還是抱著試一試的心態(tài)加上去就OK了,這個(gè)可能是因?yàn)閔ttpclient和原生的HttpsConnection在數(shù)據(jù)傳輸上的不同吧。這里沒(méi)做過(guò)多的研究。
調(diào)用這個(gè)方法就更簡(jiǎn)單了直接像下面這樣
1
2
3
4
5
6
7
8
9
|
public static void main(String args[]){ try { sendRedPack( "12828839012016101420" , "接收者的openid" , "xxx" , "100" , "1" , "恭喜發(fā)財(cái),年年有余" , "新年紅包" , "新年紅包還不快搶" , "127.0.0.1" ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
紅包發(fā)送后打印的信息如下:
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[發(fā)放成功]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <err_code><![CDATA[SUCCESS]]></err_code> <err_code_des><![CDATA[發(fā)放成功]]></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> |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。