Vasily G
Vasily G

Reputation: 476

Google Play: update prices from IInAppBillingService

I created a class for working with subscriptions in the store. In the beginning I receive from the server the list of subscriptions with the prices The code is working and on most devices there is no problem. But some users do not recive prices. It's like there's no response from the server. I've studied examples of implementation in Google documentation, but I can not understand where I can have problems in the code. Part of my code:

public class BillingActivity extends AppCompatActivity {

RelativeLayout imageLayout;
View payButton;
// WebView payWebView;
// TextView useCodeButton;

ProgressBar progress1;
ProgressBar progress2;
ProgressBar progress3;

IInAppBillingService inAppBillingService;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_billing_subscription);
    price_1monthTextView = (TextView) findViewById(R.id.price_1monthTextView);
    relative_1month = (RelativeLayout) findViewById(R.id.relative_1month);
    relative_1month.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (MainActivity.myAccount == null) return;
            if (MainActivity.myAccount.getUniqid() == null) return;
            if (subscription1m != null) {
                try {
                    Log.d("my", "purchase..." + subscription1m.storeName);
                    purchaseProduct(subscription1m);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("my", "purchase error = " + e.toString());
                }
            }
        }
    });


    progress1 = (ProgressBar) findViewById(R.id.progress1);
    startGoogleBilling();
}


private void startGoogleBilling() {
    if (serviceConnection != null) {
        unbindService(serviceConnection);
    }

    progress1.setVisibility(View.VISIBLE);


    serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            inAppBillingService = IInAppBillingService.Stub.asInterface(service);

            getSubscribtionsList();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            inAppBillingService = null;
        }
    };

    Intent serviceIntent =
            new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}


private static final int PR_CNT = 3;

List<InAppProduct> subscriptions = null;

InAppProduct subscription1m = null;
InAppProduct subscription3m = null;
InAppProduct subscription1y = null;


String[] productIds = {"eq.subscription.1m", "eq.subscription.3m.2", "eq.subscription.1y"};

private void getSubscribtionsList() {
    mSwipeRefreshLayout.setRefreshing(false);
    progress1.setVisibility(View.GONE);
    try {

        subscriptions =
                getInAppPurchases("subs", productIds[0], productIds[1], productIds[2]);

        if (subscriptions.size() == PR_CNT) {
            for (InAppProduct inAppProduct : subscriptions) {
                String productId = inAppProduct.productId;
                Log.d("my", "productId= " + productId);
                if (productId.contains(productIds[0])) subscription1m = inAppProduct;
                if (productId.contains(productIds[1])) subscription3m = inAppProduct;
                if (productId.contains(productIds[2])) subscription1y = inAppProduct;
            }
            Log.d("my", "1m= " + subscription1m.storeName + " pr=" + subscription1m.price + "\\n\\r  " +
                    "3m= " + subscription3m.storeName + " pr=" + subscription3m.price + "\\n\\r   " +
                    "1y= " + subscription1y.storeName + " pr=" + subscription1y.price + "\\n\\r   ");
            ///----------------------!!!!
            // purchaseProduct(inAppProduct);
        }

        updatePrices();

    } catch (Exception e) {
        Log.d("my", "exc  = " + e.toString());
        if (e.toString().contains("Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getSkuDetails")) {
            if (attempt < 4) {
                //getSubscribtionsList();

                // startGoogleBilling();
            } else {
            }

        }
        //  Toast.makeText(this, "Google inApp connection error", Toast.LENGTH_SHORT).show();
        //  refreshButton.setVisibility(View.VISIBLE);
        startGoogleBilling();

    }
}

private int attempt = 0;

@Override
public void onDestroy() {
    super.onDestroy();
    if (serviceConnection != null) {
        unbindService(serviceConnection);
    }
}

class InAppProduct {

    public String productId;
    public String storeName;
    public String storeDescription;
    public String price;
    public boolean isSubscription;
    public int priceAmountMicros;
    public String currencyIsoCode;

    public String getSku() {
        return productId;
    }

    String getType() {
        return isSubscription ? "subs" : "inapp";
    }
}

List<InAppProduct> getInAppPurchases(String type, String... productIds) throws Exception {
    ArrayList<String> skuList = new ArrayList<>(Arrays.asList(productIds));
    Bundle query = new Bundle();
    query.putStringArrayList("ITEM_ID_LIST", skuList);
    Bundle skuDetails = inAppBillingService.getSkuDetails(
            3, getPackageName(), type, query);
    ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
    List<InAppProduct> result = new ArrayList<>();
    for (String responseItem : responseList) {
        JSONObject jsonObject = new JSONObject(responseItem);
        InAppProduct product = new InAppProduct();
        // "com.example.myapp_testing_inapp1"
        product.productId = jsonObject.getString("productId");
        // Покупка
        product.storeName = jsonObject.getString("title");
        // Детали покупки
        product.storeDescription = jsonObject.getString("description");
        // "0.99USD"
        product.price = jsonObject.getString("price");
        // "true/false"
        product.isSubscription = jsonObject.getString("type").equals("subs");
        // "990000" = цена x 1000000
        product.priceAmountMicros =
                Integer.parseInt(jsonObject.getString("price_amount_micros"));
        // USD
        product.currencyIsoCode = jsonObject.getString("price_currency_code");
        result.add(product);
    }
    return result;
}

private void updatePrices() {
    if (subscriptions.size() == PR_CNT) {

        price_1monthTextView.setText(subscription1m.price);
        price_3monthTextView.setText(subscription3m.price);
        price_1yearTextView.setText(subscription1y.price);
    }

}


}

Upvotes: 0

Views: 157

Answers (1)

Vasily G
Vasily G

Reputation: 476

my problem was the bit depth of the X variable priceAmountMicros

The price from playmarket is multiplied by a 1000000 , and Kazakh tenge has exchange rate = 1 USD= 331,3 KZT. And if i have price 18 000 KTZ and * 1000 000. For price variable I must use long type

Upvotes: 1

Related Questions