Skip to Content
defold-typescript libraries
itzmiadm

itzmiadm/Myket-IAP-Defold/iapm

Functions and constants for doing Myket in-app purchases.

  • GitHub: itzmiadm/Myket-IAP-Defold — pinned to 1.2.0 (released while Defold 1.13.1 was current)
    1. Pick a release from itzmiadm/Myket-IAP-Defold 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 iapm from "iapm"
  • Asset Portal: Myket-IAP-Defold

Functions

iapm.buy(id: string)

Start a purchase for a Myket one-time product.

  local function iapm_listener(self, transaction, error)
    if error == nil then
      -- purchase is successful.
      assert(transaction.verified)
      print(transaction.date)
      -- required if auto finish transactions is disabled in project settings
      if (transaction.state == iapm.TRANS_STATE_PURCHASED) then
        -- do server-side verification of purchase here..
        iapm.finish(transaction)
      end
    else
      print(error.error, error.reason)
    end
  end

  function init(self)
      iapm.set_listener(iapm_listener)
      iapm.buy("my_iap_id")
  end

Parameters

  • id: string — product to buy

iapm.subscribe(id: string)

Start a purchase for a Myket subscription. The result is delivered to the listener registered with set_listener. Subscriptions are never consumed, regardless of the auto_finish_transactions setting. An error is delivered when the installed Myket client does not support subscriptions.

  function init(self)
      iapm.set_listener(function(self, transaction, error)
          if not error and transaction.type == "subs" then
              print("Subscription active:", transaction.ident)
          end
      end)
      iapm.subscribe("monthly_vip")
  end

Parameters

  • id: string — subscription product identifier

iapm.finish(transaction: table)

Consume a product transaction. Call this only for consumable products and only after content delivery succeeds. Calling iapm.finish is required for a consumable transaction if auto_finish_transactions is disabled in project settings. Calling this function with auto_finish_transactions enabled is ignored and a warning is printed. The transaction.state field must equal iapm.TRANS_STATE_PURCHASED. Calling this for a transaction whose type is subs is ignored.

Parameters

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

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

Get details for the requested Myket one-time products and subscriptions.

  local function iapm_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)
      iapm.list({"my_iap_id"}, iapm_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 keyed by product identifier describing the available Myket products.
      • ident: string — The product identifier.
      • type: string — Product type, either inapp or subs.
      • 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)

iapm.restore(): boolean

Restore owned, unconsumed products and active subscriptions. Results are sent to the listener registered with set_listener. Subscriptions are never consumed.

Returns

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

iapm.set_public_key(public_key: string): boolean

Set or replace the Base64-encoded Myket application public key used for local SHA1withRSA purchase-signature verification. Call this before set_listener, buy, or restore. Prefer the iap.public_key game.project setting so the key is available while pending purchases are processed.

Parameters

  • public_key: string — The application public key from the Myket developer panel.

Returns

  • valid: boolean — true when the value is a valid Base64-encoded RSA public key; false otherwise.

iapm.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.
      • type: string — Transaction product type, either inapp or subs.
      • state: number — The transaction state. One of the following
        • iapm.TRANS_STATE_FAILED
        • iapm.TRANS_STATE_PURCHASED
        • iapm.TRANS_STATE_PURCHASING
        • iapm.TRANS_STATE_RESTORED
        • iapm.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 — The Myket signature for the raw receipt. It has already passed local verification; verify it again on a trusted server before granting valuable content.
      • verified: boolean — true when the receipt signature passed local verification. Transactions with an invalid signature are rejected and delivered as an error instead.
    • 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
      • iapm.REASON_UNSPECIFIED
      • iapm.REASON_USER_CANCELED

Constants

iapm.REASON_UNSPECIFIED

unspecified error reason

iapm.REASON_USER_CANCELED

user canceled reason

iapm.TRANS_STATE_FAILED

transaction failed state

iapm.TRANS_STATE_PURCHASED

transaction purchased state

iapm.TRANS_STATE_PURCHASING

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

iapm.TRANS_STATE_RESTORED

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

iapm.TRANS_STATE_UNVERIFIED

transaction unverified state, requires verification of purchase

On this page