// JavaScript con fetch. Estas funciones no ejecutan operaciones al cargarse. export function imaCashClient(token) { async function request(path, body, stableKey) { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 10000); const headers = {'x-auth-token': token, 'Accept': 'application/json'}; if (body !== undefined) headers['Content-Type'] = 'application/json'; if (stableKey) headers['Idempotency-Key'] = stableKey; try { const response = await fetch('https://restapi.imaginaits.com/' + path, { method: body === undefined ? 'GET' : 'POST', headers, body: body === undefined ? undefined : JSON.stringify(body), signal: controller.signal, redirect: 'error', credentials: 'omit' }); const result = await response.json(); if (!response.ok || result.status !== 'ok') { throw new Error(result.error?.code || ('HTTP ' + response.status)); } // 202 o status=ok no significan confirmed=true. return result; } finally { clearTimeout(timeout); } } return { capabilities: () => request('v2.php?action=capabilities'), cancel: (transactionId, stableKey) => request('canceltransactionv2.php', {transaction_id: transactionId}, stableKey), operation: operationId => request('v2.php?action=operation&operation_id=' + encodeURIComponent(operationId)) }; } // Guarda operation_id; no cierres el cobro como cancelado hasta confirmed=true. // Tras timeout consulta la referencia o reenvia exactamente la misma clave V2.