Skip to Content
defold-typescript libraries
dev-masih

dev-masih/cafebazaar-iap/iapc

Functions and constants for doing Cafebazaar in-app purchases.

  • GitHub: dev-masih/cafebazaar-iap — pinned to v1.1 (released while Defold 1.2.163 was current)
    1. Pick a release from dev-masih/cafebazaar-iap releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as iapc from "iapc"
  • Asset Portal: cafebazaar-iap

Functions

iapc.buy(id: string, options: table)

Sets the listener function for In-app purchase events.

  local function iapc_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 == iapc.TRANS_STATE_PURCHASED) then
        -- do server-side verification of purchase here..
        iapc.finish(transaction)
      end
    else
      print(error.error, error.reason)
    end
  end

  function init(self)
      iapc.set_listener(iapc_listener)
      iapc.buy("my_iap_id")
  end

Parameters

  • id: string — product to buy
  • options: table — optional parameters as properties. The following parameters can be set
    • request_id: string

iapc.finish(transaction: table)

Explicitly finish a product transaction. Calling iapc.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 iapc.TRANS_STATE_PURCHASED.

Parameters

  • transaction: table — transaction table parameter as supplied in listener callback

iapc.list(ids: table, callback: function)

Get a list of all avaliable iapc products.

  local function iapc_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)
      iapc.list({"my_iap_id"}, iapc_callback)
  end

Parameters

  • ids: table — table (array) of identifiers to get products from
  • callback: function — result callback taking the following parameters
    • self: object — The current object.
    • products: table — a table describing the available iapc products.
      • ident: string — The product identifier.
      • title: string — The product title.
      • description: string — The product description.
      • price: number — The price of the product.
      • price_string: string — The price of the product, as a formatted string (amount and currency symbol).
      • currency_code: string — The currency code, this reflects the merchant's locale, instead of the user's.
    • error: table — a table containing error information. nil if there is no error. - error (the error message)

iapc.restore(): boolean

Restore previously purchased products.

Returns

  • success: boolean — value is true if current store supports handling restored transactions, otherwise false.

iapc.set_listener(listener: function)

Set the callback function to receive purchase transaction events.

Parameters

  • listener: function — 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.
      • ident: string — The product identifier.
      • state: string — The transaction state. One of the following
        • iapc.TRANS_STATE_FAILED
        • iapc.TRANS_STATE_PURCHASED
        • iapc.TRANS_STATE_PURCHASING
        • iapc.TRANS_STATE_RESTORED
        • iapc.TRANS_STATE_UNVERIFIED
      • date: string — The date and time for the transaction.
      • trans_ident: string — The transaction identifier. This field is only set when state is TRANS_STATE_RESTORED, TRANS_STATE_UNVERIFIED or TRANS_STATE_PURCHASED.
      • receipt: string — The transaction receipt. This field is only set when state is TRANS_STATE_PURCHASED or TRANS_STATE_UNVERIFIED.
      • signature: string — A string containing the signature of the purchase data that was signed with the private key of the developer.
    • error: table — a table containing error information. nil if there is no error. error - the error message. reason - the reason for the error, value can be one of the following constants
      • iapc.REASON_UNSPECIFIED
      • iapc.REASON_USER_CANCELED

Constants

iapc.REASON_UNSPECIFIED

unspecified error reason

iapc.REASON_USER_CANCELED

user canceled reason

iapc.TRANS_STATE_FAILED

transaction failed state

iapc.TRANS_STATE_PURCHASED

transaction purchased state

iapc.TRANS_STATE_PURCHASING

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

iapc.TRANS_STATE_RESTORED

transaction restored state This is only available on store providers supporting restoring purchases.

iapc.TRANS_STATE_UNVERIFIED

transaction unverified state, requires verification of purchase

On this page