Alex D.
Alex D.

Reputation: 1423

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...

Upvotes: 1

Views: 1519

Answers (3)

Oush
Oush

Reputation: 3338

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.

Upvotes: 3

Ankush Shrivastava
Ankush Shrivastava

Reputation: 574

The isPurchsed method can't catch history of error's purchase / canceled Purchase/ Retrived Purchase.

Use this and don't forget to add INTERNET permission in your manifest.

TransactionDetails transactionDetails = billingProcessor.getPurchaseTransactionDetails("productId");
 if (transactionDetails != null) {
     //Already purchased
     //You may save this as boolean in the SharedPreferences.
 }

Upvotes: 0

filol
filol

Reputation: 1681

Have you try to call bp.loadOwnedPurchasesFromGoogle(); ?

Edit

So try this :

Purchase purchase = inventory.getPurchase(product);
Log.d(TAG, "Purchase state: " + purchase.getPurchaseState());
// 0 (purchased), 1 (canceled), or 2 (refunded).
if (purchase.getPurchaseState() == 0
     || purchase.getPurchaseState() == 2) {
   showPremiumVersion();
} else {
   showFreeVersion();
}

Or this solution :

bp.isPurchased("yourSKU")

Upvotes: 1

Related Questions