android_in_app_purchases.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. .. _doc_android_in_app_purchases:
  2. Android in-app purchases
  3. ========================
  4. Godot engine has integrated GooglePaymentsV3 module with which we can implement in-app purchases in our game.
  5. The Godot engine demo project repository has an android-iap example project. It includes a gdscript interface for android IAP.
  6. Check the repository here https://github.com/godotengine/godot-demo-projects
  7. Find the iap.gd script in
  8. .. code-block:: none
  9. godot-demo-projects/misc/android_iap
  10. Add it to the Autoload list and name it as IAP so that we can reference it anywhere in the game.
  11. Getting the product details
  12. ---------------------------
  13. 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.
  14. ::
  15. #First listen to the sku details update callback
  16. IAP.connect("sku_details_complete",self,"sku_details_complete")
  17. #Then ask google the details for these items
  18. IAP.sku_details_query(["pid1","pid2"]) #pid1 and pid2 are our product ids entered in Googleplay dashboard
  19. #This will be called when sku details are retrieved successfully
  20. func sku_details_complete():
  21. print(IAP.sku_details) #This will print the details as JSON format, refer the format in iap.gd
  22. print(IAP.sku_details["pid1"].price) #print formatted localized price
  23. We can use the IAP details to display the title, price and/or description on our shop scene.
  24. Check if user purchased an item
  25. -------------------------------
  26. When starting our game, we can check if the user has purchased any product. YOU SHOULD DO THIS ONLY AFTER 2/3 SECONDS AFTER YOUR GAME IS LOADED. If we do this as the first thing when the game is launched, IAP might not be initialized and our game will crash on start.
  27. ::
  28. #Add a listener first
  29. IAP.connect("has_purchased",self,"iap_has_purchased")
  30. IAP.request_purchased() #Ask Google for all purchased items
  31. #This will call for each and every user purchased products
  32. func iap_has_purchased(item_name):
  33. print(item_name) #print the name of purchased items
  34. 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.
  35. Simple Purchase
  36. ---------------
  37. We can put this purchase logic on a product's buy button.
  38. ::
  39. #First listen for purchase_success callback
  40. IAP.connect("purchase_success",self,"purchase_success_callback")
  41. #Then call purchase like this
  42. IAP.purchase("pid1") #replace pid1 with your product id
  43. IAP.purchase("pid2") #replace pid2 with your another product id
  44. #This function will be called when the purchase is a success
  45. func purchase_success_callback(item):
  46. print(item + " has purchased")
  47. We can also implement other signals for the purchase flow and improve the user experience as you needed.
  48. ``purchase_fail`` - When the purchase is failed due to any reason
  49. ``purchase_cancel`` - When the user cancels the purchase
  50. ``purchase_owned`` - When the user already bought the product earlier
  51. Consumables and Non-Consumables
  52. -------------------------------
  53. There are two types of products - consumables and non-consumables.
  54. **Consumables** are purchased and used, for eg: healing potions which can be purchased again and again.
  55. **Non-consumables** are one time purchases, for eg: Level packs.
  56. 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.
  57. ::
  58. IAP.connect("consume_success",self,"on_consume_success")
  59. IAP.consume("pid")
  60. func on_consume_success(item):
  61. print(item + " consumed")
  62. 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.
  63. ::
  64. IAP.set_auto_consume(true)
  65. If our game has only non-consumables, we can
  66. ::
  67. IAP.set_auto_consume(false)
  68. We should set the auto consume value only once when the game starts.
  69. Testing
  70. -------
  71. 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.
  72. 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.
  73. - android.test.purchased
  74. - android.test.canceled
  75. - android.test.refunded
  76. - android.test.item_unavailable