local MarketplaceService = game:GetService("MarketplaceService")
local function Request_GetProductInfo(productId)
local success, errorOrProductInfo = pcall(function()
return MarketplaceService:GetProductInfo(productId, Enum.InfoType.Product)
end)
if not success then
print("Error: " .. errorOrProductInfo .. " / ProductId : " .. productId)
else
local productInfo = errorOrProductInfo
print("World Product Name: " .. tostring(productInfo.Name))
print("ProductId: " .. tostring(productInfo.ProductId))
print("ProductType: " .. tostring(productInfo.ProductType))
print("PriceInBLUC: " .. tostring(productInfo.PriceInBLUC))
print("Description: " .. tostring(productInfo.Description))
print("Created: " .. productInfo.Created)
print("Updated: " .. productInfo.Updated)
end
end
local MarketplaceService = game:GetService("MarketplaceService")
local function Request_GetWorldProductsAsync()
local success, errorOrWorldProducts = pcall(function()
return MarketplaceService:GetWorldProductsAsync()
end)
if not success then
print("Error: " .. errorOrWorldProducts)
else
local worldProducts = errorOrWorldProducts
local pageCount = 1
local dataList = {}
while true do
local currentPage = worldProducts:GetCurrentPage()
-- 마지막 페이지이면 루프 탈출
if worldProducts.IsFinished or currentPage == nil then
print(pageCount .. " page IsFinished : " .. tostring(worldProducts.IsFinished))
break
else
worldProducts:AdvanceToNextPageAsync()
pageCount = pageCount + 1
end
-- 한 페이지에 최대 100개의 상품 정보 구성
for _, productInfo in pairs(currentPage) do
local i = #dataList + 1
print("------ " .. i .. " ------")
print("World Product Name: " .. tostring(productInfo.Name))
print("ProductId: " .. tostring(productInfo.ProductId))
print("ProductType: " .. tostring(productInfo.ProductType))
print("PriceInBLUC: " .. tostring(productInfo.PriceInBLUC))
print("Description: " .. tostring(productInfo.Description))
print("Created: " .. productInfo.Created)
print("Updated: " .. productInfo.Updated)
table.insert(dataList, productInfo)
end
end
end
end
local MarketplaceService = game:GetService("MarketplaceService")
local function Request_PromptProductPurchase(player, productId)
local success, error = pcall(function()
MarketplaceService:PromptProductPurchase(player, productId)
end)
if not success then
print("Error: " .. error .. " / ProductId : " .. productId)
end
end
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local function OnPromptPurchaseFinished(userId, productId, isPurchased)
local player = Players:GetPlayerByUserId(userId)
print(player.Name .. " / ProductID : " .. productId .. " / isPurchased : " .. tostring(isPurchased))
end
MarketplaceService.PromptProductPurchaseFinished:Connect(OnPromptPurchaseFinished)
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ProductDeliverer = {}
-----------------------------------------------------------------------------
-- 아래와 같이 테이블에 상품 번호 별로 함수를 구성하여 상품마다 지급 로직을 구현할 수 있습니다.
ProductDeliverer[Enter the product number here.] = function(player)
local success, resultOrError = pcall(function()
-- player에게 상품 지급 및 DataStore를 이용한 저장 처리
-- Tip.
-- DataStore로 상품 정보를 저장할 때는
-- 네트워크 충돌이나 경쟁 상태(race condition)를 방지하기 위해
-- IncrementAsync 또는 UpdateAsync로 처리하는 것을 권장합니다.
-- 지급 및 저장 처리가 성공적으로 완료된 경우 true 반환
return true
end)
if success and resultOrError then
return true
else
return false, resultOrError
end
end
-----------------------------------------------------------------------------
-- 상품 구매 성공시 호출되는 영수증 처리용 콜백
local function OnProcessReceipt(receiptInfo)
-- Receipt information
local success, error = pcall(function()
print("PurchaseId: " .. receiptInfo.PurchaseId)
print("UserId: " .. receiptInfo.PlayerId)
print("ProductId: " .. receiptInfo.ProductId)
print("CurrencySpent: " .. receiptInfo.CurrencySpent)
print("PurchaseDateTime: " .. receiptInfo.PurchaseDateTime)
end)
if not success then
print("Error: " .. tostring(error))
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- 플레이어가 유효하면
local productId = receiptInfo.ProductId
local userId = receiptInfo.PlayerId
local player = Players:GetPlayerByUserId(userId)
if player == nil then
print("Error: player is nil")
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- 상품 지급 함수 호출
local delivererFunc = ProductDeliverer[productId]
local success, error = delivererFunc(player)
-- 상품 지급 성공시
if success then
-- 지급 완료 상태 반환
print("Item delivery successful / ProductId : " .. productId)
return Enum.ProductPurchaseDecision.PurchaseGranted
-- 상품 지급 실패시
else
print("Error: " .. tostring(error))
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
MarketplaceService.ProcessReceipt = OnProcessReceipt