All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.badlogic.gdx.pay.android.googleplay.billing.converter.GetSkusDetailsResponseBundleConverter Maven / Gradle / Ivy

package com.badlogic.gdx.pay.android.googleplay.billing.converter;

import android.os.Bundle;

import com.badlogic.gdx.pay.Information;
import com.badlogic.gdx.pay.android.googleplay.GoogleBillingConstants;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.badlogic.gdx.pay.android.googleplay.GoogleBillingConstants.DETAILS_LIST;
import static com.badlogic.gdx.pay.android.googleplay.GoogleBillingConstants.PRICE_AMOUNT_MICROS;
import static com.badlogic.gdx.pay.android.googleplay.GoogleBillingConstants.PRICE_CURRENCY_CODE;
import static com.badlogic.gdx.pay.android.googleplay.GoogleBillingConstants.PRODUCT_ID;
import static com.badlogic.gdx.pay.android.googleplay.billing.converter.ResponseConverters.assertResponseOk;

public class GetSkusDetailsResponseBundleConverter {

    public static Map convertSkuDetailsResponse(Bundle skuDetailsResponse) {
        assertResponseOk(skuDetailsResponse);

        ArrayList skuDetailsStringList = skuDetailsResponse.getStringArrayList(DETAILS_LIST);

        assertSkuListNotEmpty(skuDetailsStringList);


        try {
            return convertSkuDetailsToInformationMap(skuDetailsStringList);
        } catch(JSONException e) {
            throw new IllegalArgumentException("Failed to parse : " + skuDetailsResponse, e);
        }
    }

    private static void assertSkuListNotEmpty(ArrayList skuDetailsStringList) {
        if (skuDetailsStringList == null || skuDetailsStringList.isEmpty()) {
            throw new IllegalArgumentException("No skus found in response");
        }
    }

    private static Map convertSkuDetailsToInformationMap(List skuDetailsStringList) throws JSONException {
        Map products = new HashMap<>();

        for (String thisResponse : skuDetailsStringList) {
            JSONObject object = new JSONObject(thisResponse);
            String sku = object.getString(PRODUCT_ID);
            String price = object.getString(GoogleBillingConstants.SKU_PRICE);
            String title = object.getString(GoogleBillingConstants.SKU_TITLE);
            String description = object.getString(GoogleBillingConstants.SKU_DESCRIPTION);
            products.put(sku, Information.newBuilder()
                    .localName(title)
                    .localDescription(description)
                    .localPricing(price)
                    .priceInCents(priceInCents(object))
                    .priceCurrencyCode(object.getString(PRICE_CURRENCY_CODE))
                    .build());
        }

        return products;
    }

    private static Integer priceInCents(JSONObject object) throws JSONException {
        if (object.has(PRICE_AMOUNT_MICROS)) {
            return (int) object.getLong(PRICE_AMOUNT_MICROS) / 10_000;
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy