android 微信支付:统一下单接口获取

1、什么是[统一下单接口]?

首先我们要明白这个问题,需要先行看一下微信的官方文档:
https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
 

2.开发

 //微信支付,生成统一订单
    private void wxPay(String order_id) {
        String appid = WeChatInfo.WX_APP_ID;//app在微信申请的appid
        String mch_id = WeChatInfo.WX_MCH_ID;//申请支付功能的商户号
        String nonce_str = WeChatField.getRandomString();//随机码
        String body = "黄金月卡会员";//商品描述,随便写
//        String out_trade_no = UtilData.generateOrderSN();//订单编号
        String out_trade_no = order_id;//订单编号
//        (Math.Round((decimal)order.Amount * 100, 0)).ToString()
        String total_fee = "1";//总金额
//        String time_start = UtilData.getCurrentTime();//时间戳,格式yyyyMMddHHmmss
        String attach = "会员";//附加数据:深圳分店
        String trade_type = "APP";//交易类型
        String notify_url = "http://www.xxxx.net";//通知回调地址,然后后台做个回调,无参的,必须放到商户平台上配置添加
        String spbill_create_ip = WeChatField.getIPAddress(mContext);//设备ip
        SortedMap<String, String> params = new TreeMap<String, String>();
        params.put("appid", appid);
        params.put("mch_id", mch_id);
        params.put("nonce_str", nonce_str);
        params.put("attach", attach);
        params.put("body", body);
        params.put("out_trade_no", out_trade_no);
        params.put("total_fee", total_fee );
        params.put("trade_type", trade_type);
        params.put("notify_url", notify_url);
        params.put("spbill_create_ip", spbill_create_ip);

        XLog.e(XLog.getTag(),"---------out_trade_no:"+out_trade_no);

        //获取签名sign,文档有些参数是必须传的,但是少了还是能请求成功xml
        String sign = WeChatField.getSign(params);
        //参数xml化
        String xmlParams = WeChatField.parseString2Xml(params, sign);
        XLog.e(XLog.getTag(),"------下单xml化---->" + "\n" + xmlParams);
        String urlStr = "https://api.mch.weixin.qq.com/pay/unifiedorder";//下单统一接口

        try {
            final byte[] xmlbyte = xmlParams.getBytes("UTF-8");
            URL url = new URL(urlStr);
            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){
                XLog.e(XLog.getTag(),"----------请求url失败-");
                return;
            }
            InputStream is = conn.getInputStream();// 获取返回数据
            // 使用输出流来输出字符(可选)
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len;
   
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值