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.
We can request a list of Product
instances using the QueryInventoryAsync()
method:
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);
Subscription ItemType
type this time:var productIds = new string[] {
"subscription.one"
};
var subscriptions = await billing.QueryInventoryAsync(
productIds, ItemType.Subscription);
QueryInventoryError
event:billing.QueryInventoryError += (code, details) => { // there was an error querying the inventory };
string id = product.ProductId; // managed.one string title = product.Title; // Managed Product One string description = product.Description; // ... string price = product.Price; // R 10.00
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.
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.
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.
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.