이전 기사는 사용자의 OpenID를 얻었습니다
이 기사는 주로 WeChat Public 지불을 위해 Unified Single API를 호출합니다.
API 주소 : https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
문서를 살펴보면 주된 프로세스는 약 20 개의 매개 변수를 XML 형식으로 캡슐화하고 WeChat이 제공 한 인터페이스 주소로 보내면 반환 된 컨텐츠를 얻을 수 있습니다. 지불.
요청 매개 변수는 설명되지 않습니다.
그중, 임의의 문자열 : 나는 uuid를 사용하여 중간 선을 그렸습니다.
public static string create_nonce_str () {return uuid.randomuuid (). toString (). 대체 ( "-", "");판매자 주문 번호 : 각 주문 번호는 한 번만 사용할 수 있으므로 시스템 주문 번호에 추가 된 타임 스탬프가 사용됩니다.
총 금액 : 할 수 없습니다
알림 주소 : 성공 또는 실패 후 시스템으로의 콜백 주소
징후:
java.io. 개인 문자열 int total_fee; * @param IP 사용자의 IP 주소 * @Param OpenID 사용자의 OpenID */ Public PayInfo CreatePayInfo (Bizorder Bizorder, String IP, String OpenID) {PayInfo PayInfo (); constants.appid); payinfo.setdevice_info (constants.mch_id); 다음은 흰 쌀의 본문 "); payInfo.setattach (bizorder.getId ()); payInfo.setout_trade_no (vizorder.getOrdoCode (). concat ("a "). concat (dateformatutils.for mat () , "mmddhhmmss"); (OpenID); 반환 PayInfo}서명을 얻으십시오 :
/ ** * 서명 get * @param payInfo * @return * @throws Exception */ public string getSign (payInfo payInfo) 예외 {String signtemp = "appid ="+payInfo.getAppid ()+"& att getAttach () +"& body =" +payInfo.getBody () +"& device_info =" +payInfo.getDevice_info () +"& mch_id =" +payInfo.getMch_id () +"& nonc e_str =" +payInfo.getNonce_str () +" ¬ ify_url = "+payInfo.getNotify_url ()+"& openId = "+payInfo.getOpenid ()+"& out_trade_no = "+payInfo.getout_trade_no ()+"& spbill_ create_ip = "+payinfo.getspbill_create_ip ()+"+"+"+"+"+"+" PayInfo. ); 참고 : 위의 상수. 키 값은 가맹점 API 보안 API 키에 있습니다.
일부 도구 방법 : IP 주소를 가져 와서 바이트 배열을 16 진수 문자열로 변환하고 바이트를 16 진수로 변환합니다.
/ ** * 바이트 배열을 16 진수로 변환 * @param bytearray * @return */ public static string bytetostr (bytearray) {string strdigest = ""; ) {strdigest += ByteToHexStr (bytearray [i])} return strdigest}; 숫자 = { '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ',' ',' ',' ',' '', '' , '', '', '', 'a', 'b', 'c', 'e', 'f'}; [BYTES >>> & xf]; */ public static getipaddr (httpservletre quest 요청) {inetAddress addr = inetAddress.getLocalHost () {return request.getRemoteaddr (); (); int i =; i <ipaddrstr += "; iPaddrstr; 이런 식으로 서명이 얻어지고 PayInfo의 서명 및 기타 데이터는 XML 형식으로 변환되고 매개 변수로 통합 단일 주소로 전달됩니다.
PayInfo pi = pu.createpayinfo (bo, "...", ");
XML에 객체
/** * cdata */private static xstream xstream = new xstream (new xppdriver () {public HierarchicalStreamwriter createWriter (Writer Out) {re turn new PrettyPrintWriter (out) {// cdata tag boolean cdata = true; @SuppressWarnings ( "rawtypes") public void startNode (문자열 이름, 클래스 클라즈) {super.startNode (이름, Clazz)} protected void writetext (quic kwriter writer, String Text) {if (cdata) {writer.write ( "). <!! payin fo pi) {xstream.alias ( "xml", pi.getClass ());XML
@SuppressWarnings ( "확인되지 않은") public static map <string, string> parsexml (String XML)은 예외 {map <string, string> map = new Hashmap <st ring, string> (); 요소 root = getRootElement (); } 다음은 통합 주문을 호출하는 URL입니다.
log.info (messageutil.payinfotoxml (pi) .replace ( "__", "_")); pay/unifiedorder ","post ", messageutil.payinfotoxml (pi) .replace ("__ ","_ "). replace ("<! [cdata [ "," ".replace ("]]> "," ")); log.info (map); public static map <string, String> httpsRequestToxml (문자열 requestUrl, String requestMethod, String OutputStrs) {map <string, string> result = new Hashmap <> (); try {StringBuffer Buffer = httpsRequest (requestUrl, resultString ()) {log.err 또는 "Connection TimeOut :"+ce.getMessage (); CATCH (예외 e) {log.error ( "https 요청 예외 :"+ece.getMessage ()); httpsRequest () 메소드는 첫 번째 기사에 있습니다
위에서 얻은지도가 성공하면있을 것입니다
string_code = map.get ( "return_code"); return_msg) &&! return_msg.equals ( "OK") {return "Unified Single Error!"이 propay_id는 선불 ID입니다. 나중에 지불해야합니다.