store.init()

Type Function
Return value none
Revision Release 2024.3703
Keywords Google, IAP, in-app purchases, init
See also store.isActive
store.*

Overview

This call is required and must be made before making other Google IAP calls. This prepares the Google IAP library and allows you to detect storeTransaction events to the listener defined as listener.

Important

This method also dispatches an init event to the specified listener (the same listenter used for storeTransaction events). At this point, store.isActive will be true unless an error occurred.

To reiterate, you must wait until the init event is dispatched before attempting to load products, restore previously-purchased items, consume an item, etc.

Note that initialization is done ansynchronously because it may require network calls which take a variable amount of time depending on server load and network latencies.

Syntax

store.init( listener )
listener (required)

Listener. The listener that will handle storeTransaction events. Note that this listener will also handle the init event as outlined above.

Example

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 )