{
  "name": "EuQuero - PIX Payment Confirmation",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "euquero",
        "authentication": "headerAuth",
        "options": {}
      },
      "id": "webhook-pix",
      "name": "Webhook PIX",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Validar se \u00e9 evento PIX_PAYMENT_CONFIRMED\nconst data = items[0].json;\n\nif (data.event !== 'PIX_PAYMENT_CONFIRMED') {\n  throw new Error('Evento inv\u00e1lido: ' + data.event);\n}\n\n// Validar campos obrigat\u00f3rios\nif (!data.user_id || !data.payment_id || !data.amount) {\n  throw new Error('Dados obrigat\u00f3rios ausentes');\n}\n\n// Formatar dados para o pr\u00f3ximo n\u00f3\nreturn [{\n  json: {\n    user_id: data.user_id,\n    payment_id: data.payment_id,\n    amount: data.amount,\n    timestamp: data.timestamp,\n    formatted_amount: `R$ ${data.amount.toFixed(2).replace('.', ',')}`,\n    formatted_date: new Date(data.timestamp).toLocaleString('pt-BR')\n  }\n}];"
      },
      "id": "validate-data",
      "name": "Validar Dados",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.user_id }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "check-user",
      "name": "Verificar Usu\u00e1rio",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Simular busca de dados do usu\u00e1rio\n// Em produ\u00e7\u00e3o, conectar com banco de dados real\n\nconst userId = items[0].json.user_id;\n\n// Mock de dados do usu\u00e1rio\nconst mockUsers = {\n  'user_123': {\n    name: 'Jo\u00e3o Silva',\n    email: 'joao@exemplo.com',\n    phone: '(11) 99999-9999'\n  },\n  'user_456': {\n    name: 'Maria Santos',\n    email: 'maria@exemplo.com', \n    phone: '(11) 88888-8888'\n  }\n};\n\nconst userData = mockUsers[userId] || {\n  name: 'Cliente',\n  email: 'cliente@exemplo.com',\n  phone: '(11) 00000-0000'\n};\n\nreturn [{\n  json: {\n    ...items[0].json,\n    user_name: userData.name,\n    user_email: userData.email,\n    user_phone: userData.phone\n  }\n}];"
      },
      "id": "get-user-data",
      "name": "Buscar Dados Usu\u00e1rio",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "fromEmail": "noreply@euquero.com.br",
        "toEmail": "={{ $json.user_email }}",
        "subject": "Pagamento PIX Confirmado \u2705 - EuQuero",
        "text": "Ol\u00e1 {{ $json.user_name }},\n\nConfirmamos o recebimento do seu pagamento PIX!\n\n\ud83d\udccb Detalhes do Pagamento:\n\u2022 Valor: {{ $json.formatted_amount }}\n\u2022 ID do Pagamento: {{ $json.payment_id }}\n\u2022 Data: {{ $json.formatted_date }}\n\n\u2705 Seu saldo j\u00e1 foi atualizado no painel.\n\nPara acessar sua conta, visite: https://euquero.com.br/dashboard\n\nSe voc\u00ea tem alguma d\u00favida, entre em contato conosco:\n\ud83d\udce7 Email: contato@fcaq.com.br\n\ud83d\udcac Telegram: https://t.me/+9cdym9gvPQ9iOWNh\n\ud83d\udcf1 WhatsApp: +55 88 988712711\n\nAtenciosamente,\nEquipe EuQuero\n\n---\nEste \u00e9 um email autom\u00e1tico, n\u00e3o responda a esta mensagem.",
        "options": {
          "allowUnauthorizedCerts": true
        }
      },
      "id": "send-email",
      "name": "Enviar Email Confirma\u00e7\u00e3o",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1120,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "// Log de sucesso\nconst data = items[0].json;\n\nconsole.log('\u2705 Email de confirma\u00e7\u00e3o PIX enviado:', {\n  user_id: data.user_id,\n  user_email: data.user_email,\n  payment_id: data.payment_id,\n  amount: data.amount,\n  timestamp: new Date().toISOString()\n});\n\nreturn [{\n  json: {\n    status: 'success',\n    message: 'Email enviado com sucesso',\n    user_id: data.user_id,\n    payment_id: data.payment_id,\n    processed_at: new Date().toISOString()\n  }\n}];"
      },
      "id": "log-success",
      "name": "Log Sucesso",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1340,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "// Log de erro - usu\u00e1rio n\u00e3o encontrado\nconst data = items[0].json;\n\nconsole.error('\u274c Usu\u00e1rio n\u00e3o encontrado para PIX:', {\n  user_id: data.user_id,\n  payment_id: data.payment_id,\n  amount: data.amount,\n  timestamp: new Date().toISOString()\n});\n\nreturn [{\n  json: {\n    status: 'error',\n    message: 'Usu\u00e1rio n\u00e3o encontrado',\n    user_id: data.user_id,\n    payment_id: data.payment_id,\n    error_at: new Date().toISOString()\n  }\n}];"
      },
      "id": "log-error",
      "name": "Log Erro",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        900,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "// Resposta final do webhook\nreturn [{\n  json: {\n    received: true,\n    processed_at: new Date().toISOString(),\n    message: 'Webhook PIX processado com sucesso'\n  }\n}];"
      },
      "id": "webhook-response",
      "name": "Resposta Webhook",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        500
      ]
    }
  ],
  "connections": {
    "Webhook PIX": {
      "main": [
        [
          {
            "node": "Validar Dados",
            "type": "main",
            "index": 0
          },
          {
            "node": "Resposta Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validar Dados": {
      "main": [
        [
          {
            "node": "Verificar Usu\u00e1rio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verificar Usu\u00e1rio": {
      "main": [
        [
          {
            "node": "Buscar Dados Usu\u00e1rio",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log Erro",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar Dados Usu\u00e1rio": {
      "main": [
        [
          {
            "node": "Enviar Email Confirma\u00e7\u00e3o",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar Email Confirma\u00e7\u00e3o": {
      "main": [
        [
          {
            "node": "Log Sucesso",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "createdAt": "2025-01-09T17:00:00.000Z",
      "updatedAt": "2025-01-09T17:00:00.000Z",
      "id": "pix-webhook",
      "name": "PIX Webhook"
    }
  ],
  "triggerCount": 1,
  "updatedAt": "2025-01-09T17:00:00.000Z",
  "versionId": "1"
}