Untuk menggunakan JSAPI untuk pembayaran WeChat, Anda harus terlebih dahulu mendapatkan prepay_id dari WeChat, dan kemudian menyelesaikan pembayaran dengan memanggil JSAPI WeChat. Karena interaksi interaktif front -end yang rumit, get_brand_wcpay_request: Batal atau get_brand_wcpay_request: Gagal dapat ditangani secara seragam bagi pengguna untuk menghadapi kesalahan atau secara aktif menyerah, tanpa harus memperbaiki perbedaan.
Kode contoh adalah sebagai berikut:
Fungsi onsbridgeready () {weixinjsbridge.invoke ('getBrandWcpayRequest', {"appid": "WX2421B1C4370EC43B", // nama akun publik disahkan ke "stempel waktu": "139" 5712654 ", / / / timestamp," 139 "5712654", / / / / timestamp, "139" 5712654 ", / / / / timespamp," 139 "5712654", / / / timestamp, "139" 5712654 ", / / / / timespamp," 139 "5712654", // /数 "noncestr": "E61463F8EFA94090B1F366CCCFBBB444", // 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 随机串 " 63ff7fadd89 "// 微信签名}, fungsi ( res) {if (res.err_msg == "get_brand_wcpay_request: ok") {} // Gunakan metode di atas untuk menentukan pengembalian ujung depan, tim weChat Solemn: res.err_msg akan kembali ke OK setelah pembayaran pengguna berhasil, Tapi itu tidak menjamin itu. );
Parameter di atas paket parameter adalah prepaay_id
Berikut ini adalah tentang mendapatkan parameter untuk memanggil jsapi
Saat kami menelepon JSAPI, kami harus mendapatkan OpenID pengguna.
Pertama -tama tentukan objek permintaan:
Paket Com.unstoppedable.protocol; Impor String Time_Start; = Builder.mch_id; .out_trade_no; Goods_tag ify_url; void setAppID (String appId) = appId;} public void setMch_id (string mch_id) {this .mch_id = mch_id;} public void setDevice_info (string device_info) {this.device_info = device_info;} publicononononononononononononononononononononononononononononononononononononononononononononon {{{{{{{nonon {{{{{nonon {{{nonon {{{non. nonce_str;} public void setSign (tanda string) {this.sign = tanda;} public public public (string body) {this.body = body;} public void setDetail (detail string) {this.detail = detail;} public voice ch (string attach) {this.attach = lampiran;} public void setout_trade_no (string out_trade_no) {this.out_trade_no = out_trade_no;} public void setFee_type (string feed_type) {this.fee_type = feed_type;} public ic void (this.fee_type = feed_type;} public ic void (this.fee_type = feeve_type;} public Ick ) {This.total_fee = total_fee;} public void setspbill_create_ip (string spbill_create_ip) {this .spbill_create_ip = spban_create_ip;} public void_start (string time_start) {this.time_start = time_start; void setgoods_tags (string goods_tag) {this.goods_tags_tag;} public void setnotify_url (string notify_url) {this.notify_url = notify_url;} public void settrade_type (string trade_type) {this.urll; {this.product_id = product_id;} public void setlimit_pay (string limit_pay) {this.limit_pay = limit_pay;} public void setopenid (string openId) {this.openid = openId;} peta publik <string, object> tom> ap () {MAP <String, Object> Map = HashMap <String, Object> (); .get (ini); if (obj! = null) {{map.put (field.getName (), obj);}} catch (IllegalargumentedException e) {e.printstacktrace ();} catch (IllegalAccessException e) {ACE );}} Peta; Private String_Create_ip; Parameter yang lewat appid tidak bisa menjadi null ");} if (mch_id == null) {throw newalargumentException (" "Jumlah mch_id tidak bisa null");} if (body == null) {lempar baru ilegalargumentedException ("" pass parameter tubuh parameter lulus ("pass parameter body parameter baru) tidak bisa menjadi null ");} if (out_trade_no == null) {throw newAlargumentException Numbers out_trade_no tidak bisa menjadi null");} if (total_fee == null) {lempar IllegalArgumentException ("total_fee tidak bisa menjadi nol");} if (spBANGUMENTException ("Total_fee tidak bisa menjadi null");} if (spBANGUMENTException ("" == null) {throw newalArgumentException ("Jumlah parameter spbill_create_ip tidak bisa null");} if (notify_url == null) {lempar baru ilegalargumentedException ("Parameter lulus notify_url tidak bisa menjadi null"); if (trade_type == nulll tidak boleh menjadi null "); if (trade_type == nulll tidak bisa menjadi null"); if (trade_type == nol ) de_type tidak bisa menjadi null ");} this.appid = appid; this .mch_id = mch_id; this.body = body; this.out_trade_no = out_trade_no; this.total_fee = topal_fee; ate_ip; this.notify_urll = notify_url; this. = trade_type;} Public UnifyReqDatabuilder setDevice_info) {{this.device_info = device_info; Lampirkan; = time_expire;} Public UnifiedOrderReqDatabuilder setgoods_tags_tags (string goods_tag) {this.goods_tags_tags_tag; it_pay = limit_pay ; iLlegalagumentexception ("When trade_type is JSAPI, openid is a must -fill parameter");} if ("native" .equals (this.trade_type) &&PRODUCT_ID == NULL) Throw New iLlegalagumentexception ("" When trade_type is native, product_id is a harus -fill parameter ");} return new UnifiedOrderReqData (this);}}}}
Karena beberapa parameter diperlukan, beberapa parameter dipilih. Dan tanda harus menunggu semua parameter untuk masuk sebelum menghitung, sehingga mode pembangun digunakan di sini. Tentang mode pembangun.
Kami memilih HTTPClient untuk transmisi jaringan.
Paket Com.unstoppedable.com; Log; .apache.http.client.config.RequestConfig; .conn.connectionPoolTimeOnException; http.client.httpclients; Java.security.keystore;/** * dibuat oleh Hupeng pada 2015/7/28. Connect Timeout, default 10 detik static int socketTimeout = 5000; Keystore = keystore.getInstance ("pkcs12"); SSLCONTEXTS.CUSTOM () .loadKeymaterial (KEYSTORE, Configure.getcertpassword (). ToCharRay (). ild (); // Allow Tlsv1 Protocol only sslConnectionSocketFactory SSLSF = New SSLCONNECTIONSOTFACTORY (SSLContext, New String [] {"TLSV1"}, NULL, SslconnectionOtfactfactory.browser_compatible_hostname_verifier); requestConfig config =) .setConnectTimeOut (ConnectTimeout) .setConnectionRequestTimeOut (httpclients. Actory (SSLSF) .build (); {Throw NewTimee Xception baru ("Kesalahan Buat httpClient ...", e);}} public static String doGet (String RequestUrl) melempar pengecualian {httpget httpget = httpget baru (requestUrl); GetRequestline ()); GetStataScode (); Status: " + Status);}}}; return httpclient.execute (httpget, responseHandler);} akhirnya {httpget.releaseconnection ();}} string stric publik (string url, objek objek2xml) {string hasil = null; ost = Httppost baru (url); // selesaikan pasangan bug xstream xStreamForRequestPostData = baru xStream (domdriver baru ("UTF-8", XMLFriendlyNamecoder baru ("-_", "_"); // Objek data menjadi objek data yang akan menjadi objek data menjadi objek data yang akan menjadi objek data menjadi objek data yang akan menjadi objek data menjadi objek data yang harus diawasi untuk data menjadi objek data yang harus diwajibkan untuk data menjadi objek data yang menjadi objek data menjadi objek data yang harus diwajibkan untuk data menjadi data yang menjadi objek data yang menjadi objek data yang akan menjadi data yang menjadi objek data menjadi Data Diserahkan ke API dikonversi ke data format XML ke API PostDataxML = XStreamForRequestPostData .TOXML (Object2XML); -8 Pengkodean, jika tidak, orang Cina dari server API XML tidak dapat berhasil diidentifikasi. ; Entitas, "UTF-8");} catch (ConnectionPoolTimeOutException e) {Logger.Error ("http dapatkan lemparan connectionPoolTimeOutException (tunggu waktu keluar)", e);} Catch Event E) {Logger.Error ("http dapat melempar thnecttimeoutEcceptionception ", e);} catch) {logger.error (" "http dapatkan throw socketTimeOutException", e);} catch (pengecualian e) {logger.error ("http dapatkan lemparan), e);} akhirnya {httppp ost. abort ();} hasil pengembalian;}}
Lalu ada pintu masuk umum kami:
Paket Com.unstoppedable .ParserconfigurationException; Melempar IOException, SaxException, ParserconfigutException {String res) onFigure.unified_order_api, reqdata); ("appid" , "mch_id", "out_trade_no", 1, "spaby_create_ip_ip_ip", notify_url "," jsapi "). setopenid (" openId ").};
XML yang dikembalikan adalah:
<xml> <sturn_code> <! CDATA [10000100]]> </CH_ID> <NONCE_STR> <! 7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d76d7d7d76d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d yang yang ”yang” yang “ _code> <! </RESURE_CODE> <prepay_id> <!
Return_code dan result_code akan mengembalikan prepay_id saat mereka sukses. Esensi Esensi , Lalu gunakan dia di jsapi. Esensi
Artikel ini telah diselenggarakan untuk "JavaScript WeChat Development Ringkasan", "Ringkasan Tutorial Pengembangan Android WeChat", dan "Ringkasan Tutorial Pengembangan Java WeChat" menyambut semua orang untuk dipelajari dan dibaca.
Saya merekomendasikan tutorial Program Mini WeChat dengan tingkat perhatian yang lebih tinggi kepada semua orang: editor "Tutorial Pengembangan Program Mini WeChat" diatur dengan cermat untuk semua orang.
Di atas adalah semua isi artikel ini.