{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "83f1bf6b-b2d4-40eb-8cbc-bf9a4dd13e10",
      "name": "Section - Validation",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        560
      ],
      "parameters": {
        "color": 7,
        "width": 528,
        "height": 456,
        "content": "## Validation & Routing\n\nValidates payload. If valid, passes to AI for analysis."
      },
      "typeVersion": 1
    },
    {
      "id": "46f341fc-77ad-4620-acec-a821de6e5754",
      "name": "Section - AI & Logic",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1200,
        512
      ],
      "parameters": {
        "color": 7,
        "width": 648,
        "height": 450,
        "content": "## AI & Logic\n\n1. **AI Analysis**: Determines Churn Risk & drafts email.\n2. **Switch**: Routes High Value (>$500) to High Priority."
      },
      "typeVersion": 1
    },
    {
      "id": "35fc16ae-d215-4210-9b63-9ceac1c88387",
      "name": "AI Analysis",
      "type": "n8n-nodes-base.openAi",
      "position": [
        1296,
        656
      ],
      "parameters": {
        "model": "gpt-3.5-turbo",
        "prompt": {
          "messages": [
            {
              "role": "system",
              "content": "You are a financial recovery assistant."
            },
            {
              "role": "user",
              "content": "=Analyze this payment failure:\nCustomer: {{$json.customerEmail}}\nAmount: ${{$json.amount}}\nReason: {{$json.failureReason}}\n\n1. Determine Churn Risk (Low, Medium, High).\n2. Determine Urgency (Standard, High).\n3. Draft a polite recovery email to the customer.\n\nReturn JSON: { \"churnRisk\": \"...\", \"urgency\": \"...\", \"emailDraft\": \"...\" }"
            }
          ]
        },
        "options": {},
        "requestOptions": {}
      },
      "typeVersion": 1
    },
    {
      "id": "6a23440a-6af1-48a7-ade8-1f71e716bdaf",
      "name": "Priority Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        1504,
        656
      ],
      "parameters": {
        "rules": {
          "rules": [
            {
              "value2": 500,
              "operation": "larger"
            }
          ]
        },
        "value1": "={{$json.amount}}",
        "fallbackOutput": 1
      },
      "typeVersion": 1
    },
    {
      "id": "b650de29-af79-4dad-8ccc-0adf2e948df7",
      "name": "Set High Priority",
      "type": "n8n-nodes-base.set",
      "position": [
        1696,
        560
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "ticketPriority",
              "value": "High"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "a36b4305-311a-4a10-90c9-df916dc628e1",
      "name": "Set Standard Priority",
      "type": "n8n-nodes-base.set",
      "position": [
        1696,
        768
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "ticketPriority",
              "value": "Medium"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "f755de36-c8d1-4a20-9d32-d1d3c6386292",
      "name": "Main Overview1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        64
      ],
      "parameters": {
        "color": "yellow",
        "width": 550,
        "height": 550,
        "content": "## How it works\n\nThis workflow handles failed payment renewal notifications. It now includes **AI Analysis** to determine urgency and draft recovery emails.\n\n1. **Webhook**: Receives payload.\n2. **Validation**: Checks for required fields.\n3. **AI Analysis**: OpenAI analyzes the failure reason & amount to set urgency.\n4. **Routing**: High-value (> $500) or High-Risk failures get 'High' priority.\n5. **Jira**: Creates a ticket with AI-drafted content.\n6. **Slack**: Notifies the team with churn risk assessment.\n\n## Setup steps\n\n1. Configure the webhook endpoint path (`/payment-failed-renewal`)\n2. Set up Jira & Slack credentials\n3. **Configure OpenAI credentials** for the AI Analysis node\n4. Test with a POST request\n5. Activate"
      },
      "typeVersion": 1
    },
    {
      "id": "a85de952-f857-48e1-82af-e38cac66d2cc",
      "name": "Section - Success Path1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1904,
        512
      ],
      "parameters": {
        "color": 7,
        "width": 536,
        "height": 446,
        "content": "## Success Path\n\nCreates Jira ticket with AI-enhanced data and notifies Slack."
      },
      "typeVersion": 1
    },
    {
      "id": "d6344515-73bb-40f3-9881-ce6b723d1e6f",
      "name": "Section - Error Path1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1216,
        1008
      ],
      "parameters": {
        "color": 7,
        "width": 384,
        "height": 296,
        "content": "## Error Path"
      },
      "typeVersion": 1
    },
    {
      "id": "bbb3248a-d8cd-48b2-a9cd-d1d8c3c418d5",
      "name": "Webhook - Payment Failed",
      "type": "n8n-nodes-base.webhook",
      "position": [
        256,
        752
      ],
      "parameters": {
        "path": "payment-failed-renewal",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 1
    },
    {
      "id": "f885e35c-e699-4219-98b1-51479703bb99",
      "name": "Validate Payload",
      "type": "n8n-nodes-base.function",
      "position": [
        640,
        752
      ],
      "parameters": {
        "functionCode": "// Validate required fields\nconst requiredFields = ['customerId', 'customerEmail', 'subscriptionId', 'amount'];\nconst missingFields = [];\nconst payload = $input.item.json;\n\n// Check each required field\nfor (const field of requiredFields) {\n  if (!payload[field] || payload[field] === '') {\n    missingFields.push(field);\n  }\n}\n\n// Prepare validation result\nconst isValid = missingFields.length === 0;\n\nreturn {\n  json: {\n    ...payload,\n    validationStatus: isValid ? 'valid' : 'invalid',\n    missingFields: missingFields,\n    hasError: !isValid\n  }\n};"
      },
      "typeVersion": 1
    },
    {
      "id": "2e751337-57d9-45a6-ade4-6b2e3d76cd18",
      "name": "Payload Valid?",
      "type": "n8n-nodes-base.if",
      "position": [
        896,
        752
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.hasError}}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7c943f69-8f7f-424f-b0b7-2705c8eebc3c",
      "name": "Slack Error Alert",
      "type": "n8n-nodes-base.slack",
      "position": [
        1344,
        1120
      ],
      "parameters": {
        "text": "=\u26a0\ufe0f *Payment Webhook Error*\nMissing fields: {{$json.missingFields.join(', ')}}",
        "attachments": [],
        "otherOptions": {
          "mrkdwn": true
        }
      },
      "typeVersion": 1
    },
    {
      "id": "510ca07e-c8ca-4315-a7cd-d947764068c4",
      "name": "Create Jira Finance Ticket",
      "type": "n8n-nodes-base.jira",
      "position": [
        1952,
        656
      ],
      "parameters": {
        "project": "FIN",
        "summary": "=Failed Renewal - {{$json.customerEmail}} (Risk: {{$json.churnRisk}})",
        "issueType": "Task",
        "additionalFields": {
          "labels": [
            "billing-recovery",
            "={{$json.ticketPriority == 'High' ? 'urgent' : 'routine'}}"
          ],
          "description": "=**Analysis**\n- Urgency: {{$json.ticketPriority}}\n- Churn Risk: {{$json.churnRisk}}\n\n**Draft Email:**\n{{$json.emailDraft}}\n\n**Details**\n- Customer: {{$json.customerEmail}}\n- Amount: ${{$json.amount}}\n- Reason: {{$json.failureReason}}"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "6361af2f-cfa3-48dc-ab18-3ccf4f825000",
      "name": "Slack Finance Notification",
      "type": "n8n-nodes-base.slack",
      "position": [
        2192,
        656
      ],
      "parameters": {
        "text": "=\ud83d\udea8 *Failed Subscription Renewal*\n\n*Urgency:* {{$json.ticketPriority}}\n*Risk:* {{$json.churnRisk}}\n\n*Customer:* {{$json.customerEmail}}\n*Amount:* ${{$json.amount}}\n\n<{{$node['Create Jira Finance Ticket'].json.self}}|View Jira Ticket>",
        "attachments": [],
        "otherOptions": {
          "mrkdwn": true
        }
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "AI Analysis": {
      "main": [
        [
          {
            "node": "Priority Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Payload Valid?": {
      "main": [
        [
          {
            "node": "AI Analysis",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack Error Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Priority Switch": {
      "main": [
        [
          {
            "node": "Set High Priority",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set Standard Priority",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Payload": {
      "main": [
        [
          {
            "node": "Payload Valid?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set High Priority": {
      "main": [
        [
          {
            "node": "Create Jira Finance Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Standard Priority": {
      "main": [
        [
          {
            "node": "Create Jira Finance Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Payment Failed": {
      "main": [
        [
          {
            "node": "Validate Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Jira Finance Ticket": {
      "main": [
        [
          {
            "node": "Slack Finance Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}