Android實現微信支付的統(tǒng)一下單
本文實例為大家分享了Android實現微信支付統(tǒng)一下單的具體代碼,供大家參考,具體內容如下
準備工作
申請微信開發(fā)者賬號,添加應用及申請開通微信支付功能,如
查看開通流程
統(tǒng)一下單的接口文檔:
查看接口
開發(fā)
①下載sdk:
②可以導入包
在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權限
<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"/>
調用統(tǒng)一下單接口
1.務必提交必須的字段:appid,body,mch_id,nonce_str,notify_url, out_trade_no,spbill_create_ip,total_fee,trade_type,sign(都是小寫);提交到微信接口時以xml格式提交
2.sign為前面提交的參數按照參數名ASCII碼從小到大排序簽名拼接起來然后進行MD5運算,再將得到的字符串所有字符轉換為大寫得到的,如簽名生成算法
3.參與生成sign的key為商戶賬號的密鑰,key設置路徑如下:微信商戶平臺(pay.weixin.qq.com)–>賬戶設置–>API安全–>密鑰設置
下面是具體代碼(如若查看你的sign生成及提交的xml是否正確可以點擊如下:簽名生成工具)
//拼接字段,順序不能變 String A = "appid=你的appID" + "&body=jinshi" + "&mch_id=你的商戶號" + "&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數據 //拼接字段,順序不能變 xml.append("<xml>\n"); xml.append("<appid>你的appID</appid>\n"); xml.append("<body>jinshi</body>\n"); xml.append("<mch_id>你的商戶號</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();// 獲取返回數據 // 使用輸出流來輸出字符(可選) 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(" 微信返回數據 ", " --- " + string); out.close(); } catch (Exception e) { System.out.println(e); } }
注意在調用上面的方法,一定要在子線程中進行
new Thread(new Runnable() { @Override public void run() { httpThreadxml(); } }).start();
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
淺談Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File
這篇文章主要介紹了淺談Android Studio 3.0 工具新特性的使用 Android Profiler 、Device File Explorer的相關資料,需要的朋友可以參考下2017-11-11android: targetSdkVersion升級中Only fullscreen activities can r
這篇文章主要給大家介紹了關于Android target SDK和build tool版本升級中遇到Only fullscreen activities can request orientation問題的解決方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-09-09Android獲取arrays.xml里的數組字段值實例詳解
這篇文章主要介紹了Android獲取arrays.xml里的數組字段值實例詳解的相關資料,需要的朋友可以參考下2017-04-04Android?Jetpack?Compose開發(fā)實用小技巧
這篇文章主要為大家介紹了Android?Jetpack?Compose開發(fā)中的一些實用小技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11