I was just wondering if there was a ‘best way’ to use ProcessReceipt. Whether or not I should be storing purchases through a data store, etc.
This is what I’ve kinda always used
local ReceiptProcessor = {}
local DataStoreService = game:GetService('DataStoreService')
local MarketplaceService = game:GetService('MarketplaceService')
local Players = game:GetService('Players')
local PurchaseHistory = DataStoreService:GetDataStore('PurchaseHistory', 'In-Demo')
local Products = {
[123456789] = function(receipt, player)
print('Product purchased!')
return true
end
}
function MarketplaceService.ProcessReceipt(receiptInfo)
local PlayerProductKey = receiptInfo.PlayerId .. ':' .. receiptInfo.PurchaseId
if PurchaseHistory:GetAsync(PlayerProductKey) then return Enum.ProductPurchaseDecision.PurchaseGranted end
local Player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not Player then return Enum.ProductPurchaseDecision.NotProcessedYet end
local Handler
for productId, v in pairs(Products) do
if productId == receiptInfo.ProductId then
Handler = v
break
end
end
if not Handler then return Enum.ProductPurchaseDecision.PurchaseGranted end
local Success, Error = pcall(Handler, receiptInfo, Player)
if not Success then
warn('An error occured while processing a product purchase')
print('\t ProductId:', receiptInfo.ProductId)
print('\t Player:', Player)
print('\t Error message:', Error)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if not Error then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
Success, Error = pcall(function()
PurchaseHistory:SetAsync(PlayerProductKey, true)
end)
if not Success then
print('An error occured while saving a product purchase')
print('\t ProductId:', receiptInfo.ProductId)
print('\t Player:', Player)
print('\t Error message:', Error)
print('\t Handler worked fine, purchase granted')
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
return ReceiptProcessor
Is there something here that I don’t need? Or is this fine?