zoukankan      html  css  js  c++  java
  • 支付宝授权

    首先从应用服务器获取验证字符串,然后去支付宝请求验证

        private void startAliAuth(final String info) {
            Runnable authRunnable = new Runnable() {
    
                @Override
                public void run() {
                    AuthTask authTask = new AuthTask(WithdrawApplyActivity.this);
                    Map<String, String> result = authTask.authV2(info, true);
    
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = result;
                    mHandler.sendMessage(msg);
                }
            };
            Thread authThread = new Thread(authRunnable);
            authThread.start();
        }

    获取验证结果,并上报给应用服务器

        private Handler mHandler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                    if (TextUtils.equals(authResult.getResultStatus(), "9000")) {
                        if (authResult.getResultCode().equals("200")) {
                            showProgressDialog("正在绑定账户");
                            //TODO 将authResult.getResult()上报给应用服务器
                        } else {
                            DadaToast.showToast(context, "授权失败");
                        }
                    } else if (authResult.getResultStatus().equals("6001")) {
                        DadaToast.showToast(context, "用户取消");
                    } else if (authResult.getResultStatus().equals("4000")) {
                        DadaToast.showToast(context, "支付宝异常");
                    } else if (authResult.getResultStatus().equals("6002")) {
                        DadaToast.showToast(context, "网络连接出错");
                    } else {
                        DadaToast.showToast(context, "授权失败");
                    }
                }
            }
        };

    完成验证 

    public class AuthResult {
    
        private String resultStatus;
        private String result;
        private String memo;
        private String resultCode;
        private String authCode;
        private String alipayOpenId;
    
        public AuthResult(Map<String, String> rawResult, boolean removeBrackets) {
            if (rawResult == null) {
                return;
            }
    
            for (String key : rawResult.keySet()) {
                if (TextUtils.equals(key, "resultStatus")) {
                    resultStatus = rawResult.get(key);
                } else if (TextUtils.equals(key, "result")) {
                    result = rawResult.get(key);
                } else if (TextUtils.equals(key, "memo")) {
                    memo = rawResult.get(key);
                }
            }
    
            String[] resultValue = result.split("&");
            for (String value : resultValue) {
                if (value.startsWith("alipay_open_id")) {
                    alipayOpenId = removeBrackets(getValue("alipay_open_id=", value), removeBrackets);
                    continue;
                }
                if (value.startsWith("auth_code")) {
                    authCode = removeBrackets(getValue("auth_code=", value), removeBrackets);
                    continue;
                }
                if (value.startsWith("result_code")) {
                    resultCode = removeBrackets(getValue("result_code=", value), removeBrackets);
                    continue;
                }
            }
    
        }
    
        private String removeBrackets(String str, boolean remove) {
            if (remove) {
                if (!TextUtils.isEmpty(str)) {
                    if (str.startsWith(""")) {
                        str = str.replaceFirst(""", "");
                    }
                    if (str.endsWith(""")) {
                        str = str.substring(0, str.length() - 1);
                    }
                }
            }
            return str;
        }
    
        @Override
        public String toString() {
            return "resultStatus={" + resultStatus + "};memo={" + memo + "};result={" + result + "}";
        }
    
        private String getValue(String header, String data) {
            return data.substring(header.length(), data.length());
        }
    
        /**
         * @return the resultStatus
         */
        public String getResultStatus() {
            return resultStatus;
        }
    
        /**
         * @return the memo
         */
        public String getMemo() {
            return memo;
        }
    
        /**
         * @return the result
         */
        public String getResult() {
            return result;
        }
    
        /**
         * @return the resultCode
         */
        public String getResultCode() {
            return resultCode;
        }
    
        /**
         * @return the authCode
         */
        public String getAuthCode() {
            return authCode;
        }
    
        /**
         * @return the alipayOpenId
         */
        public String getAlipayOpenId() {
            return alipayOpenId;
        }
    }
  • 相关阅读:
    python练习题6.1输入列表,求列表元素和(eval输入应用)
    python练习题5.10两数之和(没有用字典)
    python练习题5.8能被3,5和7整除的数的个数(用集合实现)-没有用集合
    python练习题5.7列表去重(存在问题)
    python练习题5.6统计工龄
    python练习题5.5统计字符出现次数
    python练习题5.4分析活动投票情况
    python练习题5.3四则运算(用字典实现)
    Object.style.display = value的值完成显示隐藏
    js通过Object.style.property=new style;改变html样式
  • 原文地址:https://www.cnblogs.com/wenhui92/p/6360975.html
Copyright © 2011-2022 走看看