Developer Product Purchase

I did a :PromptPurchase for a developer product. How do I find out if they bought it or just canceled it?

1 Like

Did you search before? If you google it, you can find this: ProcessReceipt, a CallBack function of MarketplaceService.

When a player buys a Developer Product, the CallBack will be called.


local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
 
-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
 
-- Table setup containing product IDs and functions for handling purchases
local productFunctions = {}
-- ProductId 123123 for a full heal
productFunctions[123123] = function(receipt, player)
	-- Logic/code for player buying a full heal (may vary)
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		-- Heal the player to full health
		player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
		-- Indicate a successful purchase
		return true
	end
end
-- ProductId 456456 for 100 gold
productFunctions[456456] = function(receipt, player)
	-- Logic/code for player buying 100 gold (may vary)
	local stats = player:FindFirstChild("leaderstats")
	local gold = stats and stats:FindFirstChild("Gold")
	if gold then
		gold.Value = gold.Value + 100
		-- Indicate a successful purchase
		return true
	end
end
 
-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)
 
	-- Determine if the product was already granted by checking the data store  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)
	-- If purchase was recorded, the product was already granted
	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end
 
	-- 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
	
	-- Look up handler function from 'productFunctions' table above
	local handler = productFunctions[receiptInfo.ProductId]
 
	-- Call the handler function and catch any errors
	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
 
	-- Record transaction in data store so it isn't granted again
	local success, errorMessage = pcall(function()
		purchaseHistoryStore:SetAsync(playerProductKey, true)
	end)
	if not success then
		error("Cannot save purchase data: " .. errorMessage)
	end
 
	-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
	return Enum.ProductPurchaseDecision.PurchaseGranted
end
 
-- Set the callback; this can only be done once by one script on the server! 
MarketplaceService.ProcessReceipt = processReceipt

This is a example of it.

10 Likes

Hey,

to prompt a developer product purchase you have to use :PromptProductPurchase().

If you want to know if they bought it or canceled it you gotta use .ProcessReceipt.

1 Like

All the above answers are actually wrong. ProcessReceipt is what you use when you want to handle a purchased Developer Product. PromptProductPurchaseFinished is what you use when you want to determine if the user clicked Buy or Cancel, using the third parameter wasPurchased.

cc @TheTurtleMaster_2 @Sougood

7 Likes

ProcessReceipt is called when the player buys a Developer Product.

1 Like

ProcessReceipt is called when a successful purchase is made and is used to handle a product purchase. My response isn’t implying anything else, read what I’ve said again and compare it to the OP. They want to know if a purchase was bought or cancelled. ProcessReceipt doesn’t do that.

3 Likes

It says deprecated. That means I shouldnt use it

1 Like

Deprecated means there is something better to use, you can still use it.

1 Like

@PirateDevz PromptProductPurchaseFinished is only deprecated to discourage developers from using it to grant purchases instead of ProcessReceipt, which has happened and still does happen sometimes. The event isn’t actually deprecated and you need to use it. There’s no other way to do this.

@TheTurtleMaster_2 Deprecated also means that a feature can, at any time with or without notice, stop functioning or be completely removed. In almost all cases of a deprecation, yeah, you’ll want to go for what superseded the deprecated item. For PromptProductPurchaseFinished, it’s just there for the sake of discouraging its misuse.

5 Likes