iap_demo.gd 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. extends Control
  2. const TEST_ITEM_SKU = "my_in_app_purchase_sku"
  3. @onready var alert_dialog = $AlertDialog
  4. @onready var label = $Label
  5. var payment = null
  6. var test_item_purchase_token = null
  7. func _ready():
  8. if Engine.has_singleton("GodotGooglePlayBilling"):
  9. label.text += "\n\n\nTest item SKU: %s" % TEST_ITEM_SKU
  10. payment = Engine.get_singleton("GodotGooglePlayBilling")
  11. # No params.
  12. payment.connected.connect(self._on_connected)
  13. # No params.
  14. payment.disconnected.connect(self._on_disconnected)
  15. # Response ID (int), Debug message (string).
  16. payment.connect_error.connect(self._on_connect_error)
  17. # Purchases (Dictionary[]).
  18. payment.purchases_updated.connect(self._on_purchases_updated)
  19. # Response ID (int), Debug message (string).
  20. payment.purchase_error.connect(self._on_purchase_error)
  21. # SKUs (Dictionary[]).
  22. payment.sku_details_query_completed.connect(self._on_sku_details_query_completed)
  23. # Response ID (int), Debug message (string), Queried SKUs (string[]).
  24. payment.sku_details_query_error.connect(self._on_sku_details_query_error)
  25. # Purchase token (string).
  26. payment.purchase_acknowledged.connect(self._on_purchase_acknowledged)
  27. # Response ID (int), Debug message (string), Purchase token (string).
  28. payment.purchase_acknowledgement_error.connect(self._on_purchase_acknowledgement_error)
  29. # Purchase token (string).
  30. payment.purchase_consumed.connect(self._on_purchase_consumed)
  31. # Response ID (int), Debug message (string), Purchase token (string).
  32. payment.purchase_consumption_error.connect(self._on_purchase_consumption_error)
  33. # Purchases (Dictionary[])
  34. payment.query_purchases_response.connect(self._on_query_purchases_response)
  35. payment.startConnection()
  36. else:
  37. show_alert("Android IAP support is not enabled. Make sure you have enabled 'Custom Build' and installed and enabled the GodotGooglePlayBilling plugin in your Android export settings! This application will not work.")
  38. func show_alert(text):
  39. alert_dialog.dialog_text = text
  40. alert_dialog.popup_centered()
  41. func _on_connected():
  42. print("PurchaseManager connected")
  43. payment.queryPurchases("inapp") # Use "subs" for subscriptions.
  44. func _on_query_purchases_response(query_result):
  45. if query_result.status == OK:
  46. for purchase in query_result.purchases:
  47. # We must acknowledge all puchases.
  48. # See https://developer.android.com/google/play/billing/integrate#process for more information
  49. if not purchase.is_acknowledged:
  50. print("Purchase " + str(purchase.sku) + " has not been acknowledged. Acknowledging...")
  51. payment.acknowledgePurchase(purchase.purchase_token)
  52. else:
  53. print("queryPurchases failed, response code: ",
  54. query_result.response_code,
  55. " debug message: ", query_result.debug_message)
  56. func _on_sku_details_query_completed(sku_details):
  57. for available_sku in sku_details:
  58. show_alert(JSON.new().stringify(available_sku))
  59. func _on_purchases_updated(purchases):
  60. print("Purchases updated: %s" % JSON.new().stringify(purchases))
  61. # See _on_connected
  62. for purchase in purchases:
  63. if not purchase.is_acknowledged:
  64. print("Purchase " + str(purchase.sku) + " has not been acknowledged. Acknowledging...")
  65. payment.acknowledgePurchase(purchase.purchase_token)
  66. if purchases.size() > 0:
  67. test_item_purchase_token = purchases[purchases.size() - 1].purchase_token
  68. func _on_purchase_acknowledged(purchase_token):
  69. print("Purchase acknowledged: %s" % purchase_token)
  70. func _on_purchase_consumed(purchase_token):
  71. show_alert("Purchase consumed successfully: %s" % purchase_token)
  72. func _on_purchase_error(code, message):
  73. show_alert("Purchase error %d: %s" % [code, message])
  74. func _on_purchase_acknowledgement_error(code, message):
  75. show_alert("Purchase acknowledgement error %d: %s" % [code, message])
  76. func _on_purchase_consumption_error(code, message, purchase_token):
  77. show_alert("Purchase consumption error %d: %s, purchase token: %s" % [code, message, purchase_token])
  78. func _on_sku_details_query_error(code, message):
  79. show_alert("SKU details query error %d: %s" % [code, message])
  80. func _on_disconnected():
  81. show_alert("GodotGooglePlayBilling disconnected. Will try to reconnect in 10s...")
  82. await get_tree().create_timer(10).timeout
  83. payment.startConnection()
  84. # GUI
  85. func _on_QuerySkuDetailsButton_pressed():
  86. payment.querySkuDetails([TEST_ITEM_SKU], "inapp") # Use "subs" for subscriptions.
  87. func _on_PurchaseButton_pressed():
  88. var response = payment.purchase(TEST_ITEM_SKU)
  89. if response.status != OK:
  90. show_alert("Purchase error %s: %s" % [response.response_code, response.debug_message])
  91. func _on_ConsumeButton_pressed():
  92. if test_item_purchase_token == null:
  93. show_alert("You need to set 'test_item_purchase_token' first! (either by hand or in code)")
  94. return
  95. payment.consumePurchase(test_item_purchase_token)