Search code examples
androidin-app-subscription

How to get subscription price from in app subscription


I have created monthly subscription in google play console account.

Now I want to get subscription price according to my device region programmatically.

For example

If my device's region is India then I want subscription price in Indian currency

If my device's region is Sweden then I want subscription price in Sweden currency

How can I do that??

Please help me with this.


Solution

  • I have facing this same issue and after some search and with some other persons help I have done it.

    Google provide region wise price setting for In-app purchase. Use pricing template. Just follow below steps to set region wise price using pricing template.

    How to create template:

    1. Open google play console and select Settings from left side. After select Pricing templates.
    2. At the top click on NEW PRICING TEMPLATE button.
    3. Add your Name, Price and select Tax second options.
    4. Now Add your different prices as per your region wise in the Local Prices option.
    5. After click CREATE TEMPLATE button at the bottom.

    Example of pricing template

    How to link this template with your Subscription SKU Id:

    1. Open your play console. Select your app->Store presence->In-app purchase.
    2. Select your subscription item which is already created or create your new.
    3. Fill every required related info.
    4. Now in the Price section you can see the option import from pricing template with drop down arrow. Click on it you can see your create template. Select your template and your price will be automatically link with your template.

    Without template selection: enter image description here

    After template selected: enter image description here

    1. After complete other steps and save your subscription item.

    Now whenever user make request for active subscription price will be shows and used as per your template wise which you have already set as per your region wise. I hope some one will get help.

    After follow above step please follow below code

    private int REQ_FOR_QUERY_INVENTORY = 0;
        public static final int REQUEST_FOR_SKU_DETAIL = 1;
        public static final int REQUEST_FOR_CHECK_ACTIVE_SKU = 2;
    
    private void updateSubscriptionPrice() {
            try {
                Log.i(TAG, "request for check Query inventory is active or not");
                REQ_FOR_QUERY_INVENTORY = REQUEST_FOR_SKU_DETAIL;
    //            List<String> itemSku = new ArrayList<>();
                List<String> subSku = new ArrayList<>();
                subSku.add("subscription_id");
                subSku.add("subscription_id");
                mHelper.queryInventoryAsync(true, subSku, mQotInventoryListener);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    private IabHelper.QueryInventoryFinishedListener mQotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inv) {
            try {
                Log.d(TAG, "mQotInventoryListener Query inventory finished.");
                handleQueryInventoryFinishResult(result, inv, REQ_FOR_QUERY_INVENTORY);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    private void handleQueryInventoryFinishResult(IabResult result, Inventory inventory, int requestForQueryInventory) {
            try {
    
                // Have we been disposed of in the meantime? If so, quit.
                if (mHelper == null) return;
    
                // Is it a failure?
                if (result.isFailure()) {
                    Log.e(TAG, "mQotInventoryListener Failed to query inventory: " + result);
    //                complain("Error purchasing: " + result);
    //                billingCallBackListener.onError();
                    return;
                }
    
                Log.d(TAG, "mQotInventoryListener Query inventory was successful.");
    
                switch (requestForQueryInventory) {
                    case REQUEST_FOR_SKU_DETAIL:
                        try {
                            SkuDetails monthlySKU = inventory.getSkuDetails("subscription_id");
                            if (monthlySKU != null) {
                                String price = monthlySKU.getPrice();
                                Log.e(TAG, "SkuDetails are below......");
                                Log.i(TAG, "monthlySKU.getSku::->" + monthlySKU.getSku());
                                Log.i(TAG, "monthlySKU.getType::->" + monthlySKU.getType());
                                Log.i(TAG, "monthlySKU.getPrice: " + monthlySKU.getPrice());
                                Log.i(TAG, "monthlySKU.getPriceAmountMicros::->" + monthlySKU.getPrice());
    //                            Log.i(TAG, "monthlySKU.getPriceCurrencyCode::->"+monthlySKU.get);
                                Log.i(TAG, "monthlySKU.getTitle::->" + monthlySKU.getTitle());
                                Log.i(TAG, "monthlySKU.getDescription::->" + monthlySKU.getDescription());
                                Log.i(TAG, "monthlySKU.getDescription::->" + monthlySKU.getDescription());
    //                            String currencyCode = monthlySKU.getPriceCurrencyCode();
                                textview.setText(monthlySKU.getPrice().concat(" ").concat(getResources().getString(R.string.monthly_eur_1)));
    
                            } else {
                                Log.e(TAG, "monthlySKU details is null");
                            }
    
                            SkuDetails yearlySKU = inventory.getSkuDetails("subscription_id");
                            if (yearlySKU != null) {
                                String price = yearlySKU.getPrice();
                                Log.e(TAG, "yearly price : " + price);
                                textview.setText(price.concat(" ").concat(getResources().getString(R.string.yearly_eur_10)));
                            } else {
                                Log.e(TAG, "yearlySKU details is null");
                            }
    
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }