iap.gd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. extends Node
  2. signal purchase_success(item_name)
  3. signal purchase_fail
  4. signal purchase_cancel
  5. signal purchase_owned(item_name)
  6. signal has_purchased(item_name)
  7. signal consume_success(item_name)
  8. signal consume_fail
  9. signal consume_not_required
  10. signal sku_details_complete
  11. signal sku_details_error
  12. var payment
  13. func _ready():
  14. if Engine.has_singleton("GodotPayments"):
  15. payment = Engine.get_singleton("GodotPayments")
  16. else:
  17. print("GodotPayment singleton is only available on Android devices.")
  18. if payment:
  19. # set callback with this script instance
  20. payment.setPurchaseCallbackId(get_instance_id())
  21. # set consume purchased item automatically after purchase, defulat value is true
  22. func set_auto_consume(auto):
  23. if payment:
  24. payment.setAutoConsume(auto)
  25. # request user owned item, callback : has_purchased
  26. func request_purchased():
  27. if payment:
  28. payment.requestPurchased()
  29. func has_purchased(receipt, signature, sku):
  30. if sku == "":
  31. print("has_purchased : nothing")
  32. emit_signal("has_purchased", null)
  33. else:
  34. print("has_purchased : ", sku)
  35. emit_signal("has_purchased", sku)
  36. # purchase item
  37. # callback : purchase_success, purchase_fail, purchase_cancel, purchase_owned
  38. func purchase(item_name):
  39. if payment:
  40. # transaction_id could be any string that used for validation internally in java
  41. payment.purchase(item_name, "transaction_id")
  42. func purchase_success(receipt, signature, sku):
  43. print("purchase_success : ", sku)
  44. emit_signal("purchase_success", sku)
  45. func purchase_fail():
  46. print("purchase_fail")
  47. emit_signal("purchase_fail")
  48. func purchase_cancel():
  49. print("purchase_cancel")
  50. emit_signal("purchase_cancel")
  51. func purchase_owned(sku):
  52. print("purchase_owned : ", sku)
  53. emit_signal("purchase_owned", sku)
  54. # consume purchased item
  55. # callback : consume_success, consume_fail
  56. func consume(item_name):
  57. if payment:
  58. payment.consume(item_name)
  59. # consume all purchased items
  60. func consume_all():
  61. if payment:
  62. payment.consumeUnconsumedPurchases()
  63. func consume_success(receipt, signature, sku):
  64. print("consume_success : ", sku)
  65. emit_signal("consume_success", sku)
  66. # if consume fail, need to call request_purchased() to get purchase token from google
  67. # then try to consume again
  68. func consume_fail():
  69. emit_signal("consume_fail")
  70. # no purchased item to consume
  71. func consume_not_required():
  72. emit_signal("consume_not_required")
  73. # detail info of IAP items
  74. # sku_details = {
  75. # product_id (String) : {
  76. # type (String),
  77. # product_id (String),
  78. # title (String),
  79. # description (String),
  80. # price (String), # this can be used to display price for each country with their own currency
  81. # price_currency_code (String),
  82. # price_amount (float)
  83. # },
  84. # ...
  85. # }
  86. var sku_details = {}
  87. # query for details of IAP items
  88. # callback : sku_details_complete
  89. func sku_details_query(list):
  90. if payment:
  91. var sku_list = PoolStringArray(list)
  92. payment.querySkuDetails(sku_list)
  93. func sku_details_complete(result):
  94. print("sku_details_complete : ", result)
  95. for key in result.keys():
  96. sku_details[key] = result[key]
  97. emit_signal("sku_details_complete")
  98. func sku_details_error(error_message):
  99. print("error_sku_details = ", error_message)
  100. emit_signal("sku_details_error")