In App Billing getPrice() Android
Asked Answered
M

4

12

I have successfully implemented in app billing into my app which all works fine. I am now trying to retrieve the price of items (set in developer console) so that I can reflect these prices within my app without hard-coding values.

This code quite obviously only gathers prices of the items already purchased through the Inventory which is not what I'm looking for:

SkuDetails gasDetails = inventory.getSkuDetails(SKU_FULL);      

            if (gasDetails != null){
                alert("Gas is " + gasDetails.getPrice());}

I have looked a the docs querying items available for purchase but struggling to understand it. I would of thought that the Helper class would have implemented some sort of get prices method.

So, my question: Can anyone point me in the right direction?

Merylmes answered 11/5, 2013 at 23:39 Comment(0)
M
5

Ok, I have found the solution. I have deciphered the developer docs and it appears there were errors in it.

This is my solution created within IabHelper:

public String getPricesDev(String packageName) throws RemoteException, JSONException{


        ArrayList<String> skuList = new ArrayList<String>();
        skuList.add("full.discount.fetch");
        skuList.add("gas");
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList("ITEM_ID_LIST", skuList);

    Bundle skuDetails = mService.getSkuDetails(3,packageName, "inapp", querySkus);


    int response = skuDetails.getInt("RESPONSE_CODE");
    if (response == 0) {
       ArrayList<String> 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.contains("full.discount.fetch")) return price;

       }
    } 
    return "Not found";


}
Merylmes answered 11/5, 2013 at 23:54 Comment(0)
S
9

If you are using the implementation proposed in the "TrivialDrive" sample by Google, you can retrieve the info of all skus (even if they are not purchased) by passing true to the paramaters "details" and "moreSkus" in the method that queries the inventory

/**
 * Queries the inventory. This will query all owned items from the server, as well as
 * information on additional skus, if specified. This method may block or take long to execute.
 * Do not call from a UI thread. For that, use the non-blocking version {@link #refreshInventoryAsync}.
 *
 * @param querySkuDetails if true, SKU details (price, description, etc) will be queried as well
 *     as purchase information.
 * @param moreItemSkus additional PRODUCT skus to query information on, regardless of ownership.
 *     Ignored if null or if querySkuDetails is false.
 * @param moreSubsSkus additional SUBSCRIPTIONS skus to query information on, regardless of ownership.
 *     Ignored if null or if querySkuDetails is false.
 * @throws IabException if a problem occurs while refreshing the inventory.
 */
public Inventory queryInventory(boolean querySkuDetails, List<String> moreItemSkus,
                                    List<String> moreSubsSkus) throws IabException {
Stereotype answered 12/12, 2013 at 12:55 Comment(0)
M
5

Ok, I have found the solution. I have deciphered the developer docs and it appears there were errors in it.

This is my solution created within IabHelper:

public String getPricesDev(String packageName) throws RemoteException, JSONException{


        ArrayList<String> skuList = new ArrayList<String>();
        skuList.add("full.discount.fetch");
        skuList.add("gas");
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList("ITEM_ID_LIST", skuList);

    Bundle skuDetails = mService.getSkuDetails(3,packageName, "inapp", querySkus);


    int response = skuDetails.getInt("RESPONSE_CODE");
    if (response == 0) {
       ArrayList<String> 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.contains("full.discount.fetch")) return price;

       }
    } 
    return "Not found";


}
Merylmes answered 11/5, 2013 at 23:54 Comment(0)
T
2

Using the billing api

implementation 'com.android.billingclient:billing:1.1' 

Use this to fetch SKU details

public void getPrices(){


        List<String> skuList = new ArrayList<> ();
        skuList.add("id_one"); //These are the product ids in your google console
        skuList.add("id_two");
        skuList.add("id_three");
        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) {

                        for (SkuDetails details:
                             skuDetailsList) {

                           String item = details.getSku();
                           String price = details.getPrice();
                           String description = details.getDescription();
                           String currencyCode = details.getPriceCurrencyCode();
                           String title = details.getTitle();

                            Toast.makeText(InAppBillingActivity.this, "Finished", Toast.LENGTH_SHORT).show();

                           Log.d("hererereeer- item     ", item);
                           Log.d("hererereeer- price     ", price);
                           Log.d("hererereeer- descr     ", description);
                           Log.d("hererereeer- code     ", currencyCode);
                           Log.d("hererereeer- title     ", title);

                        }

                    }
                });
    }
Transponder answered 30/1, 2020 at 4:37 Comment(0)
Z
0
private fun getPrices() {
    billingClient.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                val queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
                    .setProductList(
                        ImmutableList.of(
                            QueryProductDetailsParams.Product.newBuilder()
                                .setProductId("product_id")
                                .setProductType(BillingClient.ProductType.INAPP)
                                .build()
                        )
                    )
                    .build()
                billingClient.queryProductDetailsAsync(
                    queryProductDetailsParams
                ) { res, list ->
                    val priceAmountMicros: Long =
                        list[0].subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(
                            0
                        )?.priceAmountMicros!!

                    Log.d(
                        TAG,
                        (priceAmountMicros.toDouble() / 1000000).toString()
                    )
                    Log.d(
                        TAG,
                        list[0].subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(
                            0
                        )?.priceCurrencyCode.toString()
                    )
                    Log.d(
                        TAG,
                        list[0].subscriptionOfferDetails?.get(0)?.pricingPhases?.pricingPhaseList?.get(
                            0
                        )?.formattedPrice.toString()
                    )
                }
            }
        }
Zabrine answered 25/5, 2022 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.