
Facilitates in-app purchases, offering features for product management, purchase handling, and transaction validation with a streamlined API for easy integration.
A Kotlin Multiplatform library that provides unified in-app purchase functionality for Android and iOS platforms. This library abstracts Google Play Billing (Android) and StoreKit 2 (iOS) into a single, easy-to-use API.
Add the following dependency to your
implementation("io.github.aditya-gupta99:inAppPurchase-kmp:1.0.12")Create IAPManager object to inject into activity.
Common Payment Module:
val PaymentModule = module {
singleOf(::IAPManager)
singleOf(::InAppPurchaseProvider)
}For Android, you need to provide both context and activity to the IAPManager. Set this up in your main activity:
class MainActivity : ComponentActivity() {
private val iapManager: IAPManager by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
iapManager.setContext(context = this, activity = this)
setContent {
MainApp()
}
}
}class InAppPurchaseProvider(
private val inAppPurchaseManager: IAPManager
) : PaymentProvider {
override suspend fun initialize() {
inAppPurchaseManager.initialize()
}
override suspend fun makePayment() {
val getProductState = inAppPurchaseManager.getProducts(
listOf("your_product_id")
)
when (getProductState) {
is IAPResult.Error -> {
// Handle error
}
is IAPResult.Success -> {
getProductState.data.getOrNull(0)?.let { product ->
inAppPurchaseManager.launchPurchaseFlow(product)
}
}
}
}
}The following sections provide detailed examples of how to use each method from the IAPManager class that you saw in the basic implementation above.
val result = iapManager.initialize()
when (result) {
is IAPResult.Success -> {
// Library initialized successfully
}
is IAPResult.Error -> {
// Handle initialization error
}
}val productsResult = iapManager.getProducts(listOf("product_id_1", "product_id_2"))
when (productsResult) {
is IAPResult.Success -> {
val products = productsResult.data
// Display products to user
}
is IAPResult.Error -> {
// Handle error
}
}val purchaseResult = iapManager.launchPurchaseFlow(selectedProduct)
when (purchaseResult) {
is IAPResult.Success -> {
// Purchase flow launched successfully
}
is IAPResult.Error -> {
// Handle error
}
}iapManager.getPurchaseUpdates().collect { purchase ->
// Handle new purchase
// Verify purchase on your server
// Acknowledge or consume the purchase
}For non-consumable products:
val acknowledgeResult = iapManager.acknowledgePurchase(purchase)
when (acknowledgeResult) {
is IAPResult.Success -> {
// Purchase acknowledged
}
is IAPResult.Error -> {
// Handle error
}
}For consumable products:
val consumeResult = iapManager.consumePurchase(purchase)
when (consumeResult) {
is IAPResult.Success -> {
// Purchase consumed, user can buy again
}
is IAPResult.Error -> {
// Handle error
}
}val purchasesResult = iapManager.getPurchases()
when (purchasesResult) {
is IAPResult.Success -> {
val purchases = purchasesResult.data
// Process existing purchases
}
is IAPResult.Error -> {
// Handle error
}
}val restoreResult = iapManager.restorePurchases()
when (restoreResult) {
is IAPResult.Success -> {
val restoredPurchases = restoreResult.data
// Handle restored purchases
}
is IAPResult.Error -> {
// Handle error
}
}The IAPManager is the core interface of the library that provides all necessary functions to handle in-app purchases in a cross-platform manner.
| Method | Return Type | Description |
|---|---|---|
initialize() |
IAPResult<Unit> |
Initializes the billing/store session. Must be called before making purchases. |
getProducts(productIds: List<String>) |
IAPResult<List<Product>> |
Retrieves details for a list of product IDs from the store. |
launchPurchaseFlow(product: Product) |
IAPResult<Unit> |
Launches the native purchase UI for the given product. |
getPurchaseUpdates() |
Flow<Purchase> |
A Kotlin Flow stream that emits purchase events (new, updated, restored). |
acknowledgePurchase(purchase: Purchase) |
IAPResult<Unit> |
Acknowledges a non-consumable product purchase (Google Play). |
consumePurchase(purchase: Purchase) |
IAPResult<Unit> |
Consumes a consumable purchase, allowing it to be purchased again. |
getPurchases() |
IAPResult<List<Purchase>> |
Returns a list of active or already processed purchases. |
restorePurchases() |
IAPResult<List<Purchase>> |
Restores previously made purchases (especially for iOS). |
โ ๏ธ Each method returns anIAPResult, which can be eitherSuccessorError, making it safe and predictable to handle results.
Need help with integration or found an issue?
If you're using this in a commercial project and need priority support, feel free to reach out via GitHub or email.
A Kotlin Multiplatform library that provides unified in-app purchase functionality for Android and iOS platforms. This library abstracts Google Play Billing (Android) and StoreKit 2 (iOS) into a single, easy-to-use API.
Add the following dependency to your
implementation("io.github.aditya-gupta99:inAppPurchase-kmp:1.0.12")Create IAPManager object to inject into activity.
Common Payment Module:
val PaymentModule = module {
singleOf(::IAPManager)
singleOf(::InAppPurchaseProvider)
}For Android, you need to provide both context and activity to the IAPManager. Set this up in your main activity:
class MainActivity : ComponentActivity() {
private val iapManager: IAPManager by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
iapManager.setContext(context = this, activity = this)
setContent {
MainApp()
}
}
}class InAppPurchaseProvider(
private val inAppPurchaseManager: IAPManager
) : PaymentProvider {
override suspend fun initialize() {
inAppPurchaseManager.initialize()
}
override suspend fun makePayment() {
val getProductState = inAppPurchaseManager.getProducts(
listOf("your_product_id")
)
when (getProductState) {
is IAPResult.Error -> {
// Handle error
}
is IAPResult.Success -> {
getProductState.data.getOrNull(0)?.let { product ->
inAppPurchaseManager.launchPurchaseFlow(product)
}
}
}
}
}The following sections provide detailed examples of how to use each method from the IAPManager class that you saw in the basic implementation above.
val result = iapManager.initialize()
when (result) {
is IAPResult.Success -> {
// Library initialized successfully
}
is IAPResult.Error -> {
// Handle initialization error
}
}val productsResult = iapManager.getProducts(listOf("product_id_1", "product_id_2"))
when (productsResult) {
is IAPResult.Success -> {
val products = productsResult.data
// Display products to user
}
is IAPResult.Error -> {
// Handle error
}
}val purchaseResult = iapManager.launchPurchaseFlow(selectedProduct)
when (purchaseResult) {
is IAPResult.Success -> {
// Purchase flow launched successfully
}
is IAPResult.Error -> {
// Handle error
}
}iapManager.getPurchaseUpdates().collect { purchase ->
// Handle new purchase
// Verify purchase on your server
// Acknowledge or consume the purchase
}For non-consumable products:
val acknowledgeResult = iapManager.acknowledgePurchase(purchase)
when (acknowledgeResult) {
is IAPResult.Success -> {
// Purchase acknowledged
}
is IAPResult.Error -> {
// Handle error
}
}For consumable products:
val consumeResult = iapManager.consumePurchase(purchase)
when (consumeResult) {
is IAPResult.Success -> {
// Purchase consumed, user can buy again
}
is IAPResult.Error -> {
// Handle error
}
}val purchasesResult = iapManager.getPurchases()
when (purchasesResult) {
is IAPResult.Success -> {
val purchases = purchasesResult.data
// Process existing purchases
}
is IAPResult.Error -> {
// Handle error
}
}val restoreResult = iapManager.restorePurchases()
when (restoreResult) {
is IAPResult.Success -> {
val restoredPurchases = restoreResult.data
// Handle restored purchases
}
is IAPResult.Error -> {
// Handle error
}
}The IAPManager is the core interface of the library that provides all necessary functions to handle in-app purchases in a cross-platform manner.
| Method | Return Type | Description |
|---|---|---|
initialize() |
IAPResult<Unit> |
Initializes the billing/store session. Must be called before making purchases. |
getProducts(productIds: List<String>) |
IAPResult<List<Product>> |
Retrieves details for a list of product IDs from the store. |
launchPurchaseFlow(product: Product) |
IAPResult<Unit> |
Launches the native purchase UI for the given product. |
getPurchaseUpdates() |
Flow<Purchase> |
A Kotlin Flow stream that emits purchase events (new, updated, restored). |
acknowledgePurchase(purchase: Purchase) |
IAPResult<Unit> |
Acknowledges a non-consumable product purchase (Google Play). |
consumePurchase(purchase: Purchase) |
IAPResult<Unit> |
Consumes a consumable purchase, allowing it to be purchased again. |
getPurchases() |
IAPResult<List<Purchase>> |
Returns a list of active or already processed purchases. |
restorePurchases() |
IAPResult<List<Purchase>> |
Restores previously made purchases (especially for iOS). |
โ ๏ธ Each method returns anIAPResult, which can be eitherSuccessorError, making it safe and predictable to handle results.
Need help with integration or found an issue?
If you're using this in a commercial project and need priority support, feel free to reach out via GitHub or email.