I have two subscriptions in the application, each of them individually works correctly. But when I try to do an Update/Downgrade, problems arise:
- I have two subscriptions in the application, each of them individually works correctly. But when I try to do an Update/Downgrade, problems arise: When I try to change one subscription to another, I get the correct window with changing the subscription and shows that the new subscription was purchased successfully, but when I check on Google Play, it shows only the one I bought first, after a while I try again, this time it does not appear replacement window, only the standard purchase window, the second one is bought without problems and both are visible in the Store. Maybe I have some wrong approach to updating, everything seems to be in its place otherwise, the window with the replacement would not be displayed correctly.
- My setSuccessfulScreen() method, in onPurchasesUpdated(), works correctly on the first purchase, but it crashes with an error when I try to update the subscription:
Fatal Exception: java.lang.RuntimeException: Error receiving broadcast Intent { act=com.android.vending.billing.PURCHASES_UPDATED
I thought maybe the case in Binding, I replaced with findViewById(), it does not help, without this method there is no crash. My subscription code:
private fun buySubscription() {
val skuList = ArrayList<String>()
skuList.add("")
skuList.add("")
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS)
billingClient!!.querySkuDetailsAsync(params.build()) { billingResult, skuDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
when (subsFlag) {
MONTH -> {
if (!ProviderPreferences.isSubscribed()) {
billingClient!!.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList[1]!!).build())
} else {
if (ProviderPreferences.getSubscribedType() == MONTH) {
billingClient!!.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList[1]!!).build())
} else {
val updateParams = BillingFlowParams.SubscriptionUpdateParams.newBuilder().setOldSkuPurchaseToken(ProviderPreferences.getPurchaseToken()!!).setReplaceSkusProrationMode(BillingFlowParams.ProrationMode.DEFERRED).build()
billingClient!!.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList[1]!!).setSubscriptionUpdateParams(updateParams).build())
}
}
}
YEAR -> {
if (!ProviderPreferences.isSubscribed()) {
billingClient!!.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList[0]!!).build())
} else {
if (ProviderPreferences.getSubscribedType() == YEAR) {
billingClient!!.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList[0]!!).build())
} else {
val updateParams = BillingFlowParams.SubscriptionUpdateParams.newBuilder().setOldSkuPurchaseToken(ProviderPreferences.getPurchaseToken()!!).setReplaceSkusProrationMode(BillingFlowParams.ProrationMode.DEFERRED).build()
billingClient!!.launchBillingFlow(requireActivity(), BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList[0]!!).setSubscriptionUpdateParams(updateParams).build())
}
}
}
}
}
}
}
private fun purchases() {
val acknowledgePurchaseResponseListener = AcknowledgePurchaseResponseListener { billingResult -> }
billingClient = BillingClient.newBuilder(App.getInstance()).enablePendingPurchases().setListener { billingResult: BillingResult, list: List<Purchase>? ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
val purchase = list!![0]
handlePurchases(acknowledgePurchaseResponseListener, purchase)
ProviderPreferences.saveSubscriptionType(subsFlag)
ProviderPreferences.savePurchaseToken(list[0].purchaseToken)
setSuccessfulScreen()
}
}.build()
connectToGooglePlayBilling()
}
private fun handlePurchases(acknowledgePurchaseResponseListener: AcknowledgePurchaseResponseListener?, purchase: Purchase) {
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
ProviderPreferences.saveSubscription(true)
if (!purchase.isAcknowledged) {
val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.purchaseToken).build()
billingClient!!.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener!!)
}
}
}
private fun connectToGooglePlayBilling() {
billingClient!!.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
billingClient!!.queryPurchasesAsync(BillingClient.SkuType.SUBS) { billingResult, list ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && list != null) {
}
}
}
override fun onBillingServiceDisconnected() {
connectToGooglePlayBilling()
}
})
}
private fun setSuccessfulScreen() {
group!!.visibility = View.GONE
group2!!.visibility = View.VISIBLE
subscribeBtn!!.visibility = View.GONE
lottieAnimation!!.playAnimation()
}
I am using the billing v4 library. Please tell me what could be the reason, maybe something is wrong with the subscription change code, I have not worked with subscription renewal before. And what could be the reason for the crash. Thank you in advance.