Listing available products

We need to present the details about what products or subscriptions are available for purchase so that the users can verify what they want before making any purchases.

How to do it...

We can request a list of Product instances using the QueryInventoryAsync() method:

  1. The QueryInventoryAsync() method returns the product details when invoked with the desired product IDs and the Product ItemType instance:
    var productIds = new string[] {
      "managed.one",
      "managed.two"
    };
    var products = await billing.QueryInventoryAsync(
      productIds, ItemType.Product);
    
  2. If we have any subscriptions, we can repeat the same process but we will use the Subscription ItemType type this time:
    var productIds = new string[] {
      "subscription.one"
    };
    var subscriptions = await billing.QueryInventoryAsync(
      productIds, ItemType.Subscription);
  3. Similar to do the process for all the requests, errors may occur, such as when there is no connectivity. We can respond to these errors using the QueryInventoryError event:
    billing.QueryInventoryError += (code, details) => {
      // there was an error querying the inventory
    };
  4. Irrespective of whether it is a managed product or a subscription, each returned product has a set of properties that can be used to present the product to the user:
    string id = product.ProductId; // managed.one
    string title = product.Title;  // Managed Product One
    string description = product.Description; // ...
    string price = product.Price;  // R 10.00

How it works...

Once we are connected to the Google Play service, we can begin our product interaction. The first thing we will probably want to do is to present the user with a list of available items to purchase.

Obtaining a list of products is done through the QueryInventoryAsync() method, which is asynchronous and must be awaited. We provide a list of product IDs for which we want to get the product information. If we are requesting information about managed products, we use the Product item type. For subscriptions, we use the Subscription item type.

Note

The QueryInventoryAsync() method returns the product information for a set of product IDs rather than returning a list of available products.

We need this product list because it will now contain the names, description, and localized prices for the product or subscription. This information can then be presented to the user before making a purchase.

Tip

The price received from the server contains both the currency code as well as the number value, and can thus be presented directly to the user.

Google Play caches the result from the server, so we don't have to implement our own caching mechanism. Each time we need the product details, we can simply get it from the billing handler. We can locally cache when the app launches so that we don't have to keep the connection open, but it is not necessary to persist the cache between app launches.

Tip

Because Google Play caches responses, subsequent requests do not involve network requests.

There's more...

There are also special products that are always available for testing purposes. No actual transaction takes place, but all the appropriate purchase events occur:

var productIds = new string[] {
  ReservedTestProductIDs.Canceled,
  ReservedTestProductIDs.Purchased,
  ReservedTestProductIDs.Unavailable
};
var products = await billing.QueryInventoryAsync(
  productIds, ItemType.Product);

In the case of Canceled, Google Play responds as though the purchase was canceled. This can occur when an error is encountered in the order process, such as an invalid credit card. For Purchased, the transaction always succeeds, representing a successful purchase. Finally, Unavailable acts as if the user has purchased an invalid item.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.191.169