Retour aux SDKs
A

Android SDK (Kotlin)

v1.0.0

SDK officiel pour Android (API 24+) avec Kotlin et Coroutines

GitHub

Installation

Gradle (Kotlin DSL)

// build.gradle.kts
dependencies {
    implementation("io.simiz:simiz-android:1.0.0")
}

Gradle (Groovy)

// build.gradle
dependencies {
    implementation 'io.simiz:simiz-android:1.0.0'
}

Configuration

import io.simiz.Simiz
import io.simiz.SimizConfiguration

// Dans votre Application class
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        Simiz.configure(
            apiKey = BuildConfig.SIMIZ_SECRET_KEY,
            configuration = SimizConfiguration(
                apiVersion = "2024-01",
                timeout = 30_000L
            )
        )
    }
}

Utilisation

Creer une transaction

import io.simiz.Simiz
import io.simiz.model.*

// Avec Coroutines
suspend fun createPayment(): Transaction {
    return Simiz.transactions.create(
        TransactionCreateParams(
            amount = 5000,
            currency = Currency.XAF,
            paymentMethod = PaymentMethod.ORANGE_MONEY,
            payer = Payer(
                phone = "+237690000000",
                name = "John Doe",
                email = "john@example.com"
            ),
            description = "Achat sur Ma Boutique",
            reference = "ORDER-123",
            metadata = mapOf(
                "order_id" to "123",
                "customer_id" to "cust_456"
            ),
            callbackUrl = "https://votre-site.com/webhooks/simiz",
            returnUrl = "https://votre-site.com/payment/success"
        )
    )
}

// Utilisation
lifecycleScope.launch {
    try {
        val transaction = createPayment()
        println(transaction.id)         // tx_xxx
        println(transaction.status)     // PENDING
        println(transaction.paymentUrl) // URL de paiement
    } catch (e: SimizException) {
        // Gerer l'erreur
    }
}

Recuperer une transaction

val transaction = Simiz.transactions.retrieve("tx_xxx")

if (transaction.status == TransactionStatus.COMPLETED) {
    println("Paiement reussi!")
}

Lister les transactions

val transactions = Simiz.transactions.list(
    TransactionListParams(
        limit = 20,
        status = TransactionStatus.COMPLETED,
        createdAfter = "2024-01-01"
    )
)

transactions.data.forEach { tx ->
    println("${tx.id} - ${tx.amount}")
}

Integration Jetpack Compose

import androidx.compose.runtime.*
import androidx.lifecycle.viewModelScope
import io.simiz.Simiz
import io.simiz.model.*
import kotlinx.coroutines.launch

class CheckoutViewModel : ViewModel() {
    var uiState by mutableStateOf(CheckoutUiState())
        private set

    fun createPayment(amount: Int, phone: String) {
        viewModelScope.launch {
            uiState = uiState.copy(isLoading = true)
            try {
                val tx = Simiz.transactions.create(
                    TransactionCreateParams(
                        amount = amount,
                        currency = Currency.XAF,
                        paymentMethod = PaymentMethod.ORANGE_MONEY,
                        payer = Payer(phone = phone)
                    )
                )
                uiState = uiState.copy(
                    isLoading = false,
                    paymentUrl = tx.paymentUrl
                )
            } catch (e: SimizException) {
                uiState = uiState.copy(
                    isLoading = false,
                    error = e.message
                )
            }
        }
    }
}

@Composable
fun CheckoutScreen(viewModel: CheckoutViewModel = viewModel()) {
    val uiState = viewModel.uiState
    var phone by remember { mutableStateOf("") }

    Column(
        modifier = Modifier.padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        OutlinedTextField(
            value = phone,
            onValueChange = { phone = it },
            label = { Text("Numero de telephone") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone)
        )

        Button(
            onClick = { viewModel.createPayment(5000, phone) },
            enabled = !uiState.isLoading && phone.isNotEmpty()
        ) {
            if (uiState.isLoading) {
                CircularProgressIndicator(modifier = Modifier.size(16.dp))
            } else {
                Text("Payer 5000 XAF")
            }
        }
    }

    // Ouvrir Chrome Custom Tab pour le paiement
    uiState.paymentUrl?.let { url ->
        LaunchedEffect(url) {
            val intent = CustomTabsIntent.Builder().build()
            intent.launchUrl(context, Uri.parse(url))
        }
    }
}

Gestion des erreurs

try {
    val transaction = Simiz.transactions.create(params)
} catch (e: AuthenticationException) {
    // Cle API invalide
    Log.e("Simiz", "Verifiez votre cle API: ${e.message}")
} catch (e: InvalidRequestException) {
    // Parametres invalides
    Log.e("Simiz", "Erreur: ${e.message}, Param: ${e.param}")
} catch (e: ApiException) {
    // Erreur serveur
    Log.e("Simiz", "Erreur API (${e.statusCode}): ${e.message}")
} catch (e: SimizException) {
    // Autre erreur Simiz
    Log.e("Simiz", "Erreur: ${e.message}")
}

Configuration ProGuard

Le SDK inclut automatiquement les regles ProGuard necessaires. Aucune configuration supplementaire n'est requise.