まず、公式アカウントが発行した現金赤い封筒の効果を見てみましょう。
マーチャントプラットフォームのインターフェイスを呼び出す必要があります。インターフェイス分布ルールは次のとおりです。
1。周波数制限を送信 - デフォルト1800/分
2。送信数の上限 - デフォルトの1800/minに従って計算されます
3。上限額 - 着信シーンIDに応じて、デフォルトの上限を設定および適用することができます。
4.「数量」に関する他のどの制限がありますか? - ユーザーが受信する最大回数はデフォルトで10です
5.数量が私たちのニーズを満たすことができない場合、どのようにして各上限を増やすことができますか? - 同じ日にユーザーが受信する最大額の制限と最大回数は、マーチャントプラットフォームで設定できます
注 -赤いエンベロープの量が200を超える場合、リクエストパラメーターシーン_IDを渡す必要があり、パラメーターの説明を以下に示します。
注2-規制要件によれば、新たに適用された商人アカウントに現金赤い封筒を使用するには2つの条件が必要です。1。入力時間は90日を超え、30日間の連続通常のトランザクション。
url https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpackを要求します
証明書が必要かどうかは(詳細については商人証明書を参照)
リクエストメソッド投稿
データのリクエスト例:
<xml> <sign> <![cdata [e1ee61a91c8e90f299de6ae075d60a2d]]> </sign> <mch_billno> <![cdata [0010010404201411170 000046545]> </mch_billno> <mch_id> <![cdata [888]]> </mch_id> <wxappid> <![wxcbda96de0b165486]]> </wxap pid> <send_name> <![cdata [send_name]]> </send_name> <re_openid> <![cdata [onqojjmm1tad-3ropncn-yufa6ui]]> </r e_openid> <total_amount> <![cdata [200]]> </total_amount> <total_num> <![cdata [1]]> </total_num> <wishing> <![cd ATA [金持ちになるおめでとう]]> </wishing> <client_ip> <![cdata [127.0.0.1]]> </client_ip> <act_name> <![cdata [new year red envelope]]> </act_name> <remay> <![cdata [新年赤envelope]]]]> </berem> <shene_id> <![cdata [doct_2]]> </scene_id> <consume_mch_id> <![cdata [10 000097]]> </Consume_mch_id> <nonce_str> <![cdata [50780e0cca98c8c8e814883e5caa672e]]> </nonce_str> <risk_inf o> Posttime%3D123123412%26ClientVersion%3D234134%26Mobile%3D122344545%26Deviceid%3DIOS </RISK_INFO> </XML>
インターフェイスでは、Merchantプラットフォームの証明書への呼び出しが必要であり、証明書はMerchantプラットフォームからダウンロードする必要があります。
次に、インターフェイス内の証明書を使用します。まず、新しいWeixinsSLクラスを作成します
@componentPublic class WeixinsSl { / ***証明書タイプ* / @value( "$ {werchant.storekey}")private string storekey; / ***ファイルパス*/ @value( "$ {werchant.sslfile}")private string sslfile; / ***商人番号*/ @value( "$ {werchant.merchantnumber}")private string merchantnumber; public string getStoreKey(){return storekey; } public void setStoreKey(String StoreKey){this.storekey = storekey; } public string getsslfile(){return sslfile; } public void setSslfile(string sslfile){this.sslfile = sslfile; } public string getMerchantNumber(){return merchantnumber; } public void setmerchantnumber(string merchantnumber){this.merchantnumber = merchantnumber; }}HTTPCLIENTSSLクラスをカプセル化して、HTTPSリクエストを実装して証明書を追加します。
@componentPublic class httpclientssl {@autowired private weixinssl weixinssl; //リクエストタイムアウト(ミリ秒)5秒public static requestconfig requestconfig; //応答タイムアウト(ミリ秒)60秒public static int http_response_timeout = 60 * 1000; // public static string encoding = "utf-8"をエンコードするhttpclient文字public static requestconfig getRequestConfig(){return requestConfig.custom()。setConnectTimeout(5 * 1000).setConnectionRequestTimeout(http_response_timeout).build(); } public static void setRequestConfig(requestConfig requestConfig){httpclientssl.requestConfig = requestConfig; } / *** https要求偽造証明書* @return* / public closeablehttpclient defaultsslclient(){sslcontext sslcontext = null; try {new sslcontextbuilder()。loadtrustmaterial(null、new TrustStrategy(){@Override public boolean iStrusted(x509Certificate [] chain、string authytype)Java.security.cert.cert.certificateexception {return fals;}); } catch(nosuchalgorithmexception | keystoreException e){e.printstacktrace(); } sslConnectionSocketFactory Factory = new SSLConnectionSocketFactory(sslContext); return httpclients.custom()。setSSlSocketFactory(Factory).build(); } / *** https request neft fone certificate* @return* / public closeablehttpclient defaultsslclientfile(){if(this.weixinssl == null){return this.defaultslclient(); } fileInputStream inputstream = null;キーストアキーストア= null; try {// sslタイプkeystore = keystore.getInstance(weixinssl.getStorekey()); // ssl file inputstream = new fileinputStream(weixinssl.getSslfile()); // SSLパスワードkeystore.load(inputstream、weixinssl.getmerchantnumber()。tochararray())を設定します。 } catch(keyStoreException | nosuchalgorithmexception | cermostexception | ioexception e1){e1.printstacktrace(); }最後に{try {inputstream.close(); } catch(ioexception e){e.printstacktrace(); }} sslcontext sslcontext = null; try {sslcontext = sslcontexts.custom()。loadkeymaterial(keystore、weixinssl.getmerchantnumber()。tochararray())。build(); } catch(unrecoverableKeyException | nosuchalgorithmexception | keystoreException | keymanagementexception e){e.printstacktrace(); } sslConnectionSocketFactory Factory = new SSLConnectionSocketFactory(sslContext、new String [] {"tlsv1"}、null、sslconnectionsocketfactory.browser_compatible_hostname_verifier); return httpclients.custom()。setSSlSocketFactory(Factory).build(); } / ***リクエストを送信するメソッドをカプセル化* @throws unsupportedencodingexception* / public string send(string url、string data、colosaiblehttpclient closeablehttpclient)unsupportedencodingexception {closeablehttpclient client = coloseablehttpclient; httppost httppost = new httppost(urldecoder.decode(url、encoding)); httppost.addheader( "Connection"、 "Keep-Alive"); httppost.addheader( "Accept"、 "*/*"); httppost.addheader( "content-type"、 "application/x-www-form-urlencoded; charset = utf-8"); httppost.addheader( "host"、 "api.mch.weixin.qq.com"); httppost.addheader( "x-requested with"、 "xmlhttprequest"); httppost.addheader( "cache-control"、 "max-age = 0"); httppost.addheader( "user-agent"、 "mozilla/4.0(互換; msie 8.0; windows nt 6.0)"); httppost.setConfig(this.getRequestConfig()); //パラメーターをStringentity Entity = new Stringentity(data、encoding)に配置します。 entity.setContentenCoding(エンコード); entity.setContentType( "Application/xml"); httppost.setentity(entity); try {response = client.execute(httppost); if(respons.getStatusline()。getStatusCode()== 200){httpentity httpentity =(httpentity)respons.getentity(); if(response!= null){return entityutils.toString(httpentity、encoding); }}} catch(ioexception e){e.printstacktrace(); } nullを返します。 }}このようにして、HTTPSで証明書を要求するエンティティクラスをカプセル化し、次に要求されたWeChat Red Envelopeインターフェイスを生成します。
https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpackのパラメーター署名:
/*** Red Envelope Parameter Entity Class*@Throws UnsupportEdenCodingException*/ @componentPublic class sendredpack実装{/****/private static final long serialversionuid = -100048922809916099l;プライベート文字列nonce_str; //ランダムストリングプライベートストリングサイン; //署名プライベートストリングmch_billno; // merchant Order Number private String mch_id; // merchant number private string wxappid; // merchant name private string re_openid; //ユーザープライベートint total_amount; Envelope Blessing Private String client_ip; // IPアドレスprivate string act_name; //アクティビティ名プライベート文字列bermy; // notes public string getnonce_str(){return nonce_str; } public void setnonce_str(string nonce_str){this.nonce_str = nonce_str; } public string getsign(){return sign; } public void setSign(string sign){this.sign = sign; } public string getMch_billno(){return mch_billno; } public void setmch_billno(string mch_billno){this.mch_billno = mch_billno; } public string getmch_id(){return mch_id; } public void setmch_id(string mch_id){this.mch_id = mch_id; } public string getwxappid(){return wxappid; } public void setwxappid(string wxappid){this.wxappid = wxappid; } public string getsend_name(){return send_name; } public void setsend_name(string send_name){this.send_name = send_name; } public string getre_openid(){return re_openid; } public void setre_openid(string re_openid){this.re_openid = re_openid; } public int gettotal_amount(){return total_amount; } public void settotal_amount(int total_amount){this.total_amount = total_amount; } public int gettotal_num(){return total_num; } public void settotal_num(int total_num){this.total_num = total_num; } public string getWishing(){return wishing; } public void setwishing(string wishing){this.wishing = wishing; } public string getClient_ip(){return client_ip; } public void setclient_ip(string client_ip){this.client_ip = client_ip; } public string getAct_name(){return act_name; } public void setAct_name(string act_name){this.act_name = act_name; } public string getRemark(){return arme; } public void setRemark(string arem){this.remark = berem; }}次は、赤いパケットを送信するコントローラーです。
/*** Red Envelope Controller* @Author Zengliang*/ @controller @requestMapping(value = "/redenvelopEsReceive")public class redenvelopEseSeveivecontroller {// wechatユニークな識別子@value( "$ {weixin.appid}")private string appid; // wechat開発者パスワード識別子@value( "$ {weixin.appsecret}")public string appsecret; @autowired private sendredpack sendredpack; @autowired private httpclientssl httpclientssl; /*** XMLパラメーターを送信* @Author Zengliang*/@ResponseBody @RequestMapping(value = "/sendxml")public string sendxml(string openid、long redenvelopes_id、string mch_billno){redenvelopes redenve = redenveleteservice.findone(redenvelopes_id); xmlutil xmlutil = new xmlutil(); sendredpack.setact_name(redenve.getact_name()); sendredpack.setnonce_str(xmlutil.random()); sendredpack.setre_openid(openid); sendredpack.setclient_ip(redenve.getclient_ip()); sendredpack.setmch_billno(mch_billno); sendredpack.setmch_id(redenve.getmch_id());文字列xx = redenve.getRemark(); sendredpack.setRemark(stringutils.isempty(xx)== false?xx: "empty"); sendredpack.setsend_name(redenve.getsend_name()); sendredpack.settotal_amount(redenve.gettotal_amount()); sendredpack.settotal_num(redenve.gettotal_num()); sendredpack.setwishing(redenve.getWishing()); sendredpack.setwxappid(redenve.getwxappidxx()); //署名文字列params = this.createsendredpackordersign(sendredpack、redenve.getStore_key()); sendredpack.setsign(params); xmlutil.xstream()。alias( "xml"、sendredpack.getClass()); // XSTREAMを拡張してCDATAブロック文字列requestXml = xmlutil.xStream()。toxml(sendredPack);文字列結果; try {result = httpclientssl.send( "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"、requestxml、httpclientssl.defaultsslclientfile(); System.out.println( "成功した返品値"+result);返品結果; } catch(unsupportedencodingexception e){e.printstacktrace(); } nullを返します。 }/ ** * signature * @param redpack * @return */ public string createsendredpackordersign(sendredpack redpack、string storekey){stringbuffer sign = new stringbuffer(); sight.append( "act_name =")。append(redpack.getact_name()); sight.append( "&client_ip =")。append(redpack.getclient_ip()); sign.append( "&re_billno =")。append(redpack.getmch_billno()); sign.append( "&areme =")。append(redpack.getmch_id()); sign.append( "&send_str =")。append(redpack.getnonce_str()); sight.append(redpack.getnonce_str()); sign.append( "&re_openid =")。append(redpack.getre_openid()); sight.append( "&areme =")。append(redpack.getRemark()); sign.append( "&send_name =")。append(redpack.getsend_name()); sight.append( "&total_amount =")。append(redpack.gettotal_amount()); sign.append( "&total_num =")。append(redpack.gettotal_num()); sign.append( "&wishing =")。append(redpack.getWishing()); sight.append( "&wxappid =")。append(redpack.getwxappid()); sign.append( "&key =")。append(storekey); digestutils.md5hex(sign.toString())。touppercase(); }}次に、ツールクラスを使用してXMLを解析してXMLを解析してWeChatによって返され、XMLを解析する必要があります
/** *解析XMLツールクラス * @Author Zengliang */ @componentpublic class xmlutil {/** *解析xml wechat * @param xml * @return * @throws Exception */@suppresswarnings( "Unchecked")Public Map <String> Parsexml = new Hashmap <string、string>();ドキュメントdoc = null; try {doc = documenthelper.parsetext(xml); //文字列をXML要素に変換しますrootelt = doc.getRootelement(); //ルートノードリスト<要素> list = rootelt.elements(); //すべてのノードをルートノード(要素要素:list){// tranquility node map.put(element.getName()、element.getText()); //ノードの名前はマップキーであり、テキストはマップ値です}} catch(documentexception e){e.printstacktrace(); } catch(Exception e){e.printstacktrace(); }マップを返します。 } / *** XSTREAMを拡張してCDATAブロックをサポート* /プライベートXSTREAM XSTREAM = new Xppdriver(new nonAmeCoder()){@Override public hierarchicalStreamWriter createwriter(writer out){return new prettintwriter(out){//すべてのXml nodes for contion @suppresswarnings(rawtypes ")public void startnode(super.startnode(name、clazz); write.write.write( "]>"); private XStream IncureUnderLineXStream = new XStream(new Domdriver(null、new XMLFriendlyNameCoder( "_-"、 "_"))); public XStream getXStreamInclueUnderline(){return inclueUnderLinexStream; } public XStream XSTREAM(){return XStream; } / *** enverate random数* @return* / public string random(){string random = uuid.randomuuid()。toString()。ランダムを返します。 }}次に、sendxmlメソッドを呼び出して、ユーザーに赤い封筒を送信します。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。