欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現微信支付的統(tǒng)一下單

 更新時間:2018年06月15日 10:49:40   作者:Dr_abandon  
這篇文章主要為大家詳細介紹了Android實現微信支付的統(tǒng)一下單,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現微信支付統(tǒng)一下單的具體代碼,供大家參考,具體內容如下

準備工作

申請微信開發(fā)者賬號,添加應用及申請開通微信支付功能,如
查看開通流程

統(tǒng)一下單的接口文檔:
查看接口

開發(fā)

①下載sdk:

sdk和demo下載

②可以導入包

在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 +
      "&notify_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();

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論