本文實(shí)例為大家分享了Android實(shí)現(xiàn)微信支付統(tǒng)一下單的具體代碼,供大家參考,具體內(nèi)容如下
準(zhǔn)備工作
申請微信開發(fā)者賬號(hào),添加應(yīng)用及申請開通微信支付功能,如
查看開通流程
統(tǒng)一下單的接口文檔:
查看接口
開發(fā)
①下載sdk:
②可以導(dǎo)入包
在build.gradle文件中,添加如下依賴即可:
1
2
3
|
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' } |
或
1
2
3
|
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' } |
③添加Android Manifest權(quán)限
1
2
3
4
5
|
<uses-permission android:name= "android.permission.INTERNET" /> <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name= "android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name= "android.permission.READ_PHONE_STATE" /> <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> |
調(diào)用統(tǒng)一下單接口
1.務(wù)必提交必須的字段:appid,body,mch_id,nonce_str,notify_url, out_trade_no,spbill_create_ip,total_fee,trade_type,sign(都是小寫);提交到微信接口時(shí)以xml格式提交
2.sign為前面提交的參數(shù)按照參數(shù)名ASCII碼從小到大排序簽名拼接起來然后進(jìn)行MD5運(yùn)算,再將得到的字符串所有字符轉(zhuǎn)換為大寫得到的,如簽名生成算法
3.參與生成sign的key為商戶賬號(hào)的密鑰,key設(shè)置路徑如下:微信商戶平臺(tái)(pay.weixin.qq.com)–>賬戶設(shè)置–>API安全–>密鑰設(shè)置
下面是具體代碼(如若查看你的sign生成及提交的xml是否正確可以點(diǎn)擊如下:簽名生成工具)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//拼接字段,順序不能變 String A = "appid=你的appID" + "&body=jinshi" + "&mch_id=你的商戶號(hào)" + "&nonce_str=" + nonce_str + "¬ify_url=http://www.szgsip.com/" + "&out_trade_no=" + trade_no + "&spbill_create_ip=192.168.1.1" + "&total_fee=1" + "&trade_type=APP" ; String key = "你的密鑰" ; String temp = A + "&key=" + key; // 生成sign String sign = MD5.getMessageDigest(temp.getBytes()).toUpperCase(); |
接下來提交到微信下單的接口上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
private void httpThreadxml() { //組建xml數(shù)據(jù) //拼接字段,順序不能變 xml.append( "<xml>\n" ); xml.append( "<appid>你的appID</appid>\n" ); xml.append( "<body>jinshi</body>\n" ); xml.append( "<mch_id>你的商戶號(hào)</mch_id>\n" ); xml.append( "<nonce_str>" + nonce_str + "</nonce_str>\n" ); xml.append( "<notify_url>http://www.szgsip.com/</notify_url>\n" ); xml.append( "<out_trade_no>" + trade_no + "</out_trade_no>\n" ); xml.append( "<spbill_create_ip>192.168.1.1</spbill_create_ip>\n" ); xml.append( "<total_fee>1</total_fee>\n" ); xml.append( "<trade_type>APP</trade_type>\n" ); xml.append( "<sign>" + sign + "</sign>\n" ); xml.append( "</xml>" ); try { final byte [] xmlbyte = xml.toString().getBytes( "UTF-8" ); System.out.println(xml); URL url = new URL( "https://api.mch.weixin.qq.com/pay/unifiedorder" ); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout( 5000 ); conn.setDoOutput( true ); // 允許輸出 conn.setDoInput( true ); conn.setUseCaches( false ); // 不使用緩存 conn.setRequestMethod( "POST" ); conn.setRequestProperty( "Connection" , "Keep-Alive" ); // 維持長連接 conn.setRequestProperty( "Charset" , "UTF-8" ); conn.setRequestProperty( "Content-Length" , String.valueOf(xmlbyte.length)); conn.setRequestProperty( "Content-Type" , "text/xml; charset=UTF-8" ); conn.setRequestProperty( "X-ClientType" , "2" ); //發(fā)送自定義的頭信息 conn.getOutputStream().write(xmlbyte); conn.getOutputStream().flush(); conn.getOutputStream().close(); if (conn.getResponseCode() != 200 ) throw new RuntimeException( "請求url失敗" ); InputStream is = conn.getInputStream(); // 獲取返回?cái)?shù)據(jù) // 使用輸出流來輸出字符(可選) ByteArrayOutputStream out = new ByteArrayOutputStream(); byte [] buf = new byte [ 1024 ]; int len; while ((len = is.read(buf)) != - 1 ) { out.write(buf, 0 , len); } String string = out.toString( "UTF-8" ); System.out.println(string); Log.e( " 微信返回?cái)?shù)據(jù) " , " --- " + string); out.close(); } catch (Exception e) { System.out.println(e); } } |
注意在調(diào)用上面的方法,一定要在子線程中進(jìn)行
1
2
3
4
5
6
|
new Thread( new Runnable() { @Override public void run() { httpThreadxml(); } }).start(); |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/Dr_abandon/article/details/80695156