Flutter现场身份验证插件shuftipro_onsite_sdk的使用

发布于 1周前 作者 itying888 来自 Flutter

Flutter现场身份验证插件shuftipro_onssite_sdk的使用

Shuftipro Integration

Shuftipro官方文档

import 'package:flutter/material.dart';
import 'package:shuftipro_onssite_sdk/shuftipro_onssite_sdk.dart';
import 'dart:convert';

String clientId = ""; // 这里输入客户端ID
String secretKey = ""; // 这里输入密钥

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.blue,
          scaffoldBackgroundColor: Colors.white,
          fontFamily: 'OpenSans'),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var authObject = {
    "auth_type": "basic_auth",
    "client_id": clientId,
    "secret_key": secretKey,
  };
  Map<String, Object> createdPayload = {
    "country": "",
    "language": "EN",
    "email": "",
    "callback_url": "",
    "redirect_url": "",
    "show_consent": 1,
    "show_privacy_policy": 1,
    "verification_mode": "image_only",
    "face": {
      "proof": "",
    },
    "document": {
      "supported_types": [
        "passport",
        "id_card",
        " driving_license",
        " credit_or_debit_card",
      ],
      "name": {
        "first_name": "",
        "last_name": "",
        "middle_name": "",
      },
      "dob": "",
      "document_number": "",
      "expiry_date": "",
      "issue_date": "",
      "gender": "",
      "backside_proof_required": "0",
    },
  };
  Map<String, Object> configObj = {
    "base_url": "api.shuftipro.com",
    "show_requirement_page": true,
  };

  /*
   * This function sends call to the Shuftipro's flutter plugin
   * and receives the response in a variable
   */

  Future<void> initPlatformState() async {
    String response = "";
    try {
      // response = await ShuftiproSdk.sendRequest(
      //     authObject: authObject,
      //     createdPayload: createdPayload,
      //     configObject: configObj);
      
      response = await ShuftiproSdk.registerRequest(clientId: "", customerId: "", configObject: configObj);

      var object = jsonDecode(response);
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text(object.toString()),
      ));

      print(object.toString());
      print(object["event"].toString());
    } catch (e) {
      print(e);
    }
    if (!mounted) return;
  }

  /*
   * Click listener to start the SDK flow
   */

  void continueFun() {
    var v = DateTime.now();
    var reference = "package_sample_Flutter_$v";
    createdPayload["reference"] = reference;
    initPlatformState();
  }

  /*
   * UI of demo application
   */

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: Container(
            margin: const EdgeInsets.all(10.0),
            child: OutlinedButton(
              onPressed: continueFun,
              style:
                  OutlinedButton.styleFrom(backgroundColor: Colors.blueAccent),
              child: Container(
                alignment: Alignment.center,
                child: const Text(
                  "Continue",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                    fontFamily: 'OpenSans',
                  ),
                ),
              ),
            ),
          ),
        ));
  }
}

更多关于Flutter现场身份验证插件shuftipro_onsite_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter现场身份验证插件shuftipro_onsite_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter应用中使用shuftipro_onsite_sdk插件进行现场身份验证的示例代码。假设你已经将shuftipro_onsite_sdk插件添加到了你的pubspec.yaml文件中,并且已经运行了flutter pub get

1. 添加依赖

首先,确保你的pubspec.yaml文件中包含以下依赖:

dependencies:
  flutter:
    sdk: flutter
  shuftipro_onsite_sdk: ^最新版本号  # 请替换为实际可用的最新版本号

2. 导入插件

在你的Dart文件中导入插件:

import 'package:shuftipro_onsite_sdk/shuftipro_onsite_sdk.dart';

3. 初始化SDK

在开始身份验证之前,你需要初始化SDK。这通常在你的应用的主入口文件中完成,比如main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化Shuftipro SDK
  await ShuftiproOnsiteSdk.init(
    apiKey: '你的API_KEY',  // 替换为你的Shuftipro API密钥
    environment: 'sandbox', // 或者 'production',根据你的环境选择
  );

  runApp(MyApp());
}

4. 启动身份验证流程

在你的某个页面或按钮点击事件中启动身份验证流程:

import 'package:flutter/material.dart';

class IdentityVerificationPage extends StatefulWidget {
  @override
  _IdentityVerificationPageState createState() => _IdentityVerificationPageState();
}

class _IdentityVerificationPageState extends State<IdentityVerificationPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('身份验证'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            try {
              // 启动身份验证流程
              var result = await ShuftiproOnsiteSdk.startOnsiteSession(
                sessionId: '你的SESSION_ID',  // 从后端获取的会话ID
                onSuccess: (sessionData) {
                  // 身份验证成功后的回调
                  print('身份验证成功: $sessionData');
                },
                onError: (error) {
                  // 身份验证失败或发生错误的回调
                  print('身份验证失败: $error');
                },
                onClose: () {
                  // 用户关闭身份验证流程的回调
                  print('身份验证流程已关闭');
                },
              );

              // result 可能会包含一些额外的信息,根据需要进行处理
            } catch (e) {
              print('启动身份验证流程时发生错误: $e');
            }
          },
          child: Text('开始身份验证'),
        ),
      ),
    );
  }
}

注意事项

  1. API Key和Session ID:确保你替换了示例代码中的你的API_KEY你的SESSION_ID为你的实际值。这些值通常由你的后端服务提供。
  2. 环境配置:根据你的应用是否处于生产环境,选择sandboxproduction
  3. 错误处理:在实际应用中,你应该添加更详细的错误处理逻辑,以提升用户体验。

这个示例展示了如何在Flutter应用中使用shuftipro_onsite_sdk插件进行基本的现场身份验证流程。根据你的具体需求,你可能需要调整或扩展这个示例。

回到顶部