Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response Code Item already purchased but purchase list is null

I am implementing IAP in my app. One is for removing ad and other is for adding more puzzle. I was testing that on my device but came across an issue. After buying an item, I am getting response code "Item already owned" but it is not showing in purchase list.

I am setting up my billing client like this,

private void setUpBillingClient(){
        mBillingClient = BillingClient.newBuilder(this).setListener(this).build();
        mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
                if (billingResponseCode == BillingClient.BillingResponse.OK) {
                    List skuList = new ArrayList<>();
                    skuList.add(ITEM_SKU_MORE_PUZZLE);
                    skuList.add(ITEM_SKU_REMOVE_AD);
                    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
                    mBillingClient.querySkuDetailsAsync(params.build(),
                            new SkuDetailsResponseListener() {
                                @Override
                                public void onSkuDetailsResponse(int responseCode, List skuDetailsList) {
                                    // Process the result.
                                    if (responseCode == BillingClient.BillingResponse.OK
                                            && skuDetailsList != null) {
                                        for (Object skuDetailsObject : skuDetailsList) {
                                            SkuDetails skuDetails = (SkuDetails) skuDetailsObject;
                                            String sku = skuDetails.getSku();
                                            String price = skuDetails.getPrice();
                                            if (ITEM_SKU_MORE_PUZZLE.equals(sku)) {
                                                btnMorePuzzle.setText(price);
                                            }
                                            else if(ITEM_SKU_REMOVE_AD.equals(sku)) {
                                                btnRemoveAd.setText(price);
                                            }
                                        }
                                    }
                                }
                            });
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                //Toast.makeText(getApplicationContext(),  getResources().getString(R.string.billing_connection_failure), Toast.LENGTH_SHORT);
            }
        });

        queryPurchases();
        queryPrefPurchases();
    }

First question, why is Billing response is OK here when I have already purchased the item. I don't want to set text of button as price, which is getting set from this response after product is bought.

This is my Onpurchase implementation,

@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
        for (Purchase purchase : purchases) {
            if (purchase.getSku().equals(ITEM_SKU_REMOVE_AD)) {
                mSharedPreferences.edit().putBoolean("ad_free", true).commit();
                btnRemoveAd.setText("Done");
                btnRemoveAd.setEnabled(false);
            }
            else if(purchase.getSku().equals(ITEM_SKU_MORE_PUZZLE)){
                mSharedPreferences.edit().putBoolean("more_puzzle", true).commit();
                btnMorePuzzle.setText("Done");
                btnMorePuzzle.setEnabled(false);
            }
        }
    } else if (responseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED ) {
        // I am getting response "Item already owned" here for item bought but purchase list here is empty 
        // so i can't do anything for  purchased item
    }
}

Second question, here I am getting response that my item is already bought but still list is empty. How to implement it properly?

If someone already bought a product then button should be disabled. Another doubt is while testing do I have to wait for 1-2 hrs to get that item refunded from playstore to test again or is there any other method.

I am following this code for in-app implementation. https://github.com/patpatchpatrick/Streakr/

like image 833
Surabhi Choudhary Avatar asked Jul 31 '19 13:07

Surabhi Choudhary


1 Answers

There is only one thing to point out here, that you are querying only the SKU type INAPP items only not the SUBS. I think the type of product you are providing comes under subscriptions not under in-app products which are used to be consumed. That is why your query is empty.

like image 68
Vanshaj Daga Avatar answered Nov 15 '22 04:11

Vanshaj Daga