{
  "name": "WF-007 Failed Payment Handler",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "failed-payment",
        "responseMode": "onReceived",
        "responseData": "allEntries",
        "options": {}
      },
      "id": "wf007-node-1",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "tableId": "tenants",
        "filterType": "string",
        "filterString": "=id=eq.{{ $json.body.tenant_id }}",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldName": "subscription_status",
              "fieldValue": "past_due"
            }
          ]
        }
      },
      "id": "wf007-node-2",
      "name": "Update Tenant Status",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fromEmail": "TitleFlow AI <user@example.com>",
        "toEmail": "={{ $('Webhook Trigger').item.json.body.email }}",
        "subject": "=Payment Failed \u2014 Action Required",
        "emailType": "html",
        "html": "=<!DOCTYPE html>\n<html>\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  <title>Payment Failed</title>\n</head>\n<body style=\"margin:0; padding:0; background-color:#000000; font-family:'Inter', 'Manrope', Arial, sans-serif;\">\n  <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"background-color:#000000;\">\n    <tr>\n      <td align=\"center\" style=\"padding:40px 20px;\">\n        <table role=\"presentation\" width=\"600\" cellpadding=\"0\" cellspacing=\"0\" style=\"background: linear-gradient(135deg, rgba(0,128,255,0.1), rgba(79,26,214,0.1)); border:1px solid rgba(255,255,255,0.1); border-radius:24px; overflow:hidden;\">\n          <!-- Header -->\n          <tr>\n            <td style=\"padding:40px 40px 20px; text-align:center;\">\n              <div style=\"display:inline-block; background:rgba(255,59,48,0.15); border:1px solid rgba(255,59,48,0.3); border-radius:12px; padding:8px 20px; margin-bottom:16px;\">\n                <span style=\"color:#FF3B30; font-size:14px; font-weight:600;\">PAYMENT FAILED</span>\n              </div>\n              <h1 style=\"color:#FFFFFF; font-size:24px; font-weight:700; margin:16px 0 0;\">Action Required for {{ $('Webhook Trigger').item.json.body.company_name }}</h1>\n              <div style=\"width:60px; height:4px; background:linear-gradient(90deg,#FF3B30,#FF9500); border-radius:2px; margin:16px auto 0;\"></div>\n            </td>\n          </tr>\n          <!-- Body -->\n          <tr>\n            <td style=\"padding:20px 40px;\">\n              <p style=\"color:rgba(255,255,255,0.9); font-size:16px; line-height:1.6; margin:0 0 16px;\">\n                We were unable to process your payment for the following invoice:\n              </p>\n              <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"background:rgba(255,255,255,0.05); border:1px solid rgba(255,255,255,0.1); border-radius:16px; margin:0 0 24px;\">\n                <tr>\n                  <td style=\"padding:20px;\">\n                    <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n                      <tr>\n                        <td style=\"color:rgba(255,255,255,0.5); font-size:13px; padding-bottom:8px;\">Invoice ID</td>\n                        <td style=\"color:#FFFFFF; font-size:14px; font-weight:600; text-align:right; padding-bottom:8px;\">{{ $('Webhook Trigger').item.json.body.invoice_id }}</td>\n                      </tr>\n                      <tr>\n                        <td style=\"color:rgba(255,255,255,0.5); font-size:13px; padding-top:8px; border-top:1px solid rgba(255,255,255,0.1);\">Amount Due</td>\n                        <td style=\"color:#FF3B30; font-size:18px; font-weight:700; text-align:right; padding-top:8px; border-top:1px solid rgba(255,255,255,0.1);\">${{ $('Webhook Trigger').item.json.body.amount }}</td>\n                      </tr>\n                    </table>\n                  </td>\n                </tr>\n              </table>\n              <p style=\"color:rgba(255,255,255,0.7); font-size:16px; line-height:1.6; margin:0 0 32px;\">\n                Please update your payment method within the next 7 days to avoid any service interruption. Your account has been marked as <strong style=\"color:#FF9500;\">past due</strong>.\n              </p>\n            </td>\n          </tr>\n          <!-- CTA -->\n          <tr>\n            <td style=\"padding:0 40px 32px; text-align:center;\">\n              <a href=\"https://titleflowai.ai/dashboard/billing\" style=\"display:inline-block; background:linear-gradient(135deg,#0080FF,#4F1AD6); color:#FFFFFF; text-decoration:none; font-size:16px; font-weight:600; padding:14px 40px; border-radius:9999px;\">Update Payment Method</a>\n            </td>\n          </tr>\n          <!-- Footer -->\n          <tr>\n            <td style=\"padding:24px 40px; border-top:1px solid rgba(255,255,255,0.1);\">\n              <p style=\"color:rgba(255,255,255,0.4); font-size:13px; line-height:1.5; margin:0; text-align:center;\">\n                TitleFlow AI &mdash; AI-Powered Voice Intelligence for Title Companies<br>\n                If you believe this is an error, please contact user@example.com\n              </p>\n            </td>\n          </tr>\n        </table>\n      </td>\n    </tr>\n  </table>\n</body>\n</html>",
        "options": {}
      },
      "id": "wf007-node-3",
      "name": "Send Payment Failed Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        750,
        300
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "create",
        "tableId": "notifications",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldName": "tenant_id",
              "fieldValue": "={{ $('Webhook Trigger').item.json.body.tenant_id }}"
            },
            {
              "fieldName": "type",
              "fieldValue": "payment_failed"
            },
            {
              "fieldName": "title",
              "fieldValue": "Payment Failed"
            },
            {
              "fieldName": "message",
              "fieldValue": "=Payment of ${{ $('Webhook Trigger').item.json.body.amount }} failed for invoice {{ $('Webhook Trigger').item.json.body.invoice_id }}. Please update your payment method."
            },
            {
              "fieldName": "is_read",
              "fieldValue": "false"
            }
          ]
        }
      },
      "id": "wf007-node-4",
      "name": "Insert Payment Notification",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Update Tenant Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Tenant Status": {
      "main": [
        [
          {
            "node": "Send Payment Failed Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Payment Failed Email": {
      "main": [
        [
          {
            "node": "Insert Payment Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "versionId": "wf007-v1",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": [
    {
      "name": "billing"
    },
    {
      "name": "payment"
    },
    {
      "name": "email"
    }
  ]
}