Prerequisite: iOS: Setting Up Purchasable Products in iTunes Connect
There is now an In-App Purchase action that comes with the PRO version of GameBuilder Studio. This one action has all of the necessary functionality for you to load product details, complete a product purchase, check previous purchases, and restore all previous purchases. The typical workflow when a player plays your game would involve a buy button in your product store screen maybe to unlock ads. When the player clicks that buy button iOS will prompt the player to log in with their apple id. Once the purchase is successful you will then remove all visible ads or grant whatever item/product the player just purchased.
**Note: DO NOT automatically trigger the restore purchases action when your app starts. You will get rejected by the app store.
When purchasing a product trigger the In-App Purchase with the action type "Purchase Product" selected. Make sure to select which type of product is being purchased. Either consumable or non-consumable. This is important because the action automatically stores (caches) successfully purchased non-consumable products on the players device on completion. This allows the "Check Purchase" functionality to verify purchases quicker the next time the player opens your game. This also saves you from having to store this information manually which you could do using a Local Storage action. Consumable products are not stored. So if you provide consumable products that are used over time you will need to keep track of the usage and store that information on your own.
This action works on both iOS and Android so it is a good idea to use the same product id on both devices so that you can use the same action on both devices.
Loading Product Info
Product details are already known ahead of time so you don't really need to use the Get Product Info functionality of the In-App Purchase action but this is available to you. When product information is loaded it returns a Product object and stores that object on a List Data component if one is provided. You can then loop through the list of entries in that list to display products if desired. The product object has the properties below.
- identifier - this is the unique product id
- name - the full name of the product for display purposes
- price - the number value cost of the product
- description - the long explanation of what the product is
If this object is loaded to a list you can access the value like this: Entity.ListData.item0.identifier
Handling Purchase Restoration
**Note: You MUST include a restore purchases button somewhere in your game to allow the player to reload any previous purchase if for example they get a new phone or delete your app and re-install it. If you don't provide this functionality Apple will reject your game.
Usually on the main screen or no a settings screen in your game there should be a restore purchases button. Trigger the In-App Purchase action with the Restore Purchases type selected. When the purchases are restored the On Load action will be triggered. You could decide to trigger logic at this point to enable any feature or product that was previously purchased by triggering the Check Purchase action type of the In-App Purchase action and then unlocking items if that action is successful.
Testing In-App Purchases On Your Device
In completing the steps in iOS: Setting Up Purchasable Products in iTunes Connect, you created a test user in iTunes Connect for testing the purchase flow without actually spending money. Follow these steps to use the test user account to test your implementation:
- Make sure that the Debug Mode checkbox is selected when testing on a test device. This verifies the purchase agains the Apple test sandbox server. Disable this checkbox when ready to submit to the app store.
- Make sure your game bundle id is set up with the App ID you created for your app.
- Make sure that you built your game with the Ad Hoc Distribution option selected in the configuration section of iOS settings of the publish window inside GameBuilder Studio.
- Make sure that you built your game with the development provisioning profile (.mobileprovision) file you created.
- Before starting your app on the device, go to Settings > Store, select your Apple ID, and tap Sign Out.
- Start your test game. When you try to make a purchase, it will prompt you to sign in, and the dialog box should be labeled with "Sandbox". Use the test user email and password you created in iTunes Connect.