{
  "name": "Stripe to ZUGFeRD E-Rechnung Generator & Email Dispatch",
  "nodes": [
    {
      "parameters": {
        "event": "checkout.session.completed",
        "options": {}
      },
      "id": "node-stripe-trigger",
      "name": "Stripe Trigger: Checkout abgeschlossen",
      "type": "n8n-nodes-base.stripeTrigger",
      "typeVersion": 1,
      "position": [
        180,
        240
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "generate-zugferd-invoice",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "node-webhook-trigger",
      "name": "Webhook: Manuell / Externer Aufruf",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        180,
        460
      ],
      "webhookId": "generate-zugferd-invoice"
    },
    {
      "parameters": {
        "jsCode": "// Format invoice data according to European Standard EN 16931 (ZUGFeRD 2.2 / Factur-X)\nconst item = $input.item.json;\nconst now = new Date();\nconst todayStr = now.toISOString().split('T')[0];\n\nconst customerEmail = item.customer_details?.email || item.buyer_email || 'kunde@beispiel.de';\nconst customerName = item.customer_details?.name || item.buyer_name || 'Max Mustermann';\nconst invoiceNum = item.invoice_number || 'INV-' + now.getFullYear() + '-' + Math.floor(1000 + Math.random() * 9000);\n\nconst lineItems = item.line_items || [\n  {\n    description: item.description || 'Digitale Dienstleistung / Softwarelizenz',\n    quantity: '1.0',\n    unit_price: item.amount_total ? (item.amount_total / 100 / 1.19).toFixed(2) : '100.00',\n    tax_rate_percent: '19.0'\n  }\n];\n\nconst invoicePayload = {\n  invoice_number: invoiceNum,\n  issue_date: todayStr,\n  currency: 'EUR',\n  seller: {\n    name: 'Meine Firma GmbH',\n    street: 'Musterstraße 10',\n    city: 'Frankfurt am Main',\n    zip_code: '60311',\n    country: 'DE',\n    vat_id: 'DE123456789',\n    iban: 'DE89370400440532013000'\n  },\n  buyer: {\n    name: customerName,\n    street: item.customer_details?.address?.line1 || 'Kundenweg 5',\n    city: item.customer_details?.address?.city || 'Berlin',\n    zip_code: item.customer_details?.address?.postal_code || '10115',\n    country: item.customer_details?.address?.country || 'DE'\n  },\n  line_items: lineItems\n};\n\nreturn {\n  buyer_email: customerEmail,\n  buyer_name: customerName,\n  invoice_number: invoiceNum,\n  invoice_payload: invoicePayload\n};"
      },
      "id": "node-code-format",
      "name": "Code: Daten für EN 16931 formatieren",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        350
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://e-rechnung-cloud.de/v1/invoice/generate",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{ $env.ZUGFERD_API_KEY || 'zg_test_ouroboros_001' }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.invoice_payload) }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file",
              "outputPropertyName": "invoice_pdf"
            }
          }
        }
      },
      "id": "node-http-api",
      "name": "HTTP: ZUGFeRD API aufrufen",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        720,
        350
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "cond-binary-exists",
              "leftValue": "={{ $binary.invoice_pdf ? true : false }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "node-if-success",
      "name": "IF: PDF erfolgreich?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        960,
        350
      ]
    },
    {
      "parameters": {
        "fromEmail": "rechnung@e-rechnung-cloud.de",
        "toEmail": "={{ $(\"Code: Daten für EN 16931 formatieren\").item.json.buyer_email }}",
        "subject": "={{ \"Ihre E-Rechnung \" + $(\"Code: Daten für EN 16931 formatieren\").item.json.invoice_number }}",
        "html": "={{ \"<p>Guten Tag \" + $(\"Code: Daten für EN 16931 formatieren\").item.json.buyer_name + \",</p><p>anbei erhalten Sie Ihre rechtskonforme elektronische Rechnung (ZUGFeRD 2.2 / Factur-X nach EN 16931) als PDF/A-3 mit eingebettetem XML-Datensatz.</p><p>Vielen Dank für Ihre Bestellung!</p>\" }}",
        "attachments": "invoice_pdf",
        "options": {}
      },
      "id": "node-email-send",
      "name": "Email: Rechnung an Kunden senden",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1240,
        240
      ]
    },
    {
      "parameters": {
        "respondWith": "binary",
        "responseDataSource": "firstNodeOutput",
        "dataPropertyName": "invoice_pdf",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/pdf"
              },
              {
                "name": "Content-Disposition",
                "value": "={{ \"attachment; filename=\\\"\" + $(\"Code: Daten für EN 16931 formatieren\").item.json.invoice_number + \".pdf\\\"\" }}"
              }
            ]
          }
        }
      },
      "id": "node-respond-webhook",
      "name": "Respond to Webhook: PDF zurückgeben",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1240,
        460
      ]
    },
    {
      "parameters": {
        "content": "### ⚡ End-to-End E-Rechnung Workflow Blueprint\n\nDieser Blueprint automatisiert die gesamte Rechnungsstellung nach dem deutschen Wachstumschancengesetz 2025/2026:\n\n1. **Trigger:** Startet vollautomatisch bei erfolgreichem **Stripe Checkout** oder per **Webhook**.\n2. **Transform:** Bereitet Käufer, Posten & 19% USt nach EU-Norm **EN 16931** auf.\n3. **API-Call:** Erzeugt ein hybrides PDF/A-3 mit Factur-X / ZUGFeRD 2.2 XML über `https://e-rechnung-cloud.de`.\n4. **Zustellung:** Versendet das PDF automatisch per E-Mail an den Käufer und gibt es per Webhook zurück.",
        "height": 280,
        "width": 580
      },
      "id": "note-architecture",
      "name": "Architektur-Übersicht",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        160,
        -70
      ]
    },
    {
      "parameters": {
        "content": "### ⚙️ Konfiguration & API-Schlüssel\n\n- **Entwickler-Testmodus:** Ohne Registrierung mit Test-Key `zg_test_ouroboros_001` sofort lauffähig (erzeugt Test-Wasserzeichen, verbraucht kein Guthaben).\n- **Produktion:** Trage deinen API-Key in den Header `X-API-Key` oder die n8n-Variable `ZUGFERD_API_KEY` ein.\n- **Verkäuferdaten:** Passe deine Firmendaten (Name, IBAN, USt-IdNr) im Code-Knoten an.",
        "height": 280,
        "width": 540
      },
      "id": "note-config",
      "name": "Konfigurations-Hinweis",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        780,
        -70
      ]
    }
  ],
  "connections": {
    "Stripe Trigger: Checkout abgeschlossen": {
      "main": [
        [
          {
            "node": "Code: Daten für EN 16931 formatieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook: Manuell / Externer Aufruf": {
      "main": [
        [
          {
            "node": "Code: Daten für EN 16931 formatieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Daten für EN 16931 formatieren": {
      "main": [
        [
          {
            "node": "HTTP: ZUGFeRD API aufrufen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP: ZUGFeRD API aufrufen": {
      "main": [
        [
          {
            "node": "IF: PDF erfolgreich?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: PDF erfolgreich?": {
      "main": [
        [
          {
            "node": "Email: Rechnung an Kunden senden",
            "type": "main",
            "index": 0
          },
          {
            "node": "Respond to Webhook: PDF zurückgeben",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "Stripe"
    },
    {
      "name": "ZUGFeRD"
    },
    {
      "name": "Factur-X"
    },
    {
      "name": "E-Rechnung"
    },
    {
      "name": "Email Automation"
    }
  ]
}