WeChat 결제에 JSAPI를 사용하려면 먼저 WeChat에서 선불을 얻은 다음 JS API의 반환 결과는 get_brand_wcpay_request입니다. 복잡한 프론트 -엔드 대화식 상호 작용으로 인해 get_brand_wcpay_request : 취소 또는 get_brand_wcpay_request : 사용자가 오류에 직면하거나 적극적으로 포기하지 않으면 서 실패를 균일하게 처리 할 수 있습니다.
예제 코드는 다음과 같습니다.
function onbridgeready () {weixinjsbridge.invoke ( 'getbrandwcpayrequest', { "appid": "wx2421b1c4370ec43b", // 공개 계정의 이름은 "139"5712654 ", 1970 년 이후"139 "5712654"로 전달되었습니다. "비정규": "e61463f8efa94090b1f366cccfbb444", // 随机串 "패키지": "u802345jgfjsdfgsdg888", "md5": // 微信签名方式 微信签名方式 微信签名方式 : "Paysign": "70a57396bb 4c63ff7fadd89 "// 微信签名}, 함수 ( res) {if (res.err_msg == "get_brand_wcpay_request : ok") {} // 위의 메소드를 사용하여 프론트 엔드 리턴을 결정합니다. 그러나 그것은 그것을 보장하지 않습니다});} if (weixinjsbridge == "undefined") {if (docume ridgeready, false);} else if (document.attachevent) {weixinjsbridgeready ', onbridgeready. ); 문서. ATTACHEVENT ( 'onweixinjsbridgeready', onbridgeready);
매개 변수 패키지의 위 매개 변수는 prepaay_id입니다
다음은 JSAPI를 호출 할 매개 변수를 얻는 것입니다
JSAPI에 전화하면 사용자의 OpenID를 가져와야합니다.
먼저 요청의 객체를 정의합니다.
Com.Stoppedable.httpservice; java.util string_start; = builder.device_info; .fee_no builder.goods_tag ify_url; trade_type 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;} public void setnonononononononononononononononononononononononononoNonoNonOnonOnOnonOnOnonOnOnonOnOnonOnonOnonOnonOnonOnonOnonOnonOnOnonOnOnonOnOnonOnOnonOnOnonOnOnonOnOnonOnOnonons는 { nonce_str;} public void setsign (문자열 부호) {this.sign = sign} public void setbody (string body = body;} public void setDetail (String detail) ch (문자열 첨부) {this.attach = att ) {This.total_fee = Total_fee;} Public void Setspbill_Create_ip (String SPBILL_CREATE_IP) {This .spbill_create_ip = spban_create_ip;} Public void settime_start (string time_start) {this.time_start = time_start; String time_expire) {this.time_expire = time_expire;} public 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.tr Ade_type = Trade_type;} Public Void SetProduct_id (String Product_id) {this.product_id = product_id;} public void setlimit_pay (string lemit_pay) {this.limit_pay = limit_pay;} public void setopenid (String openid) {this.openid = openid;} public map <string, object> tom> ap () {map <객체> 객체> (); .get (this); if (obj! = null) {{map.put (field.getname (), obj);}} catch (delegalArgumentedException e) {e.printstacktrace ();} catch (ehonegalaccessexception e) {ace ()}} 공개 정적 문자열은 개인 문자열 첨부; 개인 문자열 _Start; 전달 매개 변수 Appid가 null이 될 수 없습니다 ");} if (mch_id == null) {throw newalargumentException (" "" ""mch_id의 숫자는 null이 될 수 없습니다 "); null ");} if (out_trade_no == null) {newalargumentexception 번호를 던지십시오 out_trade_no"); == NULL) {Throw NewAlargumentException ( "매개 변수 수는 null이 될 수 없음");} if (notify_url == null) {throw new 불법 불법 행정 exception ( "Pass Parameter Notify_url이 null이 될 수 없음");} if (trade_type == null )) de_type는 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_url = notify_url; this.trade_type. = trade_type;} public unifyreqdatabuilder setdevice_info) {{this.device_info = retract this; retud this this; = time_expire;} public UnifiedOrderReqDatAbuilder setGoods_tags_tags (string goods_tag) {goods_tags_tags_tag; imit_pay = limit_pay reptly this; 불법적 인 경우 ( "Trade_Type가 JSAPI 일 때, OpenID는 필수 -수유 매개 변수");} if ( "avatiel".equals (this.trade_type) && product_id == null) Trade_type가 기본이 될 때 "", uppubled_id는 a. Must -fill 매개 변수 ");} return new UnifiedOrderReqData (this);}}}}
일부 매개 변수가 필요하므로 일부 매개 변수가 선택됩니다. 그리고 부호는 계산하기 전에 모든 매개 변수가 전달 될 때까지 기다려야하므로 여기에서 빌더 모드가 사용됩니다. 빌더 모드 정보.
우리는 네트워크 전송을 위해 httpclient를 선택합니다.
Com.Unstoppedable.com.Thoughts.xstream.xstream; org.apache .apache.http.client.config.requestconfig; .connectionpooltime http.ssl. http.httpclients; java.security.keystore. 시간 초과 시간, 기본 10 초 개인 정적 int sockettimeout = 5000; keystore = keystore.getInstance ( "PKCS12"); fileInputStream instream = new File (configure.getCertLocalPath ()); // HTTPS 암호화 전송에 대한 로컬 인증서를로드합니다. sslcontexts.custom () .loadkeymaterial (keystore, configure.getCertPassword (). toCharray (); ild (); // 허용 tlsv1 프로토콜 만 SSLConnectionSocketFactory SSLSF = 새로운 sslConnectionSotFactory (sslcontext, new String [] {tlsv1, null, sslconnectionsotfactfactory.browser_compatible_hostname_verifier) lsf) .build (); {새로운 runtimee Xception ( "오류 생성 httpclient ...", e);}} public static string doget (string requestUrl)은 예외를 던집니다. getRequestline (); // 사용자 정의 응답 핸들러 <stringhandler = new response <string> () {@override public string httpresponse response를 작성합니다. getStatuscode (); 상태 : " + status);}}}; return httpclient.execute (httpget, responskhandler);} 마지막으로 {httpget.releaseconnection ();}} public stric string vopost (String URL, Object2xml) {String result = null; null; Ost = 새로운 httppost (url); // xstream 쌍의 xstream xstreamforrequestpostdata (new domdriver ( "utf-8", new xmlfrientlynamecoder ( "-_", "_")를 해결합니다. API에 제출 된 것은 XML 형식 데이터로 변환됩니다. PostDataxml = XStreamForRequestPostData (Object2xml); -8 인코딩, 그렇지 않으면 API 서버 XML이 성공적으로 식별 할 수 없습니다. ; 엔티티, "UTF-8");} catch (ConnectionPoolTimeOutException e) {logger.error ( "http get tash connectionpooltimeoutexception (대기 시간 출력)", e);} catch event e) {logger.error ( "http thnect timeoutexception ", e);} catch) {logger.error (" "http get tash sockettimeoutexception", e);} catch (예외 e) {logger.error ( "http get strash), e);} 마침내 {httppp ost. abort ();} return}}.
그런 다음 일반적인 입구가 있습니다.
Com.Service .parserconfigurationException; IoException, saxException, parserconfigutexception {string res) onfigure.unife_order_api, reqdata); , "mch_id", "out_trade_no", 1, "spaby_create_ip_ip_ip", notify_url ","jsapi "). setopenid ("openid "). build (); system.out.println (unifiedorder (reqdata));}}
반환 된 XML은 다음과 같습니다.
<xml> <sturn_code> <! cdata [100000100]]> </mch_id> <nonce_str> <! 7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d75e8e8e8e8e8e8e8e8e8e8e6 2 ODE> <! [CDATA [성공]]> </resent_code> <prepay_id> <!
return_code 및 result_code는 성공할 때 prepay_id를 반환합니다. 본질 본질 그런 다음 jsapi에서 그를 사용하십시오. 본질
이 기사는 "JavaScript WeChat Development Summary", "Android WeChat Development Tutorial Summary"및 "Java Wechat Development Tutorial Summary"로 구성되었습니다.
나는 모든 사람에게 더 많은 관심을 가진 Wechat Mini 프로그램 튜토리얼을 추천합니다. "Wechat Mini 프로그램 개발 튜토리얼"의 편집자는 모든 사람을 위해 신중하게 구성됩니다.
위는이 기사의 모든 내용입니다.