为你的 KMP 应用添加应用内购买功能
使用 Adapty 的 KMP SDK 在 iOS 和 Android 上集成应用内购买。处理收据、追踪订阅状态,无需自建后端——一次集成即可实现全部功能。
import com.adapty.kmp.Adapty
import com.adapty.kmp.models.AdaptyConfig
val config = AdaptyConfig
.Builder("PUBLIC_SDK_KEY")
.build()
Adapty.activate(configuration = config)
.onSuccess {
Log.d("Adapty", "SDK initialised")
}
.onError { error ->
Log.e("Adapty", "Adapty init error: ${error.message}")
}
你无需为 KMP 应用内购买功能构建后端
Adapty SDK 处理所有通常在服务器上完成的任务:收据验证、订阅状态、续订、试用和退款。你只需连接一次,即可获得适用于 iOS 和 Android 的KMP 应用内购买配置。
安排演示为什么选择 Adapty SDK?
跨平台追踪订阅状态
你将始终知道用户在 iOS 和 Android 上是否拥有活跃订阅。
在后端验证收据
无需自建验证系统,Adapty 会自动完成。
处理各种订阅状态
支持免费试用、升级、促销优惠、家庭共享、续订等。
企业级可扩展核心
我们频繁更新,保持 SDK 稳定,并运行在 >99.99% SLA 上。
配置平台
安装 Adapty SDK
await adapty.activate( 'PUBLIC_SDK_KEY', { customerUserId: 'YOUR_USER_ID', });
处理购买事件
使用 5 个 SDK 方法处理 KMP IAP
import com.adapty.kmp.Adapty
import com.adapty.kmp.models.AdaptyPurchaseResult
Adapty.makePurchase(product = product).onSuccess { purchaseResult ->
when (purchaseResult) {
is AdaptyPurchaseResult.Success -> {
val profile = purchaseResult.profile
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true) {
// Grant access to the paid features
}
}
is AdaptyPurchaseResult.UserCanceled -> {
// Handle the case where the user canceled the purchase
}
is AdaptyPurchaseResult.Pending -> {
// Handle deferred purchases (e.g., the user will pay offline with cash)
}
}
}.onError { error ->
// Handle the error
}
import com.adapty.kmp.Adapty
Adapty.restorePurchases().onSuccess { profile ->
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true) {
// successful access restore
}
}.onError { error ->
// handle the error
}
import com.adapty.kmp.Adapty
Adapty.identify("YOUR_USER_ID") // Unique for each user
.onSuccess {
// successful identify
}
.onError { error ->
// handle the error
}
import com.adapty.kmp.Adapty
import com.adapty.kmp.models.AdaptyProfile
import com.adapty.kmp.models.AdaptyProfileParameters
val builder = AdaptyProfileParameters.Builder()
.withEmail("[email protected]")
.withPhoneNumber("+18888888888")
.withFirstName("John")
.withLastName("Appleseed")
.withGender(AdaptyProfile.Gender.FEMALE)
.withBirthday(AdaptyProfile.Date(1970, 1, 3))
Adapty.updateProfile(builder.build())
.onSuccess {
// profile updated successfully
}
.onError { error ->
// handle the error
}
import com.adapty.kmp.models.AdaptyProfileParameters
val builder = AdaptyProfileParameters.Builder()
// You can set your own custom attributes:
builder.withCustomAttribute("key1", "value1")
// To remove existing key, use .withRemovedCustomAttribute() method:
builder.withRemovedCustomAttribute("key2")