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)- Pick a release from itzmiadm/Myket-IAP-Defold releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as iapm from "iapm"
- Pick a release from itzmiadm/Myket-IAP-Defold releases and add its Source code (zip) URL (or a packaged
- 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")
endParameters
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")
endParameters
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)
endParameters
ids:table— table (array) of identifiers to get products fromcallback:function— result callback taking the following parametersself: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, eitherinapporsubs.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.nilif 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 istrueif current store supports handling restored transactions, otherwisefalse.
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, eitherinapporsubs.state:number— The transaction state. One of the followingiapm.TRANS_STATE_FAILEDiapm.TRANS_STATE_PURCHASEDiapm.TRANS_STATE_PURCHASINGiapm.TRANS_STATE_RESTOREDiapm.TRANS_STATE_UNVERIFIED
date:string— The date and time for the transaction.trans_ident:string— The transaction identifier. This field is only set whenstateisTRANS_STATE_RESTORED,TRANS_STATE_UNVERIFIEDorTRANS_STATE_PURCHASED.receipt:string— The transaction receipt. This field is only set whenstateisTRANS_STATE_PURCHASEDorTRANS_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.nilif there is no error.error- the error message.reason- the reason for the error, value can be one of the following constantsiapm.REASON_UNSPECIFIEDiapm.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