android微信和支付宝支付

1 支付宝支付 相对于微信来说支付宝官方demo已经很好了,但是支付宝demo有个缺点就是在demo里面演示的时候把后台和前段做得混到一起了,
   虽然说已经说明了,但是对于刚开始集成的我们来说真的头大,下面我总结一下支付宝客户端要做得事情。


1 确认订单接口
 
 /**
     * 确认订单接口
     * 必填
     * 1107001:微信
     * 1107002:支付宝
     */
    private void VerifyOrderinInfosInterface(String mOrderinfosBean, String payfor) {
        if (null == mProgressDialog) {
            mProgressDialog = new ProgressDialog(SelectOrderInfoActivity.this);
        }
        mProgressDialog.show();
        Map map = new TreeMap();
        map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
        map.put(GsonUtils.orderCode, mOrderinfosBean);//订单编号
        map.put(GsonUtils.payWay, payfor);//支付方式
        Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key进行排序
        String sn = GsonUtils.buildSN(resultMap);
        try {
            sn = URLEncoder.encode(sn, "UTF-8");
            sn = MD5Coder.getMD5Code(sn);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        map.put(GsonUtils.sn, sn);
        String json = GsonUtils.buildJson(map);


        try {
            NetUtils.getmInstance().postJsons(NetGlobalParams.CAPTIONINITOKINFO, json, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    UIHelper.ToastMessage(SelectOrderInfoActivity.this, "网络连接超时");
                    mProgressDialog.dismiss();
                }


                @Override
                public void onResponse(String response, int id) {
                    mPayWayBeans = GsonUtils.GsonToBean(response, PayWayBeans.class);
                    if (mPayWayBeans.getStatus() == 0) {//请求成功
                        Log.i(response, "onResponse: " + response);
                        //得到订单编号
                        //  String ppayFororderCode = mPayWayBeans.getDetail().getOrderCode();
                        //获取签名后的订单信息 ,需要订单编号和订单金额
                        if (orderCode != null) {
                            getAlipayOderInfo(orderCode, mPriceTotal);
                            //得到签名订单信息后调用支付宝pay方法支付
                        }
                    }
                }




            });


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
2 获取签名后的订单信息
 /**
     * 获取订单签名后的信息
     *
     * @param payFororderCode
     * @param mPriceTotal
     */
    private void getAlipayOderInfo(String payFororderCode, double mPriceTotal) {
        Map map = new TreeMap();
        map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
        if (!MyApplication.getInstance().getPreferencesUtils().getUserid().equals("")) {
            map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid());
            map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid());//订单编号
        }
        map.put(GsonUtils.out_trade_no, payFororderCode);//订单编号
        map.put(GsonUtils.total_amount, String.valueOf(mPriceTotal));
        Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key进行排序
        String sn = GsonUtils.buildSN(resultMap);
        try {
            sn = URLEncoder.encode(sn, "UTF-8");
            sn = MD5Coder.getMD5Code(sn);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        map.put(GsonUtils.sn, sn);
        String json = GsonUtils.buildJson(map);


        try {
            NetUtils.getmInstance().postJsons(NetGlobalParams.APLPAYAPP, json, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Log.i("onError", "onResponse: " + e);
                }


                @Override
                public void onResponse(String response, int id) {
                    Log.i("responseres", "onResponse: " + response);
                    //  alipay(fffff);
                    mPayForApp = GsonUtils.GsonToBean(response, PayForApp.class);
                    if (mPayForApp != null) {
                        if (mPayForApp.getStatus() == 0) {//请求成功
                            String detail = mPayForApp.getDetail();
                            alipay(detail);
                            mProgressDialog.dismiss();
                        }
                    }


                }




            });


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}




