{
  "name": "kalu → tu CRM (flujo base)",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "kalu",
        "options": {}
      },
      "name": "Webhook de kalu",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "5b0c7a52-6d8e-4a3e-9a0f-b1c2d3e4f5a6"
    },
    {
      "parameters": {
        "jsCode": "// Verifica que el envío viene de kalu. Pega tu secreto de firma\n// (Ajustes → Conectar con tu CRM) entre las comillas.\nconst SECRETO = 'whsec_PEGA_AQUI_TU_SECRETO';\nconst crypto = require('crypto');\n\nconst firma = String($json.headers['x-kalu-signature'] || '');\nconst partes = Object.fromEntries(firma.split(',').map((p) => p.split('=')));\nconst t = Number(partes.t);\nconst cuerpo = JSON.stringify($json.body);\nconst esperada = crypto.createHmac('sha256', SECRETO).update(`${t}.${cuerpo}`).digest('hex');\nconst reciente = Math.abs(Date.now() / 1000 - t) <= 300;\nconst valida =\n  reciente &&\n  typeof partes.v1 === 'string' &&\n  partes.v1.length === esperada.length &&\n  crypto.timingSafeEqual(Buffer.from(partes.v1), Buffer.from(esperada));\n\nreturn [{ json: { ...$json.body, firma_valida: valida } }];"
      },
      "name": "Verificar firma",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.firma_valida }}",
              "value2": true
            }
          ]
        }
      },
      "name": "¿Firma válida?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        480,
        0
      ]
    },
    {
      "parameters": {},
      "name": "Agrega aquí el nodo de tu CRM",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        720,
        -100
      ]
    },
    {
      "parameters": {},
      "name": "Descartar (firma inválida)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        720,
        100
      ]
    }
  ],
  "connections": {
    "Webhook de kalu": {
      "main": [
        [
          {
            "node": "Verificar firma",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verificar firma": {
      "main": [
        [
          {
            "node": "¿Firma válida?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "¿Firma válida?": {
      "main": [
        [
          {
            "node": "Agrega aquí el nodo de tu CRM",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Descartar (firma inválida)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
