Android应用程序内购买

godot引擎集成了googlePaymentsv3模块,我们可以在游戏中实现应用内购买。

Godot引擎演示项目库有一个Android IAP示例项目。它包括一个用于Android IAP的gdscript接口。

在这里查看存储库https://github.com/godoengine/godot-demo-projects

在中查找iap.gd脚本

godot-demo-projects/misc/android_iap

将它添加到自动加载列表中并命名为iap,这样我们就可以在游戏中的任何地方引用它。

获取产品详细信息

当我们开始游戏时,我们需要从Google获取项目的详细信息,比如产品价格、描述和本地化的价格字符串等。

#First listen to the sku details update callback
IAP.connect("sku_details_complete",self,"sku_details_complete")

#Then ask google the details for these items
IAP.sku_details_query(["pid1","pid2"]) #pid1 and pid2 are our product ids entered in Googleplay dashboard


#This will be called when sku details are retrieved successfully
func sku_details_complete():
    print(IAP.sku_details) #This will print the details as JSON format, refer the format in iap.gd
    print(IAP.sku_details["pid1"].price) #print formatted localized price

我们可以使用IAP细节在我们的商店场景中显示标题、价格和/或描述。

检查用户是否购买了商品

开始游戏时,我们可以检查用户是否购买了任何产品。你应该在游戏加载2/3秒后才这样做。如果我们在游戏启动时首先这样做,IAP可能不会初始化,我们的游戏将在启动时崩溃。

#Add a listener first
IAP.connect("has_purchased",self,"iap_has_purchased")
IAP.request_purchased() #Ask Google for all purchased items

#This will call for each and every user purchased products
func iap_has_purchased(item_name):
    print(item_name) #print the name of purchased items

谷歌IAP政策说,如果用户更换手机或重新安装同一个应用程序,游戏应该恢复用户的购买。我们可以使用上面的代码来检查用户购买了哪些产品,我们可以让我们的游戏做出相应的响应。

简单购买

我们可以把这个购买逻辑放到产品的购买按钮上。

#First listen for purchase_success callback
IAP.connect("purchase_success",self,"purchase_success_callback")

#Then call purchase like this
IAP.purchase("pid1") #replace pid1 with your product id
IAP.purchase("pid2") #replace pid2 with your another product id

#This function will be called when the purchase is a success
func purchase_success_callback(item):
    print(item + " has purchased")

我们还可以为采购流程实施其他信号,并根据需要改进用户体验。

purchase_fail -因任何原因导致购买失败时

purchase_cancel -当用户取消购买时

purchase_owned -当用户提前购买产品时

消耗品和非消耗品

有两种类型的产品-消耗品和非消耗品。 消耗品 购买和使用,例如:治疗药水,可以购买一次又一次。 Non-consumables 一次性购买,例如:水平包装。

谷歌的仪表盘中没有这种分离。如果我们的产品是消耗品,并且用户已经购买了它,在消费之前,它将不可购买。所以我们应该为我们的消耗品调用消耗方法,而不是为您的非消耗品调用消耗。

IAP.connect("consume_success",self,"on_consume_success")
IAP.consume("pid")

func on_consume_success(item):
    print(item + " consumed")

如果我们的游戏只有消耗品,我们就不必这么做。我们可以设置它在购买后自动消费物品。

IAP.set_auto_consume(true)

如果我们的游戏只有非消耗品,我们可以

IAP.set_auto_consume(false)

我们应该在游戏开始时只设置一次自动消费值。

测试

如果我们在Google Dashboard中添加一个Gmail ID作为测试人员,该测试人员可以购买项目,并且不会收取费用。另一种测试IAP的方法是使用我们为我们的游戏生成的兑换代码,因为购买流程是相同的。

第三种测试方法是在开发方面。如果我们把产品ID放在下面,我们将根据产品ID得到一个静态的固定响应。这是一种在进入仪表板之前快速测试事物的方法。

  • android.test.purchased

  • android.test.canceled

  • android.test.refunded

  • android.test.item_unavailable