Search code examples
androidplay-billing-librarygoogle-play-billing

Google play billing - How to check user already purchased subscription for 2nd opening the app?


I followed the guide from Google developer document: https://developer.android.com/google/play/billing/integrate?hl=vi

Finished step process Purchase Flow, and handle Purchase result and handle Acknowledge Purchase Response. In this steps, I can determine user purchased success or not.

And currently I don't have any back-end for doing next step of the guide : https://developer.android.com/google/play/billing/backend?hl=vi

When running the app again, how can I determine user had purchased subscription already (without process a purchase flow again ) ? And how to determine user subscription when they changed device ?


Solution

  • it can be done with queryPurchasesAsync

        void QueryPurchase(){
            QueryPurchasesParams queryPurchasesParams = QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build();
            billingClient.queryPurchasesAsync(queryPurchasesParams, new PurchasesResponseListener() {
                @Override
                public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
                    if(list==null){
                        return;
                    }
                    Log.d(TAG, "onQueryPurchasesResponse, list.size="+String.valueOf(list.size()));
                    for(Purchase purchase: list){
                        boolean bIsAutoRenewing = purchase.isAutoRenewing();
                        Log.d(TAG, "bIsAutoRenewing = "+String.valueOf(bIsAutoRenewing));
                    }
                }
            });
        }
    

    then it can be determine by bIsAutoRenewing