芝麻認(rèn)證

芝麻認(rèn)證:

項(xiàng)目要做芝麻認(rèn)證,以為只是簡(jiǎn)單調(diào)一下SDK,其實(shí)不然,官方文檔并不是很詳細(xì),所以把自己踩的坑總結(jié)一下:

總體流程如下:

1.調(diào)用 customer.certification.initialize 接口進(jìn)行認(rèn)證初始化,并獲取返回值 biz_no。biz_no 是本次認(rèn)證的標(biāo)識(shí),在后面的認(rèn)證接口和查詢接口會(huì)用到。
2.跳轉(zhuǎn)到 customer.certification.certify 頁(yè)面接口或者使用客戶端 SDK 讓用戶完成認(rèn)證,用戶完成認(rèn)證后會(huì)通知商戶結(jié)果。這個(gè)接口支持多種方式接入,可以靈活使用。必須在這個(gè)接口的請(qǐng)求中傳入 return_url 才能回跳到商戶, return_url 也支持多個(gè)協(xié)議,可以按照需要使用
3 可以根據(jù)第一步返回的 biz_no 查詢本次認(rèn)證的狀態(tài)和結(jié)果。

可以看下我從官網(wǎng)扣下的流程圖:


image.png

通過(guò)芝麻提供的暴露接口,商戶回調(diào)。拿到對(duì)應(yīng)的標(biāo)識(shí),發(fā)起認(rèn)證。

代碼如下:
1.認(rèn)證初始化(服務(wù))

ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();
request.setPlatform("zmop");
request.setTransactionId("ZGYD201610252323000001234");// 必要參數(shù)
request.setProductCode("w1010100000000002978");// 必要參數(shù)
request.setBizCode("FACE");// 必要參數(shù)
request.setIdentityParam("{\"identity_type\":\"CERT_INFO\",\"cert_type\":\"IDENTITY_CARD\",\"cert_name\":\"收委\",\"cert_no\":\"260104197909275964\"}");// 必要參數(shù)
request.setExtBizParam("{}");// 必要參數(shù)
DefaultZhimaClient client = new DefaultZhimaClient(
    "https://zmopenapi.zmxy.com.cn/openapi.do",
    "app_id",
    "your private_key",
    "zhima_public_key");
try {
    ZhimaCustomerCertificationInitializeResponse response = (ZhimaCustomerCertificationInitializeResponse) client
        .execute(request);
    System.out.println(response.isSuccess());
    System.out.println(response.getErrorCode());
    System.out.println(response.getErrorMessage());
} catch (ZhimaApiException e) {
    e.printStackTrace();
}

2.生成認(rèn)證URL(服務(wù))

ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();
request.setPlatform("zmop");
request.setBizNo("ZM201612013000000242400404124269");// 必要參數(shù)
// 設(shè)置回調(diào)地址,必填. 如果需要直接在支付寶APP里面打開(kāi)回調(diào)地址使用alipay協(xié)議
// alipay://www.taobao.com 或者 alipays://www.taobao.com,分別對(duì)應(yīng)http和https請(qǐng)求
request.setReturnUrl("http://www.taobao.com");// 必要參數(shù)
DefaultZhimaClient client = new DefaultZhimaClient(
    "https://zmopenapi.zmxy.com.cn/openapi.do";, "app_id", "your private_key",
    "zhima_public_key");
try {
    String url = client.generatePageRedirectInvokeUrl(request);
    System.out.println("generateCertifyUrl url:" + url);
} catch (ZhimaApiException e) {
    e.printStackTrace();
}

本地認(rèn)證方式:
1.直接根據(jù)后臺(tái)生成的url 做中轉(zhuǎn):

/**
 * 啟動(dòng)支付寶進(jìn)行認(rèn)證
 * @param url 開(kāi)放平臺(tái)返回的URL
 */
private void doVerify(String url) {
    if (hasApplication()) {
        Intent action = new Intent(Intent.ACTION_VIEW);
        StringBuilder builder = new StringBuilder();
       // 這里使用固定appid 20000067
        builder.append("alipays://platformapi/startapp?appId=20000067&url=");
        builder.append(URLEncoder.encode(url));
        action.setData(Uri.parse(builder.toString()));
        startActivity(action);
    } else {
        // 處理沒(méi)有安裝支付寶的情況
        new AlertDialog.Builder(this)
            .setMessage("是否下載并安裝支付寶完成認(rèn)證?")
            .setPositiveButton("好的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent action = new Intent(Intent.ACTION_VIEW);
                action.setData(Uri.parse("https://m.alipay.com"));
                startActivity(action);
            }
        }).setNegativeButton("算了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }
}

/**
 * 判斷是否安裝了支付寶
 * @return true 為已經(jīng)安裝
 */
private boolean hasApplication() {
    PackageManager manager = getPackageManager();
    Intent action = new Intent(Intent.ACTION_VIEW);
    action.setData(Uri.parse("alipays://"));
    List list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER);
    return list != null && list.size() > 0;
}

對(duì)應(yīng)上面這種方式會(huì)存在跳轉(zhuǎn)到支付寶頁(yè)面認(rèn)證完成后會(huì)發(fā)現(xiàn)沒(méi)有對(duì)應(yīng)的回調(diào)

對(duì)此我給出的解決方式是:
指定activity 處理支付寶的隱似跳轉(zhuǎn)

   <activity
            android:name=".module.login.OcrResultiActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />

                <data
                    android:host="zmcert"
                    android:path="/ocrresult"
                    android:scheme="ixmsdk" />
            </intent-filter>
        </activity>

對(duì)于上面格式不是很明白的可以看看下面我推薦的博客,講的很詳細(xì),可以學(xué)習(xí)一下

https://blog.csdn.net/lixpjita39/article/details/78201689

第二種集成方式:直接app 內(nèi)集成。 直接拿對(duì)應(yīng)回調(diào)處理

       final ZMCertification zmCertification = ZMCertification.getInstance();
        zmCertification.getBuildInfo();
        zmCertification.setZMCertificationListener(new ZMCertificationListener() {
            @Override
           public void onFinish(boolean isCanceled, boolean isPassed, int errorCode) {
                zmCertification.setZMCertificationListener(null);
                if (isCanceled)
                    Toast.makeText(mContext, "cancel : 芝麻驗(yàn)證失敗,原因是:" + errorCode, Toast.LENGTH_SHORT).show();
               else {
                   if (isPassed){
                        setResult(Activity.RESULT_OK);
                        finish();
                        Toast.makeText(mContext, "complete : 芝麻驗(yàn)證成功,原因是:" + errorCode, Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(mContext, "complete : 芝麻驗(yàn)證失敗,原因是:" + errorCode, Toast.LENGTH_SHORT).show();
                        startActivityForResult(new Intent(mContext,FaceResultAliActivity.class),REQUEST_AUTH_RESULT);
                   }
                }
           }
        });

我把自己寫(xiě)的demo 上傳到GitHub 上
地址:https://github.com/guiyanbing/MyApplication2
歡迎一起學(xué)習(xí)探討

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容