本文介绍了银行卡核验的Java SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI开发者门户在线调试工具调试API接口。该工具会自动生成相应API的SDK调用示例代码,详细信息,请参见:

本章节提供的是最新版本的Java SDK调用示例,如果您需要查看之前版本,请参见Java SDK调用示例(V1版本)

前提条件

使用Maven引入SDK

<dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>cloudauth20200618</artifactId>
            <version>1.0.5</version>
</dependency>

调用示例

InitSmartVerify接口调用示例

import java.util.Arrays;
import java.util.List;

import com.aliyun.cloudauth20200618.Client;
import com.aliyun.cloudauth20200618.models.InitSmartVerifyRequest;
import com.aliyun.cloudauth20200618.models.InitSmartVerifyResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.tearpc.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;


public class InitSmartVerifyRequestTest {

    public static void main(String[] args) {

        // 通过以下代码创建API请求并设置参数。
        InitSmartVerifyRequest request = new InitSmartVerifyRequest();
        // 场景ID。
        request.setSceneId(xxxxL);
        request.setOuterOrderNo(商户请求的唯一标识);
        // 证件类型,固定值。
        request.setCertType(BANK_CARD);
        request.setMode(OCR_VERIFY);
        // MetaInfo环境参数,需要通过客户端SDK获取。
        request.setMetaInfo(xxxx);
        request.setMobile(130xxxxxxxx);
        request.setIdName(张三);
        request.setIdNo(330103xxxxxxxxxxxx);
        request.setIp(114.xxx.xxx.xxx);
        request.setUserId(12345xxxx);
        request.setCallbackUrl(https://www.xxxx.com);
        request.setCallbackToken(xxxxxx);

        // 推荐,支持服务路由。
        InitSmartVerifyResponse response = initSmartVerifyAutoRoute(request);

        // 不支持服务自动路由。
        // InitSmartVerifyResponse response = initSmartVerify("cloudauth.cn-shanghai.aliyuncs.com", request);

        System.out.println(response.getRequestId());
        System.out.println(response.getCode());
        System.out.println(response.getMessage());
        System.out.println(response.getResultObject().getCertifyId());
    }

    private static InitSmartVerifyResponse initSmartVerifyAutoRoute(InitSmartVerifyRequest request) {
        // 第一个为主区域Endpoint,第二个为备区域Endpoint。
        List<String> endpoints = Arrays.asList(cloudauth.cn-shanghai.aliyuncs.com, cloudauth.cn-beijing.aliyuncs.com);
        InitSmartVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                InitSmartVerifyResponse response = initSmartVerify(endpoint, request);
                lastResponse = response;

                // 服务端错误,切换到下个区域调用。
                if (response != null && "500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (Exception e) {
                // 网络异常,切换到下个区域调用。
                if (e.getCause() instanceof TeaException) {
                    TeaException teaException = ((TeaException)e.getCause());
                    if (teaException.getData() != null && "ServiceUnavailable".equals(
                        teaException.getData().get("Code"))) {
                        continue;
                    }
                }

                if (e.getCause() instanceof TeaUnretryableException) {
                    continue;
                }
            }
        }

        return lastResponse;
    }

    private static InitSmartVerifyResponse initSmartVerify(String endpoint, InitSmartVerifyRequest request)
        throws Exception {
        Config config = new Config();
        config.setAccessKeyId(您的AccessKey ID);
        config.setAccessKeySecret(您的AccessKey Secret);
        config.setEndpoint(endpoint);
        // 设置https代理。
        //config.setHttpsProxy(https://xx.xx.xx.xx:xxxx);
        // 设置http代理。
        //config.setHttpProxy(http://xx.xx.xx.xx:xxxx);
        Client client = new Client(config);

        // 创建RuntimeObject实例并设置运行参数。
        RuntimeOptions runtime = new RuntimeOptions();
        runtime.readTimeout = 10000;
        runtime.connectTimeout = 10000;
        runtime.autoretry = false;

        return client.initSmartVerify(request, runtime);
    }
}

DescribeSmartVerify接口调用示例

import java.util.Arrays;
import java.util.List;

import com.aliyun.cloudauth20200618.Client;
import com.aliyun.cloudauth20200618.models.DescribeSmartVerifyRequest;
import com.aliyun.cloudauth20200618.models.DescribeSmartVerifyResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaUnretryableException;
import com.aliyun.tearpc.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class DescribeSmartVerifyTest {

    public static void main(String[] args) throws Exception {

        // 通过以下代码创建API请求并设置参数。
        DescribeSmartVerifyRequest request = new DescribeSmartVerifyRequest();
        // 场景ID。
        request.setSceneId(xxxxL);
        request.setCertifyId(xxxxx);

        // 推荐,支持服务路由。
        DescribeSmartVerifyResponse response = describeSmartVerifyAutoRoute(request);

        // 不支持服务自动路由。
        //DescribeSmartVerifyResponse response = describeSmartVerify(cloudauth.cn-shanghai.aliyuncs.com, request);

        System.out.println(response.getRequestId());
        System.out.println(response.getCode());
        System.out.println(response.getMessage());
        System.out.println(response.getResultObject().getPassed());
        System.out.println(response.getResultObject().getSubCode());
        System.out.println(response.getResultObject().getMaterialInfo());
    }

    private static DescribeSmartVerifyResponse describeSmartVerifyAutoRoute(DescribeSmartVerifyRequest request) {
        // 第一个为主区域Endpoint,第二个为备区域Endpoint,选择就近的作为主区域。
        List<String> endpoints = Arrays.asList(cloudauth.cn-shanghai.aliyuncs.com, cloudauth.cn-beijing.aliyuncs.com);
        DescribeSmartVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                DescribeSmartVerifyResponse response = describeSmartVerify(endpoint, request);
                lastResponse = response;

                // 服务端错误,切换到下个区域调用。
                if (response != null && "500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (Exception e) {
                // 网络异常,切换到下个区域调用。
                if (e.getCause() instanceof TeaException) {
                    TeaException teaException = ((TeaException)e.getCause());
                    if (teaException.getData() != null && "ServiceUnavailable".equals(
                        teaException.getData().get("Code"))) {
                        continue;
                    }
                }

                if (e.getCause() instanceof TeaUnretryableException) {
                    continue;
                }
            }
        }

        return lastResponse;
    }

    private static DescribeSmartVerifyResponse describeSmartVerify(String endpoint, DescribeSmartVerifyRequest request)
        throws Exception {
        Config config = new Config();
        config.setAccessKeyId(您的AccessKey ID);
        config.setAccessKeySecret(您的AccessKey Secret);

        config.setEndpoint(endpoint);
        // 设置https代理。
        //config.setHttpsProxy(https://xx.xx.xx.xx:xxxx);
        // 设置http代理。
        //config.setHttpProxy(http://xx.xx.xx.xx:xxxx);
        Client client = new Client(config);

        // 创建RuntimeObject实例并设置运行参数。
        RuntimeOptions runtime = new RuntimeOptions();
        runtime.readTimeout = 10000;
        runtime.connectTimeout = 10000;
        runtime.autoretry = false;

        return client.describeSmartVerify(request, runtime);
    }
}