Skip to main content
Version: 1.0-rc

Complete Implementation

IAPKit - In-App Purchase Made Simple
tip

The complete working example can be found at example/composeApp.

Flow Overview

Connect → Fetch Products → Request Purchase → Validate → Finish Transaction

Example Screens

ScreenDescriptionSource
Purchase FlowOne-time purchasesPurchaseFlowScreen.kt
Subscription FlowRecurring purchasesSubscriptionFlowScreen.kt

Key Patterns

ViewModel with StateFlow

class StoreViewModel : ViewModel() {
private val _state = MutableStateFlow(StoreState())
val state: StateFlow<StoreState> = _state.asStateFlow()

init {
viewModelScope.launch {
kmpIapInstance.initConnection()
kmpIapInstance.purchaseUpdatedListener.collect { purchase ->
handlePurchase(purchase)
}
}
}
}

Product Configuration

object ProductConfig {
private val products = mapOf(
"coins_100" to ProductType.CONSUMABLE,
"remove_ads" to ProductType.NON_CONSUMABLE,
"premium_monthly" to ProductType.SUBSCRIPTION
)

fun isConsumable(productId: String) =
products[productId] == ProductType.CONSUMABLE
}

Error Recovery

when (error.code) {
ErrorCode.NetworkError -> retry(delay = 2000)
ErrorCode.AlreadyOwned -> restorePurchases()
else -> showError(error.message)
}

Security Best Practices

  1. Server-side validation - Always validate receipts on your backend
  2. Secure storage - Use platform-specific secure storage for sensitive data
  3. HTTPS only - All network requests must use TLS
  4. Code obfuscation - Enable R8/ProGuard for release builds

Resources