美文网首页Android程序员
Android微信支付接入

Android微信支付接入

作者: 吃泥巴的猫 | 来源:发表于2018-12-13 21:57 被阅读18次

    微信支付的appid获取什么的就不提了
    第三方依赖
    implementation 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:5.1.4'
    需要新建包wxapi,新建activity名字需为WXPayEntryActivity并且实现IWXAPIEventHandler接口,该界面为显示微信支付结果页面

    resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX&&String.valueOf(resp.errCode).equals("0")
    

    为支付成功,不过支付成功的判断还是需要请求后台获取。
    微信支付的封装,封装了两种方式
    一种不需要在客户端签名,由后台返回数据,调起微信支付
    另一种是需求需要在客户端签名,由后台返回数据进行签名后调起微信支付

    public class WXPayUtils {
    
    
        private IWXAPI iwxapi; //微信支付api
    
        private WXPayBuilder builder;
    
        private WXPayUtils(WXPayBuilder builder) {
            this.builder = builder;
        }
    
        /**
         * 调起微信支付的方法,不需要在客户端签名
         **/
        public void toWXPayNotSign(Context context, String appid) {
            iwxapi = WXAPIFactory.createWXAPI(context, null); //初始化微信api
            iwxapi.registerApp(appid); //注册appid  appid可以在开发平台获取
    
            Runnable payRunnable = new Runnable() {  //这里注意要放在子线程
                @Override
                public void run() {
                    PayReq request = new PayReq(); //调起微信APP的对象
                    //下面是设置必要的参数,也就是前面说的参数,这几个参数从何而来请看上面说明
                    request.appId = builder.getAppId();
                    request.partnerId = builder.getPartnerId();
                    request.prepayId = builder.getPrepayId();
                    request.packageValue = "Sign=WXPay";
                    request.nonceStr = builder.getNonceStr();
                    request.timeStamp = builder.getTimeStamp();
                    request.sign = builder.getSign();
                    Log.e("chx", "run: " + request.appId + request.nonceStr + request.sign);
                    iwxapi.sendReq(request);//发送调起微信的请求
                }
            };
            Thread payThread = new Thread(payRunnable);
            payThread.start();
        }
    
        /**
         * 调起微信支付的方法,需要在客户端签名
         **/
        public void toWXPayAndSign(Context context, String appid, final String key) {
            iwxapi = WXAPIFactory.createWXAPI(context, null); //初始化微信api
            iwxapi.registerApp(appid); //注册appid  appid可以在开发平台获取
            Runnable payRunnable = new Runnable() {  //这里注意要放在子线程
                @Override
                public void run() {
                    if (TextUtils.isEmpty(builder.getAppId())
                            || TextUtils.isEmpty(builder.getPartnerId())
                            || TextUtils.isEmpty(builder.getPrepayId())) {
                        Log.e("chx", "toWXPayAndSign: " + "必须在builder中设置appId、PartnerId、PrepayId");
                        return;
                    }
                    PayReq request = new PayReq(); //调起微信APP的对象
                    //下面是设置必要的参数,也就是前面说的参数,这几个参数从何而来请看上面说明
                    request.appId = builder.getAppId();
                    request.partnerId = builder.getPartnerId();
                    request.prepayId = builder.getPrepayId();
                    request.packageValue = "Sign=WXPay";
                    request.nonceStr = genNonceStr();
                    request.timeStamp = String.valueOf(genTimeStamp());
                    request.sign = builder.getSign();
                    //签名
                    LinkedHashMap<String, String> signParams = new LinkedHashMap<>();
                    signParams.put("appid", request.appId);
                    signParams.put("noncestr", request.nonceStr);
                    signParams.put("package", request.packageValue);
                    signParams.put("partnerid", request.partnerId);
                    signParams.put("prepayid", request.prepayId);
                    signParams.put("timestamp", request.timeStamp);
                    request.sign = genPackageSign(signParams, key);
                    iwxapi.sendReq(request);//发送调起微信的请求
                }
            };
            Thread payThread = new Thread(payRunnable);
            payThread.start();
        }
    
        /**
         * 调起微信APP支付,签名
         * 生成签名
         */
        private String genPackageSign(LinkedHashMap<String, String> params, String key) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                sb.append(entry.getKey());
                sb.append('=');
                sb.append(entry.getValue());
                sb.append('&');
            }
            sb.append("key=");
            sb.append(key);
            String packageSign = getMessageDigest(sb.toString().getBytes()).toUpperCase();
            return packageSign;
        }
    
        /**
         * md5加密
         *
         * @param buffer
         * @return
         */
        private String getMessageDigest(byte[] buffer) {
            char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            try {
                MessageDigest mdTemp = MessageDigest.getInstance("MD5");
                mdTemp.update(buffer);
                byte[] md = mdTemp.digest();
                int j = md.length;
                char str[] = new char[j * 2];
                int k = 0;
                for (int i = 0; i < j; i++) {
                    byte byte0 = md[i];
                    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                    str[k++] = hexDigits[byte0 & 0xf];
                }
                return new String(str);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 获取随机数
         *
         * @return
         */
        private String genNonceStr() {
            Random random = new Random();
            return getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
        }
    
    
        /**
         * 获取时间戳
         *
         * @return
         */
        private long genTimeStamp() {
            return System.currentTimeMillis() / 1000;
        }
    
        public static class WXPayBuilder {
            public String appId;
            public String partnerId;
            public String prepayId;
            public String packageValue;
            public String nonceStr;
            public String timeStamp;
            public String sign;
    
            public WXPayUtils build() {
                return new WXPayUtils(this);
            }
    
            public String getAppId() {
                return appId;
            }
    
            public WXPayBuilder setAppId(String appId) {
                this.appId = appId;
                return this;
            }
    
            public String getPartnerId() {
                return partnerId;
            }
    
            public WXPayBuilder setPartnerId(String partnerId) {
                this.partnerId = partnerId;
                return this;
            }
    
            public String getPrepayId() {
                return prepayId;
            }
    
            public WXPayBuilder setPrepayId(String prepayId) {
                this.prepayId = prepayId;
                return this;
            }
    
            public String getPackageValue() {
                return packageValue;
            }
    
            public WXPayBuilder setPackageValue(String packageValue) {
                this.packageValue = packageValue;
                return this;
            }
    
            public String getNonceStr() {
                return nonceStr;
            }
    
            public WXPayBuilder setNonceStr(String nonceStr) {
                this.nonceStr = nonceStr;
                return this;
            }
    
            public String getTimeStamp() {
                return timeStamp;
            }
    
            public WXPayBuilder setTimeStamp(String timeStamp) {
                this.timeStamp = timeStamp;
                return this;
            }
    
            public String getSign() {
                return sign;
            }
    
            public WXPayBuilder setSign(String sign) {
                this.sign = sign;
                return this;
            }
        }
    
    
    }
    

    调用方法,从后台获取到数据后设置调用微信支付(该为无需客户端签名的调用方法)

    WXPayUtils.WXPayBuilder builder = new WXPayUtils.WXPayBuilder();
                            JSONObject object = jsonObject.optJSONObject("obj");
                            builder.setAppId(object.optString("appid"))
                                    .setPartnerId(object.optString("partnerid"))
                                    .setPrepayId(object.optString("prepayid"))
                                    .setPackageValue(object.optString("package"))
                                    .setNonceStr(object.optString("noncestr"))
                                    .setTimeStamp(object.optString("timestamp"))
                                    .setSign(object.optString("sign"))
                                    .build().toWXPayNotSign(MainActivity.this, object.optString("appid"));
    

    需要后台签名的调用方法toWXPayAndSign
    不会写后台,没有微信支付权限的我,今天没有demo~~~

    相关文章

      网友评论

        本文标题:Android微信支付接入

        本文链接:https://www.haomeiwen.com/subject/jvrihqtx.html