博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android微信生成支付二维码URL
阅读量:5771 次
发布时间:2019-06-18

本文共 9767 字,大约阅读时间需要 32 分钟。

生成微信支付二维码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
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
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
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("
"); requestStr.append("
"); requestStr.append(""); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); // 发送请求 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("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); requestStr.append("
"); // 发送请求 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(); }}复制代码

转载于:https://juejin.im/post/5a41a3af6fb9a0451969d515

你可能感兴趣的文章
痞子衡嵌入式:串口调试工具Jays-PyCOM诞生记(1)- 环境搭建(Python2.7.14 + pySerial3.4 + wxPython4.0.3)...
查看>>
搭建httpd服务
查看>>
使用sort()时的一点注意
查看>>
11、ViewModelLocator-Prism的MVVM一血
查看>>
SQLite 入门教程(一)基本控制台(终端)命令 (转)
查看>>
[Usaco2006 Dec]Wormholes
查看>>
window编译caffe总结
查看>>
C#之Invoke学习
查看>>
响应式网页设计中的视频处理技巧
查看>>
find the nth digit
查看>>
工作框架各种使用整理---按层次返回相关联的数据
查看>>
诚信、透明、公平、分享
查看>>
高一寒假集训总结
查看>>
bzoj3555[Ctsc2014]企鹅QQ
查看>>
微信分享config:ok 但自定义内容无效
查看>>
OPENGL和DX的不同.
查看>>
Linux下如何删除非空目录
查看>>
原生js模拟jquery写法
查看>>
java jar 服务自启动存在的坑及解决办法
查看>>
Servlet学习 (HttpServletRequest HttpServletResponse)
查看>>