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