前の記事では、ユーザーのOpenIDが取得されました
この記事では、主にWeChat Public PaymentのUnified SingleAPIを呼び出します
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.randomuid()。toString( " - " - "、" ");販売者の注文番号:各注文番号は1回しか使用できないため、システムの注文番号にタイムスタンプが追加されます。
合計金額:できません
通知アドレス:WeChatの支払いが成功または失敗した後のシステムへのコールバックのアドレス
サイン:
java.io.serializableを実装します。 Private out_trade_no; BIZORDERシステムビジネス注文番号 * @Param IPユーザーのIPアドレス * @Param OpenIDユーザーのOpenID */ payinfo createpayinfo(bizorder bizorder、string ip、string){payinfo payinfo = new payinfo(); constants.appid)これが白米の体です "); payinfo.setattach(bizorder.getid()); payinfo.setout_trade_no(bizorder.getOrderCode()。concat(" a ")。 、「mmdhhmmss」)))) (OpenID);署名を取得します:
/ ** signature * @param payinfo * @return * @throws例外 */ public string getsign(payinfo payinfo)スロー例外{string signtemp = "appid ="+payinfo.getappid()+"&aittle ="+payinfo。 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。 )MD.UPDATE( "utf-");注:上記の定数。Key値は、Merchant API Secure APIキーにあります。
いくつかのツールの方法:IPアドレスを取得し、バイト配列を16進文字列に変換し、バイトをヘキサディシマルストリングに変換します
/ ** *バイトアレイを160個の文字列に変換します * @param bytearray */ public static string bytetostr(bytearray){string strdigest = ""; ){strdigest += bytetohexstr(bytearray [i]); digit = {''、 ''、 '' ''、 ''、 ''、 ''、 ''、 '' '、' '、' '、' '' '、' '、' '、' '、 、 ''、 ''、 '、'、 'b'、 'd'、 'f'}; [BYTES >>>&XF]; */ public Static String getipaddr(httpservletre request){inetAddress addr = null; (); string ipaddrstr = ""; i <ipaddr; i ++){ipaddrstr = "。} iPaddrStr;このようにして、署名が取得され、PayInfoの署名およびその他のデータがXML形式に変換され、パラメーターとして統一された単一アドレスに渡されます。
Payinfo pi = pu.createpayinfo( "..."、 ");
XMLにオブジェクト
/** * cdataをサポートするためにxStreamを拡張 */private static xstream xstream = new Xpriver(){public hierarchicalStreamWriter createwRiter(writer out){re turn new PrettyPrintWriter(out){// cdata tag boolean cdata = true; @suppresswarnings( "rawtypes")public void startnode(string name、class clazz){super.startnode(name、clazz); <![cdata ["); writer.write(text); writer.write("]]> ");} else {writer.write(text);}}};}}}}); public static string payinfotoxml( payin fo pi){xstream.alias( "xml"、pi.getclass());マッピングするXML
@suppresswarnings( "Unchecked")public static Map <string、string> parsexml(string xml)throws {map <string> map = new hashmap <string、string>(); ); document.getRootelement(); }以下は、統一順序を呼び出すためのURLです
log.info(mesageutil.payinfotoxml(pi).replace( "__"、 "_"); Pay/UnifiedOrder "、" post "、messageutil.payinfotoxml(pi).replace(" __ "、" _ ")。 "); log.info(map); public static map <string、string> httpsrequesttoxml(string requesturl、string requestmethod、string outputstr){map <string、string> result = new Hashmap <>(); try {stringbuffer buffer = httpsRequest(requestmethod、outputstr); catch(例外){log.error( "https request:"+ece.getMessage());メソッドhttpsRequest()は最初の記事にあります
上記で得られたマップが成功した場合、そこにあります
string return_code = map.get( "return_code"); return_msg)&&!return_msg.equals( "ok")){unified single error! "}このPrepay_idは、プリペイIDです。後で支払う必要があります。