Skip to content

Instantly share code, notes, and snippets.

@AGulev
Last active Dec 14, 2021
Embed
What would you like to do?
local cfg = require "modules.onet.config"
local msgs = require "modules.msgs"
local M = {}
local list = {}
local callback_function
local function iap_listener(self, transaction, error)
if error == nil then
if transaction.state == iap.TRANS_STATE_PURCHASED or transaction.state == iap.TRANS_STATE_RESTORED then
callback_function(self, transaction.ident)
if iap.get_provider_id() == iap.PROVIDER_ID_APPLE or cfg.IAP_CONSUMABLE[transaction.ident] then
iap.finish(transaction)
else
iap.acknowledge(transaction)
end
msg.post(msgs.P_S_SPINER, msgs.C_HIDE)
elseif transaction.state ~= iap.TRANS_STATE_PURCHASING then
msg.post(msgs.P_S_SPINER, msgs.C_HIDE)
end
else
msg.post(msgs.P_S_SPINER, msgs.C_HIDE)
end
end
local function check_no_connection(show_after)
if sys.get_connectivity() == sys.NETWORK_DISCONNECTED then
msg.post(msgs.P_S_ENTRY_POINT_PATH, msgs.C_NO_CONNECTION, show_after or {})
return true
end
end
function M.buy(self, id, callback, show_after)
if check_no_connection(show_after) then
return
end
if not iap then
callback(self, id)
return
end
if not next(list) then
M.recieve_list(cfg.IAP_IDS)
end
msg.post(msgs.P_S_SPINER, msgs.C_SHOW)
iap.set_listener(iap_listener)
callback_function = callback
iap.buy(id)
end
-- I call it when use tap button in settings screen
function M.restore_purchases(callback, show_after)
if check_no_connection(show_after) then
if callback then
callback()
end
return
end
if iap then
callback_function = callback
iap.set_listener(iap_listener)
iap.restore()
else
if callback then
callback()
end
end
end
local function list_callback(self, products, error)
if error == nil then
list = products
else
print(error.error)
end
end
-- I call it in the very first init()
function M.recieve_list(callback)
if iap then
callback_function = callback
iap.set_listener(iap_listener)
iap.list(cfg.IAP_IDS, list_callback)
end
end
function M.get_price(iap_id)
local price
if iap and list[iap_id] then
price = list[iap_id].price_string
else
price = cfg.IAP_PRICES[iap_id]
end
return price
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment