Listing purchased products

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.

How to do it...

We can access the list of purchased products through the GetPurchases() method:

  1. We list purchased products by passing the Product item type to the GetPurchases() method:
    var prods = billing.GetPurchases(ItemType.Product);
  2. To list purchased subscriptions, we pass the Subscription item type instead:
    var subs = billing.GetPurchases(ItemType.Subscription);
  3. The 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;
  4. If there are any errors processing the request, we can handle them in the error events:
    billing.OnGetProductsError += (code, bundle) => {
      // error loading the purchased items
    };
    billing.OnInvalidOwnedItemsBundleReturned += (bundle) => {
      // error validating the purchased items
    };

How it works...

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.

Tip

As Google Play caches the list of purchases on the device, multiple queries should be made to the handler instead of implementing a custom cache.

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.

Tip

The Purchase instance only contains the product ID, so the QueryInventoryAsync() method can be used to fetch the Product instance.

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.

Note

The OnGetProductsError and OnInvalidOwnedItemsBundleReturned events correspond to Google Play service errors, while InAppBillingProcessingError corresponds to library errors.

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

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