Usage
Use in an app
To make full use of the potential of the sdk, the ACCESS_FINE_LOCATION permission needs to be granted.
import com.ironchip.ironchiplbfraudandroidsdk.LBFraudSDK;
...
// Replace APIKEY with the desired generated api key.
LBFraudSDK fraud = new LBFraudSDK(this, "APIKEY");
// By default our SDK target to the production environment.
// In case you desire to target a diferent enviroment:
// LBFraudSDK fraud = new LBFraudSDK(this, "APIKEY", LBFraudSDK.Environment.Testing);
//public enum Environment {
// Production,
// Testing,
// Development
//}
String transactionID = "random_identifier_generated"; // Transaction identifier request for fraud results
String userID = "john.doe@gmail.com"; // User identifier
Map<String, Object> extraData = new HashMap<>(); // Extra information for analysis
extraData.put("amount", new Integer(30));
extraData.put("operation", "login");
// TransactionID (required,unique): transaction identifier request for fraud results
// UserID (required): User identifier
// ExtraData (optional): extra information for analysis
// The sendTransaction can be provided with 2 callbacks, one is executed when the transaction is finished
// and the other one is called in case an error did occure during the transaction process.
fraud.sendTransaction(transactionID, userID, extraData, () -> {
// Add here any code you want to be executed after the transaction
// has finished.
}, exception -> {
// Add here any code you want to perform in case of an error
// during the transaction.
});
Example
import com.ironchip.ironchiplbfraudandroidsdk.LBFraudSDK;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
...
// Replace APIKEY with the desired generated api key.
LBFraudSDK fraud = new LBFraudSDK(this, "APIKEY");
// Asynchronous method to perform user login
private void login(String username, String password) {
performLogin(username, password).thenAccept(loginSuccessful -> {
if (loginSuccessful) {
// If login is successful, call sendTransaction with the necessary parameters
String transactionID = generateRandomTransactionID(10); // Generate or fetch your transaction ID
String userID = "john.doe@gmail.com"; // User identifier
String anonymizedUserID = anonymizeUserID(userID); // Anonymized user ID
Map<String, Object> extraData = new HashMap<>(); // Extra information for analysis
extraData.put("amount", 30); // Use Integer.valueOf(30) if needed
extraData.put("operation", "login");
sendTransactionAfterLogin(transactionID, anonymizedUserID, extraData);
} else {
// Handle login failure
}
});
}
// Asynchronous login function that returns a Future<Boolean>
private CompletableFuture<Boolean> performLogin(String username, String password) {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
// Replace with your actual login logic
// For example, authenticate the user and return true if successful
return true;
} catch (InterruptedException e) {
return false; // In case of an error, return false
}
});
}
// Function to send transaction after successful login
private void sendTransactionAfterLogin(String transactionID, String anonymizedUserID, Map<String, Object> extraData) {
// Sending transaction
fraud.sendTransaction(transactionID, anonymizedUserID, extraData, () -> {
// Add here any code you want to be executed after the transaction has finished.
}, exception -> {
// Add here any code you want to perform in case of an error during the transaction.
});
}
// Function to trim, convert to lowercase, and hash the userID
public static String anonymizeUserID(String userID) {
String trimmedLowercaseUserID = userID.trim().toLowerCase();
return hashString(trimmedLowercaseUserID);
}
// Function to perform hashing using SHA-256
public static String hashString(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Error while calculating hash", e);
}
}
private String generateRandomTransactionID(int length) {
// Implement your method to generate a unique transaction ID
const String chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890";
Random rnd = new Random();
return IntStream.range(0, length)
.mapToObj(i -> String.valueOf(chars.charAt(rnd.nextInt(chars.length()))))
.collect(Collectors.joining());
}
Updated: October 17, 2024