您好,登录后才能下订单哦!
本文实例为大家分享了Android实现微信支付统一下单的具体代码,供大家参考,具体内容如下
准备工作
申请微信开发者账号,添加应用及申请开通微信支付功能,如 
查看开通流程
统一下单的接口文档: 
查看接口
开发
①下载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"/>
调用统一下单接口
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");//发送自定义的头信息
   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();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。