首页 > 代码库 > Anroid Studio第十期 - 招商银行两种支付方式

Anroid Studio第十期 - 招商银行两种支付方式

    这回要讲讲了,招行的支付比较蛋疼,一种是用来测试(他们开发人员说测试用的- -!坑死了~),另一种是上线的,测试是桥接,上线的是html5的,代码已经梳理好了,需要注意的是桥接自己写回调,还有js的配置以及key的替换,html5的直接替换上线的key就可以了~差点忘了提醒大家,招行有一个加密键盘,别忘导入,支付的时候调起用的- -!


    大概的代码如下:

    桥接代码:

public class PayChooseActivityQrCodeYuan extends BaseActivity implements View.OnClickListener {
    private static PayChooseActivityQrCodeYuan instance = null;
    private static int time = 40000;
    private LinearLayout ll_back;
    private TextView tv_title1;
    //    private ImageView btn_pay1;
    private String from;
    private AnimationDrawable ad;
    static private BridgeWebView webview;
    private String url;
    private String testUrl = "file:///android_asset/2.html";
    //    private CMBKeyboardFunc kbFunc = null;
    private CMBPayStateCallback payStateCallback;

    //    public static PayChooseFragment getInstance() {
//        if (instance == null) {
//            synchronized (PayChooseFragment.class) {
//                if (instance == null) {
//                    instance = new PayChooseFragment();
//                }
//            }
//        }
//        return instance;
//    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_paychoose_qrcodeyuan;
    }


    @Override
    protected void setup(@Nullable Bundle savedInstanceState) {
        SmartBarInject.inject(this).show(SmartBar.HOME | SmartBar.BACK);
        findviews();
        addlisteners();
        doNetWork();//第一次加载
    }

    private void LoadUrl() {
        try {

            CookieSyncManager.createInstance(PayChooseActivityQrCodeYuan.this.getApplicationContext());
            CookieManager.getInstance().removeAllCookie();
            CookieSyncManager.getInstance().sync();
        } catch (Exception e) {

        }
        webview.loadUrl(url);
    }

    private void doNetWork() {
        ShowLoadingUtil.showProgressDialog2(PayChooseActivityQrCodeYuan.this, getResources().getString(R.string.loading));

    }


    public String subStirngGetMoney(String url) {

        int posStart = url.indexOf("total_fee=") + 10;
        int posEnd = url.indexOf("&", posStart);
        String money = null;
        if (posStart < posEnd) {
            money = url.substring(posStart, posEnd);
        }
        return money;
    }

    private void addlisteners() {
        ll_back.setOnClickListener(this);
    }

    private void findviews() {
        from = getIntent().getExtras().getString(ConstantUtil.Bundle_FROM);
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        tv_title1 = (TextView) findViewById(R.id.tv_title1);
        //招商银行部分
        tv_title1.setText(getString(R.string.pay_choose114));
        url = getIntent().getExtras().getString(ConstantUtil.Bundle_INFO1);
        MyLogUtil.e("888", url);
//        url=testUrl;

        webview = (BridgeWebView) findViewById(R.id.webview);

        // 对WebView进行设置
        WebSettings set = webview.getSettings();
        // 支持JS
        set.setJavaScriptEnabled(true);
        set.setSaveFormData(false);
        set.setSavePassword(false);
        set.setSupportZoom(false);
//        set.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// WebSettings.LOAD_CACHE_ELSE_NETWORK

        webview.setDefaultHandler(new DefaultHandler());

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
            }
        });


        //呼出安全键盘
        webview.setOnShouldOverrideUrlLoading(new BridgeWebView.OnShouldOverrideUrlLoading() {
            public boolean onShouldOverrideUrlLoading(WebView view, String url) {
                CMBKeyboardFunc kbFunc = new CMBKeyboardFunc(PayChooseActivityQrCodeYuan.this);

                if (url.contains("http://pay.success.compay.com/return_url.jsp")) {
                    //支付成功
                    SpUtils.getInstance().put(PayParams.Pay_money,subStirngGetMoney(url));

                }
                MyLogUtil.e("******",url);
                return kbFunc.HandleUrlCall(webview, url);


            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap bitmap) {

            }

            public void onPageFinished(WebView view, String url) {

            }

        });

        LoadUrl();


        //description:由于招行一网通测试站与正式站使用的桥名不一样,
        // 测试站:WebViewJavascriptBridge
        // 正式站:CMBMerchantJSBridge
        // js桥回调handler均为:initCmbSignNetPay
        // 如需更换其他桥名,请修改library下assets目录下的WebViewJavascriptBridge.js文件,
        // 把文件名及文件内WebViewJavascriptBridge替换成对应桥名并执行下面代码即可

        //=======================使用招行正式站时调用以下代码即可==========================
        webview.setCustom(true, "CMBMerchantJSBridge");

        //=======================招行一网通js桥回调==========================
        webview.registerHandler("initCmbSignNetPay", new BridgeHandler() {

            @Override
            public void handler(String data, CallBackFunction function) {
                //在这里解析回调数据并执行处理
                //Log.i(TAG, "回传结果:" + data);
//                Toast.makeText(PayChooseActivityQrCodeYuan.this, data, Toast.LENGTH_SHORT).show();
                initCmbSignNetPay(data);
            }
        });

        webview.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // newProgress 1-100之间的整数
                // Log.i("---------------RegActivity-------------",
                // "newProgress:"+newProgress);
                // mHandler.sendEmptyMessage(newProgress);
                // if (newProgress == 100) {
                // // 网页加载完毕,关闭ProgressDialog
                // closeDialog();
                // } /*else {
                // // 网页正在加载,打开ProgressDialog
                // // openDialog(newProgress);
                // if (dialog == null) {
                // mHandler.sendEmptyMessage(START_PROGRESS);
                // }else{
                //
                // }
                //
                // }*/
                // mdialog.setProgress(newProgress);
                if (newProgress == 100) {
                    // dialog.dismiss();
                    ShowLoadingUtil.dismissProgressDialog2();
                }

                super.onProgressChanged(view, newProgress);
            }

            private void closeDialog() {
                ShowLoadingUtil.dismissProgressDialog2();
            }

            private void openDialog(int newProgress) {
                ShowLoadingUtil.showProgressDialog2(PayChooseActivityQrCodeYuan.this
                        , getResources().getString(R.string.loading));
            }
        });

    }


    @Override
    public void onResume() {
        super.onResume();

    }


    public void onPause() {
        super.onPause();
        // 停止动画
//        ad.stop();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
//                handler1.removeCallbacks(runnable);
                onBackPressed();
                break;
//            case R.id.btn_pay1:
//                // 实例化SelectPicPopupWindow
//                handler1.removeCallbacks(runnable);
//                menuWindow2 = new PopPaysuccess2(this, null, -1);
//                // 显示窗口
//                menuWindow2.showAtLocation(
//                        findViewById(R.id.main), Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL
//                        , 0, 0); // 设置layout在PopupWindow中显示的位置
//                break;
            default:
                break;
        }
    }


    public static final String RESULT_KEY = "pay_status";
    public static final int RESULT_PAYING = 0;
    public static final int RESULT_FAILED = 1;
    public static final int RESULT_SUCCESS = 2;

    private int resultCode = RESULT_PAYING;

    /**
     * 支付H5页面状态回调
     *
     * @param payData
     */
    public void initCmbSignNetPay(final String payData) {
//        Toast.makeText(activity, "***", 4000).show();
        // TODO Auto-generated method stub
        try {
//           PayUtils.paySPGet();
            resultCode = RESULT_PAYING;
            if (TextUtils.isEmpty(payData)) {
                return;
            }
            JSONObject result = JSON.parseObject(payData);
            //TODO test
//            String resultCodetest = (String) result.get("sign_status"); {"pay_status":0}
            String resultCodetest = (String) result.get("pay_status");
//            ToastUtil.showToastLong(result.toString());

            if (!TextUtils.isEmpty(result.toString()) && result.containsKey(RESULT_KEY)) {
//                ToastUtil.showToastLong(result.toString());
                resultCode =  result.getIntValue(RESULT_KEY);
                if (resultCode == RESULT_PAYING) {
                    //支付中
//                    ToastUtil.showToastLong(resultCode + "***");

                }
                if (resultCode == RESULT_FAILED) {
                    //支付失败
//                    ToastUtil.showToastLong(resultCode + "");
                    PayUtils.payfail(PayChooseActivityQrCodeYuan.this);
                }
                if (resultCode == RESULT_SUCCESS) {
                    //支付成功
//                    ToastUtil.showToastLong(resultCode + "");
//                    if (SpUtils.getInstance().get(PayParams.Pay_money, null).toString()){
//
//                    }
                    Intent intent = new Intent(PayChooseActivityQrCodeYuan.this,
                            PayChooseSuccessActivity.class);
                    intent.putExtra(ConstantUtil.Bundle_FROM, PayParams.Pay_company2);
//                    intent.putExtra(ConstantUtil.INTENT_INFO1, SpUtils.getInstance().get(PayParams.Pay_money, null).toString());
                    startActivity(intent);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private void shezhisp() {
        SpUtils.getInstance().put(PayParams.Pay_merger_id, "");
        SpUtils.getInstance().put(PayParams.Pay_order_ids, "");
        SpUtils.getInstance().put(PayParams.Pay_payChoose, "");
        SpUtils.getInstance().put(PayParams.Pay_money, "");
    }


}

    正常的HTML5代码:

public class PayChooseActivityQrCodeYuanOld extends BaseActivity implements View.OnClickListener {
    private static PayChooseActivityQrCodeYuanOld instance = null;
    private static int time = 40000;
    private LinearLayout ll_back;
    private TextView tv_title1;
    //    private ImageView btn_pay1;
    private String from;
    private AnimationDrawable ad;
    private WebView webView;
    private String url;
    private String testUrl = "file:///android_asset/2.html";
    //    private CMBKeyboardFunc kbFunc = null;
    private CMBPayStateCallback payStateCallback;

    //    public static PayChooseFragment getInstance() {
//        if (instance == null) {
//            synchronized (PayChooseFragment.class) {
//                if (instance == null) {
//                    instance = new PayChooseFragment();
//                }
//            }
//        }
//        return instance;
//    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_paychoose_qrcode;
    }

    @Override
    public void onBackPressed() {
        //失败页面跳转
        Intent intent = new Intent(PayChooseActivityQrCodeYuanOld.this,
                MyOrderActivity.class);
        intent.putExtra(ConstantUtil.Bundle_FROM, getClass().getName());
        intent.putExtra(ConstantUtil.INTENT_INFO1, ConstantUtil.pay_fail);
        startActivity(intent);
        finish();
        PayUtils.paySPPut();
    }

    @Override
    protected void setup(@Nullable Bundle savedInstanceState) {
        SmartBarInject.inject(this).show(SmartBar.BACK);
        findviews();
        addlisteners();
        doNetWork();//第一次加载
    }

    private void LoadUrl() {
        try {

            CookieSyncManager.createInstance(PayChooseActivityQrCodeYuanOld.this.getApplicationContext());
            CookieManager.getInstance().removeAllCookie();
            CookieSyncManager.getInstance().sync();
        } catch (Exception e) {

        }
        webView.loadUrl(url);
        ShowLoadingUtil.dismissProgressDialog2();
        MyLogUtil.e("***loadUrl", url);
    }

    private void doNetWork() {
        ShowLoadingUtil.showProgressDialog2(PayChooseActivityQrCodeYuanOld.this, getResources().getString(R.string.loading));

    }


    private void addlisteners() {
        ll_back.setOnClickListener(this);
    }

    private void findviews() {
        from = getIntent().getExtras().getString(ConstantUtil.Bundle_FROM);
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        tv_title1 = (TextView) findViewById(R.id.tv_title1);
        //招商银行部分
        tv_title1.setText(getString(R.string.pay_choose114));
        url = getIntent().getExtras().getString(ConstantUtil.Bundle_INFO1);
        MyLogUtil.e("***", url);
//        url=testUrl;

        webView = (WebView) findViewById(R.id.webview);

//        kbFunc = new CMBKeyboardFunc(PayChooseActivityQrCodeYuan.this);

//        webView.loadUrl(url);

        WebSettings settings = webView.getSettings();
//        settings.setBuiltInZoomControls(true);
//        settings.setDisplayZoomControls(false); // 隐藏webview缩放按钮
//        settings.setUseWideViewPort(true);
//        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setSaveFormData(false);
        settings.setSavePassword(false);
        settings.setSupportZoom(true);
//        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);// WebSettings.LOAD_CACHE_ELSE_NETWORK

//        payStateCallback = new CMBPayStateCallback(this);
//        webView.addJavascriptInterface(payStateCallback, "CMBMerchantJSBridge");

        LoadUrl();
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 使用当前的WebView加载页面
                CMBKeyboardFunc kbFunc = new CMBKeyboardFunc(PayChooseActivityQrCodeYuanOld.this);
//                MyLogUtil.e("***","1");
                //: http://pay.success.smarthaier.com/return_url.jsp?total_fee=1
                //http://bjdnserror4.wo.com.cn:8080/bj/index_m.htm
                MyLogUtil.e("***kbFunc", url);
                if (kbFunc.HandleUrlCall(webView, url) == false) {
//                    MyLogUtil.e("***","2");
                    if (url.contains("http://pay.success.smarthaier.com/return_url.jsp")) {
                        //支持成功old
//                        String money = PayUtils.subStirngGetMoneyOld(url);
//                        MyLogUtil.e("***money",money);
//                        SpUtils.getInstance().put(PayParams.Pay_money, money);
//                        Intent intent = new Intent(PayChooseActivityQrCodeYuanOld.this,
//                                PayChooseSuccessActivity.class);
//                        intent.putExtra(ConstantUtil.Bundle_FROM, PayParams.Pay_alipay);
////                        intent.putExtra(ConstantUtil.INTENT_INFO1, money);
//                        startActivity(intent);

                        //支付成功new
                        Intent intent = new Intent(PayChooseActivityQrCodeYuanOld.this,
                                MyOrderActivity.class);
                        intent.putExtra(ConstantUtil.Bundle_FROM, getClass().getName());
                        intent.putExtra(ConstantUtil.INTENT_INFO1, ConstantUtil.pay_success);
                        startActivity(intent);
                        finish();
                        PayUtils.paySPPut();


                    }else{

                    }
                    return super.shouldOverrideUrlLoading(view, url);
                } else {

                    return true;
                }

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    public void onPause() {
        super.onPause();
        // 停止动画
//        ad.stop();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
//                handler1.removeCallbacks(runnable);
                onBackPressed();
                break;
//            case R.id.btn_pay1:
//                // 实例化SelectPicPopupWindow
//                handler1.removeCallbacks(runnable);
//                menuWindow2 = new PopPaysuccess2(this, null, -1);
//                // 显示窗口
//                menuWindow2.showAtLocation(
//                        findViewById(R.id.main), Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL
//                        , 0, 0); // 设置layout在PopupWindow中显示的位置
//                break;
            default:
                break;
        }
    }

}

    完事~中秋节快乐~对了下面给大家一个批量修改名字的软件~

    Demo地址:http://down.51cto.com/data/2241395

    批量修改名字:http://down.51cto.com/data/2241396

本文出自 “梁肖技术中心” 博客,请务必保留此出处http://liangxiao.blog.51cto.com/3626612/1852308

Anroid Studio第十期 - 招商银行两种支付方式