前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android实现微信支付的统一下单

Android实现微信支付的统一下单

作者头像
砸漏
发布2020-11-05 11:41:48
7440
发布2020-11-05 11:41:48
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例为大家分享了Android实现微信支付统一下单的具体代码,供大家参考,具体内容如下

准备工作

申请微信开发者账号,添加应用及申请开通微信支付功能,如 查看开通流程

统一下单的接口文档: 查看接口

开发

①下载sdk:

sdk和demo下载

②可以导入包

在build.gradle文件中,添加如下依赖即可:

代码语言:javascript
复制
dependencies {
 compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}

代码语言:javascript
复制
dependencies {
 compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}

③添加Android Manifest权限

代码语言:javascript
复制
<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是否正确可以点击如下:签名生成工具)

代码语言:javascript
复制
//拼接字段,顺序不能变
    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();

接下来提交到微信下单的接口上

代码语言:javascript
复制
 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);
  }
}

注意在调用上面的方法,一定要在子线程中进行

代码语言:javascript
复制
new Thread(new Runnable() {
    @Override
    public void run() {
     httpThreadxml();
   }
 }).start();

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档