دعونا أولاً نلقي نظرة على تأثير الأظرف الحمراء النقدية الصادرة عن الحساب الرسمي:
من الضروري استدعاء واجهة النظام الأساسي للتاجر ، وقواعد توزيع الواجهة هي كما يلي:
1. إرسال الحد من التردد - الافتراضي 1800/دقيقة
2. الحد الأعلى لعدد الإرسال - محسوب وفقًا لما قاله 1800/دقيقة افتراضيًا
3. الحد الأعلى للمبلغ - اعتمادًا على معرف المشهد الوارد ، يمكن تعيين الحد الأعلى الافتراضي وتطبيقه في إعدادات منتجات منصة التاجر ، بحد أقصى لا يزيد عن 4999 يوان لكل وحدة
4. ما هي القيود الأخرى على "الكمية" الموجودة؟ - الحد الأقصى لعدد المرات التي يتلقاها المستخدم في ذلك اليوم 10 بشكل افتراضي
5. إذا لم تتمكن الكمية من تلبية احتياجاتنا ، فكيف يمكننا زيادة كل الحد الأعلى؟ -الحد الأقصى للمبلغ والحد الأقصى لعدد المرات التي يتلقاها المستخدم في نفس اليوم يمكن تعيينها على النظام الأساسي للتاجر
ملاحظة- عندما يكون مبلغ المغلف الأحمر أكبر من 200 ، يجب تمرير مشهد معلمة الطلب _id ، ويظهر وصف المعلمة أدناه.
ملاحظة 2 - وفقًا للمتطلبات التنظيمية ، يتعين على شرطين استخدام مظاريف حمراء نقدية للحسابات التجارية المطبقة حديثًا: 1. يتجاوز وقت الدخول 90 يومًا 2. المعاملات العادية المستمرة لمدة 30 يومًا.
طلب url https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
ما إذا كانت الشهادة مطلوبة (انظر شهادة التاجر للحصول على التفاصيل)
طلب طريقة
طلب مثال على البيانات:
<xml> <sign> <! 000046545]]> </mch_billno> <mch_id> <! pid> <send_name> <! E_OpenId> <Total_amount> <! ata [تهانينا على الحصول على غنية]]> </splising> <client_ip> <! Envelope]]> </semples> <sceply_id> <! 000097]]> </sunume_mch_id> <nonce_str> <! o> posttime ٪ 3d123123412 ٪ 26clientversion ٪ 3d234134 ٪ 26mobile ٪ 3d122344545 ٪ 26deviceid ٪ 3dios </risk_info> </sml>
تتطلب الواجهة المكالمة إلى شهادة Merchant Platform ، ويجب تنزيل الشهادة من منصة التاجر:
ثم استخدم الشهادة في الواجهة. أولاً ، نقوم بإنشاء فئة Weixinssl جديدة
ComponentPublic Class Weixinssl { / *** نوع الشهادة* / value ("$ {werchant.storekey}") String storkekek ؛ / *** مسار الملف*/ value ("$ {werchant.sslfile}") سلسلة خاصة sslfile ؛ / *** رقم التاجر*/ value ("$ {werchant.merchantnumber}") سلسلة MerchantNumber الخاصة ؛ السلسلة العامة getStoreKey () {return storekey ؛ } public void setStoreKey (String Storekey) {this.storekey = storekey ؛ } السلسلة العامة getSslfile () {return sslfile ؛ } public void setsslfile (String sslfile) {this.sslfile = sslfile ؛ } السلسلة العامة getMerChantNumber () {return MerchantNumber ؛ } public void setMerChantNumber (String MerchantNumber) {this.merChantNumber = merchantnumber ؛ }}تغليف فئة HTTPCLIENSSL لتنفيذ طلب HTTPS لإضافة شهادة:
componentpublic class httpclientssl {autowired private weixinssl weixinssl ؛ // طلب مهلة (ميلي ثانية) 5 ثوانٍ عامة requestConfig requestConfig ؛ // مهلة الاستجابة (milliseconds) 60 ثانية ثابتة عامة int http_response_timeout = 60 * 1000 ؛ // httpclient حرف ترميز سلسلة ثابتة public charging = "utf-8" ؛ requestConfig requestConfig getRequestConfig () {return requestConfig.custom (). } public static void setRequestConfig (requestConfig requestConfig) {httpclientssl.requestconfig = requestConfig ؛ } / *** شهادة التزوير HTTPS* REGRERN* / public clovablehttpclient defaultsslclient () {sslContext sslContext = null ؛ جرب {new sslContextBuilder (). loadTrustMaterial (null ، new trustStrategy () {Override public boolean isTrusted (x509certificate [] chain ، string uplttype) rems java.security.cert.certificateException {return false ؛}}}) ؛ } catch (nosuchalgorithMexception | keystoreException e) {e.printstacktrace () ؛ } sslconnectionsocketfactory factory = جديد sslConnectionSocketFactory (sslContext) ؛ إرجاع httpclients.custom (). setsslsocketfactory (المصنع) .Build () ؛ } / *** طلب https لإضافة شهادة* regurn* / public clovablehttpclient defaultsslclientfile () {if (this.weixinssl == null) {return this.defaultsslclient () ؛ } fileInputStream inputStream = null ؛ keystore keystore = null ؛ حاول {// ssl type keystore = keystore.getInstance (weixinssl.getStoreKey ()) ؛ // ssl file inputStream = new FileInputStream (weixinssl.getsslfile ()) ؛ // قم بتعيين ssl password keystore.load (inputStream ، weixinssl.getMerChantNumber (). Tochararray ()) ؛ } catch (keystoreException | nosuchalgorithMexception | certificeException | ioException e1) {e1.printstacktrace () ؛ } أخيرًا {try {inputStream.close () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ }} sslContext sslContext = null ؛ حاول {sslContext = sslcontexts.custom (). loadkeymaterial (keystore ، weixinssl.getmerchantnumber (). } catch (unrecoverableKeyException | nosuchalgorithMexception | keystoreException | keymanagementexception e) {e.printstacktrace () ؛ } sslconnectionsocketfactory factory = new SSLConnectionsOctionFactory (sslContext ، سلسلة جديدة [] {"tlsv1" ، null ، sslconnectionsocketfactory.browser_compatible_hostname_verifier) ؛ إرجاع httpclients.custom (). setsslsocketfactory (المصنع) .Build () ؛ } / *** طريقة تغليف لإرسال الطلبات* Throws UnsupportedEncodingException* / STRING SEND (url url string ، بيانات السلسلة ، closablehtpclient closablehttpclient) يلقي UnsupportedenCodingException {closablehttpclient client = closablehtpclient ؛ httppost httppost = new httppost (urldecoder.decode (url ، الترميز)) ؛ httppost.addheader ("connection" ، "keep-alive") ؛ httppost.addheader ("قبول" ، "*/*") ؛ httppost.addheader ("نوع المحتوى" ، "التطبيق/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 ("Assion-Agent" ، "Mozilla/4.0 (متوافق ؛ MSIE 8.0 ؛ Windows NT 6.0)") ؛ httppost.setConfig (this.getRequestConfig ()) ؛ // تعيين timeout clostablehttpresponse استجابة = null ؛ // ضع المعلمة في كيان صارمة = صارمة جديدة (البيانات ، الترميز) ؛ entity.setContentEncoding (الترميز) ؛ entity.setContentType ("application/xml") ؛ httppost.setentity (الكيان) ؛ حاول {response = client.execute (httppost) ؛ if (response.getStatusLine (). if (response! = null) {return entityUtils.ToString (httpentity ، الترميز) ؛ }}} catch (ioException e) {E.PrintStackTrace () ؛ } إرجاع فارغ ؛ }} وبهذه الطريقة ، نقوم بتغليف فئة كيان يطلب الشهادة مع HTTPS ، ثم ننشئ واجهة WeChat Red Envelope المطلوبة:
توقيع المعلمة من https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack:
/*** الفئة المعلمة المظروف الأحمر*athrows UnsupportedEncodingException*/ @componentpublic class sendRedPack تسلسس {/****/private static final long serialversionuid = -1000489228099916099l ؛ سلسلة خاصة nonce_str ؛ // سلسلة عشوائية علامة سلسلة خاصة ؛ // توقيع السلسلة الخاصة mch_billno ؛ // رقم طلب التاجر سلسلة خاصة mch_id ؛ // رقم التاجر سلسلة خاصة wxappid ؛ // public account string send_name ؛ // merchant name private string refrice ؛ Wishing ؛ // Red Envelope Bareing Private String Client_ip ؛ // عنوان IP السلسلة الخاصة ACT_NAME ؛ // اسم النشاط ملاحظة السلسلة الخاصة ؛ // ملاحظات السلسلة العامة getNonce_str () {return nonce_str ؛ } public void setnonce_str (string nonce_str) {this.nonce_str = nonce_str ؛ } السلسلة العامة getSign () {return sign ؛ } public void setSign (scre string) {this.sign = sign ؛ } السلسلة العامة getMch_billno () {return mch_billno ؛ } public void setmch_billno (String mch_billno) {this.mch_billno = mch_billno ؛ } السلسلة العامة getMch_id () {return mch_id ؛ } public void setmch_id (String mch_id) {this.mch_id = mch_id ؛ } السلسلة العامة getWxAppId () {return wxappid ؛ } public void setwxappid (String wxappid) {this.wxappid = wxappid ؛ } السلسلة العامة getend_name () {return send_name ؛ } public void setSend_Name (String send_name) {this.send_name = send_name ؛ } السلسلة العامة 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 ؛ } السلسلة العامة getWishing () {return wishing ؛ } public void setwishing (سلسلة Wishing) {this.wishing = Wishing ؛ } السلسلة العامة getClient_ip () {return client_ip ؛ } public void setClient_ip (string client_ip) {this.client_ip = client_ip ؛ } السلسلة العامة getact_name () {return act_name ؛ } public void setact_name (string act_name) {this.act_name = act_name ؛ } السلسلة العامة getRemark () {return compl ؛ } public void setRemark (string commer) {this.remark = compl ؛ }}التالي هو وحدة التحكم التي ترسل الحزمة الحمراء:
/*** وحدة تحكم المغلف الأحمر* Author Zengliang*/ @controller @requestMapping (value = "/redenvelesreceive") public class redenvenvesreceivecontroller {// wechat requat quientifiervalue ("$ {weixin.appid}") appid appid ؛ // WeChat Developer Password Identifier value ("$ {weixin.appsecret}") appsecret appsecret ؛ Autowired private sendRedPack sendRedPack ؛ @autowired httpclientssl httpclientssl ؛ /*** إرسال معلمات XML* Author Zengliang*/responsebody RquestMapping (value = "/sendxml") السلسلة العامة sendxml (string openId ، redenves_id ، string mch_billno) {redenvelopes = redenvicevice.findone ( 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: "فارغة") ؛ 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 string signature = this.createSendRedPackordersIgn (sendRedPack ، redenve.getStore_Key ()) ؛ sendRedPack.SetSign (params) ؛ xmlutil.xstream (). الاسم المستعار ("XML" ، sendRedPack.getClass ()) ؛ // تمديد xstream لدعم CDATA block string requestxml = xmlutil.xstream (). toxml (sendRedPack) ؛ نتيجة السلسلة حاول {result = httpclientssl.send ("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" ، requestxml ، httpclientssl.defaultsslclientfile ()) ؛ System.out.println ("قيمة الإرجاع الناجحة"+نتيجة) ؛ نتيجة العودة } catch (UnsupportedEncodingException e) {E.PrintStackTrace () ؛ } إرجاع فارغ ؛ }/ ** * إنشاء توقيع * param redpack * return */ السلسلة العامة createSendRedPackordersIgn (sendRedPack redpack ، String storekey) {StringBuffer sign = new StringBuffer () ؛ sign.append ("act_name ="). append (redpack.getact_name ()) ؛ sign.append ("& client_ip ="). append (redpack.getClient_ip ()) ؛ sign.append ("& re_billno ="). append (redpack.getmch_billno ()) ؛ sign.append ("& ember ="). append (redpack.getMch_id ()) ؛ sign.append ("& send_str ="). append (redpack.getnonce_str ()) ؛ sign.append (redpack.getnonce_str ()) ؛ sign.append ("& re_openid ="). append (redpack.getre_openid ()) ؛ sign.append ("& ember ="). append (redpack.getRemark ()) ؛ sign.append ("& send_name ="). append (redpack.getSend_Name ()) ؛ sign.append ("& total_amount ="). append (redpack.gettotal_amount ()) ؛ sign.append ("& total_num ="). append (redpack.gettotal_num ()) ؛ sign.append ("& wishing ="). append (redpack.getWishing ()) ؛ sign.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 {/** * parsing xml تم إرجاعه بواسطة wechat * @param xml * reghn * @string xml) New HashMap <string ، string> () ؛ مستند المستند = فارغ ؛ حاول {doc = documentHelper.ParSetext (xml) ؛ // قم بتحويل السلسلة إلى element element rootelt = doc.getRootElement () ؛ // احصل على قائمة عقدة الجذر <Element> list = rootelt.elements () ؛ // الحصول على جميع العقد تحت عقدة الجذر لـ (العنصر العنصر: قائمة) {// tranquility عقدة map.put (element.getName () ، element.getText ()) ؛ . } catch (استثناء e) {E.PrintStackTrace () ؛ } خريطة الإرجاع ؛ } / *** تمديد Xstream لدعم CDATA Block* / Private Xstream Xstream = New Xstream (XppDriver (New Nonamecoder ()) suppressWarnings ("Rawtypes") public void startNode (اسم سلسلة ، clazz) {super.startnode (name ، clazz) ؛ الكاتب (النص) ؛ Xstream الخاص الخاص inlureEnderLinexStream = new Xstream (domdriver (null ، xmlfriendlynamecoder ("_-" ، "_"))) ؛ Public Xstream getxStreamInclueNderLine () {return inlureAnderLinexStream ؛ } public xstream xstream () {return xstream ؛ } / *** إنشاء رقم عشوائي* regurn* / public string random () {string random = uuid.randomuuid (). toString (). استبدال ("،" ") ؛ العودة عشوائيا. }}ثم ندعو طريقة sendXML لإرسال الأظرف الحمراء إلى المستخدم.
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.