类型 函数 返回值 无 修订版本 版本 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.v3" ) 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 )