вот как я это реализовал,
Код: Выделить всё
@AndroidEntryPoint
class MainActivity2 : ComponentActivity(), PurchasesUpdatedListener {
@Inject
lateinit var userPreferencesRepository: UserPreferencesRepository
private lateinit var billingClient: BillingClient
private val productId = "premium_features"
@OptIn(ExperimentalPermissionsApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
billingClient = BillingClient.newBuilder(this)
.setListener(this)
.enablePendingPurchases()
.build()
startBillingConnection() // If I not call this method, purchase system is not working
setContent {
MainApp(
onUpgradeSubmitClick = {
launchPurchaseFlow()
}
)
}
}
private fun startBillingConnection() {
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
Toast.makeText(this@MainActivity2, "Billing Client Ready", Toast.LENGTH_SHORT).show()
// I never see this toast message
}
}
override fun onBillingServiceDisconnected() {
Toast.makeText(this@MainActivity2, "Billing Service Disconnected", Toast.LENGTH_SHORT).show()
// I never see this toast message
}
})
}
private fun launchPurchaseFlow() {
val params = QueryProductDetailsParams.newBuilder()
.setProductList(
listOf(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(productId)
.setProductType(BillingClient.ProductType.INAPP)
.build()
)
)
.build()
billingClient.queryProductDetailsAsync(params) { billingResult, productDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && productDetailsList.isNotEmpty()) {
val productDetails = productDetailsList[0]
val purchaseParams = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(
listOf(
BillingFlowParams.ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.build()
)
)
.build()
billingClient.launchBillingFlow(this, purchaseParams)
} else {
Toast.makeText(this, "Premium is not available", Toast.LENGTH_SHORT).show()
}
}
}
override fun onPurchasesUpdated(billingResult: BillingResult, purchases: MutableList?) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
for (purchase in purchases) {
handlePurchase(purchase)
}
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
Toast.makeText(this, "Purchase canceled!", Toast.LENGTH_SHORT).show()
lifecycleScope.launch{
userPreferencesRepository.changePremiumStatus(false)
}
}
else if (billingResult.responseCode == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED){
Toast.makeText(this, "Welcome back to Premium!", Toast.LENGTH_SHORT).show()
lifecycleScope.launch{
userPreferencesRepository.changePremiumStatus(true)
}
}
else {
Toast.makeText(this, "Purchase failed!", Toast.LENGTH_SHORT).show()
lifecycleScope.launch{
userPreferencesRepository.changePremiumStatus(false)
}
}
}
private fun handlePurchase(purchase: Purchase) {
Toast.makeText(this, "Welcome to Groovy Premium!", Toast.LENGTH_SHORT).show()
lifecycleScope.launch{
userPreferencesRepository.changePremiumStatus(true)
}
}
override fun onDestroy() {
super.onDestroy()
billingClient.endConnection()
}
}
Библиотека, которую я использую,
Код: Выделить всё
implementation("com.android.billingclient:billing:6.0.1")
Подробнее здесь: https://stackoverflow.com/questions/793 ... ompose-app