类型 函数 返回值 无 修订版 发行 2024.3703 版 关键字 Google、IAP、应用内购买、init 另请参见 store.isActive store.*
在进行其他 Google IAP 调用之前,必须进行此调用。此调用会准备 Google IAP 库,使您能够检测到监听程序接收到的 storeTransaction 事件,此监听程序定义为 listener
。
此方法还会将 init 事件派发到指定监听程序(用于 storeTransaction 事件的监听程序)。在这一点上,除非发生错误,否则 store.isActive 将为 true
。
重复说明,您必须等到派发 init 事件后,再尝试 加载产品、还原之前购买的
请注意,初始化是异步完成的,因为它可能需要进行网络调用,这会根据服务器负载和网络延迟花费可变时间。
store.init( listener )
Listener. 将处理 storeTransaction 事件的监听程序。请注意,此监听程序还将处理 init 事件,如上所述。
local store = require( "plugin.google.iap.billing" ) local json = require( "json" ) local function transactionListener( event ) -- Google IAP initialization event if ( event.name == "init" ) then if not ( event.transaction.isError ) then -- Perform steps to enable IAP, load products, etc. else -- Unsuccessful initialization; output error details print( event.transaction.errorType ) print( event.transaction.errorString ) end -- Store transaction event elseif ( event.name == "storeTransaction" ) then if not ( event.transaction.state == "failed" ) then -- Successful transaction print( json.prettify( event ) ) print( "event.transaction: " .. json.prettify( event.transaction ) ) else -- Unsuccessful transaction; output error details print( event.transaction.errorType ) print( event.transaction.errorString ) end end end -- Initialize Google IAP store.init( transactionListener )