Use in an app

The library initialization method must be called in the main thread.

import LBFraudSDKiOS
import Foundation
import CommonCrypto

...

// Replace apikey with the desired generated api key.
let ironchipLBFraud = LBFraudSDKiOS.init(apikey: "XXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
// By default our SDK target to the production environment.
// In case you desire to target a diferent enviroment:
// let ironchipLBFraud = LBFraudSDKiOS.init(apikey: "XXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", environment: Environment.Testing)

//public enum Environment: String {
//    case Production
//    case Testing
//    case Development
//}

//Call Ironchip Location Based Antifraud to Analyze transaction

let data: [String: Any] = [
    "amount": 60,
    "operation": "login"
]
let userId = "john.doe@gmail.com" // Original user identifier
let transactionId = "random_identifier_generated" // Anonymized user ID

//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.

ironchipLBFraud.sendTransaction(transactionId: transactionId, userId: userId, extraData: extraData, finish: {
    // Add here any code you want to be executed after the transaction has finished.
    }, onError: { err in

    // Add here any code you want to perform in case of an error
    // during the transaction.

    // example:
        //if(err is TransactionError) {

            // let transactionError = err as! TransactionError

            // print(transactionError.traceability_id)
            // print(transactionError.message)
            // print(transactionError.http_code)
            // print(transactionError.code)
        //} else {
            // print("NetworkError: ", err)
        //}
    })

Example

import LBFraudSDKiOS
import Foundation
import CommonCrypto

// Replace apikey with the desired generated api key.
let ironchipLBFraud = LBFraudSDKiOS.init(apikey: "XXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

// Function to perform login and call transaction if successful
func login(username: String, password: String) {
    Task {
        let loginSuccessful = await performLogin(username: username, password: password)
        
        if loginSuccessful {
            // If login is successful, call sendTransaction with the necessary parameters
            let transactionID = generateRandomTransactionID(length: 10) // Generate a unique transaction ID
            let anonymizedUserID = anonymizeUserID(username) // Anonymized user ID

            let extraData: [String: Any] = [
                "amount": 30, // Use an appropriate amount
                "operation": "login" // Specify the operation
            ]

            // Call sendTransaction after successful login
            await sendTransactionAfterLogin(transactionID: transactionID, anonymizedUserID: anonymizedUserID, extraData: extraData)
        } else {
            // Handle login failure
        }
    }
}

// Simulated async login function
func performLogin(username: String, password: String) async -> Bool {
    await Task.sleep(2 * 1_000_000_000)
    
    return true
}

// Function to send transaction after successful login
func sendTransactionAfterLogin(transactionID: String, anonymizedUserID: String, extraData: [String: Any]) async {
    // Send the transaction
    ironchipLBFraud.sendTransaction(transactionId: transactionID, userId: anonymizedUserID, extraData: extraData, finish: {
        // Add here any code you want to be executed after the transaction has finished.
    }, onError: { err in
        // Add here any code you want to perform in case of an error during the transaction.
    })
}

// Function to generate a transaction ID
func generateRandomTransactionID(length: Int) -> String {
    let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    var randomID = ""
    for _ in 0..<length {
        if let randomCharacter = characters.randomElement() {
            randomID.append(randomCharacter)
        }
    }
    return randomID
}

// Function to trim, lowercase, and hash the userId
func anonymizeUserID(_ userId: String) -> String {
    let trimmedLowercaseUserID = userId.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
    return sha256Hash(trimmedLowercaseUserID)
}

// Function to perform hashing using SHA-256
func sha256Hash(_ input: String) -> String {
    let inputData = Data(input.utf8)
    var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
    inputData.withUnsafeBytes {
        _ = CC_SHA256($0.baseAddress, CC_LONG(inputData.count), &hash)
    }
    
    return hash.map { String(format: "%02x", $0) }.joined()
}