With the Qonversion API, I am adding a feature for users to purchase subscriptions. When the user comes back after the purchased subscription, I want to check the subscription status, but I get a "null" value from checkEntitlements as a return. At the same time, although the purchase is positive in the purchase function, it does not enter onSuccess at all. How can I check the user's subscription status? (When I try to subscribe again after the purchase is made, google says I am already subscribed, so the subscription process is successful.)
Fragment:
class PurchaseFragment : Fragment() {
private var _binding: FragmentPurchaseBinding? = null
private val binding get() = _binding!!
private var product: QProduct? = null
Displaying product:
private fun displayProducts() {
Qonversion.shared.offerings(object : QonversionOfferingsCallback {
override fun onSuccess(offerings: QOfferings) {
val mainOffering = offerings.main
if (mainOffering != null && mainOffering.products.isNotEmpty()) {
binding.monthlyPrice.text = mainOffering.products[0].prettyPrice
product = mainOffering.products[0]
}
}
override fun onError(error: QonversionError) {
Log.d(TAG, "Error: $error")
}
})
}
Making purchase:
private fun makePurchase(product: QProduct) {
Qonversion.shared.purchase(
requireActivity(),
product,
callback = object : QonversionEntitlementsCallback {
override fun onSuccess(entitlements: Map<String, QEntitlement>) {
val premiumEntitlement = entitlements["premium_permisson"]
if (premiumEntitlement != null && premiumEntitlement.isActive) {
Log.d(TAG, "Entitlements: $premiumEntitlement")
} else {
Log.d(TAG, "Entitlements else: $premiumEntitlement")
}
}
override fun onError(error: QonversionError) {
// Handle error here
if (error.code === QonversionErrorCode.CanceledPurchase) {
Log.d(TAG, "Canceled")
}
}
})
}
Check status of user:
private fun isItPremiumUser() {
Qonversion.shared.checkEntitlements(object : QonversionEntitlementsCallback {
override fun onSuccess(entitlements: Map<String, QEntitlement>) {
val premiumEntitlement = entitlements["premium_permisson"]
Log.d(TAG, "isItPremiumUser $premiumEntitlement") // it returns null
if (premiumEntitlement != null && premiumEntitlement.isActive) {
Log.d(TAG, "isItPremiumUser Entitlements: $premiumEntitlement")
// handle active entitlement here
// also you can check renew state if needed
// for example to check if user has canceled subscription and offer him a discount
when (premiumEntitlement.renewState) {
QEntitlementRenewState.NonRenewable -> {
Log.d(TAG, "NonRenewable Entitlements: $premiumEntitlement")
// NonRenewable is the state of a consumable or non-consumable in-app purchase
}
QEntitlementRenewState.WillRenew -> {
Log.d(TAG, "WillRenew Entitlements: $premiumEntitlement")
// WillRenew is the state of an auto-renewable subscription
}
QEntitlementRenewState.BillingIssue -> {
Log.d(TAG, "BillingIssue Entitlements: $premiumEntitlement")
// Prompt the user to update the payment method.
}
QEntitlementRenewState.Canceled -> {
Log.d(TAG, "Canceled Entitlements: $premiumEntitlement")
// The user has turned off auto-renewal for the subscription, but the subscription has not expired yet.
// Prompt the user to resubscribe with a special offer.
}
else -> {
Log.d(TAG, "else Entitlements: $premiumEntitlement")
}
}
}
}
override fun onError(error: QonversionError) {
// handle error here
Log.d(TAG, "onError: $error")
}
})
}