Java微信支付-微信紅包
微信紅包的使用已經(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)文檔說明
先看幾個(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ù)自己需求來決定。
1.java封裝一個(gè)紅包對(duì)象
/** * 紅包對(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)文檔要求如下:
/** * 紅包工具類 * @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)金紅包接口
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為空。其他都可以填充?,F(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ì)比看加密后是否一致。
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ā)送紅包
RedPackService service = new RedPacService(); String result = service.redpackOrder(xml);//請(qǐng)求返回的數(shù)據(jù)是否成功
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; } } /** * 通過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過去的數(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ā)送 所以返回如下信息
<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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用位運(yùn)算、值交換等方式反轉(zhuǎn)java字符串的多種方法(四種方法)
這篇文章主要介紹了使用位運(yùn)算、值交換等方式反轉(zhuǎn)java字符串,本文通過四種方式給大家講解,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Hibernate雙向一對(duì)一映射關(guān)系配置代碼實(shí)例
這篇文章主要介紹了Hibernate雙向一對(duì)一映射關(guān)系配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10java管道piped輸入流與輸出流應(yīng)用場(chǎng)景案例分析
這篇文章主要介紹了java管道流PipedInputStream與PipedOutputStream(輸入流與輸出流)的應(yīng)用場(chǎng)景案例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry
這篇文章主要介紹了解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry:問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL
本文主要介紹了Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02MybatisPlus 自動(dòng)填充的實(shí)現(xiàn)
這篇文章主要介紹了MybatisPlus 自動(dòng)填充的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法
這篇文章主要介紹了mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法,需要的朋友可以參考下2018-03-03Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(33)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07