类型 函数 返回值 无 修订版本 版本 2024.3703 关键词 Google,IAP,应用内购买,初始化 另请参阅 store.isActive store.*
此调用是必需的,并且必须在进行其他 Google IAP 调用之前进行。 这将准备 Google IAP 库,并允许您检测定义为 `listener` 的监听器的 storeTransaction 事件。
此方法还会向指定的监听器(与用于 storeTransaction 事件的监听器相同)调度一个 init 事件。此时,除非发生错误,否则 store.isActive 将为 `true`。
再次强调,您必须等到 init 事件被调度后,才能尝试加载产品、恢复
请注意,初始化是异步完成的,因为它可能需要网络调用,而网络调用所需的时间会因服务器负载和网络延迟而异。
store.init( 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 )