In-app purchases

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

iap.PROVIDER_ID_AMAZON

provider id for Amazon


iap.PROVIDER_ID_APPLE

provider id for Apple


iap.PROVIDER_ID_FACEBOOK

provider id for Facebook


iap.PROVIDER_ID_GOOGLE

iap provider id for Google


iap.REASON_UNSPECIFIED

unspecified error reason


iap.REASON_USER_CANCELED

user canceled reason


iap.TRANS_STATE_FAILED

transaction failed state


iap.TRANS_STATE_PURCHASED

transaction purchased state


iap.TRANS_STATE_PURCHASING

transaction purchasing state

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


iap.TRANS_STATE_RESTORED

transaction restored state

This is only available on store providers supporting restoring purchases.


iap.TRANS_STATE_UNVERIFIED

transaction unverified state, requires verification of purchase


iap.buy()

buy product

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 following parameters can be set:

EXAMPLES

local function iap_listener(self, transaction, error)
  if error == nil then
    -- purchase is successful.
    print(transaction.date)
    -- 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()

finish buying product

Explicitly finish a product transaction.

Calling iap.finish is required on a successful transaction if auto_finish_transactions is disabled in project settings. Calling this function with auto_finish_transactions set will be ignored and a warning is printed. The transaction.state field must equal iap.TRANS_STATE_PURCHASED.

PARAMETERS

transaction -

table transaction table parameter as supplied in listener callback


iap.get_provider_id()

get current provider id

RETURN

id -

constant provider id.


iap.list()

list in-app products

Get a list of all avaliable iap products. Products are described as a table with the following fields:

ident
The product identifier.
title
The product title.
description
The product description.
price
The price of the product.
price_string
The price of the product, as a formatted string (amount and currency symbol).
currency_code
The currency code. On Google Play, this reflects the merchant's locale, instead of the user's.

Nested calls, that is calling iap.list() from within the 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 Table describing the available iap products. See above for details.
error
table a table containing error information. nil if there is no error. - error (the error message)

EXAMPLES

local function iap_callback(self, products, error)
  if error == nil then
    for k,p in pairs(products) do
      -- present the product
      print(p.title)
      print(p.description)
    end
  else
    print(error.error)
  end
end

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


iap.restore()

restore products (non-consumable)

Restore previously purchased products.

RETURN

success -

boolean true if current store supports handling restored transactions, otherwise false.


iap.set_listener()

set purchase transaction listener

Set the callback function to receive purchase transaction events. Transactions are described as a table with the following fields:

ident
The product identifier.
state
The transaction state. See iap.TRANS_STATE_*.
date
The date and time for the transaction.
trans_ident
The transaction identifier. This field is only set when state is TRANS_STATE_RESTORED, TRANS_STATE_UNVERIFIED or TRANS_STATE_PURCHASED.
receipt
The transaction receipt. This field is only set when state is TRANS_STATE_PURCHASED or TRANS_STATE_UNVERIFIED.
original_trans
Apple only. The original transaction. This field is only set when state is TRANS_STATE_RESTORED.
signature
Google Play only. A string containing the signature of the purchase data that was signed with the private key of the developer.
request_id
Facebook only. This field is set to the optional custom unique request id request_id if set in the iap.buy() call parameters.
user_id
Amazon Pay only. The user ID.
is_sandbox_mode
Amazon Pay only. If true, the SDK is running in Sandbox mode. This only allows interactions with the Amazon AppTester. Use this mode only for testing locally.
cancel_date
Amazon Pay only. The cancel date for the purchase. This field is only set if the purchase is canceled.
canceled
Amazon Pay only. Is set to true if the receipt was canceled or has expired; otherwise false.

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. See above for details.
error
table a table containing error information. nil if there is no error. - error (the error message) - reason (the reason for the error, see iap.REASON_*)