Android微信生成支付二维码URL
来源:互联网 发布:java log4j 格式 编辑:程序博客网 时间:2024/05/17 10:39
生成微信支付二维码URL,让别人扫一扫收钱,就是微信的收钱码
/***微信二维码支付**/public class WXPayUtils { private static String strResponse = null; private static String url = ""; public static String nonceStr = ""; public static String outTradeNo = ""; /** * 微信生成签名 */ private static String genPackageSign(List<NameValuePair> params) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < params.size(); i++) { sb.append(params.get(i).getName()); sb.append('='); sb.append(params.get(i).getValue()); sb.append('&'); } sb.append("key="); sb.append(AlipayUtils.key); String packageSign = MD5.getMessageDigest(sb.toString().getBytes()) .toUpperCase(); Log.e("orion", packageSign); return packageSign; } /*** 微信获取签名 **/ public static String genProductArgs(String nonceStr, String outTradeNo, String totalFee) { try { List<NameValuePair> packageParams = new LinkedList<>(); packageParams.add(new BasicNameValuePair("appid", AlipayUtils.appid)); packageParams.add(new BasicNameValuePair("body", AlipayUtils.bodyname)); packageParams .add(new BasicNameValuePair("mch_id", AlipayUtils.mchid)); packageParams.add(new BasicNameValuePair("nonce_str", nonceStr)); packageParams.add(new BasicNameValuePair("notify_url", "http://www.weixin.qq.com/wxpay/pay.php")); packageParams .add(new BasicNameValuePair("out_trade_no", outTradeNo)); packageParams.add(new BasicNameValuePair("spbill_create_ip", AlipayUtils.ip)); packageParams.add(new BasicNameValuePair("total_fee", totalFee)); packageParams.add(new BasicNameValuePair("trade_type", "NATIVE")); String sign = genPackageSign(packageParams); return sign; } catch (Exception e) { LogUtils.saveFileToSMB(LogUtils.getExceptionInfo(e)); e.printStackTrace(); return null; } } /*** 微信获取签名 **/ public static String QgenProductArgs(String nonceStr, String outTradeNo) { try { List<NameValuePair> packageParams = new LinkedList<>(); packageParams.add(new BasicNameValuePair("appid", AlipayUtils.appid)); packageParams .add(new BasicNameValuePair("mch_id", AlipayUtils.mchid)); packageParams.add(new BasicNameValuePair("nonce_str", nonceStr)); packageParams .add(new BasicNameValuePair("out_trade_no", outTradeNo)); String sign = genPackageSign(packageParams); return sign; } catch (Exception e) { LogUtils.saveFileToSMB(LogUtils.getExceptionInfo(e)); e.printStackTrace(); return null; } } /** 微信获取随机字符串nonce_str **/ public static String getNonceStr() { Random random = new Random(); nonceStr = MD5.getMessageDigest(String.valueOf( System.currentTimeMillis() + random.nextInt(10000)).getBytes()); return nonceStr; } /** 微信获取随机字符串out_trade_no **/ public static String getOutTradNo() { Random random = new Random(); outTradeNo = MD5.getMessageDigest(String.valueOf( System.currentTimeMillis() + random.nextInt(10000)).getBytes()); return outTradeNo; } /**** 微信生成二维码URL **/ public static void sendWxPayRequest(final String body, final String nonceStr, final String outTradeNo, final String totalFee, final Handler handler) { new Thread(new Runnable() { @Override public void run() { // 构造HTTP请求 HttpClient httpclient = new HttpClient(); PostMethod postMethod = new PostMethod( "https://api.mch.weixin.qq.com/pay/unifiedorder"); StringBuffer requestStr = new StringBuffer("<xml>"); requestStr.append("<appid><![CDATA["); requestStr.append(AlipayUtils.appid); requestStr.append("]]></appid>"); requestStr.append("<body><![CDATA["); requestStr.append(body); requestStr.append("]]></body>"); requestStr.append("<mch_id><![CDATA["); requestStr.append(AlipayUtils.mchid); requestStr.append("]]></mch_id>"); requestStr.append("<nonce_str><![CDATA["); requestStr.append(nonceStr); requestStr.append("]]></nonce_str>"); requestStr.append("<notify_url><![CDATA["); requestStr.append("http://www.weixin.qq.com/wxpay/pay.php"); requestStr.append("]]></notify_url>"); requestStr.append("<out_trade_no><![CDATA["); requestStr.append(outTradeNo); requestStr.append("]]></out_trade_no>"); requestStr.append("<spbill_create_ip><![CDATA["); requestStr.append(AlipayUtils.ip); requestStr.append("]]></spbill_create_ip>"); requestStr.append("<total_fee><![CDATA["); requestStr.append(totalFee); requestStr.append("]]></total_fee>"); requestStr.append("<trade_type><![CDATA["); requestStr.append("NATIVE"); requestStr.append("]]></trade_type>"); requestStr.append("<sign><![CDATA["); requestStr.append(WXPayUtils.genProductArgs(nonceStr, outTradeNo, totalFee)); requestStr.append("]]></sign>"); requestStr.append("</xml>"); // 发送请求 try { RequestEntity entity = new StringRequestEntity( requestStr.toString(), "text/xml", "UTF-8"); postMethod.setRequestEntity(entity); httpclient.executeMethod(postMethod); strResponse = new String(postMethod.getResponseBody(), "utf-8"); Log.e("strResponse", strResponse);// LogUtils.saveToFile(strResponse); Message msg = Message.obtain(); if (!"FAIL".equals(strResponse.split("CDATA\\[")[1].split("]]")[0])){ url = "weixin" + strResponse.split("weixin")[1].split("]]")[0]; msg.what = 0; msg.obj = url; }else { msg.what = 8; } handler.sendMessage(msg); } catch (HttpException e) { LogUtils.saveFileToSMB(LogUtils.getExceptionInfo(e)); e.printStackTrace(); } catch (IOException e) { LogUtils.saveFileToSMB(LogUtils.getExceptionInfo(e)); e.printStackTrace(); } finally { postMethod.releaseConnection(); } } }).start(); } /**** 微信查询订单状态 **/ public static void queryWxPayRequest(final String nonceStr, final String outTradeNo, final Handler handler) { new Thread(new Runnable() { @Override public void run() { // 构造HTTP请求 HttpClient httpclient = new HttpClient(); PostMethod postMethod = new PostMethod( "https://api.mch.weixin.qq.com/pay/orderquery"); StringBuffer requestStr = new StringBuffer("<xml>"); requestStr.append("<appid><![CDATA["); requestStr.append(AlipayUtils.appid); requestStr.append("]]></appid>"); requestStr.append("<mch_id><![CDATA["); requestStr.append(AlipayUtils.mchid); requestStr.append("]]></mch_id>"); requestStr.append("<nonce_str><![CDATA["); requestStr.append(nonceStr); requestStr.append("]]></nonce_str>"); requestStr.append("<out_trade_no><![CDATA["); requestStr.append(outTradeNo); requestStr.append("]]></out_trade_no>"); requestStr.append("<sign><![CDATA["); requestStr.append(WXPayUtils.QgenProductArgs(nonceStr, outTradeNo)); requestStr.append("]]></sign>"); requestStr.append("</xml>"); // 发送请求 try { RequestEntity entity = new StringRequestEntity(requestStr .toString(), "text/xml", "UTF-8"); postMethod.setRequestEntity(entity); httpclient.executeMethod(postMethod); strResponse = new String(postMethod.getResponseBody(), "utf-8"); Log.e("strResponse", strResponse); String state = ""; if (!"FAIL".equals(strResponse.split("CDATA\\[")[1].split("]]")[0])){ state = strResponse.split("trade_state")[1] .split("\\[")[2].split("]]")[0]; } Message msg = Message.obtain(); msg.what = 1; msg.obj = state; handler.sendMessage(msg); Log.e("state", state); } catch (Exception e) { LogUtils.saveFileToSMB(LogUtils.getExceptionInfo(e)); e.printStackTrace(); } finally { postMethod.releaseConnection(); } } }).start(); }}
阅读全文
0 0
- Android微信生成支付二维码URL
- 生成微信支付二维码及微信支付
- 微信支付生成二维码方法封装
- 微信支付--二维码
- 微信支付二维码
- 微信支付 生成付款二维码链接 并生成支付二维码图片 .net C#
- 请问下大家,微信扫码支付时,出现:原生支付URL参数错误!请问这具体是什么错误呢? 是按照微信二维码格式生成的内容
- android 微信扫码支付模式一生成二维码 原生支付url参数错误
- 微信二维码扫描支付
- 微信二维码支付接口
- Java 微信二维码支付
- 微信二维码生成
- 微信生成二维码
- 微信二维码生成
- 微信 二维码生成
- 微信生成二维码
- 生成微信二维码
- 微信开发 -- 二维码生成
- 反编译javaEE项目 .war
- 在Linux系统系自带的python shell 模式下支持tab键补全功能
- 成为Java高级架构师必看的15本书
- python -m的那些实用功能
- seo经典教程是怎么用网络赚钱卖出去的
- Android微信生成支付二维码URL
- 自定义会话管理器->SESSION
- java实现文件上传的简单demo
- openCV 中值滤波算法解析
- jquery内容显示与隐藏; toggle()使用
- C++ 字符函数,判断各种字符的出现
- Java 在给定路径上创建文件,所在文件夹不存在时,如何正确创建。
- java.security.InvalidAlgorithmParameterException原因及其处理方式
- 远程桌面无法复制拷贝到本地