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
#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
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.request_purchased() #Ask Google for user purchased items
#This will call for each and every user purchased products
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.
You can put the purchase logic on a shop button.
#First listen for 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
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,
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.
You should do this once and only once at game start.
If your game has only non-consumables, you can