PsychoChris
PsychoChris

Reputation: 241

Android In-App billing item price

How can the price of an in-app billing item be retrieved before actually displaying the android market frontend for the in-app purchase? Currently it looks like the user only can find out the price for an in-app item in the purchase dialog and i would like to avoid to store the prices in the application for all supported currencies.

Upvotes: 24

Views: 15625

Answers (4)

Anas Iqbal
Anas Iqbal

Reputation: 206

If you are not following the google trivia example then you can get price by using this code.

private void getItemsPrice(){
    List<String> skuList = new ArrayList<>();
    skuList.add("example1");
    skuList.add("example2");
    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<SkuDetails> skuDetailsList) {
                    if (responseCode == BillingClient.BillingResponse.OK
                            && skuDetailsList != null) {
                        for (SkuDetails skuDetails : skuDetailsList) {
                            String sku = skuDetails.getSku();
                            String price = skuDetails.getPrice();
                            Log.d("item price",price);
                            if ("example1".equals(sku)) {
                                    tvPlan1.setText(price);
                            }
                            else if ("example2".equals(sku)){
                                    tvPlan2.setText(price);

                            }
                        }
                    }

                }
            });
}

Upvotes: 0

C&#237;cero Moura
C&#237;cero Moura

Reputation: 2333

String price = inventory.getSkuDetails("sku_of_your_product").getPrice();

Upvotes: 3

Will Kamp
Will Kamp

Reputation: 171

If you are using the Trivial Drive example and included IabHelper class you need to pass a list of skus to queryInventoryAsync.

    String[] moreSkus = {"SKU_ITEMONE", "SKU_ITEMTWO"};
    mHelper.queryInventoryAsync(true, Arrays.asList(moreSkus), mGotInventoryListener);

Upvotes: 17

Sergey Glotov
Sergey Glotov

Reputation: 20346

It is possible now with Billing API v3. You can get information with getSkuDetails() method. Example is here.

ArrayList skuList = new ArrayList();
skuList.add("premiumUpgrade"); 
skuList.add("gas");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);

Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), “inapp”, querySkus);

int response = skuDetails.getInt("RESPONSE_CODE");
if (response == 0) {
    ArrayList responseList = skuDetails.getStringArrayList("DETAILS_LIST");

    for (String thisResponse : responseList) {
        JSONObject object = new JSONObject(thisResponse);
        String sku = object.getString("productId");
        String price = object.getString("price");
        if (sku.equals(“premiumUpgrade”)) {
            mPremiumUpgradePrice = price;
        } else if (sku.equals(“gas”)) { 
            mGasPrice = price;
        }
    }
}

Upvotes: 17

Related Questions