-- Table setup containing product IDs and functions for handling purchases

local productFunctions = {
	[1233952204] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 5
			return true
		end
	end;
	
	[1233952208] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 10
			return true
		end
	end;
	
	[1233952212] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 50
			return true
		end
	end;
	
	[1233955620] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 100
			return true
		end
	end;
	
	[1233955602] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 500
			return true
		end
	end;
	
	[1233955632] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 1000
			return true
		end
	end;
	
	[1233955638] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 5000
			return true
		end
	end;
	
	[1233955645] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 10000
			return true
		end
	end;
	
	[1233955664] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 50000
			return true
		end
	end;
	
	[1233955679] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 100000
			return true
		end
	end;
	
	[1233955695] = function(receipt, player)
		local stats = player:FindFirstChild("DonationValue")
		local donation = stats
		if donation then
			donation.Value = donation.Value + 500000
			return true
		end
	end;
}


MarketplaceService.ProcessReceipt = function(receiptInfo)
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)

	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end


	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end


	local handler = productFunctions[receiptInfo.ProductId]

	local success, result = pcall(handler, receiptInfo, player)
	if not success or not result then
		warn("Error occurred while processing a product purchase")
		print("\nProductId:", receiptInfo.ProductId)
		print("\nPlayer:", player)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end


	local success, errorMessage = pcall(function()
		purchaseHistoryStore:SetAsync(playerProductKey, true)
	end)
	if not success then
		error("Cannot save purchase data: " .. errorMessage)
	end


	return Enum.ProductPurchaseDecision.PurchaseGranted
end
