/
iaps.lua
367 lines (282 loc) · 8.16 KB
/
iaps.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
--- Eva IAPs module
-- Can process all in-apps in your application
-- @submodule eva
local app = require("eva.app")
local log = require("eva.log")
local luax = require("eva.luax")
local const = require("eva.const")
local fun = require("eva.libs.fun")
local db = require("eva.modules.db")
local game = require("eva.modules.game")
local saver = require("eva.modules.saver")
local proto = require("eva.modules.proto")
local token = require("eva.modules.token")
local device = require("eva.modules.device")
local events = require("eva.modules.events")
local wallet = require("eva.modules.wallet")
local logger = log.get_logger("eva.iaps")
local M = {}
local function get_iaps_config()
local settings = app.settings.iaps
local is_ios = device.is_ios()
local config_name = is_ios and settings.config_ios or settings.config_android
local config = db.get(config_name)
return config and config.iaps or {}
end
local function load_config()
app.iap_products = {}
local iaps = get_iaps_config()
for iap_id, info in pairs(iaps) do
app.iap_products[iap_id] = {
is_available = false,
category = info.category,
price = info.price,
ident = info.ident,
reward_id = info.token_group_id,
price_string = info.price .. " $",
currency_code = "USD",
title = "",
description = "",
}
end
end
local function get_id_by_ident(ident)
for iap_id, value in pairs(app.iap_products) do
if value.ident == ident then
return iap_id
end
end
return false
end
local function list_callback(self, products, error)
if not error then
local products_string = table.concat(luax.table.list(products, "ident"), ", ")
logger:info("Get product list", { products = products_string })
for k, v in pairs(products) do
local iap_id = get_id_by_ident(v.ident)
local iap_info = app.iap_products[iap_id]
iap_info.is_available = true
iap_info.currency_code = v.currency_code
iap_info.title = v.title
iap_info.description = v.description
iap_info.price_string = v.price_string
iap_info.price = v.price
end
else
logger:warn("IAP update products error", { error = error })
end
events.event(const.EVENT.IAP_UPDATE)
end
local function fake_transaction(iap_id, state)
return {
ident = iap_id,
state = state or const.IAP.STATE.PURCHASED,
date = game.get_current_time_string(),
trans_ident = device.get_uuid(),
is_fake = true,
}
end
local function get_fake_products()
local products = {}
for iap_id, value in pairs(app.iap_products) do
products[value.ident] = {
currency_code = "USD",
ident = value.ident,
title = "Fake title",
description = "Fake description",
price_string = "$" .. value.price,
price = value.price
}
end
return products
end
local function save_iap(iap_id, transaction)
local purchased = app[const.EVA.IAPS].purchased_iaps
local iap_data = proto.get(const.EVA.IAP_INFO)
iap_data.transaction_id = transaction.trans_ident
iap_data.ident = transaction.ident
iap_data.iap_id = iap_id
iap_data.state = transaction.state
iap_data.date = transaction.date
table.insert(purchased, iap_data)
end
local function consume(iap_id, transaction)
local item = M.get_iap(iap_id)
if not item then
logger:error("The iap_id is not exist", { iap_id = iap_id })
return
end
if item.reward_id then
wallet.add_group(item.reward_id, const.REASON.IAP)
end
save_iap(iap_id, transaction)
if iap and (not item.forever or device.is_ios()) then
iap.finish(transaction)
end
events.event(const.EVENT.IAP_PURCHASE, { iap_id = iap_id, ident = item.ident })
end
local function iap_listener(self, transaction, error)
if not error then
if luax.math.is(transaction.state, const.IAP.STATE.PURCHASED, const.IAP.STATE.RESTORED) then
local iap_id = get_id_by_ident(transaction.ident)
consume(iap_id, transaction)
end
else
if error.reason == const.IAP.REASON.CANCELED then
local ident = transaction and transaction.ident or "n/a"
local iap_id = get_id_by_ident(ident)
events.event(const.EVENT.IAP_CANCEL, { ident = ident, iap_id = iap_id })
else
logger:warn("Error while IAP processing", transaction)
end
end
end
--- Buy the inapp
-- @function eva.iaps.buy
-- @tparam string iap_id In-game inapp ID from iaps settings
function M.buy(iap_id)
local products = app.iap_products
local item = products[iap_id]
if not item then
logger:error("The IAP is not exist", { iap_id = iap_id })
return
end
events.event(const.EVENT.IAP_START, { ident = item.ident, iap_id = iap_id })
if iap then
logger:info("Start process IAP", { iap_id = iap_id })
iap.buy(item.ident)
else
M.test_buy_with_fake_state(iap_id, const.IAP.STATE.PURCHASED)
end
return true
end
function M.test_buy_with_fake_state(iap_id, state, is_canceled)
local products = app.iap_products
local item = products[iap_id]
if not item then
logger:error("The IAP is not exist", { iap_id = iap_id })
return
end
logger:info("No IAP module, fake free transaction", { iap_id = iap_id })
local ident = products[iap_id].ident
local iap_error = nil
if is_canceled then
iap_error = {
reason = const.IAP.REASON.CANCELED
}
end
iap_listener(nil, fake_transaction(ident, state), iap_error)
return true
end
--- Get reward from iap_id
-- @function eva.iaps.get_reward
-- @tparam string iap_id the inapp id
function M.get_reward(iap_id)
local iap_data = get_iaps_config()[iap_id]
if not iap_data then
logger:error("No iap with id", { iap_id = iap_id })
end
return token.get_token_group(iap_data.token_group_id)
end
--- Get iap info by iap_id
-- @function eva.iaps.get_iap
-- @tparam string iap_id the inapp id
function M.get_iap(iap_id)
return app.iap_products[iap_id]
end
--- Get all iaps. Can be selected by category
-- @function eva.iaps.get_iaps
-- @tparam string category Category of iap
-- @treturn list of iap products
function M.get_iaps(category)
if not category then
return app.iap_products
else
return fun.filter(function(id, iap_data)
return iap_data.category == category
end, app.iap_products):tomap()
end
end
--- Check is iap is available
-- @function eva.iaps.is_available
-- @tparam string iap_id the inapp id
-- @treturn bool Is available
function M.is_available(iap_id)
local data = M.get_iap(iap_id)
if not data then
logger:warn("No iap with id to check available", { iap_id = iap_id })
return false
end
return data.is_available
end
--- Get price from iap_id
-- @function eva.iaps.get_price
-- @tparam string iap_id the inapp id
-- @treturn number Price of iap
function M.get_price(iap_id)
return app.iap_products[iap_id].price
end
--- Get price_string from iap_id
-- @function eva.iaps.get_price_string
-- @tparam string iap_id the inapp id
-- @treturn string The iap price string
function M.get_price_string(iap_id)
return app.iap_products[iap_id].price_string
end
--- Refresh iap list.
-- It make request to stores to get new info
-- Throw EVENT.IAP_UPDATE at end
-- @function eva.iaps.refresh_iap_list
function M.refresh_iap_list()
if not iap or device.is_web() then
logger:debug("No iap on current platform. Fake iap module")
list_callback(nil, get_fake_products())
return
end
iap.set_listener(iap_listener)
iap.list(luax.table.list(app.iap_products, "ident"), list_callback)
end
--- Get total lifetime value (from iaps)
-- @function eva.iaps.get_ltv
-- @treturn number Player's LTV
function M.get_ltv()
if not app[const.EVA.IAPS] then
return 0
end
local ltv = 0
local purchased = app[const.EVA.IAPS].purchased_iaps
for i = 1, #purchased do
local info = purchased[i]
local price = M.get_price(info.iap_id) or 0
ltv = ltv + price
end
return ltv
end
--- Get player max payment
-- @function eva.iaps.get_max_payment
-- @treturn number Max player payment
function M.get_max_payment()
if not app[const.EVA.IAPS] then
return 0
end
local max_pay = 0
local purchased = app[const.EVA.IAPS].purchased_iaps
for i = 1, #purchased do
local info = purchased[i]
local price = M.get_price(info.iap_id) or 0
max_pay = math.max(max_pay, price)
end
return max_pay
end
function M.before_eva_init()
app.iap_products = {}
end
function M.on_eva_init()
app[const.EVA.IAPS] = proto.get(const.EVA.IAPS)
saver.add_save_part(const.EVA.IAPS, app[const.EVA.IAPS])
load_config()
end
function M.after_eva_init()
M.refresh_iap_list()
end
return M