After the user has purchased products or subscriptions, we need to be able to adjust the app accordingly when the app is launched the next time.
We can access the list of purchased products through the GetPurchases()
method:
Product
item type to the GetPurchases()
method:var prods = billing.GetPurchases(ItemType.Product);
Subscription
item type instead:var subs = billing.GetPurchases(ItemType.Subscription);
GetPurchases()
method returns a collection of Purchase
instances, each of which contain data that describe the purchase:Purchase purchase = prods[0]; string id = purchase.ProductId; string token = purchase.PurchaseToken; string payload = purchase.DeveloperPayload;
billing.OnGetProductsError += (code, bundle) => { // error loading the purchased items }; billing.OnInvalidOwnedItemsBundleReturned += (bundle) => { // error validating the purchased items };
Once the user has purchased products or subscriptions, they will want to be able to view those purchases. When the app begins, we might want to display the additional features on the basis of the purchase. We can do this by requesting the list of purchased products using the GetPurchases()
method.
The GetPurchases()
method returns a list of Purchase
instances, which we can use in our app. When we want to access the list of products, we pass the Product
item type as a parameter, and for subscriptions, we use the Subscription
item type.
The Purchase
instances will contain details such as the product ID and purchase token. These values are stored in the ProductId
and PurchaseToken
properties, respectively. If we want to access other details, such as the title and price, we can invoke the QueryInventoryAsync()
method by passing the product ID.
In addition to the product ID and purchase token, there is the DeveloperPayload
property. This payload is the same data that was provided when the purchase was originally made. This data can be used to verify a purchase or perform some other form of validation.
In the event of any error, there are various events that can be raised. The OnGetProductsError
event is raised if a valid error code from the Google Play service is received. If there is a problem validating the result data in the case of a successful response, the OnInvalidOwnedItemsBundleReturned
event is raised. If there are any errors deserializing the result, outside the instance of an invalid service response, the InAppBillingProcessingError
event will be raised.