บทความนี้แบ่งปันข้อมูลที่เกี่ยวข้องเกี่ยวกับการชำระเงิน JSAPI + การส่งข้อความเทมเพลตสำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1. เครื่องมือ
ดูหมวดหมู่เครื่องมือ: WeChat Pay Java เวอร์ชันการชำระเงินแบบดั้งเดิม
2. การตั้งค่าบัญชีสาธารณะ
3. การใช้งานรหัส
OpenID: OpenID เป็นตัวบ่งชี้ที่ไม่ซ้ำกันระหว่างผู้ใช้และบัญชีสาธารณะ คลาสต่อไปนี้เกี่ยวข้องกับการสร้างโทเค็นปิดการเรียกอินเทอร์เฟซคำสั่งซื้อการรับข้อมูลไฟล์การกำหนดค่าและคลาสเครื่องมือ มีการใช้รหัสเฉพาะในบทความอื่น ๆ
แพ็คเกจ com.zhrd.bussinss.platform.controller.rest; นำเข้า java.io.file; นำเข้า java.io.fileinputstream; นำเข้า java.io.inputstream; นำเข้า java.security.keystore; java.util.hashmap; นำเข้า java.util.list; นำเข้า java.util.map; นำเข้า javax.net.ssl.sslcontext; นำเข้า javax.servlet.http.httpservletRequest; นำเข้า org.apache.http.htttentity; นำเข้า org.apache.http.client.methods.closeablehttpresponse; นำเข้า org.apache.http.client.methods.httppost; นำเข้า org.apache.http.conn.ssl.sslcontexts; นำเข้า org.apache.http.entity.stringentity; นำเข้า org.apache.http.impl.client.closeablehttpclient; นำเข้า org.http.impl.htp. org.apache.http.util.entityutils; นำเข้า org.dom4j.document; นำเข้า org.dom4j.documenthelper; นำเข้า org.dom4j.element; นำเข้า org.dom4j.io.saxreader; นำเข้า org.springframework.beans.beans. org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestmethod; นำเข้า org.springframework.web.bind.annotation.responsebody; com.zhrd.bussinss.platform.bo.jspay; นำเข้า com.zhrd.bussinss.platform.bo.payhist; นำเข้า com.zhrd.bussinss.platform.constants.payhistorypaystatus; com.zhrd.bussinss.platform.service.getweixinaccesstokenservice; นำเข้า com.zhrd.bussinss.platform.service.weixinpayservice; นำเข้า com.zhrd.bussinss.platform.utils.closeweixinordils; com.zhrd.bussinss.platform.utils.customizedpropertyplaceholderconfigurer นำเข้า com.zhrd.bussinss.platform.weixinpayutils.clientcustomssl;@restcontroller@requestmapping ("/rest/weixinsendmessage" Weixinpayservice Weixinpayservice; @autowired getWeixinAccessTokenService getWeixinAccessTokenService; มาตรฐานคงที่แบบคงที่ส่วนตัว = 1662652800000L; /** * WeChat Send Message * @param Request * @param Response * @return */@requestmapping (value = "/weixinsend", method = requestMethod.get) @ResponseBody วัตถุสาธารณะ System.out.println ("================================================== - - - - - - - CustomizedPropertyPlaceHolderConfigurer.getContextProperty ("wx.cert"). toString ()); sslContexts.custom (). loadKeYmaterial (KeyStore, "10061401" .ToCharArray ()). build (); // อนุญาตให้ใช้โปรโตคอล TLSV1 เฉพาะ sslConnectionsOcketFactory sslsf = ใหม่ sslconnectionsocketFactory (sslContext, สตริงใหม่ [] {"tlsv1"}, null, sslconnectionsocketfactory.allow_hostname_verifier); closeablehttpClient httpClient = httpClients.custom () .setsslsocketFactory (SSLSF) .build (); // httpget httpget = new // httpget ("https://api.mch.weixin.qq.com/secapi/pay/refund"); // สำหรับรายละเอียดที่จะได้รับโทเค็นดูบทความเกี่ยวกับการรับโทเค็น httppost httppost = httppost ใหม่ ("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" payhist pH = null; รายการ <map <string, object >> td = weixinpayservice.getTrade (OrderNo); วันที่ dt = วันที่ใหม่ (); SimpledateFormat SDF = ใหม่ SimpleDateFormat ("yyyymmddhhmmss"); สตริง noncestr = sdf.format (dt) .toString (); วันที่ตอนนี้ = วันที่ใหม่ (); String tradepayno = orderno+string.format ("%10d", Standardtime - Now.getTime ()). Substring (0, 10); System.out.println ("คำสั่งซื้อ orderno ====================================================================== number ==================== "+string.format ("%10d ", standardtime - ตอนนี้. getTime ()). substring (0, 10)); ราคาสตริง = math.round (float.valueof (td.get (0) .get (" ราคา ") รายละเอียดโปรดดูบทความเกี่ยวกับการกำหนดค่าไฟล์ dtsdf = ใหม่ simpledateformat ("yyyymmddhhmmss"); System.out.println ("ราคา =="+ราคา); jsonObject resultjspay = (jsonObject) getJSpay (noncestr, "คำสั่ง", tradepayno, ราคา, noncestr, timeExpire, openId); resultjspay.getString ("prepayid"); System.out.println ("================= 222Prepay_id222 =========================="+resultjspay.getString ("prepayid"). tostring ()); รายการ <แผนที่ <สตริง, แผนที่ <สตริง, สตริง >>> data = new ArrayList <แผนที่ <สตริง, แผนที่ <สตริง, สตริง >>> (); แผนที่ <สตริง, แผนที่ <สตริง, สตริง >> firstParam = ใหม่ hashmap <สตริง, แผนที่ <สตริง, สตริง >> (); แผนที่ <string, string> valueparam = new hashmap <string, string> (); valueparam.put ("value", td.get (0) .get ("buyerName")+"(Mr./MS.) คำสั่งซื้อ"); Valueparam.put ("color", "#173177"); แผนที่ <สตริง, สตริง> valueparam1 = ใหม่ hashmap <สตริง, สตริง> (); valueparam1.put ("ค่า", orderno); Valueparam1.put ("color", "#173177"); แผนที่ <สตริง, สตริง> valueparam2 = ใหม่ hashmap <สตริง, สตริง> (); Valueparam2.put ("ค่า", "ชุมชนอัจฉริยะรักคำสั่งซื้อใหม่"); Valueparam2.put ("color", "#173177"); แผนที่ <สตริง, สตริง> valueparam3 = ใหม่ hashmap <สตริง, สตริง> (); valueparam3.put ("value", td.get (0) .get ("ราคา")+"meta"); valueparam3.put ("สี", "#173177"); แผนที่ <สตริงสตริง> valueparam4 = ใหม่ hashmap <สตริงสตริง> (); Valueparam4.put ("ค่า", "รอการชำระเงิน"); Valueparam4.put ("color", "#173177"); แผนที่ <สตริง, สตริง> valueparam5 = ใหม่ hashmap <สตริง, สตริง> (); valueparam5.put ("value", "คลิกเพื่อจ่าย"); valueparam5.put ("color", "#173177"); FirstParam.put ("First", Valueparam); firstParam.put ("คำหลัก 1", valueparam1); FirstParam.put ("Keyword2", ValueParam2); firstParam.put ("คำหลัก 3", valueparam3); firstParam.put ("คำหลัก 4", valueparam4); FirstParam.put ("หมายเหตุ", Valueparam5); data.add (FirstParam); jspay jp = new jspay (); JP.Settouser (OpenID); jp.settemplate_id ("miexd4-4uqrfmdjnvqmjh0egfypm16r5dpdt6p9gpgg"); jp.settopcolor ("#173177"); jp.seturl ("http: // ลิงก์สำหรับการตั้งค่าบัญชีสาธารณะ/weixin_jspay/weixinjspay/init.action? packageValue ="+resultjspay.getString ("prepayid"). toString ()); jp.setData (ข้อมูล); jsonObject jsonObject = jsonObject.fromobject (jp); string str = สตริงใหม่ (jsonobject.toString (). getBytes ("UTF-8"), "iso8859-1"); // สตริง xml = clientcustomssl.refundnativepackage (weixinrefundservice.gettradepayno (orderno), noncestr, totalfee, refundefee, noncestr); ลอง {system.out.println ("data ========================================================================================================================== clientcustomssl.refundnativepackage (weixinrefundservice.gettradepayno (orderno), noncestr, noncestr); httpentity entity = respondyentry.getEntity (); len ++;} in.close (); JsonObject.FromObject (สตริงใหม่ (b, 0, len)); Ph.SetTradepayurl (""); Ph.SetPayTradeno (orderno); Ph.SetTradepayno (Tradepayno); Ph.SetPayStatus (PayHistoryPayStatus.wechat_pay_status_wait); Ph.SetPayType (PayHistoryPayType.wechat_js_pay); รายการ <PayHist> PayHistList = WeixInPayService.getPayHist (OrderNo, "WeChat", ""); if (payhistList == null || payhistlist.size () == 0) {weixinpayservice.addpayhist (pH); } else {// สำหรับรายละเอียดเกี่ยวกับคำสั่งปิดดูบทความเกี่ยวกับการยกเลิกคำสั่งซื้อ jsonObject strstatus = (jsonobject) closeweixinorderutils.closeweixinorder (payhistlist.get (0) .getTradepayno ()); if (strstatus.getString ("สถานะ"). เท่ากับ ("ความสำเร็จ")) {system.out.println (ph.getTradepayno ()); Weixinpayservice.updatePayhist (pH); }}} ผลการส่งคืน; } entityUtils.consume (เอนทิตี); } ในที่สุด {ResponseEntry.close (); }} ในที่สุด {httpClient.close (); } return null; } catch (exception e) {e.printstacktrace (); jsonObject result = new JSonObject (); result.put ("สถานะ", "ข้อผิดพลาด"); result.put ("msg", e.getMessage ()); ผลการกลับมา; }} วัตถุคงที่สาธารณะ getJSpay (สตริง noncestr, สตริง orderDescribe, สตริงลำดับที่ไม่ได้, ราคาสตริง, สตริง timestart, สตริง timexpire, สตริง openId) {ลอง {keystore keystore = keystore.getInstance ("PKCS12"); FileInputStream Instream = ใหม่ FileInputStream (ไฟล์ใหม่ (กำหนดเอง ProPropertyPlaceHolderConfigurer.getContextProperty ("wx.cert"). ToString ())); ลอง {keystore.load (stream, "10061401" .tochararray ()); } ในที่สุด {enterstream.close (); } // Trust CA ของตัวเองและ Certs SylContext SSLContext = sslContexts.custom (). loadKeYmaterial (KeyStore, "ดูอีเมล" .toCharArray ()). build (); // อนุญาตให้ใช้โปรโตคอล TLSV1 เฉพาะ sslConnectionsOcketFactory sslsf = ใหม่ sslconnectionsocketFactory (sslContext, สตริงใหม่ [] {"tlsv1"}, null, sslconnectionsocketfactory.allow_hostname_verifier); closeablehttpClient httpClient = httpClients.custom () .setsslsocketFactory (SSLSF) .build (); // httpget httpget = new // httpget ("https://api.mch.weixin.qq.com/secapi/pay/refund"); httppost httppost = new httppost ("https://api.mch.weixin.qq.com/pay/unifiedOrder"); String xml = clientcustomssl.createJsapipackage (noncestr, orderdescribe, orderno, ราคา, timestart, timeExpire, openId); ลอง {stringEntity se = new StringEntity (XML); httppost.setEntity (SE); System.out.println ("การดำเนินการคำขอ" + httppost.getRequestline ()); closeblehtpResponse ResponseEntry = httpClient.execute (HTTPPOST); ลอง {httpentity entity = responseentry.getEntity (); System.out.println ("----------------------------------------"); System.out.println (ResponseEntry.getStatusline ()); if (เอนทิตี! = null) {system.out.println ("ความยาวเนื้อหาตอบกลับ:" + entity.getContentLength ()); saxReader saxReader = new SaxReader (); เอกสารเอกสาร = saxReader.read (entity.getContent ()); Element Rootelt = document.getRootElement (); Documentxml = documenthelper.parsetext (XML); Element RootelTxml = documentxml.getRootelement (); System.out.println ("รูทโหนด:" + rootelt.getName ()); System.out.println ("==="+rootelt.elementText ("result_code")); System.out.println ("==="+rootelt.elementText ("return_msg")); String resultCode = rootelt.ElementText ("result_code"); jsonObject result = new JSonObject (); if (resultcode.equals ("ความสำเร็จ")) {system.out.println ("========================================================================================"+ rootelt.elementText ("prepay_id")); result.put ("prepayid", rootelt.elementText ("prepay_id")); result.put ("sign", rooteltxml.elementText ("sign")); result.put ("สถานะ", "ความสำเร็จ"); result.put ("msg", "ความสำเร็จ"); } else {result.put ("สถานะ", "false"); result.put ("msg", rootelt.elementText ("err_code_des")); } ผลตอบแทนผลลัพธ์; } entityUtils.consume (เอนทิตี); } ในที่สุด {ResponseEntry.close (); }} ในที่สุด {httpClient.close (); } return null; } catch (exception e) {e.printstacktrace (); jsonObject result = new JSonObject (); result.put ("สถานะ", "ข้อผิดพลาด"); result.put ("msg", e.getMessage ()); ผลการกลับมา; - 4. รหัสเลเยอร์การควบคุมการใช้งานรหัสในโครงการ Weixin_JSpay
แพ็คเกจ com.weixin.jspay.controller; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.springframework.stereotype.controller; org.springframework.web.bind.annotation.requestmethod;@controller@requestmapping ("/weixinjspay") คลาสสาธารณะ weixinjspaycontroller {@requestmapping (value = "/init" paysign) {system.out.println ("==================== 微信 jspay 开始 =================="); System.out.println ("PackageValue ================"+PackageValue); System.out.println ("Paysign ======================"+Paysign); request.setAttribute ("packageValue", "prepay_id =" + packageValue); request.setAttribute ("paysign", paysign); System.out.println ("===================== 微信 jspay 页面跳转开始 ==================="); กลับ "Weixin"; -
รหัส JSP
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <! doctype html สาธารณะ "-// w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"><html><head> <script type =" ข้อความ/javascript "src ="/weixin_jspay/js/md5.js " '$ {packageValue}'; var paysign = '$ {paysign}'; การแจ้งเตือน ("packageValue ============"+packageValue); การแจ้งเตือน ("Paysign ================"+Paysign); var signstring = "appid = ดูบัญชีสาธารณะ & noncestr = สุ่มสตริง & package ="+packageValue+"& paysign ="+paysign+"& signType = md5 & timestamp = เวลา"; // Alert (Signstring); var md5signValue = ("" + cryptojs.md5 (signstring)). touppercase (); // Alert (MD5SignValue); weixinjsbridge.invoke ('getBrandWcPayRequest', {"appid": "<span style =" font-family: Arial, Helvetica, sans-serif; "> ดูบัญชีสาธารณะ </span>", "timestamp": "1395712654", " : "md5", "paysign": md5signvalue}, ฟังก์ชั่น (res) {alert (res.err_msg); "GET_BRAND_WCPAY_REQUEST: CANCEL") {Alert ("การชำระเงินการยกเลิกผู้ใช้"); </body> </html>WeChat Paym
บทความนี้ได้รวบรวมไว้ใน "สรุปทักษะการพัฒนา JavaScript WeChat" และทุกคนยินดีที่จะเรียนรู้และอ่าน
ฉันขอแนะนำบทช่วยสอนเกี่ยวกับ WeChat Applets ที่มีความกังวลอย่างมากในขณะนี้: บรรณาธิการของ "การสอนการพัฒนาโปรแกรม WeChat Mini" ได้รวบรวมไว้อย่างระมัดระวังสำหรับทุกคนฉันหวังว่าคุณจะชอบ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น