In-App billing with BillingClient. ResponceCode = -1. Service connection is disconnected
Asked Answered
C

3

6

In my application, a donation function was made. (The application with permission to purchase inside the application is available in google play and in the downloaded version, this function works) I did it using the easy library from anjlab. I started to update the application, and rewrote it at the same time on Kotlin. Donat stopped working for some reason. I decided to redo the code from the anjlab library to the new billing library from google. When I try to make a purchase, I get

responseCode = -1 (service connection is disconnected).

I tried to resume the connection using startConnection (this), tried to change the version in the gradle, loaded the new version into the library apk. Neither the old nor the new does not work (if you download from google play, the one that is published, it works). Code wrote on the guideline from Google. I test the application signed on a real device

SettingsFramgent.kt (only sacrament code)

package *

import android.content.Intent
import androidx.preference.PreferenceFragmentCompat
import android.os.Bundle
import android.widget.Toast
import androidx.preference.Preference

import com.starikov.datecalc.R

class SettingsFragment : PreferenceFragmentCompat(), SettingsContract.View {

    private lateinit var presenter: SettingsContract.Presenter

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, rootKey)

        presenter = SettingsPresenter(activity!!)
        presenter.attachView(this)

val makeDonatePref: Preference = findPreference(MAKE_DONATE_KEY)!!

        makeDonatePref.setOnPreferenceClickListener { presenter.makeDonateClicked(); true }

        presenter.viewIsReady()
    }

    override fun showToast(resId: Int) {
        Toast.makeText(activity, resId, Toast.LENGTH_LONG).show()
    }

    override fun startActivity(intent: Intent) {
        activity!!.startActivity(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.detachView()
        if (activity!!.isFinishing) {
            presenter.destroy()
        }
    }

    companion object {
        private const val MAKE_DONATE_KEY = "make_donate"
    }
}

SettingsPresenter.kt

package *

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import com.android.billingclient.api.*

import com.starikov.datecalc.common.PresenterBase
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.SkuDetailsParams
import com.android.billingclient.api.BillingFlowParams
import com.starikov.datecalc.R
import java.util.*


internal class SettingsPresenter internal constructor(private val activity: Activity)
    : PresenterBase<SettingsContract.View>(), SettingsContract.Presenter {
    private lateinit var billingClient: BillingClient
    private var skuDetailsMap: HashMap<String, SkuDetails> = HashMap()

    override fun makeDonateClicked() {
        initBillingProcessor()
        launchBilling(DONATE_PRODUCT_ID)
    }

    override fun destroy() {
        billingClient.endConnection()
    }

    override fun viewIsReady() {}

    private fun initBillingProcessor() {
        billingClient = BillingClient.newBuilder(activity)
                .enablePendingPurchases()
                .setListener {
            billingResult, purchases ->
            if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
                //purchase done
                view!!.showToast(R.string.thanks_for_donate)
            }
        }.build()

        billingClient.startConnection(object : BillingClientStateListener {
            override fun onBillingSetupFinished(billingResult: BillingResult?) {
                if (billingResult?.responseCode == BillingClient.BillingResponseCode.OK) {
                     // **place of logs**
                    querySkuDetails() 
                }
            }

            override fun onBillingServiceDisconnected() {
                //if wrong
            }
        })
    }

    private fun launchBilling(skuId: String) {
        val billingFlowParams = BillingFlowParams.newBuilder()
                .setSkuDetails(skuDetailsMap[skuId])
                .build()
        billingClient.launchBillingFlow(activity, billingFlowParams)
    }

    private fun querySkuDetails() {
        val skuDetailsParamsBuilder = SkuDetailsParams.newBuilder()
        val skuList = ArrayList<String>()
        skuList.add(DONATE_PRODUCT_ID)
        skuDetailsParamsBuilder.setSkusList(skuList).setType(BillingClient.SkuType.INAPP)
        billingClient.querySkuDetailsAsync(skuDetailsParamsBuilder.build()) { billingResult, skuDetailsList ->
            if (billingResult?.responseCode ==  BillingClient.BillingResponseCode.OK) {
                for (skuDetails in skuDetailsList!!) {
                    skuDetailsMap[skuDetails.sku] = skuDetails
                }
            }
        }
    }

    companion object {
        private const val DONATE_PRODUCT_ID = "donate"
    }
}

In "place of logs", i checked billingResult.responseCode = -1; debugMessage = "Service connection is disconnected"

I have no idea what to do. Thanks in advance for your help

Capercaillie answered 18/6, 2019 at 11:59 Comment(0)
P
4

In my case I was using a lazy initialization.

That way, the Google Billing setup started immediately before the launchBillingFlow method call.

That caused the service to be disconnected and not ready yet for the precise moment the billing flow was started.

I now start the BillingClient connection since the activity is started, and the flow works as expected when requested by the user.

Payee answered 12/5, 2020 at 2:22 Comment(0)
A
2

Not sure if you are still having this problem, but I had it and debugging showed that the billing client was still using some old AIDL stuff I had not yet removed in the old com.android.vending package. Once I removed all that code and gutted my app's support for the old stuff, I was able to proceed with the purchase without error.

Alienate answered 14/10, 2019 at 20:21 Comment(0)
H
-2

Faced such a problem. my decision is adding into application xml node

<!-- GOOGLE PLAY BILLING -->
    <activity
        android:name="com.android.billingclient.api.ProxyBillingActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
Humane answered 10/7, 2020 at 5:28 Comment(1)
This is just a regular activity. What is unique about it?Intertwine

© 2022 - 2024 — McMap. All rights reserved.