Search code examples
androidin-app-billingin-app-subscriptionplay-billing-library

Checking if in-app subscriptions is active in my serverless android app


I have the serverless android app with simple functional: if user has some in-app subscription (auto renewable), then he can use functional in app, otherwise there is no. I know, how to make functional with obtaining subscriptions info (price, title etc) and calling payment. But I can not check if current user has active (not cancelled) subscriptions. I read so many information on many sites and tutorials, and there was written that I must use google API in my server. But I do not have my own server. I used two different libraries for in-app subscriptions:

'com.anjlab.android.iab.v3:library:1.0.44'

and

'com.android.billingclient:billing:1.1'

but no one helped me for checking if user has active subscriptions. So, how to make this task? Help me please, maybe I missed some information...


Solution

  • Edit: Anjlab library hasn't been updated in the longest time ever. Kindly use Google's own billing library, this step-by-step process should help you easily integrate it into your app - Google In App Billing library

    Using the anjlab in-app-billing library I was also facing the similar. This is what I did to get around it.

    Invoke the method billingProcessor.loadOwnedPurchasesFromGoogle(); Then check the value of transactionDetails, if the TransactionDetails object return null it means, that the user did not subscribe or cancelled their subscription, otherwise they are still subscribed.

    void checkIfUserIsSusbcribed(){
      Boolean purchaseResult = billingProcessor.loadOwnedPurchasesFromGoogle();
      if(purchaseResult){
         TransactionDetails subscriptionTransactionDetails = billingProcessor.getSubscriptionTransactionDetails(YOUR_SUBSCRIPTION_ID);
    
         if(subscriptionTransactionDetails!=null)
            //User is still subscribed
         else  
            //Not subscribed
         }
    }
    

    Also, point to note is that the TransactionDetails object will only return null after the period of the subscription has expired.