如何获取应用内购买的单独价格和货币信息?

Pan*_*ama 26 android in-app-purchase in-app-billing

我正在为支持多个国家/地区的应用实施应用内购买.

应用内结算版本3 API 声称:

不需要货币转换或格式化:价格以用户的货币报告,并根据其区域设置进行格式化.

实际上是正确的,因为skuGetDetails()它会处理所有必要的格式.响应包括一个名为的字段price,其中包含

物料的格式化价格,包括其货币符号.价格不含税.

但是,我需要ISO 4217货币代码(如果我知道商店区域设置可检索)和实际的非格式化价格(最好是浮点数或十进制变量),所以我可以自己做进一步的处理和分析.

解析回归skuGetDetails()不是一个可靠的想法,因为许多国家共享相同的货币符号.

货币

如何使用应用内结算版本3 API获取应用内购买的ISO 4217货币代码和非格式化价格?

Ste*_*aft 17

完整的信息可以在这里找到,但主要是在2014年8月,这解决了:

getSkuDetails()方法

此方法返回产品ID列表的产品详细信息.在Google Play发送的响应包中,查询结果存储在映射到DETAILS_LIST键的String ArrayList中.详细信息列表中的每个字符串都包含JSON格式的单个产品的产品详细信息.包含产品详细信息的JSON字符串中的字段总结如下

price_currency_code ISO 4217价格的货币代码.例如,如果以英镑英镑指定价格,则price_currency_code为"GBP".

price_amount_micros以微单位表示的价格,其中1,000,000个微单位等于一个单位的货币.例如,如果价格为"€7.99",则price_amount_micros为"7990000".


Nik*_*kov 12

你不能,目前不支持.有一个功能请求,但它可能会或可能不会实现.

https://code.google.com/p/marketbilling/issues/detail?id=93


mar*_*bdq 8

您可以.您需要像下面一样修改SkuDetails.java.

脚步:

  1. 检查他们的示例应用以获取应用内结算.
  2. 修改SkuDetails.java文件,如下所示:
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;
    } 
}
Run Code Online (Sandbox Code Playgroud)

  • 不仅仅是改变代码,我认为解释它背后的推理更为重要.2014年8月左右,计费API的变化在`getSkuDetails()`返回值中引入了一个新字段:`price_currency_code`.这一变化最终使得获得特定交易的货币代码成为可能. (2认同)

Gil*_* SH 5

这是我的解决方法:)

private static Locale findLocale(String price) {
    for (Locale locale : Locale.getAvailableLocales()){
        try {
            Currency currency = Currency.getInstance(locale);
            if (price.endsWith(currency.getSymbol())) 
                return locale;
        }catch (Exception e){

        }

    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

用法:

Locale locale = findLocale(price);
String currency = Currency.getInstance(locale).getCurrencyCode();
double priceClean = NumberFormat.getCurrencyInstance(locale).parse(price).doubleValue();
Run Code Online (Sandbox Code Playgroud)