支付寶授權


首先從應用服務器獲取驗證字符串,然后去支付寶請求驗證

    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;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM