Part 5 - Purchasing an Item

By Richard Davey on 12th March 2019   @photonstorm

You've done all the hard work in convincing your players to buy an item for your game. They've finally bought up the catalog and selected something they'd like, so they hit that 'Buy' button in-game.

The API call to actually buy the item is surprisingly simple, and like all things with Facebook Instant Games, it relies on a single call to the purchase method, which will invoke one of two possible event-based outcomes:

this.facebook.on('purchase', this.purchaseSuccess, this);

this.facebook.on('purchasefail', this.purchaseError, this);


Where productID is the ID you configured in your App Dashboard and which was returned in the Catalog, and purchaseSuccess and purchaseError are two methods that will handle the response.

You can pass in an optional developerPayload after the product ID. This is a string sent with the purchase request that is returned to the purchase success handler.

If playing on desktop the user will be presented with a pop-up that appears above your instant game, which will still be running in the background. It looks something like this:


Obviously, this will vary based on different factors such as locale, and if the user has bought something from Facebook before and has a card stored, but you can clearly see a couple of important elements:

1) Your product image is displayed along with the product title and the name of the game. In our case you can see it says "Stack of Coins" with the little coins image next to it. "Phaser 3 Test App 1" is our super-exciting test app name, hopefully yours will be something better :) What's important, though, is that the product title matches exactly what your game said they were going to buy. If there is any element of confusion in the buyers mind they are highly likely to cancel the purchase.

2) There are two possible outcomes from this screen. The first is to cancel the purchase, by clicking the cross in the top-right. The second is to click "Buy". Each fires a different event that your game needs to listen for.

Canceling an order

If, for whatever reason, the user decides they don't want your item any more, and closes down the purchase pop-up, your purchasefail event handler will be called. It will be sent an error object where the code is USER_INPUT and the message is "User canceled the order.". You should respect this decision and handle it gracefully, perhaps tracking their choice in-game, so you can offer related perks later on.

You need to be careful with your messaging relating to this event. It doesn't always mean the user canceled the order. It means they clicked to close the pop-up, but they may have done that for any number of reasons. For example, if they tried adding a new payment card but it was declined for some reason, this pop-up window will show them an error and leave them no choice but to close it down.

What this means is that while in most cases the USER_INPUT event signifies they actively decided they didn't want to buy the item any longer, sometimes it's possible for a user to be forced to make that decision, i.e. they may still want the item, they just can't buy it at this time.

An order may also be canceled for other reasons. Perhaps there is a network error when submitting it, or a card has expired or been blocked. In all cases you'll get the fail event and need to handle it. All things being well, however, the purchase will go through.

Purchase Success

All being well, the payment will go through and you'll have a new customer. In this case the purchase event handler will be invoked and sent details of the transaction in the form of a single Product object. This object has the following properties:


Property Type
developerPayload A developer-specified string, provided during the purchase of the product.
paymentID The identifier for the purchase transaction.
productID The product's game-specified identifier.
purchaseTime Unix timestamp of when the purchase occurred.
purchaseToken A token representing the purchase that may be used to consume the purchase.
signedRequest A Server-signed encoding of the purchase request.


The player should be rewarded immediately with their purchase.

You can obtain a list of all items the player has purchased when your game starts, which we'll cover in the next section.