Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse price from Google Play In-app Billing

I use the follow code to parse price from Google Play In-app Billing:

private static Number parsePrice(String priceFromGoogle) {
    Locale currencyLocale = getCurrencyLocale(priceFromGoogle);
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(currencyLocale);
    Number number = null;
    try {
        number = numberFormat.parse(priceFromGoogle);
    } catch (ParseException e) {
        e.printStackTrace(); 
    }
    return number;
} 

private Locale getCurrencyLocale(String price) {
    Locale locale = null;
    for (Locale availableLocale : Locale.getAvailableLocales()) {
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(availableLocale);
        try {
            numberFormat.parse(price);
            locale = availableLocale;
            break;
        } catch (ParseException e) {
            //do nothing
        }
    }
    return locale;
}

It works fine on my test devices and in my locale. But on some devices and in some countries I encounter prices like this: "Php1,337.07", "US$ 29.99", "MX$374.79". My approach doesn't work in this case.

Is there an universal approach to solve this problem?

like image 823
Sergei Vasilenko Avatar asked Aug 31 '13 10:08

Sergei Vasilenko


People also ask

What is Google Play in-app billing?

Google Play's billing system is a service that enables you to sell digital products and content in your Android app. You can use Google Play's billing system to sell a one-time product or subscriptions on a recurring basis.


1 Answers

Check their In-app billing sample project and modify SkuDetails.java so that you can get that information as well:

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Represents an in-app product's listing details.
 */
public class SkuDetails {
    String mItemType;
    String mSku;
    String mType;
    int mPriceAmountMicros;
    String mPriceCurrencyCode;
    String mPrice;
    String mTitle;
    String mDescription;
    String mJson;

    public SkuDetails(String jsonSkuDetails) throws JSONException {
        this(IabHelper.ITEM_TYPE_INAPP, jsonSkuDetails);
    }

    public SkuDetails(String itemType, String jsonSkuDetails) throws JSONException {
        mItemType = itemType;
        mJson = jsonSkuDetails;
        JSONObject o = new JSONObject(mJson);
        mSku = o.optString("productId");
        mType = o.optString("type");
        mPrice = o.optString("price");
        mPriceAmountMicros = o.optInt("price_amount_micros");
        mPriceCurrencyCode = o.optString("price_currency_code");
        mTitle = o.optString("title");
        mDescription = o.optString("description");
    }

    public String getSku() { return mSku; }
    public String getType() { return mType; }
    public String getPrice() { return mPrice; }
    public String getTitle() { return mTitle; }
    public String getDescription() { return mDescription; }
    public int getPriceAmountMicros() { return mPriceAmountMicros; }
    public String getPriceCurrencyCode() { return mPriceCurrencyCode; }

    @Override
    public String toString() {
        return "SkuDetails:" + mJson;
    }
}
like image 94
marbdq Avatar answered Nov 09 '22 05:11

marbdq