Android實(shí)現(xiàn)微信支付的統(tǒng)一下單
本文實(shí)例為大家分享了Android實(shí)現(xiàn)微信支付統(tǒng)一下單的具體代碼,供大家參考,具體內(nèi)容如下
準(zhǔn)備工作
申請(qǐng)微信開發(fā)者賬號(hào),添加應(yīng)用及申請(qǐng)開通微信支付功能,如
查看開通流程
統(tǒng)一下單的接口文檔:
查看接口
開發(fā)
①下載sdk:
②可以導(dǎo)入包
在build.gradle文件中,添加如下依賴即可:
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}
或
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
③添加Android Manifest權(quán)限
<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)擊如下:簽名生成工具)
//拼接字段,順序不能變
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();
接下來提交到微信下單的接口上
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("請(qǐng)求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)行
new Thread(new Runnable() {
@Override
public void run() {
httpThreadxml();
}
}).start();
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android6.0運(yùn)行時(shí)權(quán)限完美封裝方法
今天小編就為大家分享一篇android6.0運(yùn)行時(shí)權(quán)限完美封裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android編程實(shí)現(xiàn)的超炫圖片瀏覽器
這篇文章主要介紹了Android編程實(shí)現(xiàn)的超炫圖片瀏覽器,涉及Android針對(duì)圖片的查看與顯示方法,包含對(duì)圖片的各種常見操作技巧,需要的朋友可以參考下2015-12-12
Android隨機(jī)給出加減乘除的四則運(yùn)算算術(shù)題
這篇文章主要為大家詳細(xì)介紹了Android隨機(jī)給出加減乘除的四則運(yùn)算算術(shù)題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android 截取手機(jī)屏幕兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android 截取手機(jī)屏幕兩種實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-05-05
詳解Kotlin Android開發(fā)中的環(huán)境配置
這篇文章主要介紹了詳解Kotlin Android開發(fā)中的環(huán)境配置的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android 使用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)及輪播效果
ViewPager是一個(gè)常用的Android組件,不過通常我們使用ViewPager的時(shí)候不能實(shí)現(xiàn)左右無限循環(huán)滑動(dòng),在滑到邊界的時(shí)候會(huì)看到一個(gè)不能翻頁的動(dòng)畫,可能影響用戶體驗(yàn),接下來通過本文給大家介紹Android 使用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)及輪播效果,一起看看吧2017-02-02
Kotlin實(shí)現(xiàn)Android系統(tǒng)懸浮窗詳解
大家好,本篇文章主要講的是Kotlin實(shí)現(xiàn)Android系統(tǒng)懸浮窗詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android使用SoundPool實(shí)現(xiàn)播放音效
這篇文章主要為大家詳細(xì)介紹了Android使用SoundPool實(shí)現(xiàn)播放音效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

