+1 vote

I added godot payments v3 to modules.
Saved iap.gd from here
Now I dont know what to do.

I see there are many functions which I dont understand but I just need simple on button pressed purchase (from google play).
So how do i set purchase price, things player receives and script?

in Engine by (330 points)

1 Answer

+6 votes
Best answer

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)
by (750 points)
selected by

Wow... so detail explanation.
It should be in official doc somewhere.
What do you think @Akien? :)

@vinod would you make a PR for this on https://github.com/godotengine/godot-docs/tree/master/reference ?

Thanks for very detailed answer.
1 more question... Can I somehow test it if I did everything right?

I forgot to add it.
You can use any of these as the product id.
android.test.purchased
android.test.cancelled

Another way is to add another account as a tester and you will not be charged. You can also test with the redeem codes too.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.