android端⽀付V3版本地签名统⼀下单详解
满满的都是坑,因为服务器偷懒让客服端写统⼀下单,服务器只给了通知的url。的⽀付demo并没有统⼀下单的代码。⼀步步的来先根据统⼀下单的参数介绍⼯具:
1. 获取到当前的ip:
<span >public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = InetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = Element();
if (!inetAddress.isLoopbackAddress()) {
HostAddress().toString();
}
}
}
} catch (SocketException ex) {
}
return null;
}
private String getWifiIp() {
//获取wifi服务
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = ConnectionInfo();
int ipAddress = IpAddress();
String ip = intToIp(ipAddress);
return ip;
}
private String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
(i >> 24 & 0xFF);
}
</span>
2.随机订单号⽣成 test 你们可根据⾃⼰⽣成随机数:
<span >private String genOutTradNo() {
Random random = new Random();
MessageDigest(String.Int(10000)).getBytes());
}</span>
3.签名⼯具:
<span >private String genAppSign(List<NameValuePair> params) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < params.size(); i++) {
sb.(i).getName());
sb.append('=');
sb.(i).getValue());
sb.append('&');
}
sb.append("key=");
sb.append(Constants.API_KEY);
android获取真正的签名this.sb.append("sign str\n"+sb.toString()+"\n\n");
String appSign = String().getBytes());
Log.e("orion",appSign);
return appSign;
}</span>
差不多了现在我们需要⽣成传递的参数参数要求是xml 格式的:
<span >private String genProductArgs() {
StringBuffer xml = new StringBuffer();
String ip = getWifiIp();
if (ip == "" && ip == "") {
ip = getLocalIpAddress();
}
try {
String nonceStr = genNonceStr();
xml.append("</xml>");
List<NameValuePair> packageParams = new LinkedList<NameValuePair>();
packageParams.add(new BasicNameValuePair("appid", Constants.APP_ID));
packageParams.add(new BasicNameValuePair("body", "APP pay test"));
packageParams.add(new BasicNameValuePair("mch_id", Constants.MCH_ID));
packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));
packageParams.add(new BasicNameValuePair("notify_url",ConfigUtil.NOTIFY_URL));
packageParams.add(new BasicNameValuePair("out_trade_no",genOutTradNo()));
packageParams.add(new BasicNameValuePair("spbill_create_ip",ip));
packageParams.add(new BasicNameValuePair("total_fee", "1"));
packageParams.add(new BasicNameValuePair("trade_type", "APP"));
String sign = genPackageSign(packageParams);
packageParams.add(new BasicNameValuePair("sign", sign));
String xmlstring =toXml(packageParams);
return xmlstring;
} catch (Exception e) {
Log.e("TAG", "fail, ex = " + e.getMessage());
return null;
}
}</span>
其中 toxml:
<span >private String toXml(List<NameValuePair> params) {
StringBuilder sb = new StringBuilder();
sb.append("<xml>");
for (int i = 0; i < params.size(); i++) {
sb.append("<"+(i).getName()+">");
sb.(i).getValue());
sb.append("</"+(i).getName()+">");
}
sb.append("</xml>");
Log.e("orion",sb.toString());
String();
}</span>
<span > private class GetPrepayIdTask extends AsyncTask<Void, Void, Map<String,String>> { private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(PayActivity.this, getString(R.string.app_tip), getString(ing_prepayid));
}
@Override
protected void onPostExecute(Map<String,String> result) {
if (dialog != null) {
dialog.dismiss();
}
sb.append("prepay_id\n"+("prepay_id")+"\n\n");
resultunifiedorder=result;
}
@Override
protected void onCancelled() {
}
@Override
protected Map<String,String> params) {
String url = String.format("h.weixin.qq/pay/unifiedorder");
String entity = genProductArgs();
Log.e("orion",entity);
byte[] buf = Util.httpPost(url, entity);
String content = new String(buf);
Log.e("orion", content);
Map<String,String> xml=decodeXml(content);
return xml;
}
}</span>
其中decodexml就是:
<span >public Map<String,String> decodeXml(String content) {
try {
Map<String, String> xml = new HashMap<String, String>();
XmlPullParser parser = wPullParser();
parser.setInput(new StringReader(content));
int event = EventType();
while (event != XmlPullParser.END_DOCUMENT) {
String Name();
switch (event) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if("xml".equals(nodeName)==false){
//实例化student对象
xml.put(Text());
}
break;
case XmlPullParser.END_TAG:
break;
}
event = ();
}
return xml;
} catch (Exception e) {
Log.e("orion",e.toString());
}
return null;
}</span>
下单完成,第⼆部就是给⽀付传递调起⽀付的参数(具体参数看⽂档说明): <span >private void genPayReq() {
req.appId = Constants.APP_ID;
req.partnerId = Constants.MCH_ID;
req.prepayId = ("prepay_id");
req.packageValue = "prepay_id="+("prepay_id");
req.timeStamp = String.valueOf(genTimeStamp());
List<NameValuePair> signParams = new LinkedList<NameValuePair>();
signParams.add(new BasicNameValuePair("appid", req.appId));
signParams.add(new BasicNameValuePair("noncestr", Str));
signParams.add(new BasicNameValuePair("package", req.packageValue));
signParams.add(new BasicNameValuePair("partnerid", req.partnerId));
signParams.add(new BasicNameValuePair("prepayid", req.prepayId));
signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));
req.sign = genAppSign(signParams);
sb.append("sign\n"+req.sign+"\n\n");
Log.e("orion", String());
}</span>
第三部,调⽀付:
<span >private void sendPayReq() {
msgApi.sendReq(req);
}</span>
其中:
<span >Constants.APP_ID</span>
是appid 在开发者平台获取
<span > req.partnerId = Constants.MCH_ID;</span>
商户id
<span >ConfigUtil.NOTIFY_URL</span>
⽀付后的回调通知地址。
签名两次,然后⽤的api——key是商户平台api安全⾥⾯⾃定义的。
对了还有个获取时间⼯具
<span >private long genTimeStamp() {
return System.currentTimeMillis() / 1000;
}</span>
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论