local A=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local p=A:WaitForChild("\077\111\100\117\108\101\115")local f=game:GetService("\080\108\097\121\101\114\115")local R=require(p.src.utils.Logger)local u=require(p.src.GameDatastore)local K=game:GetService("\077\097\114\107\101\116\112\108\097\099\101\083\101\114\118\105\099\101")local d=966181357 local i=1086783078 local g=1086783194 local S=1086783346 local T={COINS_1=d,COINS_2=i,COINS_3=g;COINS_4=S}local J={[d]=1000,[i]=10000;[g]=100000;[S]=1000000}local c={}c[d]=function(A,p)u:incrementCoins(p,J[d])return true end c[i]=function(A,p)u:incrementCoins(p,J[i])return true end c[g]=function(A,p)u:incrementCoins(p,J[g])return true end c[S]=function(A,p)u:incrementCoins(p,J[S])return true end function T.promptPurchase(p,A)local R=f.LocalPlayer K:PromptProductPurchase(R,A)end function T.getProduct(p,A)local f=K:GetProductInfo(A,Enum.InfoType.Product)return{name=f.Name,description=f.Description,price=f.PriceInRobux;isDeveloperProduct=true}end function T.processReceipt(A)local p=f:GetPlayerByUserId(A.PlayerId)if not p then return Enum.ProductPurchaseDecision.NotProcessedYet end local K=u:hasProductPurchased(p,A.PurchaseId)if K then R:d("\065\108\114\101\097\100\121 \112\117\114\099\104\097\115\101\100 \112\114\111\100\117\099\116 "..tostring(A.PurchaseId))return Enum.ProductPurchaseDecision.PurchaseGranted end local d=c[A.ProductId]if not d then R:e("\078\111 \112\114\111\100\117\099\116 \104\097\110\100\108\101\114 \102\111\114 "..tostring(A.ProductId))return Enum.ProductPurchaseDecision.NotProcessedYet end local i,g=pcall(d,A,p)if not i or not g then R:w("\069\114\114\111\114 \111\099\099\117\114\114\101\100 \119\104\105\108\101 \112\114\111\099\101\115\115\105\110\103 \097 \112\114\111\100\117\099\116 \112\117\114\099\104\097\115\101\046 \080\114\111\100\117\099\116\073\100\058"..(tostring(A.ProductId)..("\046 \080\108\097\121\101\114\058"..tostring(p.UserId))),g)return Enum.ProductPurchaseDecision.NotProcessedYet end u:setProductPurchased(p,A.PurchaseId)return Enum.ProductPurchaseDecision.PurchaseGranted end return T
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Modules = ReplicatedStorage:WaitForChild('Modules')
local Players = game:GetService('Players')
local logger = require(Modules.src.utils.Logger)

local GameDatastore = require(Modules.src.GameDatastore)
local MarketplaceService = game:GetService('MarketplaceService')

local COINS_1 = 966181357
local COINS_2 = 1086783078
local COINS_3 = 1086783194
local COINS_4 = 1086783346

local DeveloperProducts = {
	COINS_1 = COINS_1,
	COINS_2 = COINS_2,
	COINS_3 = COINS_3,
	COINS_4 = COINS_4,
}

local rewards = {
	[COINS_1] = 1000,
	[COINS_2] = 10000,
	[COINS_3] = 100000,
	[COINS_4] = 1000000,
}

local productFunctions = {}

productFunctions[COINS_1] = function(receipt, player)
	GameDatastore:incrementCoins(player, rewards[COINS_1])
	return true
end
productFunctions[COINS_2] = function(receipt, player)
	GameDatastore:incrementCoins(player, rewards[COINS_2])
	return true
end
productFunctions[COINS_3] = function(receipt, player)
	GameDatastore:incrementCoins(player, rewards[COINS_3])
	return true
end
productFunctions[COINS_4] = function(receipt, player)
	GameDatastore:incrementCoins(player, rewards[COINS_4])
	return true
end

function DeveloperProducts:promptPurchase(productId)
	local player = Players.LocalPlayer

	MarketplaceService:PromptProductPurchase(player, productId)
end

function DeveloperProducts:getProduct(productId)
	local product = MarketplaceService:GetProductInfo(productId, Enum.InfoType.Product)

	return {
		name = product.Name,
		description = product.Description,
		price = product.PriceInRobux,
		isDeveloperProduct = true,
	}
end

function DeveloperProducts.processReceipt(receiptInfo)
	-- Find the player who made the purchase in the server
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		-- The player probably left the game
		-- If they come back, the callback will be called again
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	-- Determine if the product was already granted by checking the data store

	local purchased = GameDatastore:hasProductPurchased(player, receiptInfo.PurchaseId)

	if purchased then
		logger:d('Already purchased product ' .. tostring(receiptInfo.PurchaseId))
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	local applyProductHandler = productFunctions[receiptInfo.ProductId]

	if not applyProductHandler then
		logger:e('No product handler for ' .. tostring(receiptInfo.ProductId))
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	-- Call the handler function and catch any errors
	local success, result = pcall(applyProductHandler, receiptInfo, player)
	if not success or not result then
		logger:w(
			'Error occurred while processing a product purchase. ProductId:' .. tostring(
				receiptInfo.ProductId
			) .. '. Player:' .. tostring(player.UserId),
			result
		)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	-- Record transaction in data store so it isn't granted again
	GameDatastore:setProductPurchased(player, receiptInfo.PurchaseId)

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

return DeveloperProducts