Android in-app purchases
The Godot engine demo project repository has an android-iap example project. It includes a gdscript interface for android IAP.
Check the repository here
Find the iap.gd script in
Add it to the Autoload list and name it as IAP so that we can reference it anywhere in the game.
When starting our game, we will need to get the item details from Google such as the product price, description and localized price string etc.
IAP.connect("sku_details_complete",self,"sku_details_complete")
#Then ask google the details for these items
#This will be called when sku details are retrieved successfully
print(IAP.sku_details) #This will print the details as JSON format, refer the format in iap.gd
print(IAP.sku_details["pid1"].price) #print formatted localized price
We can use the IAP details to display the title, price and/or description on our shop scene.
Google IAP policy says the game should restore the user’s purchases if the user replaces their phone or reinstalls the same app. We can use the above code to check what products the user has purchased and we can make our game respond accordingly.
We can put this purchase logic on a product’s buy button.
#First listen for purchase_success callback
IAP.connect("purchase_success",self,"purchase_success_callback")
IAP.purchase("pid1") #replace pid1 with your product id
#This function will be called when the purchase is a success
func purchase_success_callback(item):
print(item + " has purchased")
We can also implement other signals for the purchase flow and improve the user experience as you needed.
purchase_fail
- When the purchase is failed due to any reason
purchase_cancel
- When the user cancels the purchase
purchase_owned
- When the user already bought the product earlier
Google doesn’t have this separation in their dashboard. If our product is a consumable, and if a user has purchased it, it will not be available for purchase until it is consumed. So we should call the consume method for our consumables and don’t call consume for your non-consumables.
If our game has only consumables, we don’t have to do this. We can set it to consume the item automatically after a purchase.
If our game has only non-consumables, we can
We should set the auto consume value only once when the game starts.
If we add a gmail id as a tester in Google dashboard, that tester can purchase items and they will not be charged. Another way to test IAP is using redeem codes generated by us for our game because the purchase flow is the same.
Third way of testing is in development side. If we put the product ids as shown below, we will get a static fixed response according to the product id. This is a quick way of testing things before going to the dashboard.
- android.test.purchased
- android.test.canceled
- android.test.item_unavailable