anjlab / android-inapp-billing-v3

A lightweight implementation of Android In-app Billing Version 3
Other
2.2k stars 536 forks source link

Canceled Subscriptions is not working. #520

Open pavelsust opened 2 years ago

pavelsust commented 2 years ago

After successful subscription I had unsubscribe from my google play. But still, now I am getting purchaseData.autoRenewing true. So how I can solve this issue.

Is there any other way to know user unsubscribed from my service?

faizan-ali-brainx commented 2 years ago

@pavelsust Faced the same issue. The only solution left for me was to use the official Google API to check this. Let me know if you need any help with that.

tripping-alien commented 2 years ago

Hello @faizan-ali-brainx, could you explain your method please? I'm facing the same problem, it caches the result but it's unclear when does it refresh the cache. Maybe it's possible to add a method to refresh the cache.

tripping-alien commented 2 years ago

After successful subscription I had unsubscribe from my google play. But still, now I am getting purchaseData.autoRenewing true. So how I can solve this issue.

Is there any other way to know user unsubscribed from my service?

You should be using isSubscribed("aaa");

faizan-ali-brainx commented 2 years ago

@tripping-alien Yes, the problem lies somewhere in cashing. When you subscribe for the first time. It always returns true from onward whether you cancel from the play store or your subscription expires. Yes, I was using the isSubscribed() method provided by the library. The pull request # 517 solved this issue I guess. Not sure though. Either you can wait for it to merge or solve it on your own. I didn't had time to wait this long so I opted to use the official Google API for this. You can use this or modify it according to your needs.

`class SubscriptionUtils() {

private val purchasesUpdatedListener =
    PurchasesUpdatedListener { billingResult, purchases ->

    }

fun startConnection(
    context: Context,
    isMonthlyOrYearly: (Boolean, Boolean) -> Unit
) {
    val billingClient = BillingClient.newBuilder(context)
        .setListener(purchasesUpdatedListener)
        .enablePendingPurchases()
        .build()

    billingClient.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                billingClient.queryPurchasesAsync(
                    BillingClient.SkuType.SUBS
                ) { code, list ->
                    if (list.size > 0) {
                        if (list[0].skus[0] == "Your subscription Id here") {
                            if (list[0].isAutoRenewing) {
                                // Your logic here
                                isMonthlyOrYearly(true,false)
                            }
                        }
                    } else {
                        isMonthlyOrYearly(false, false)
                    }
                }
            }
        }

        override fun onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    })
}

}`

And then simply call like this where you want to check

SubscriptionUtils().startConnection(this) { isMonthly, isYearly -> }

Here isMonthly or isYearly indicates whether your subscription was canceled or expired. True means yes and false means no. Happy coding!!!

lee4jun2 commented 2 years ago

When I used bp.loadOwnedPurchasesFromGoogleAsync, I got the correct autoRenewing value.

bp.loadOwnedPurchasesFromGoogleAsync(object: BillingProcessor.IPurchasesResponseListener{
            override fun onPurchasesSuccess() {
                if(bp.isPurchased(productId)){
                    // ~~
                }else if(bp.isSubscribed(productId)){
                     // ~~
                }else{
                    // ~~
                }
            }

            override fun onPurchasesError() {
            }

        })