How to get separate price and currency information for an in-app purchase?

后端 未结 8 1509
攒了一身酷
攒了一身酷 2020-12-24 07:02

I am implementing in-app purchases for an app with support for several countries.

The In-App Billing Version 3 API claims that:

No currency co

8条回答
  •  情歌与酒
    2020-12-24 07:36

    Here's a complete code:

    ArrayList skuList = new ArrayList<>();
    skuList.add("foo");
    skuList.add("bar");
    
    final String[] prices = new String[skuList.size()];
    
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
    
    Bundle skuDetails = _appActivity.mService.getSkuDetails(3, _appActivity.getPackageName(), "inapp", querySkus);
    
    int response = skuDetails.getInt("RESPONSE_CODE");
    if(response == 0) {
        ArrayList responseList = skuDetails.getStringArrayList("DETAILS_LIST");
    
        int i = 0;
        for (String thisResponse : responseList) {
            JSONObject object = new JSONObject(thisResponse);
            String sku = object.getString("productId");
            final String priceCurrency = object.getString("price_currency_code");
            String priceAmountMicros = object.getString("price_amount_micros");
            float priceAmount = Float.parseFloat(priceAmountMicros) / 1000000.0f;
            String price = priceAmount + " " + priceCurrency;
    
            if(skuList.contains(sku)){
                prices[i] = price;
                i++;
            }
        }
    }
    

    From documentation:

    price_currency_code ISO 4217 currency code for price. For example, if price is specified in British pounds sterling, price_currency_code is "GBP".

    price_amount_micros Price in micro-units, where 1,000,000 micro-units equal one unit of the currency. For example, if price is "€7.99", price_amount_micros is "7990000". This value represents the localized, rounded price for a particular currency.

    Now you just have to divide price_amount_micros by a 1000000 and concatenate it with currency.

提交回复
热议问题