From b767820b29147e4687df222b1bc954c82fdfb1f9 Mon Sep 17 00:00:00 2001 From: zhangqin <1245366907@qq.com> Date: Fri, 2 Mar 2018 15:51:31 +0800 Subject: [PATCH] fix wechat --- ReadMe.md | 79 +------ quicklib/build.gradle | 2 +- .../module/wechat/pay/WechatPayTools.java | 196 ++---------------- 3 files changed, 21 insertions(+), 256 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 6f4f8e2..5072798 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -18,6 +18,8 @@ minSdkVersion 16 targetSdkVersion 27 +# 微信支付无法获取结果回调,正在修复 + # 注意事项 1. 当前项目依赖Qmui 1.0.7,即在主项目中配置了Qmui,quicklib中的样式无需再次配置就与主项目中的样式一致,后期可能会脱离出来 @@ -69,80 +71,3 @@ # Apk http://fir.im/x97v -# 本项目中的支付与二维码模块clone自RxTools - -https://github.com/vondear/RxTools - -## 支付工具类 -### 支付宝支付: - - AliPayTools.aliPay(mContext, - APP_ID,//支付宝分配的APP_ID - isRSA2,//是否是 RSA2 加密 - RSA_PRIVATE,// RSA 或 RSA2 字符串 - new AliPayModel(order_id,//订单ID (唯一) - money,//价格 - name,//商品名称 - detail),//商品描述详情 (用于显示在 支付宝 的交易记录里) - new onRequestListener() { - @Override - public void onSuccess(String s) {RxToast.success("支付成功");} - - @Override - public void onError(String s) {RxToast.error("支付失败"); - } - }); - -### 微信支付: - -> 第一种情景: 支付操作全部在APP端完成(包括统一下单接口) 即支付过程无后台参与 - - WechatPayTools.wechatPayUnifyOrder(mContext, - WX_APP_ID, //微信分配的APP_ID - WX_PARTNER_ID, //微信分配的 PARTNER_ID (商户ID) - WX_PRIVATE_KEY, //微信分配的 PRIVATE_KEY (私钥) - new WechatModel(order_id, //订单ID (唯一) - money, //价格 - name, //商品名称 - detail), //商品描述详情 - new onRequestListener() { - @Override - public void onSuccess(String s) {} - - @Override - public void onError(String s) {} - }); - -> 第二种情景: 从后台获取到 prepayid(预支付订单ID) 之后,在App端进行支付操作 - - wechatPayApp(mContext, - app_id, //微信分配的APP_ID - partner_id, //微信分配的 PARTNER_ID (商户ID) - wx_private_key, //微信分配的 PRIVATE_KEY (私钥) - prepay_id, //订单ID (唯一) - new onRequestListener() { - @Override - public void onSuccess(String s) {} - - @Override - public void onError(String s) {} - }); - -### 微信分享: - -> 分享网页 - - WechatShareTools.init(mContext, WX_APP_ID);//初始化 - - String url = "https://github.com/vondear/RxTools";//网页链接 - - String description = "工欲善其事必先利其器!";//描述 - - Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);//获取Bitmap - byte[] bitmapByte = RxImageTool.bitmap2Bytes(bitmap, Bitmap.CompressFormat.PNG);//将 Bitmap 转换成 byte[] - - mWechatShareModel = new WechatShareModel(url, "APP名称", description, bitmapByte); - - //Friend 分享微信好友,Zone 分享微信朋友圈,Favorites 分享微信收藏 - WechatShareTools.shareURL(mWechatShareModel, WechatShareTools.SharePlace.Friend);//分享操作 - diff --git a/quicklib/build.gradle b/quicklib/build.gradle index 2e1cb89..745d6c1 100644 --- a/quicklib/build.gradle +++ b/quicklib/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'com.jfrog.bintray' -version = "1.1.7" +version = "1.1.7_beta1" // gradlew install // gradlew bintrayUpload diff --git a/quicklib/src/main/java/com/sdwfqin/quicklib/module/wechat/pay/WechatPayTools.java b/quicklib/src/main/java/com/sdwfqin/quicklib/module/wechat/pay/WechatPayTools.java index c4db4f6..b1662e4 100644 --- a/quicklib/src/main/java/com/sdwfqin/quicklib/module/wechat/pay/WechatPayTools.java +++ b/quicklib/src/main/java/com/sdwfqin/quicklib/module/wechat/pay/WechatPayTools.java @@ -1,141 +1,27 @@ package com.sdwfqin.quicklib.module.wechat.pay; import android.content.Context; -import android.util.Log; import com.blankj.utilcode.util.LogUtils; import com.google.gson.Gson; import com.sdwfqin.quicklib.module.interfaces.OnRequestListener; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.Random; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; - /** - * 描述:微信下单(一般由服务器来做) - * * @author 张钦 * @date 2018/1/25 */ public class WechatPayTools { - //微信统一下单接口 - public static final String WX_TOTAL_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder"; - - /** - * 商户发起生成预付单请求 - * - * @return - */ - public static String wechatPayUnifyOrder(final Context mContext, final String appid, final String mch_id, final String wx_private_key, WechatModel wechatModel, final OnRequestListener OnRequestListener) { - String nonce_str = getRandomStringByLength(8);//随机码 - String body = wechatModel.getDetail();//商品描述 - String out_trade_no = wechatModel.getOut_trade_no();//商品订单号 - String product_id = wechatModel.getOut_trade_no();//商品编号 - String total_fee = wechatModel.getMoney();//总金额 分 - String time_start = getCurrTime();//交易起始时间(订单生成时间非必须) - String trade_type = "APP";//App支付 - String notify_url = "https://github.com/vondear/RxTools";//"http://" + "域名" + "/" + "项目名" + "回调地址.do";//回调函数 - SortedMap params = new TreeMap(); - params.put("appid", appid); - params.put("mch_id", mch_id); - params.put("device_info", "WEB"); //设备号 - params.put("nonce_str", nonce_str); - params.put("body", body);//商品描述 - params.put("out_trade_no", out_trade_no); - params.put("product_id", product_id); - params.put("total_fee", total_fee); - params.put("time_start", time_start); - params.put("trade_type", trade_type); - params.put("notify_url", notify_url); - String sign = "";//签名(该签名本应使用微信商户平台的API证书中的密匙key,但此处使用的是微信公众号的密匙APP_SECRET) - sign = getSign(params, wx_private_key); - //参数xml化 - String xmlParams = parseString2Xml(params, sign); - //判断返回码 - final String[] jsonStr = {""}; - - OkHttpClient client = new OkHttpClient(); - RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=utf-8"), xmlParams); - Request request = new Request.Builder() - .post(requestBody) - .url(WX_TOTAL_ORDER) - .build(); - client.newCall(request).enqueue(new Callback() { - @Override - public void onFailure(Call call, IOException e) { - - } - - @Override - public void onResponse(Call call, Response response) throws IOException { - String s = response.body().string(); - Log.d("微信统一下单", s); - jsonStr[0] = s; - - Map mapXml = null; - try { - mapXml = getMapFromXML(s); - } catch (ParserConfigurationException | IOException | SAXException e) { - e.printStackTrace(); - } - String time = getCurrTime(); - - SortedMap params = new TreeMap(); - params.put("appid", appid); - params.put("noncestr", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"); - params.put("package", "Sign=WechatPay"); - params.put("partnerid", mch_id); - params.put("prepayid", mapXml.get("prepay_id")); - params.put("timestamp", time); - - wechatPayApp(mContext, appid, mch_id, wx_private_key, params, OnRequestListener); - } - }); - - if (!jsonStr[0].contains("FAIL") && jsonStr[0].trim().length() > 0) {//成功 - return "success"; - } else {//失败 - return "fail"; - } - } - - public static void wechatPayApp(Context mContext, String appid, String mch_id, String wx_private_key, SortedMap params, OnRequestListener onRxHttp) { - String sign = getSign(params, wx_private_key); - - WechatPayModel wechatPayModel = new WechatPayModel(appid, mch_id, params.get("prepayid"), "Sign=WechatPay", params.get("noncestr"), params.get("timestamp"), sign); - String pay_param = new Gson().toJson(wechatPayModel); - WechatPayTools.doWXPay(mContext, appid, pay_param, onRxHttp); - } - - public static void wechatPayApp(Context mContext, String app_id, String partner_id, String wx_private_key, String prepay_id, OnRequestListener onRxHttp) { + public static void wechatPayApp(Context mContext, String app_id, String partner_id, + String wx_private_key, String prepay_id, OnRequestListener onRequestListener) { SortedMap params = new TreeMap(); params.put("appid", app_id); params.put("noncestr", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"); @@ -148,31 +34,24 @@ public static void wechatPayApp(Context mContext, String app_id, String partner_ WechatPayModel wechatPayModel = new WechatPayModel(app_id, partner_id, prepay_id, "Sign=WechatPay", params.get("noncestr"), params.get("timestamp"), sign); String pay_param = new Gson().toJson(wechatPayModel); - WechatPayTools.doWXPay(mContext, app_id, pay_param, onRxHttp); + WechatPayTools.doWXPay(mContext, app_id, pay_param, onRequestListener); } - /** - * 参数进行XML化 - * - * @param map,sign - * @return - */ - public static String parseString2Xml(Map map, String sign) { - StringBuffer sb = new StringBuffer(); - sb.append(""); - Set es = map.entrySet(); - Iterator iterator = es.iterator(); - while (iterator.hasNext()) { - Map.Entry entry = (Map.Entry) iterator.next(); - String k = (String) entry.getKey(); - String v = (String) entry.getValue(); - sb.append("<" + k + ">" + v + ""); - } - sb.append("" + sign + ""); - sb.append(""); - return sb.toString(); - } + public static void wechatPayApp(Context mContext, String app_id, String noncestr, + String wpackage, String partner_id, String prepay_id, + String timestamp, String sign, OnRequestListener onRequestListener) { + SortedMap params = new TreeMap<>(); + params.put("appid", app_id); + params.put("noncestr", noncestr); + params.put("package", wpackage); + params.put("partnerid", partner_id); + params.put("prepayid", prepay_id); + params.put("timestamp", timestamp); + WechatPayModel wechatPayModel = new WechatPayModel(app_id, partner_id, prepay_id, wpackage, noncestr, timestamp, sign); + String pay_param = new Gson().toJson(wechatPayModel); + WechatPayTools.doWXPay(mContext, app_id, pay_param, onRequestListener); + } /** * 获取签名 md5加密(微信支付必须用MD5加密) @@ -199,23 +78,6 @@ public static String getSign(SortedMap params, String wx_private return sign; } - /** - * 获取一定长度的随机字符串 - * - * @param length 指定字符串长度 - * @return 一定长度的字符串 - */ - public static String getRandomStringByLength(int length) { - String base = "abcdefghijklmnopqrstuvwxyz0123456789"; - Random random = new Random(); - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < length; i++) { - int number = random.nextInt(base.length()); - sb.append(base.charAt(number)); - } - return sb.toString(); - } - /** * 获取当前时间 yyyyMMddHHmmss * @@ -228,29 +90,6 @@ public static String getCurrTime() { return s; } - //xml解析 - public static Map getMapFromXML(String xmlString) throws ParserConfigurationException, IOException, SAXException { - //这里用Dom的方式解析回包的最主要目的是防止API新增回包字段 - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - InputStream is = new ByteArrayInputStream(xmlString.getBytes()); - Document document = builder.parse(is); - //获取到document里面的全部结点 - NodeList allNodes = document.getFirstChild().getChildNodes(); - Node node; - Map map = new HashMap(); - int i = 0; - while (i < allNodes.getLength()) { - node = allNodes.item(i); - if (node instanceof Element) { - map.put(node.getNodeName(), node.getTextContent()); - } - i++; - } - return map; - } - - public static void doWXPay(Context mContext, String wx_appid, String pay_param, final OnRequestListener onRxHttpString) { WechatPay.init(mContext, wx_appid);//要在支付前调用 WechatPay.getInstance().doPay(pay_param, new WechatPay.WXPayResultCallBack() { @@ -262,6 +101,7 @@ public void onSuccess() { @Override public void onError(int error_code) { + LogUtils.e(error_code); switch (error_code) { case WechatPay.NO_OR_LOW_WX: LogUtils.e("未安装微信或微信版本过低");