3 同步通知自己的app支付结果;(注意异步的已经交给后台了,后台那边支付结果会相应的修改订单状态的)
 private void alipay(final String orderInfo) {


        Runnable payRunnable = new Runnable() {


            @Override
            public void run() {
                PayTask alipay = new PayTask(SelectOrderInfoActivity.this);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());


                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };


        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }


  @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
                        Toast.makeText(SelectOrderInfoActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        // 该笔订单真实的支付结果,需要依赖服务端的异步通知。
                        Toast.makeText(SelectOrderInfoActivity.this, "支付失败", Toast.LENGTH_SHORT).show();


                    }
                    break;
                }
                case SDK_AUTH_FLAG:




                default:
                    break;
            }
        }


        ;
    };




  支付宝支付很简单没有像微信那样有很多坑,加密,私钥,商户id等都在后台做,前端只需要传入订单账号和支付的选择方式即可
2 微信支付


  微信支付坑太多,主要总结一下和对应的解决方法


        1 WXpayEntryActivity 包名和类名必须一致;坑------微信demo里面的是WXEntryActivity导致支付不了
        2 清单文件 
          
     <activity
            android:name="com.dplustours.b2c.wxapi.WXPayEntryActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="你自己的appid" />
            </intent-filter>
        </activity>
        <receiver
            android:name="com.dplustours.b2c.View.receiver.AppRegister"
            android:permission="com.tencent.mm.plugin.permission.SEND">
            <intent-filter>
                <action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP" />
            </intent-filter>
        </receiver>
 3 微信支付没有网页支付,ios那边有时候会跳转出来,反正android没有 支付宝有网页支付,多以支付前要判断微信客户端安装了没有


4 打包签名,这个是最重要的,签名的keystore要和平台申请的一致,微信测试必须要签名打包去验证平台的key,验证成功才可以打开app里面的微信支付页面,要是 打不开就去找申请负责的人,去验证keystore的MD5字符串是否正确
 
      
 /**
     * 判断微信客户端是否存在
     *
     * @param appid
     * @return
     */
    private boolean isWXAppInstalledAndSupported(String appid) {
        IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
        msgApi.registerApp(appid);


        boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled() ;//&& msgApi.isWXAppSupportAPI()


        return sIsWXAppInstalledAndSupported;
    }


