Dilazak
Dilazak

Reputation: 149

How to query old (one time) purchased items with play billing libray v6.0.1

I am migrating from In-App Billing v3 to Play Billing Library v6.0.1. I am stucked at a point where I need to query and confirm old (one time) purchases with play billing library v6.0.1. With in-app billing v3 it was easy to query a purchase by using this method

.isPurchased()

and it would return if an item is purchased or not. In this new play billing library v6.0.1 there exits no such convenient method to perform this task easily.

Provide a code sample where I am able to query and confirm old purchases with play billing library v6.0.1.

Upvotes: 0

Views: 826

Answers (1)

Keyur Patel
Keyur Patel

Reputation: 19

You can query if user has already purchased any of your subscription or one-time products using this snippet :

public static void isAlreadyPurchaseQuery() {
    if (billingClient.isReady()) {

        billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build(), new PurchasesResponseListener() {
            @Override
            public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
                if (!list.isEmpty()){
                    if (list.get(0).getProducts().contains("your one-time purchase key")){
                        // purchased = true
                    } else {
                        billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(), new PurchasesResponseListener() {
                            @Override
                            public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
                                if (!list.isEmpty()){
                                    if (list.get(0).getProducts().contains("Your subscription key")){
                                        // purchased = true
                                    } else {
                                        // purchased = false
                                    }
                                } else {
                                    // purchased = false
                                }
                            }
                        });
                    }
                } else {
                    billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(), new PurchasesResponseListener() {
                        @Override
                        public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
                            if (!list.isEmpty()){
                                if (list.get(0).getProducts().contains("Your subscription key")){
                                    // purchased = true
                                } else {
                                    // purchased = false
                                }
                            } else {
                                // purchased = false
                            }
                        }
                    });
                }
            }
        });

        

Use this method after initialising billing client. You can also refer to official documentation here.

Upvotes: 1

Related Questions