Welcome!

You are just one step away from creating awesome games!

You need a Google account to use Defold

- Select ref document -

In-app purchases API documentation

version 1.2.118

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

  • request_id ( Facebook only. Optional custom unique request id to set for this transaction. The id becomes attached to the payment within the Graph API.)
id

string product to buy

[options]

table optional parameters as properties. The following parameters can be set:

  • request_id ( Facebook only. Optional custom unique request id to set for this transaction. The id becomes attached to the payment within the Graph API.)

Examples

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

iap.buy_premium()

Performs a purchase of a premium game license. The purchase transaction is handled like regular iap purchases; calling the currently set iap_listener with the transaction results.

This function does not work when testing the application locally in the Gameroom client.

Examples

local function iap_listener(self, transaction, error)
  if error == nil then
    -- purchase is ok
    print(transaction.date)
    print(transaction.)
    -- 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)
  -- set the listener function for iap transactions
  iap.set_listener(iap_listener)
  -- purchase premium license
  iap.buy_premium()
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. 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

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
  • iap.PROVIDER_ID_GAMEROOM
id

constant provider id.

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

iap.has_premium

iap.has_premium(callback)

Checks if a license for the game has been purchased by the user. You should provide a callback function that will be called with the result of the check.

This function does not work when testing the application locally in the Gameroom client.

Parameters

callback

function(self, has_premium) result callback

self
object The current object.
has_premium
boolean true if the user has premium license, false otherwise.
callback

function(self, has_premium) result callback

self
object The current object.
has_premium
boolean true if the user has premium license, false otherwise.

Examples

local function premium_result(self, has_premium)
  if has_premium then
     -- User has purchased this premium game.
  end
end

function init()
  -- check is user has bought the game
  iap.has_premium(premium_result)
end

iap.list

iap.list(ids, callback)

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)
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

iap.restore()

Restore previously purchased products.

Returns

success

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

success

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

iap.set_listener

iap.set_listener(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.
purchase_token
Facebook Gameroom only. The purchase token.
currency
Facebook Gameroom only. The currency used for the purchase.
amount
Facebook Gameroom only. The amount the player will be charged for a single unit of this product.
quantity
Facebook Gameroom only. The quantity of this item the user is purchasing.
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_*)
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_*)

Constants

iap.PROVIDER_ID_AMAZON

iap.PROVIDER_ID_APPLE

iap.PROVIDER_ID_FACEBOOK

iap.PROVIDER_ID_GAMEROOM

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.