下面是微信支付流程,客户端走流程


 /**
     * 微信支付
     *
     * @param orderCode
     * @param s
     */
    private void VerifyOrderinInfosInterfaceWeixin(final String orderCode, String s) {
        if (null == mProgressDialog) {
            mProgressDialog = new ProgressDialog(SelectOrderInfoActivity.this);
        }
        mProgressDialog.show();
        Map map = new TreeMap();
        map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
        map.put(GsonUtils.orderCode, orderCode);//订单编号
        map.put(GsonUtils.payWay, s);//支付方式
        Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key进行排序
        String sn = GsonUtils.buildSN(resultMap);
        try {
            sn = URLEncoder.encode(sn, "UTF-8");
            sn = MD5Coder.getMD5Code(sn);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        map.put(GsonUtils.sn, sn);
        String json = GsonUtils.buildJson(map);


        try {
            NetUtils.getmInstance().postJsons(NetGlobalParams.CAPTIONINITOKINFO, json, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    UIHelper.ToastMessage(SelectOrderInfoActivity.this, "网络连接超时");
                    mProgressDialog.dismiss();
                }


                @Override
                public void onResponse(String response, int id) {
                    mPayWayBeans = GsonUtils.GsonToBean(response, PayWayBeans.class);
                    if (mPayWayBeans.getStatus() == 0) {//请求成功
                        Log.i(response, "onResponse: " + response);
                        //得到订单编号
                        //  String ppayFororderCode = mPayWayBeans.getDetail().getOrderCode();
                        //获取签名后的订单信息 ,需要订单编号和订单金额


                        if (orderCode != null) {
                            getAlipayOderInfoWeixing(orderCode);
                            //得到签名订单信息后调用支付宝pay方法支付




                        }


                    }
                }




            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }




  /**
     * 微信zhifu
     *
     * @param orderCode
     */
    private void getAlipayOderInfoWeixing(String orderCode) {
        Map map = new TreeMap();
//        map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
//        if (MyApplication.getInstance().getPreferencesUtils().getUserid() != null && MyApplication.getInstance().getPreferencesUtils().getSessionid() != null) {
//            map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid().toString());
//            map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid().toString());
//        }
//        map.put(GsonUtils.out_trade_no, orderCode);//订单编号
//        Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key进行排序
//        String sn = GsonUtils.buildSN(resultMap);
//
        map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
        if (!MyApplication.getInstance().getPreferencesUtils().getUserid().equals("")) {
            map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid());
            map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid());//订单编号
        }
        map.put(GsonUtils.out_trade_no, orderCode);//订单编号
        map.put(GsonUtils.total_amount, String.valueOf(mPriceTotal));
        Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key进行排序
        String sn = GsonUtils.buildSN(resultMap);
        try {
            sn = URLEncoder.encode(sn, "UTF-8");
            sn = MD5Coder.getMD5Code(sn);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        map.put(GsonUtils.sn, sn);
        String json = GsonUtils.buildJson(map);


        try {
            NetUtils.getmInstance().postJsons(NetGlobalParams.PAYWEIXIN, json, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    UIHelper.ToastMessage(SelectOrderInfoActivity.this, "网络连接超时");
                    mProgressDialog.dismiss();
                }


                @Override
                public void onResponse(String response, int id) {
                    Log.i(response, "onResponse: " + response);
                    mProgressDialog.dismiss();
                    try {
                        PayWayWeixinBeans mPayWayWeixinBeans = GsonUtils.GsonToBean(response, PayWayWeixinBeans.class);
                        if (mPayWayWeixinBeans != null) {
                            if (mPayWayWeixinBeans.getDetail() != null) {


                                //开始跳转到微信界面进行支付
                                String appid = mPayWayWeixinBeans.getDetail().getAppid();
                                String timestamp = mPayWayWeixinBeans.getDetail().getTimestamp();
                                String noncestr = mPayWayWeixinBeans.getDetail().getNoncestr();
                                String packageX = mPayWayWeixinBeans.getDetail().getPackageX();
                                String partnerid = mPayWayWeixinBeans.getDetail().getPartnerid();
                                String sign = mPayWayWeixinBeans.getDetail().getSign();
                                String prepayid = mPayWayWeixinBeans.getDetail().getPrepayid();
                                //开始调用微信支付
                                StartWinxinPayFor(appid, timestamp, noncestr, packageX, partnerid, sign, prepayid);


                            }
                        }
                    } catch (Exception e) {
                        UIHelper.ToastMessage(SelectOrderInfoActivity.this, "数据格式错误");
                    }


                }




            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }




 /**
     * app微信支付参数
     *
     * @param appid
     * @param timestamp
     * @param noncestr
     * @param packageX
     * @param partnerid
     * @param sign
     * @param prepayid
     */
    private void StartWinxinPayFor(String appid, String timestamp, String noncestr, String packageX, String partnerid, String sign, String prepayid) {
        // 将该app注册到微信
        if (isWXAppInstalledAndSupported(appid)) {
            MyApplication.getInstance().getPreferencesUtils().clearOdercode();
            MyApplication.getInstance().getPreferencesUtils().clearselectWay();
            MyApplication.getInstance().getPreferencesUtils().setOrderCode(orderCode);
            MyApplication.getInstance().getPreferencesUtils().setselectWay(selectWay);
            msgApi = WXAPIFactory.createWXAPI(SelectOrderInfoActivity.this, null);
            msgApi.registerApp(appid);
            PayReq request = new PayReq();
            request.appId = appid;
            request.partnerId = partnerid;
            request.prepayId = prepayid;
            request.packageValue = packageX;
            request.nonceStr = noncestr;
            request.timeStamp = timestamp;
            request.sign = sign;
            msgApi.sendReq(request);


        } else {
            Toast.makeText(this, "请安装微信客户端", Toast.LENGTH_LONG).show();
        }
    }


    /**
     * 判断微信客户端是否存在
     *
     * @param appid
     * @return
     */
    private boolean isWXAppInstalledAndSupported(String appid) {
        IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
        msgApi.registerApp(appid);


        boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled() ;//&& msgApi.isWXAppSupportAPI()


        return sIsWXAppInstalledAndSupported;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BACKWASH2038

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值