android_in_app_purchases.rst 4.7 KB

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