Skip to content

Commit

Permalink
Implement getPriceCurrencyImage (#24)
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
aglitchman authored Jun 19, 2024
1 parent 965673f commit 47609a7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ The best way to integrate SDK into your game is to read [the official documentat
| `ysdk.getPayments(options)` | `yagames.payments_init(options, callback)` |
| `payments.purchase(options)` | `yagames.payments_purchase(options, callback)` |
| `payments.getPurchases()` | `yagames.payments_get_purchases(callback)`<br>The result has the format `{ purchases = { ... }, signature = "..." }` |
| `payments.getCatalog()` | `yagames.payments_get_catalog(callback)` |
| `payments.getCatalog()` | `yagames.payments_get_catalog([options], callback)`<br>The argument `options` is an optional Lua table `{ getPriceCurrencyImage = "size" }`, where `size` (string) can be `medium`, `small` and `svg`, the currency image url will be injected to the `getPriceCurrencyImage` field of each product. |
| `payments.consumePurchase(purchaseToken)` | `yagames.payments_consume_purchase(purchase_token, callback)` |
| **Leaderboards** [(docs)](https://yandex.ru/dev/games/doc/en/sdk/sdk-leaderboard) | |
| `ysdk.getLeaderboards()` | `yagames.leaderboards_init(callback)` |
Expand Down
6 changes: 5 additions & 1 deletion example/ysdkdebug/pg_payments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ function M.init_handler(self)
end

function M.get_catalog_handler(self)
yagames.payments_get_catalog(function(self, err, catalog)
-- `options` is optional and can be `nil` or omitted.
local options = {
getPriceCurrencyImage = "medium"
}
yagames.payments_get_catalog(options, function(self, err, catalog)
print("yagames.payments_get_catalog:", err or table_util.tostring(catalog))
end)
end
Expand Down
6 changes: 5 additions & 1 deletion yagames/helpers/mock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,12 @@ function M.payments_get_purchases(cb_id)
M.send(cb_id, NO_ERR, rxi_json.encode(tmp))
end

function M.payments_get_catalog(cb_id)
function M.payments_get_catalog(cb_id, options)
assert(M._payments)
if type(options) == "string" then
options = rxi_json.decode(options)
-- do something...
end

M.send(cb_id, NO_ERR, rxi_json.encode(M._payments.catalog))
end
Expand Down
12 changes: 11 additions & 1 deletion yagames/lib/web/lib_yagames.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,22 @@ var LibYaGamesPrivate = {
}
},

YaGamesPrivate_Payments_GetCatalog: function (cb_id) {
YaGamesPrivate_Payments_GetCatalog: function (cb_id, coptions) {
var self = YaGamesPrivate;
try {
var options = coptions === 0 ? {} : self.parseJson(UTF8ToString(coptions));
self._payments
.getCatalog()
.then((products) => {
if (typeof options.getPriceCurrencyImage === "string") {
const newResults = [];
for (const product of products) {
const result = JSON.parse(JSON.stringify(product));
result.getPriceCurrencyImage = product.getPriceCurrencyImage(options.getPriceCurrencyImage);
newResults.push(result);
}
products = newResults;
}
self.send(cb_id, null, JSON.stringify(products));
})
.catch((err) => {
Expand Down
4 changes: 2 additions & 2 deletions yagames/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern "C"
void YaGamesPrivate_GetPayments(const int cb_id, const char* options);
void YaGamesPrivate_Payments_Purchase(const int cb_id, const char* options);
void YaGamesPrivate_Payments_GetPurchases(const int cb_id);
void YaGamesPrivate_Payments_GetCatalog(const int cb_id);
void YaGamesPrivate_Payments_GetCatalog(const int cb_id, const char* options);
void YaGamesPrivate_Payments_ConsumePurchase(const int cb_id, const char* purchase_token);
void YaGamesPrivate_GetPlayer(const int cb_id, const char* options);
const char* YaGamesPrivate_Player_GetPayingStatus();
Expand Down Expand Up @@ -541,7 +541,7 @@ static int Payments_GetPurchases(lua_State* L)

static int Payments_GetCatalog(lua_State* L)
{
YaGamesPrivate_Payments_GetCatalog(luaL_checkint(L, 1));
YaGamesPrivate_Payments_GetCatalog(luaL_checkint(L, 1), lua_isstring(L, 2) ? luaL_checkstring(L, 2) : 0);
return 0;
}

Expand Down
16 changes: 14 additions & 2 deletions yagames/yagames.lua
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,24 @@ function M.payments_get_purchases(callback)
end

--- Get a list of available purchases and their cost.
-- @tparam[opt] {getPriceCurrencyImage=string} options
-- @tparam function callback
function M.payments_get_catalog(callback)
function M.payments_get_catalog(options, callback)
assert(M.payments_ready, "Payments subsystem is not initialized.")
-- Backward compatibility
if type(options) == "function" and not callback then
callback = options
options = nil
end
assert(type(options) == "table" or type(options) == "nil", "`options` should be a table or nil.")
assert(type(callback) == "function")

yagames_private.payments_get_catalog(helper.wrap_for_promise(function(self, err, catalog)
if catalog then
catalog = rxi_json.decode(catalog)
end
callback(self, err, catalog)
end))
end), options and rxi_json.encode(options) or nil)
end

--- Consume an in-game purchase.
Expand Down Expand Up @@ -708,6 +715,7 @@ function M.flags_get(options, callback)
end), options and rxi_json.encode(options) or nil)
end

--- DEPRECATED
-- @tparam function callback
function M.banner_init(callback)
assert(type(callback) == "function")
Expand All @@ -721,6 +729,7 @@ function M.banner_init(callback)
end))
end

--- DEPRECATED
function M.banner_create(rtb_id, options, callback)
assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.")
assert(type(rtb_id) == "string")
Expand All @@ -735,13 +744,15 @@ function M.banner_create(rtb_id, options, callback)
end) or 0)
end

--- DEPRECATED
function M.banner_destroy(rtb_id)
assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.")
assert(type(rtb_id) == "string")

yagames_private.banner_destroy(rtb_id)
end

--- DEPRECATED
function M.banner_refresh(rtb_id, callback)
assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.")
assert(type(rtb_id) == "string")
Expand All @@ -755,6 +766,7 @@ function M.banner_refresh(rtb_id, callback)
end) or 0)
end

--- DEPRECATED
function M.banner_set(rtb_id, property, value)
assert(M.banner_ready, "Yandex Advertising Network SDK is not initialized.")
assert(type(rtb_id) == "string")
Expand Down

0 comments on commit 47609a7

Please sign in to comment.