This is the code to check my VIP user status; I placed it in my application. My user is not considered a VIP user after purchasing my in-app package "iap_lifetime". I'm not sure if there's a problem with my code. I called acknowledgePurchase after receiving a purchase callback from the Listener billingClient.
val acknowledgeParams =
AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.purchaseToken)
.build()
client.acknowledgePurchase(acknowledgeParams) { billingResult: BillingResult ->
runOnUiThread {
processingPurchaseTokens.remove(purchase.purchaseToken)
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
val productId = purchase.products.firstOrNull() ?: ""
AdAnalyticsHelper.trackIapRevenue(purchase, productDetailsMap[productId])
} else {
showToast("Acknowledge failed: ${billingResult.debugMessage}")
}
finalizeVipActivation(grant)
}
}
fun checkSubscription() {
try {
val billingClient = BillingClient.newBuilder(this)
.enablePendingPurchases()
.setListener { _, _ -> }
.build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingServiceDisconnected() {
}
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
VipManager.setVip(false)
return
}
// Check SUBS
billingClient.queryPurchasesAsync(
QueryPurchasesParams.newBuilder()
.setProductType(BillingClient.ProductType.SUBS)
.build()
) { subsResult, subsList ->
var isVip = false
if (subsResult.responseCode == BillingClient.BillingResponseCode.OK) {
isVip = subsList.any { purchase ->
purchase.purchaseState == Purchase.PurchaseState.PURCHASED &&
purchase.products.isNotEmpty()
}
}
if (!isVip) {
billingClient.queryPurchasesAsync(
QueryPurchasesParams.newBuilder()
.setProductType(BillingClient.ProductType.INAPP)
.build()
) { inAppResult, inAppList ->
if (inAppResult.responseCode == BillingClient.BillingResponseCode.OK) {
isVip = inAppList.any { purchase ->
purchase.purchaseState == Purchase.PurchaseState.PURCHASED &&
purchase.products.contains("iap_lifetime")
}
}
VipManager.setVip(isVip)
}
} else {
VipManager.setVip(true)
}
}
}
})
} catch (_: Exception) {
}
}