Part 6 - Getting Purchased Items

By Richard Davey on 12th March 2019   @photonstorm

It's possible to get a list of items the player has purchased by using the getPurchases method:

this.facebook.on('getpurchases', this.showPurchases, this);

this.facebook.getPurchases();

You can only call this once your game has started, and if the paymentsPurchaseAsync service is available. If the call succeeds, the getpurchases event handler is invoked and is sent an array of Product objects. In the following screen shot you can see that this player has purchased 3 items from the game:

Purchases

Once getPurchases has been called, the list of purchases is available at any point via this.facebook.purchases, meaning you don't have to store a local copy of it yourself. It's always accessible in the Facebook Instant Games plugin.

We can use the productID to work out which items they have bought. The Facebook Instant Games plugin has a method called getProduct, which takes a Product ID as its only parameter, and returns a fully-formed Product object:

this.facebook.on('getpurchases', (purchases) => {

    for (const item of purchases)
    {
        let product = this.facebook.getProduct(item.productID);

        console.log(product.title);
    }

}, this);

this.facebook.getPurchases();

Note: This method was added in Phaser 3.17. If using a previous version, please build a new copy of the Facebook Plugin from the source.

By looking at the list of purchases we can see the player has bought a Demon Horn, the small gold pack and also paid to unlock all of the Advanced Spells. Because they bought the Advanced Spells item our game should now make sure it always presents them with anything attributed to that purchase.

It's a good idea to store permanent item purchases, such as these, in the players data. For something like gold you could store is as a game stat, because it's an integer value. See this tutorial for more details.

However, the Demon Horn is a single-use item. The player now has access to it and can use it at any point during play. But once they've used it, it's gone and shouldn't be allowed to be used again. These are known as consumable items and are covered in the next section.