android - 如何在kotlin中实现Android in app purchase?

标签 android kotlin android-inapp-purchase play-billing-library

我想为我的 Android 应用程序的应用程序内购买实现 Google Play 的计费,这是用 kotlin 编写的。我正在关注 this tutorial .

这是我的代码:

private lateinit var billingClient: BillingClient
    private lateinit var productsAdapter: ProductsAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupBillingClient()
    }

    private fun setupBillingClient() {
        billingClient = BillingClient
            .newBuilder(this)
            .setListener(this)
            .build()

        billingClient.startConnection(object : BillingClientStateListener {
            override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
                if (billingResponseCode == BillingClient.BillingResponse.OK) {
                    println("BILLING | startConnection | RESULT OK")
                } else {
                    println("BILLING | startConnection | RESULT: $billingResponseCode")
                }
            }

            override fun onBillingServiceDisconnected() {
                println("BILLING | onBillingServiceDisconnected | DISCONNECTED")
            }
        })
    }

    fun onLoadProductsClicked(view: View) {
        if (billingClient.isReady) {
            val params = SkuDetailsParams
                .newBuilder()
                .setSkusList(skuList)
                .setType(BillingClient.SkuType.INAPP)
                .build()
            billingClient.querySkuDetailsAsync(params) { responseCode, skuDetailsList ->
                if (responseCode == BillingClient.BillingResponse.OK) {
                    println("querySkuDetailsAsync, responseCode: $responseCode")
                    initProductAdapter(skuDetailsList)
                } else {
                    println("Can't querySkuDetailsAsync, responseCode: $responseCode")
                }
            }
        } else {
            println("Billing Client not ready")
        }
    }


    private fun initProductAdapter(skuDetailsList: List<SkuDetails>) {
        productsAdapter = ProductsAdapter(skuDetailsList) {
            val billingFlowParams = BillingFlowParams
                .newBuilder()
                .setSkuDetails(it)
                .build()
            billingClient.launchBillingFlow(this, billingFlowParams)
        }
        //products.adapter = productsAdapter

    }

    override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) {
        println("onPurchasesUpdated: $responseCode")
        allowMultiplePurchases(purchases)
    }

    private fun allowMultiplePurchases(purchases: MutableList<Purchase>?) {
        val purchase = purchases?.first()
        if (purchase != null) {
            billingClient.consumeAsync(purchase.purchaseToken) { responseCode, purchaseToken ->
                if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
                    println("AllowMultiplePurchases success, responseCode: $responseCode")
                } else {
                    println("Can't allowMultiplePurchases, responseCode: $responseCode")
                }
            }
        }
    }

    private fun clearHistory() {
        billingClient.queryPurchases(BillingClient.SkuType.INAPP).purchasesList
            .forEach {
                billingClient.consumeAsync(it.purchaseToken) { responseCode, purchaseToken ->
                    if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
                        println("onPurchases Updated consumeAsync, purchases token removed: $purchaseToken")
                    } else {
                        println("onPurchases some troubles happened: $responseCode")
                    }
                }
            }
    }

    companion object {
        private val skuList = listOf("get_5_coins", "get_10_coins")
    }

我还在我的 build.gradle 中添加了 implementation 'com.android.billingclient:billing:1.2.2' 并且还添加了 com.android.vending .BILLING 和 Internet uses-permission 在我的 menifest 文件中。

当我运行这段代码时,它显示计费客户端尚未准备好。

最佳答案

  1. 通常计费客户端在模拟器中不起作用。因此,要么让模拟器工作,要么更简单的方法是在真实的 Android 手机中检查它,应用程序将使用测试人员的电子邮件下载。
  2. 非常仔细地完成 google play 设置。那部分非常有用。我在那边使用的文档实际上非常好。我一开始无法完全理解它。这都是我的错。另外,您可以查看 this documentation .

关于android - 如何在kotlin中实现Android in app purchase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55466643/

相关文章:

kotlin - 如何在 Kotlin 中使用公共(public)访问器定义 protected 字段

java - "Return type of ... is not a subtype of the return type of the overridden member"?

android - Android 短信状态的 70 值代表什么?

Android 应用内购买降级-升级

java - 空字符串是...从不为空

java - 使用 Retrofit Android 发送 ArrayList<String> 作为一部分

android - 创建 GamesClient 对象

Android:购买后消费产品(应用内计费)

google-play - 突然无法在 google play 控制台中添加应用产品(请检查下面的列表是否有问题。)

java - 创建实现 Serialized/Parcelable 的 Firebase POJO 类