Consuming purchases

Some purchases are used to indicate additional features that are not initially available to the user. Other products are consumable products and only provide a once-off feature, which will have to be re-purchased to be re-used. These products are consumable products and only provide a once-off feature.

How to do it...

Some products are meant to be consumed, or exhausted, when used; the user is required to purchase them again before they can be reused:

  1. To consume a product, we pass the purchase to the ConsumePurchase() method:
    var success = billing.ConsumePurchase(purchase);
  2. We don't really require the Purchase instance but rather just the purchase token. When a product is purchased, we can just store the purchase token and use that:
    var token = purchase.PurchaseToken;
    var success = billing.ConsumePurchase(token);
  3. If the consumption is successful, we can use the return value, or make use of the OnPurchaseConsumed event:
    billing.OnPurchaseConsumed += (token) => {
      // consumed product
    };
  4. Similarly, if there are any errors, we can use the return value, or the OnPurchaseConsumedError event:
    billing.OnPurchaseConsumedError += (code, token) => {
      // error consuming the purchase
    };

How it works...

Besides subscriptions, there are two main types of products: consumables and nonconsumables. Consumable products are meant to be purchased and then used once. To make use of the product again, they have to be repurchased. Nonconsumable products are only bought once and can be reused without having to be repurchased. If we want the user to repurchase the product, we have to first consume it.

Note

Products can be consumed, subscriptions cannot be consumed.

A nonconsumable product is a product that is bought once and never expires. An example of this type of product would be a feature, such as the removal of ads. Although it is possible to consume this product, we wouldn't.

Product consumption can be done after a period from the time of being bought or directly after the purchase. An example of a consumable purchase would be in-game money, where we would want the user to be able to buy the same amount multiple times.

Tip

Consumable products should be consumed when the app is launched so that they can be purchased again.

In the case of money, we would actually consume the purchase immediately but track the total amount separately. Then, as the player uses the money, we would subtract from the local wallet. To protect against data loss, we would also backup or sync the wallet to an online storage. This will also allow the user to resume the game from a new device.

To consume a purchase, we can either pass the Purchase instance or the purchase token to the ConsumePurchase() method. This method will return true if the purchase is successful, or false if there is an error.

Tip

It is a good practice to only perform the specified operation when a successful result is obtained from a consumption request.

In addition to the return value, the OnPurchaseConsumed event will be raised when the consumption is successful. If there is an error, the OnPurchaseConsumedError will be raised.

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

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