Android应用内购买

自Godot 3.2.2以来,Godot提供了一个第一方的 GodotGooglePlayBilling 安卓插件.新插件使用 `Google Play Billing库<https://developer.android.com/google/play/billing>`__ ,而不是现在已经废弃的AIDL IAP实现.

如果您通过查看示例更好地学习,可以在此处找到演示项目 这里.

从Godot 3.2.1和更低版本迁移(GodotPaymentsV3)

新的 GodotGooglePlayBilling API与其前身 GodotPaymentsV3 不兼容.

修改

用法

入门

如果还没有完成,请确保你已经启用并成功设置 Android Custom Builds.从 releases page“中抓取``GodotGooglePlayBilling``插件二进制文件和配置文件,并将两者放入`res://android/plugins`中.现在该插件应该出现在Android导出设置中,你可以启用它.

入门

要使用 GodotGooglePlayBilling API,你首先要获得 GodotGooglePlayBilling 单例,并启动连接:

  1. var payment
  2. func _ready():
  3. if Engine.has_singleton("GodotGooglePlayBilling"):
  4. payment = Engine.get_singleton("GodotGooglePlayBilling")
  5. # These are all signals supported by the API
  6. # You can drop some of these based on your needs
  7. payment.connect("connected", self, "_on_connected") # No params
  8. payment.connect("disconnected", self, "_on_disconnected") # No params
  9. payment.connect("connect_error", self, "_on_connect_error") # Response ID (int), Debug message (string)
  10. payment.connect("purchases_updated", self, "_on_purchases_updated") # Purchases (Dictionary[])
  11. payment.connect("purchase_error", self, "_on_purchase_error") # Response ID (int), Debug message (string)
  12. payment.connect("sku_details_query_completed", self, "_on_sku_details_query_completed") # SKUs (Dictionary[])
  13. payment.connect("sku_details_query_error", self, "_on_sku_details_query_error") # Response ID (int), Debug message (string), Queried SKUs (string[])
  14. payment.connect("purchase_acknowledged", self, "_on_purchase_acknowledged") # Purchase token (string)
  15. payment.connect("purchase_acknowledgement_error", self, "_on_purchase_acknowledgement_error") # Response ID (int), Debug message (string), Purchase token (string)
  16. payment.connect("purchase_consumed", self, "_on_purchase_consumed") # Purchase token (string)
  17. payment.connect("purchase_consumption_error", self, "_on_purchase_consumption_error") # Response ID (int), Debug message (string), Purchase token (string)
  18. payment.startConnection()
  19. else:
  20. print("Android IAP support is not enabled. Make sure you have enabled 'Custom Build' and the GodotGooglePlayBilling plugin in your Android export settings! IAP will not work.")

所有的API方法只有在API被连接的情况下才会工作,你可以使用 payment.isReady() 检查连接状态.

查询可获取的项目

只要连接了API,你就可以使用 querySkuDetails 来查询SKU.

完整示例:

  1. func _on_connected():
  2. payment.querySkuDetails(["my_iap_item"], "inapp") # "subs" for subscriptions
  3. func _on_sku_details_query_completed(sku_details):
  4. for available_sku in sku_details:
  5. print(available_sku)

购买项目

要启动一个项目的购买流程,请调用 purchase .在启动购买流程之前,你必须**查询一个项目的SKU详情.

  1. payment.purchase("my_iap_item")

检查用户是否购买了某个项目

要获得所有的购买,调用 queryPurchases .与大多数其他函数不同的是, queryPurchases 是一个同步操作,并返回一个 Dictionary 的状态码和一个购买数组或一个错误信息.

完整示例:

  1. var query = payment.queryPurchases("inapp") # Or "subs" for subscriptions
  2. if query.status == OK:
  3. for purchase in query.purchases:
  4. if purchase.sku == "my_iap_item":
  5. premium = true # Entitle the user to the content they bought
  6. if !purchase.is_acknowledged:
  7. payment.acknowledgePurchase(purchase.purchase_token)

消耗品

如果你的应用内物品不是一次性购买,而是可以多次购买的消耗品(如硬币),你可以用购买令牌调用 consumePurchase 来消耗物品.调用 queryPurchases 来获取购买令牌.调用 consumePurchase 会自动确认购买.

  1. var query = payment.queryPurchases("inapp") # Or "subs" for subscriptions
  2. if query.status == OK:
  3. for purchase in query.purchases:
  4. if purchase.sku == "my_consumable_iap_item":
  5. if !purchase.is_acknowledged:
  6. payment.consumePurchase(purchase.purchase_token)
  7. # Check the _on_purchase_consumed callback and give the user what they bought

订阅

订阅的工作原理和普通的应用内项目没有太大区别.只要使用 "subs" 作为 querySkuDetails 的第二个参数,就可以得到订阅的详细信息.在 queryPurchases() 的结果中检查 is_auto_renewing 来查看用户是否取消了自动更新的订阅