Usage
Visit pub.dev to learn more about our flutter plugin:
Once you have set up your project correctly, required the necessary permissions, then you just have to import the package with:
import 'package:ironchip_lbfraud_plugin/ironchip_lbfraud.dart';
In order to send transaction, you must first initialize the sdk by providing a valid api key.
// Replace APIKEY with the desired generated api key.
// By default our SDK target to the production environment.
// In case you desire to target a diferent enviroment:
// LBFraudSDK fraud = new LbfraudFlutterPlugin.initFraudSDK("APIKEY", env: Environment.testing);
await _lbfraudPlugin.initFraudSDK(apiKey);
Once the service has been initialized, you can now perform transactions:
try {
// TransactionID (required,unique): transaction identifier request for fraud results
// UserID (required): User identifier
// extraData (optional): extra information for analysis
// You can omit the await in case the result of the action isn't necessary
var result =
await _lbfraudPlugin.sendTransaction(transactionID, userID, {});
_showResult = result!;
_resultError = false;
} catch (e) {
_showResult = e.toString();
_resultError = true;
}
Although the sendTransaction is async, it’s not necessary to perform an ‘await’ unless you want to check the actual result of the action. In addition, if you want to use a specific proxy when performing a transaction, you can configure it using:
try {
var result =
await _lbfraudPlugin.setProxy(host, port);
} catch (e) {
}
Example
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
import 'dart:convert'; // Import for base64Encode and utf8
import 'package:flutter/services.dart';
import 'package:ironchip_lbfraud/ironchip_lbfraud.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _lbfraudPlugin = IronchipLbfraud();
String apiKey = "APIKEY"; // Replace APIKEY with the desired generated api key.
bool _isLbfraudInitialized = false;
String userID = "john.doe@gmail.com"; // User identifier
String _showResult = "";
bool _resultError = false;
@override
void initState() {
super.initState();
}
// LBFraud methods are asynchronous, so we initialize in an async method.
Future<void> initLBFraudState() async {
try {
await _lbfraudPlugin.initFraudSDK(apiKey);
_isLbfraudInitialized = true;
} catch (e) {
_isLbfraudInitialized = false;
}
setState(() {
_isLbfraudInitialized = _isLbfraudInitialized;
});
}
// Function to generate a random transaction ID
String generateRandomTransactionID(int length) {
const _chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random _rnd = Random();
return String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
}
// Function to perform hashing using SHA-256
Future<String> sha256Hash(String text) async {
var bytes = utf8.encode(text);
var digest = await compute(_hash, bytes);
return base64UrlEncode(digest);
}
// If you want, you can use the crypto library to hash the user ID; otherwise, implement a method.
// pubspec.yml
//(dependencies:
// flutter:
// sdk: flutter
// crypto: ^3.0.1 # Make sure to have the latest version.
// )
static List<int> _hash(List<int> bytes) {
var digest = sha256.convert(bytes);
return digest.bytes;
}
// Function to trim, lowercase, and hash the userID
Future<String> anonymizeUserID(String userID) async {
String trimmedLowercaseUserID = userID.trim().toLowerCase();
return await sha256Hash(trimmedLowercaseUserID);
}
// Simulated function to perform login
Future<bool> performLogin(String username, String password) async {
// Simulate a delay for authentication
await Future.delayed(Duration(seconds: 2));
return true; // Change as needed
}
Future<void> sendTransaction() async {
try {
// Simulate user input for login
const username = 'testuser'; // Replace with the actual username
const password = 'testpassword'; // Replace with the actual password
bool loginSuccessful = await performLogin(username, password);
if (loginSuccessful) {
// Anonymize the userID before sending the transaction
String anonymizedUserID = await anonymizeUserID(userID);
// TransactionID (required, unique): transaction identifier request for fraud results
var result = await _lbfraudPlugin.sendTransaction(
generateRandomTransactionID(10),
anonymizedUserID,
{},
);
_showResult = result!;
_resultError = false;
} else {
_showResult = 'Login failed.';
_resultError = true;
}
} catch (e) {
_showResult = e.toString();
_resultError = true;
}
setState(() {
_resultError = _resultError;
_showResult = _showResult;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Ironchip LBFraud example app'),
backgroundColor: Color(0xFF005255),
),
body: Container(
margin: EdgeInsets.all(40.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
onChanged: (value) {
apiKey = value;
},
decoration: const InputDecoration(labelText: "API Key"),
),
SizedBox(height: 20),
Text(
_isLbfraudInitialized
? "LBFraud initialized"
: "LBFraud is not initialized",
style: TextStyle(
color: _isLbfraudInitialized
? Color(0xFF495c11f)
: Color(0xFFF44336),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: initLBFraudState,
style: ElevatedButton.styleFrom(
primary: Color(0xFF495c11f), // Set button color to #4eed56
),
child: Text('Initialize LBFraud'),
),
TextField(
onChanged: (value) {
userID = value;
},
decoration: const InputDecoration(labelText: "User ID"),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _isLbfraudInitialized ? sendTransaction : null,
style: ElevatedButton.styleFrom(
primary: Color(0xFF495c11f), // Set button color to #4eed56
),
child: Text('Send Transaction'),
),
SizedBox(height: 20),
Text(
_showResult,
style: TextStyle(
color: _resultError ? Color(0xFFF44336) : Colors.black,
),
),
],
),
),
),
),
);
}
}
Updated: October 17, 2024