You need to set the iap.gd as a singleton. Let us say you set it as IAP
Get iap details
When starting your game, you may need to get the item details from Google such as price and localized price string etc.
#First listen to the sku details update callback
IAP.connect("sku_details_complete",self,"sku_details_complete")
#Then ask google the details for these items
IAP.sku_details_query(["pid1","pid2"])
#This will be called when sku details are retrieved successfully
func sku_details_complete():
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
You can use the iap details to display the title,price and/or description on your shop scene
Check if user purchased an item
When starting your game, you can check if user has purchased an item. YOU SHOULD DO THIS ONLY AFTER 2/3 SECONDS AFTER YOUR GAME IS LOADED. I see sevaral games(including other engines) do this right at the start and the iap has not been initialized and your app will crash on start.
#Add a listener first
IAP.connect("has_purchased",self,"iap_has_purchased")
IAP.request_purchased() #Ask Google for user purchased items
#This will call for each and every user purchased products
func iap_has_purchased(item_name):
print(item_name) #print the name of purchased items
Google iap policies says the game should restore the user's purchases if the user replaces his phone or reinstall the same app. You can use the above code to do that.
Simple Purchase
You can put the purchase logic on a shop button.
#First listen for purchase_success callback
IAP.connect("purchase_success",self,"purchase_success_callback")
#Then call purchase like this
IAP.purchase("pid1") #replace pid1 with your product id
IAP.purchase("pid2") #replace pid2 with your another product id
#This function will be called when the purchase is a success
func purchase_success_callback(item):
print(item + " has purchased")
There are other several signals in the iap.gd. You should check those also and improve the user experience as you needed.
Consumables and Non-Consumables
There are two types of products - consumables and non-consumables.
Consumables are purchased and used, for eg: healing potions which can be purchased again and again.
Non-consumables are one time purchases, for eg: Level packs.
But Google doesn't have this separation. If your product is a consumable, and if a user purchased it, it will not be available for purchase until it is consumed. So you should call consume for your consumables and don't call consume for your non-consumables. To do that call,
IAP.connnect("consume_success",self,"on_consume_success")
IAP.consume("pid")
func on_consume_success(item):
print(item + " consumed")
If your game has only consumables, you don't have to do this. You can set it to consume the item automatically after a purchase like this.
IAP.set_auto_consume(true)
You should do this once and only once at game start.
If your game has only non-consumables, you can
IAP.set_auto_consume(false)