--!strict -- 개발자 상품 결제 처리: 결제 한 번에 정확히 한 번만 지급합니다(DECISIONS.md 2절). -- 로블록스 API에 직접 기대지 않게 프로필과 지급 함수를 받아서, lune 단위 테스트로도 검증합니다. local Receipts = {} Receipts.CACHE_SIZE = 100 -- Enum.ProductPurchaseDecision 대신 문자열을 돌려주고, 호출하는 쪽에서 Enum으로 바꿉니다. export type Decision = "Granted" | "NotProcessedYet" export type ProfileLike = { Data: { PurchaseIdCache: { string }? }, LastSavedData: { PurchaseIdCache: { string }? }, IsActive: (self: any) -> boolean, Save: (self: any) -> (), OnAfterSave: { Wait: (self: any) -> () }, } local function isSaved(profile: ProfileLike, purchaseId: string): boolean local saved = profile.LastSavedData and profile.LastSavedData.PurchaseIdCache return saved ~= nil and table.find(saved, purchaseId) ~= nil end -- profile이 nil이면(아직 로드 안 됨, 이미 나감) 나중에 다시 시도하게 합니다. function Receipts.process(profile: ProfileLike?, purchaseId: string, grant: () -> boolean): Decision if profile == nil or not profile:IsActive() then return "NotProcessedYet" end local cache = profile.Data.PurchaseIdCache if cache == nil then return "NotProcessedYet" end if table.find(cache, purchaseId) == nil then local ok, granted = pcall(grant) if not ok or granted ~= true then if not ok then warn("[Bunsik] 상품 지급 오류:", granted) end return "NotProcessedYet" end while #cache >= Receipts.CACHE_SIZE do table.remove(cache, 1) end table.insert(cache, purchaseId) end -- 지급과 PurchaseId가 같이 저장된 것을 확인한 뒤에만 완료로 알립니다. if isSaved(profile, purchaseId) then return "Granted" end while profile:IsActive() do local last = profile.LastSavedData profile:Save() if profile.LastSavedData == last then profile.OnAfterSave:Wait() end if isSaved(profile, purchaseId) then return "Granted" end end return "NotProcessedYet" end return Receipts