local QBCore = exports['qb-core']:GetCoreObject() local baseUrl = GetConvar('altura_rewards_api', '') local bridgeKey = GetConvar('altura_rewards_key', '') local pending = {} local function configured() return baseUrl ~= '' and bridgeKey ~= '' and baseUrl:match('^https://') end local function request(method, endpoint, payload, callback) if not configured() then if callback then callback(false, 'Configura altura_rewards_api HTTPS y altura_rewards_key en server.cfg') end return end PerformHttpRequest(baseUrl .. endpoint, function(status, body) local result = {} if body and body ~= '' then local ok, decoded = pcall(json.decode, body) if ok and type(decoded) == 'table' then result = decoded end end if callback then callback(status >= 200 and status < 300, result.error or result, status) end end, method, payload and json.encode(payload) or '', { ['Content-Type'] = 'application/json', ['X-Altura-Key'] = bridgeKey }) end RegisterCommand('vinculartwitch', function(source, args) if source <= 0 then return end local code = tostring(args[1] or ''):upper() if not code:match('^ALT%-%x%x%x%x%x%x%x%x$') then TriggerClientEvent('QBCore:Notify', source, 'Usa /vinculartwitch ALT-XXXXXXXX', 'error') return end local Player = QBCore.Functions.GetPlayer(source) if not Player or not Player.PlayerData or not Player.PlayerData.citizenid then return end local citizenid = tostring(Player.PlayerData.citizenid) local license = GetPlayerIdentifierByType(source, 'license') or '' request('POST', '/api/altura/verify-link', { code = code, citizenid = citizenid, license = license }, function(ok, response) if ok then TriggerClientEvent('QBCore:Notify', source, 'Tu personaje se vinculó con Twitch correctamente.', 'success') else local msg = type(response) == 'string' and response or 'No fue posible vincular la cuenta.' TriggerClientEvent('QBCore:Notify', source, msg, 'error') end end) end, false) local function refreshPending() request('GET', '/api/altura/pending', nil, function(ok, response) if ok and type(response) == 'table' and type(response.requests) == 'table' then pending = response.requests end end) end CreateThread(function() Wait(5000) if not configured() then print('[altura_twitch_rewards] Falta configuracion privada del puente. No se procesan solicitudes.') return end while true do refreshPending() Wait(60000) end end) -- Requiere un recurso autorizado que realice la entrega real antes de llamar al ACK. exports('GetApprovedRewards', function() return pending end) exports('ConfirmDelivery', function(redemptionId, deliveryReference, callback) if type(redemptionId) ~= 'string' or type(deliveryReference) ~= 'string' or #deliveryReference < 3 then if callback then callback(false, 'Debes incluir una referencia verificable de entrega.') end return end request('POST', '/api/altura/ack', { id = redemptionId, delivery_ref = deliveryReference }, function(ok, result) if ok then refreshPending() end if callback then callback(ok, result) end end) end) print('[altura_twitch_rewards] Vinculacion Twitch–citizenid habilitada. Entregas automáticas deshabilitadas.')