Tích hợp Android
- Yêu cầu Android SDK >= 6.0 (API level 23).
Bước 1:
- Tải về bộ tích hợp SDK tại https://github.com/VNPTSmartCA/android_one_time_ca_sdk/releases và giải nén ra thư mục.
Bước 2: Cấu hình file app/build.grandle như dưới
repositories {
maven {
//Đường dẫn đến thư mục giải nén
url '..path_to_android_one_time_ca_sdk_folder\\repo'
}
maven {
url "https://storage.googleapis.com/download.flutter.io"
}
}
dependencies {
// ...
implementation files('..path_to_android_one_time_ca_sdk_folder\\onetimeca_vnpt_smartca_library.aar')
debugImplementation 'com.vnpt.vnpt_smartca_sdk.onetime_ca:flutter_debug:1.0'
releaseImplementation 'com.vnpt.vnpt_smartca_sdk.onetime_ca:flutter_release:1.0'
}
Bước 3: Khởi tạo SDK tại nơi muốn bắt đầu kết nối
class MainActivity : AppCompatActivity() {
var onetimeVNPTSmartCA = OnetimeVNPTSmartCA()
@override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val config = ConfigSDK()
config.context = this
config.partnerId = "xxx-xxx-xxx-xxx" // clientId của đối tác được VNPTSmartCA cung cấp khi yêu cầu tích hợp.
//Cấu hình môi trường Dev-test hay Production cùa SmartCA
config.environment = SmartCAEnvironment.DEMO_ENV
//Cấu hình ngôn ngữ app (vi/en)
config.lang = SmartCALanguage.VI
onetimeVNPTSmartCA.initSDK(config)
//....
}
Thêm FlutterActivity trong file AndroidManifest.xml như sau
<application
<activity android:name="io.flutter.embedding.android.FlutterActivity"
android:theme="@style/Theme.Smartca_android_example"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"/>
</application>
Bước 4: Sử dụng các hàm chính
- Kích hoạt tài khoản/lấy thông tin xác thực người dùng (accessToken và credentialId)
- Xác nhận giao dịch ký số
- Thêm đoạn code dưới đây tại Activity muốn kết nối với SDK trước khi sử dụng các chức năng:
📦 Hàm kích hoạt tài khoản/lấy accessToken và credentialId của người dùng
SDK sẽ thực hiện kiểm tra trạng thái tài khoản và chứng thư của khách hàng như: đã kích hoạt hay chưa, chứng thư hợp lệ hay không, tự động làm mới token khi hết hạn,.... Thành công SDK sẽ trả về accessToken và credentialId của người dùng
fun getAuthentication() {
try {
// SDK tự động xử lý các trường hợp về: chưa kích hoạt, hết hạn token...
onetimeVNPTSmartCA.getAuthentication { result ->
// Nếu ko lấy được token, credentialId sẽ hiển thị giao diện thông báo
when (result.status) {
SmartCAResultCode.SUCCESS_CODE -> {
// SDK trả lại token, credential của khách hàng
val obj = Json.decodeFromString(CallbackResult.serializer(), result.data.toString())
val token = obj.accessToken
val credentialId = obj.credentialId
// Đối tác tạo giao dịch cho khách hàng và lấy transId
// ...
// Sau đó gọi getWaitingTransaction để người dùng xác nhận giao dịch
// ...
}
else -> {
// Xử lý lỗi
}
}
}
} catch (ex: Exception) {
throw ex;
}
}
📦 Hàm xác nhận giao dịch
Sau khi lấy được accessToken và credentialId của người dùng từ getAuthentication Đối tác tích hợp tạo giao dịch ký số cho khách hàng, lấy transId sau đó gọi hàm xác nhận ký số getWaitingTransaction
onetimeVNPTSmartCA.getWaitingTransaction(transId) { result ->
when (result.status) {
SmartCAResultCode.SUCCESS_CODE -> {
// Xử lý khi confirm thành công
}
else -> {
// Xử lý khi confirm thất bại
}
}
}
📦 Hàm hủy kết nối SDK:
override fun onDestroy() {
onetimeVNPTSmartCA.destroySDK()
super.onDestroy()
}