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

时间: 2024-10-13 02:16:37

Anroid Studio第十期 - 招商银行两种支付方式的相关文章

Android Studio第三十期 - 介绍几种网络请求方式写法

框架git地址:附(爱一个人就要爱他(loader)的全部~) https://github.com/geeklx/MakeJarAAR 1:http://192.168.200.96/v2/weather/weather.get.info 请求方式:POST     请求示例 {    "verify_info":{        "app":{            "app_id":"2e263d60-2d48-43d9-b699

android studio学习---签名打包的两种方式

注:给我们自己开发的app签名,就代表着我自己的版权,以后要进行升级,也必须要使用相同的签名才行.签名就代表着自己的身份(即keystore),多个app可以使用同一个签名. 如果不知道签名是啥意思,请自行百度哦.在eclipse中签名的方法是:选中工程,邮件选择"export-android-export android application", 1.方式1:通过Android Studio进行签名: 选中app这个module,选择菜单栏"Build-Generate

Android Studio导入Eclipse项目的两种方式

1.兼容Eclipse目录结构--- studio的目录结构和Eclipse是不一样的,这种方式不会影响原来的Eclipse目录结构.是用Ant模式构建的 1.1选中导入Eclipse项目 2.重点来了.注意选择的是哪个 3.选择构建方式

Android Studio第二十期 - 识别Recorder声纹显示水波泡泡效果

代码已经整理好,效果如下图: 总结:此版本控制在6.0以下,以上的自己手动改targetSdkVersion,另外要想想怎么做到语音识别的时候并且播放音乐和此通用一个Record,以后要写一写~ 地址:http://down.51cto.com/data/2260152

Anroid Studio第七期 - 语音动画

代码已经整理好,效果如下图: 地址:

Android第五期 - 更新自己的apk本地与网络两种方法

首先是本地: ParseXmlService部分: package com.szy.update; import java.io.InputStream; import java.util.HashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element

爪哇国新游记之十二----线程创建的两种形式

public class Thread1 extends Thread{ public void run(){ int i=0; while(i<10){ i++; System.out.println(i); } } public static void main(String[] args){ Thread1 t=new Thread1(); t.start(); } } public class Thread2 implements Runnable{ @Override public v

将Eclipse代码导入到Android Studio的两种方式

转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建的项目,我们都会面临的问题是原先Eclipse的代码该怎么导入到Android Studio中使用.这方面相关的资料还比较少,自己摸索了一下,总结出这篇博客,希望能让开发者少走些弯路.OK,进入正题. Google为了让我们更加方便的使用AndroidStudio,提供了两种导入Eclipse代码的

(转发 )将Eclipse代码导入到Android Studio的两种方式

这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 说到使用Android Studio,除了新建的项目,我们都会面临的问题是原先Eclipse的代码该怎么导入到Android Studio中使用.这方面相关的资料还比较少,自己摸索了一下,总结出这篇博客,希望能让开发者少走些弯路.OK,进入正题. Google为了让我们更加方便的使用AndroidStudio,提供了两种导入E