android端微信支付V3版本地签名统一下单详解
满满的都是坑,因为服务器偷懒让客服端写统一下单,服务器只给了通知的url。微信的支付demo并没有统一下单的代码。
读此文前先阅读:https://pay.weixin.qq.com/wiki/doc/api/app/app.PHP?chapter=9_1
一步步的来 先根据统一下单的参数介绍工具:
1.获取到当前的ip:
<spanstyle="font-size:14px;">publicStringgetLocalIpAddress(){ try{ for(Enumeration<NetworkInterface>en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){ NetworkInterfaceintf=en.nextElement(); for(Enumeration<InetAddress>enumIpAddr=intf.getInetAddresses();enumIpAddr.hasMoreElements();){ InetAddressinetAddress=enumIpAddr.nextElement(); if(!inetAddress.isLoopbackAddress()){ returninetAddress.getHostAddress().toString(); } } } }catch(SocketExceptionex){ } returnnull; } privateStringgetWifiIp(){ //获取wifi服务 WifiManagerwifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE); //判断wifi是否开启 if(!wifiManager.isWifiEnabled()){ wifiManager.setWifiEnabled(true); } WifiInfowifiInfo=wifiManager.getConnectionInfo(); intipAddress=wifiInfo.getIpAddress(); Stringip=intToIp(ipAddress); returnip; } privateStringintToIp(inti){ return(i&0xFF)+"."+ ((i>>8)&0xFF)+"."+ ((i>>16)&0xFF)+"."+ (i>>24&0xFF); } </span>
2.随机订单号生成test你们可根据自己生成随机数:
<spanstyle="font-size:14px;">privateStringgenOutTradNo(){ Randomrandom=newRandom(); returnMD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes()); }</span>
3.签名工具:
<spanstyle="font-size:14px;">privateStringgenAppSign(List<NameValuePair>params){ StringBuildersb=newStringBuilder(); for(inti=0;i<params.size();i++){ sb.append(params.get(i).getName()); sb.append('='); sb.append(params.get(i).getValue()); sb.append('&'); } sb.append("key="); sb.append(Constants.API_KEY); this.sb.append("signstr\n"+sb.toString()+"\n\n"); StringappSign=MD5.getMessageDigest(sb.toString().getBytes()); Log.e("orion",appSign); returnappSign; }</span>
差不多了 现在我们需要生成传递的参数 参数要求是xml格式的:
<spanstyle="font-size:14px;">privateStringgenProductArgs(){ StringBufferxml=newStringBuffer(); Stringip=getWifiIp(); if(ip==""&&ip==""){ ip=getLocalIpAddress(); } try{ StringnonceStr=genNonceStr(); xml.append("</xml>"); List<NameValuePair>packageParams=newLinkedList<NameValuePair>(); packageParams.add(newBasicNameValuePair("appid",Constants.APP_ID)); packageParams.add(newBasicNameValuePair("body","APPpaytest")); packageParams.add(newBasicNameValuePair("mch_id",Constants.MCH_ID)); packageParams.add(newBasicNameValuePair("nonce_str",nonceStr)); packageParams.add(newBasicNameValuePair("notify_url",ConfigUtil.NOTIFY_URL)); packageParams.add(newBasicNameValuePair("out_trade_no",genOutTradNo())); packageParams.add(newBasicNameValuePair("spbill_create_ip",ip)); packageParams.add(newBasicNameValuePair("total_fee","1")); packageParams.add(newBasicNameValuePair("trade_type","APP")); Stringsign=genPackageSign(packageParams); packageParams.add(newBasicNameValuePair("sign",sign)); Stringxmlstring=toXml(packageParams); returnxmlstring; }catch(Exceptione){ Log.e("TAG","fail,ex="+e.getMessage()); returnnull; } }</span>
其中 toxml:
<spanstyle="font-size:14px;">privateStringtoXml(List<NameValuePair>params){ StringBuildersb=newStringBuilder(); sb.append("<xml>"); for(inti=0;i<params.size();i++){ sb.append("<"+params.get(i).getName()+">"); sb.append(params.get(i).getValue()); sb.append("</"+params.get(i).getName()+">"); } sb.append("</xml>"); Log.e("orion",sb.toString()); returnsb.toString(); }</span>
得到传递的参数,根据文档指示,我们需要用post去吊连接URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder:
<spanstyle="font-size:14px;">privateclassGetPrepayIdTaskextendsAsyncTask<Void,Void,Map<String,String>>{ privateProgressDialogdialog; @Override protectedvoidonPreExecute(){ dialog=ProgressDialog.show(PayActivity.this,getString(R.string.app_tip),getString(R.string.getting_prepayid)); } @Override protectedvoidonPostExecute(Map<String,String>result){ if(dialog!=null){ dialog.dismiss(); } sb.append("prepay_id\n"+result.get("prepay_id")+"\n\n"); resultunifiedorder=result; } @Override protectedvoidonCancelled(){ super.onCancelled(); } @Override protectedMap<String,String>doInBackground(Void...params){ Stringurl=String.format("https://api.mch.weixin.qq.com/pay/unifiedorder"); Stringentity=genProductArgs(); Log.e("orion",entity); byte[]buf=Util.httpPost(url,entity); Stringcontent=newString(buf); Log.e("orion",content); Map<String,String>xml=decodeXml(content); returnxml; } }</span>
其中decodexml就是:
<spanstyle="font-size:14px;">publicMap<String,String>decodeXml(Stringcontent){ try{ Map<String,String>xml=newHashMap<String,String>(); XmlPullParserparser=Xml.newPullParser(); parser.setInput(newStringReader(content)); intevent=parser.getEventType(); while(event!=XmlPullParser.END_DOCUMENT){ StringnodeName=parser.getName(); switch(event){ caseXmlPullParser.START_DOCUMENT: break; caseXmlPullParser.START_TAG: if("xml".equals(nodeName)==false){ //实例化student对象 xml.put(nodeName,parser.nextText()); } break; caseXmlPullParser.END_TAG: break; } event=parser.next(); } returnxml; }catch(Exceptione){ Log.e("orion",e.toString()); } returnnull; }</span>
下单完成,第二部就是给微信支付传递调起微信支付的参数(具体参数看文档说明):
<spanstyle="font-size:14px;">privatevoidgenPayReq(){ req.appId=Constants.APP_ID; req.partnerId=Constants.MCH_ID; req.prepayId=resultunifiedorder.get("prepay_id"); req.packageValue="prepay_id="+resultunifiedorder.get("prepay_id"); req.nonceStr=genNonceStr(); req.timeStamp=String.valueOf(genTimeStamp()); List<NameValuePair>signParams=newLinkedList<NameValuePair>(); signParams.add(newBasicNameValuePair("appid",req.appId)); signParams.add(newBasicNameValuePair("noncestr",req.nonceStr)); signParams.add(newBasicNameValuePair("package",req.packageValue)); signParams.add(newBasicNameValuePair("partnerid",req.partnerId)); signParams.add(newBasicNameValuePair("prepayid",req.prepayId)); signParams.add(newBasicNameValuePair("timestamp",req.timeStamp)); req.sign=genAppSign(signParams); sb.append("sign\n"+req.sign+"\n\n"); Log.e("orion",signParams.toString()); }</span>
第三部,调微信支付:
<spanstyle="font-size:14px;">privatevoidsendPayReq(){ msgApi.registerApp(Constants.APP_ID); msgApi.sendReq(req); }</span>
其中:
<spanstyle="font-size:14px;">Constants.APP_ID</span>
是appid在开发者平台获取
<spanstyle="font-size:14px;">req.partnerId=Constants.MCH_ID;</span>
商户id
<spanstyle="font-size:14px;">ConfigUtil.NOTIFY_URL</span>
支付后的回调通知地址。
签名两次,然后用的api——key是商户平台api安全里面自定义的。
对了还有个获取时间工具
<spanstyle="font-size:14px;">privatelonggenTimeStamp(){ returnSystem.currentTimeMillis()/1000; }</span>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。