Welcome!

You are just one step away from creating awesome games!

Sign up with Google

You need a Google account to use Defold

- Select ref document -

In-app purchases API documentation

version 1.2.111

Functions and constants for interacting with Apple's In-app purchases and Google's In-app billing.

Functions

iap.buy

iap.buy(id, [options])

Perform a product purchase.

Calling iap.finish is required on a successful transaction if auto_finish_transactions is disabled in project settings.

Parameters

id

string product to buy

[options]

table optional parameters as properties.

The options table has the following members:

  • request_id : optional custom unique request id to set for this transaction. The id becomes attached to the payment within the Graph API. Only available for Facebook IAP transactions.
id

string product to buy

[options]

table optional parameters as properties.

The options table has the following members:

  • request_id : optional custom unique request id to set for this transaction. The id becomes attached to the payment within the Graph API. Only available for Facebook IAP transactions.

Examples

local function iap_listener(self, transaction, error)
    if error == nil then
        print(transaction.ident)
        print(transaction.state)
        print(transaction.date)
        print(transaction.trans_ident) -- only available when state == TRANS_STATE_PURCHASED, TRANS_STATE_UNVERIFIED or TRANS_STATE_RESTORED
        print(transaction.receipt)     -- only available when state == TRANS_STATE_PURCHASED or TRANS_STATE_UNVERIFIED
        print(transaction.request_id)  -- only available for Facebook IAP transactions (and if used in the iap.buy call parameters)
        print(transaction.user_id)     -- only available for Amazon IAP transactions

        -- required if auto finish transactions is disabled in project settings
        if (transaction.state == iap.TRANS_STATE_PURCHASED) then
            -- do server-side verification of purchase here..
            iap.finish(transaction)
        end
    else
        print(error.error, error.reason)
    end
end

function init(self)
    iap.set_listener(iap_listener)
    iap.buy("my_iap")
end

iap.finish

iap.finish(transaction)

Explicitly finish a product transaction.

Calling iap.finish is required on a successful transaction if auto_finish_transactions is disabled in project settings (otherwise ignored). The transaction.state field must equal iap.TRANS_STATE_PURCHASED.

Parameters

transaction

table transaction table parameter as supplied in listener callback

transaction

table transaction table parameter as supplied in listener callback

iap.get_provider_id

iap.get_provider_id()

Returns

id

constant provider id.

  • iap.PROVIDER_ID_GOOGLE
  • iap.PROVIDER_ID_AMAZON
  • iap.PROVIDER_ID_APPLE
  • iap.PROVIDER_ID_FACEBOOK
id

constant provider id.

  • iap.PROVIDER_ID_GOOGLE
  • iap.PROVIDER_ID_AMAZON
  • iap.PROVIDER_ID_APPLE
  • iap.PROVIDER_ID_FACEBOOK

iap.list

iap.list(ids, callback)

Get a list of all avaliable iap products.

Nested calls, that is calling iap.list from within callback is not supported. Doing so will result in call being ignored with the engine reporting "Unexpected callback set".

Parameters

ids

table table (array) of identifiers to get products from

callback

function(self, products, error) result callback

self

object The current object.

products

table The available iap products.

error

table Any error message. nil if there is no error.

ids

table table (array) of identifiers to get products from

callback

function(self, products, error) result callback

self

object The current object.

products

table The available iap products.

error

table Any error message. nil if there is no error.

Examples

local function iap_callback(self, products, error)
    if error == nil then
        for k,v in pairs(products) do
            print(v.ident)
            print(v.title)
            print(v.description)
            print(v.price)
            print(v.price_string)
            print(v.currency_code) -- only available on iOS
        end
    else
        print(error.error)
    end
end

function init(self)
    iap.list({"my_iap"}, iap_callback)
end

iap.restore

iap.restore()

Returns

success

boolean false if current store doesn't support handling restored transactions, otherwise true

success

boolean false if current store doesn't support handling restored transactions, otherwise true

iap.set_listener

iap.set_listener(listener)

Set the callback function to receive transaction events.

Parameters

listener

function(self, transaction, error) listener callback function. Pass an empty function if you no longer wish to receive callbacks.

self
object The current object.
transaction
table a table describing the transaction. The table contains the following fields:
  • ident: product identifier
  • state: transaction state
  • date: transaction date
  • original_trans: original transaction (only set when state == TRANS_STATE_RESTORED)
  • trans_ident : transaction identifier (only set when state == TRANS_STATE_RESTORED, TRANS_STATE_UNVERIFIED or TRANS_STATE_PURCHASED)
  • request_id: transaction request id. (only if receipt is set and for Facebook IAP transactions when used in the iap.buy call parameters)
  • receipt: receipt (only set when state == TRANS_STATE_PURCHASED or TRANS_STATE_UNVERIFIED)
error
table a table containing any error information. The error parameter is nil on success.
listener

function(self, transaction, error) listener callback function. Pass an empty function if you no longer wish to receive callbacks.

self
object The current object.
transaction
table a table describing the transaction. The table contains the following fields:
  • ident: product identifier
  • state: transaction state
  • date: transaction date
  • original_trans: original transaction (only set when state == TRANS_STATE_RESTORED)
  • trans_ident : transaction identifier (only set when state == TRANS_STATE_RESTORED, TRANS_STATE_UNVERIFIED or TRANS_STATE_PURCHASED)
  • request_id: transaction request id. (only if receipt is set and for Facebook IAP transactions when used in the iap.buy call parameters)
  • receipt: receipt (only set when state == TRANS_STATE_PURCHASED or TRANS_STATE_UNVERIFIED)
error
table a table containing any error information. The error parameter is nil on success.

Constants

iap.PROVIDER_ID_AMAZON

iap.PROVIDER_ID_APPLE

iap.PROVIDER_ID_FACEBOOK

iap.PROVIDER_ID_GOOGLE

iap.REASON_UNSPECIFIED

iap.REASON_USER_CANCELED

iap.TRANS_STATE_FAILED

iap.TRANS_STATE_PURCHASED

iap.TRANS_STATE_PURCHASING

This is an intermediate mode followed by TRANS_STATE_PURCHASED. Store provider support dependent.

iap.TRANS_STATE_RESTORED

This is only available on store providers supporting restoring purchases.

iap.TRANS_STATE_UNVERIFIED


I want to report anonymously.