{
  "name": "Failed payment recovery (New Theory template)",
  "nodes": [
    {
      "parameters": {
        "content": "## Failed payment recovery: n8n starter\nFree template from New Theory (newtheory.marketing). Built from our playbook records. It is not an official n8n, HubSpot or Zapier template. Every credential is a placeholder: connect your own before you turn it on.\n\n**Fill in, in this order**\n1. **Stripe invoice events**: connect your Stripe credential and set a Signature Secret on it, so unsigned or stale requests are rejected. Build in test mode, then switch to a live credential.\n2. **Google Sheets**: connect it and make a Recovery tab with columns invoice_id, subscription_id, customer_email, amount_due, currency, failed_at, attempt_count, status, recovered_at.\n3. **Klaviyo**: add a Header Auth credential named Authorization with the value `Klaviyo-API-Key <private key>` (the key needs events:write and profiles:write). Check the revision header against Klaviyo's docs.\n4. **Above MRR threshold?**: set your own line. 500 is only a starting point.\n5. **Slack**: connect it and pick your billing saves channel.\n\nIn Stripe, turn off Send emails when card payments fail if Klaviyo sends your dunning.",
        "height": 440,
        "width": 580,
        "color": 4
      },
      "id": "e4bac071-6c51-4d41-a923-7942f51fbb4f",
      "name": "Start here",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -60
      ]
    },
    {
      "parameters": {
        "events": [
          "invoice.payment_failed",
          "invoice.paid"
        ]
      },
      "id": "f9d6aed7-4cdb-4566-a5c5-46cead05c697",
      "name": "Stripe invoice events",
      "type": "n8n-nodes-base.stripeTrigger",
      "typeVersion": 1,
      "position": [
        0,
        520
      ],
      "webhookId": "9f0ab97f-6755-4dde-a3ae-5d2092c0714a",
      "credentials": {
        "stripeApi": {
          "id": "replace-me",
          "name": "Stripe (connect yours)"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "2f4e3703-a6ec-4086-a3f7-b9d9d91b6d8f",
              "leftValue": "={{ $json.type }}",
              "rightValue": "invoice.payment_failed",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "ec0bc885-16e8-4f56-a887-604204354540",
      "name": "Failed or paid?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        220,
        520
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "0cfea7d2-dac3-4c3c-a006-452702c23fda",
              "leftValue": "={{ $json.data.object.billing_reason }}",
              "rightValue": "subscription_cycle",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "4ef19725-a32e-4d64-ab9f-992c51c95de1",
      "name": "Renewal only?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        440,
        420
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ec34110b-05ca-4c42-a93d-1684b7adbf41",
              "name": "invoice_id",
              "value": "={{ $json.data.object.id }}",
              "type": "string"
            },
            {
              "id": "7a6f4dfe-5835-4ab6-add3-ada780b657c0",
              "name": "subscription_id",
              "value": "={{ $json.data.object.subscription }}",
              "type": "string"
            },
            {
              "id": "356b8946-f7da-4f82-a581-df7e379fa002",
              "name": "customer_email",
              "value": "={{ String($json.data.object.customer_email || '').trim().toLowerCase() }}",
              "type": "string"
            },
            {
              "id": "cdf6a904-8d08-4bf8-a70c-9431ec902dc2",
              "name": "amount_due",
              "value": "={{ $json.data.object.amount_due / 100 }}",
              "type": "number"
            },
            {
              "id": "b6cf25fe-9aa5-4f27-a661-5dc196eb12fe",
              "name": "currency",
              "value": "={{ String($json.data.object.currency || '').toUpperCase() }}",
              "type": "string"
            },
            {
              "id": "95c40515-48b8-4f70-a72f-010ab3819266",
              "name": "attempt_count",
              "value": "={{ $json.data.object.attempt_count }}",
              "type": "number"
            },
            {
              "id": "9375af6e-c768-4354-a229-69705912b1e4",
              "name": "hosted_invoice_url",
              "value": "={{ $json.data.object.hosted_invoice_url }}",
              "type": "string"
            },
            {
              "id": "24c2965b-91e6-4e58-a1b9-62497fd3f354",
              "name": "failed_at",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            },
            {
              "id": "fd0138de-7509-41fb-acf4-ef14bb9ca573",
              "name": "status",
              "value": "failed",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "dcdcf0b3-4f21-4af7-af03-1a7416181c4c",
      "name": "Shape the failure",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        660,
        400
      ]
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_YOUR_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Recovery",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [
            "invoice_id"
          ],
          "schema": []
        },
        "options": {}
      },
      "id": "4bc36d6c-6624-4727-a5d8-91c4da8317d3",
      "name": "Log the failure",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        880,
        400
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "replace-me",
          "name": "Google Sheets (connect yours)"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://a.klaviyo.com/api/events",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "revision",
              "value": "2024-10-15"
            },
            {
              "name": "accept",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"data\": {\n    \"type\": \"event\",\n    \"attributes\": {\n      \"metric\": { \"data\": { \"type\": \"metric\", \"attributes\": { \"name\": \"Payment Failed\" } } },\n      \"profile\": { \"data\": { \"type\": \"profile\", \"attributes\": { \"email\": \"{{ $('Shape the failure').item.json.customer_email }}\", \"properties\": { \"payment_status\": \"failed\" } } } },\n      \"value\": {{ $('Shape the failure').item.json.amount_due }},\n      \"unique_id\": \"{{ $('Shape the failure').item.json.invoice_id }}-{{ $('Shape the failure').item.json.attempt_count }}\",\n      \"properties\": { \"pay_link\": \"{{ $('Shape the failure').item.json.hosted_invoice_url }}\", \"attempt\": {{ $('Shape the failure').item.json.attempt_count }}, \"currency\": \"{{ $('Shape the failure').item.json.currency }}\" }\n    }\n  }\n}",
        "options": {}
      },
      "id": "792194de-dd8c-474c-af75-0a0570414a14",
      "name": "Payment Failed event to Klaviyo",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        400
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "replace-me",
          "name": "Klaviyo (Header Auth: Authorization = Klaviyo-API-Key <key>)"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "4d7a3aa2-38a6-44f0-ae8a-21eda46e0d60",
              "leftValue": "={{ $('Shape the failure').item.json.amount_due }}",
              "rightValue": 500,
              "operator": {
                "type": "number",
                "operation": "gte"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "dedf7196-0705-40dd-a46d-a3834fabb3d1",
      "name": "Above MRR threshold?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1320,
        400
      ]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "#billing-saves",
          "mode": "name"
        },
        "text": "=Failed renewal: *{{ $('Shape the failure').item.json.amount_due }} {{ $('Shape the failure').item.json.currency }}* from {{ $('Shape the failure').item.json.customer_email }}\nAttempt {{ $('Shape the failure').item.json.attempt_count }}. Invoice {{ $('Shape the failure').item.json.invoice_id }}\nOwner: call or email today. Pay link: {{ $('Shape the failure').item.json.hosted_invoice_url }}",
        "otherOptions": {}
      },
      "id": "60701214-548a-4b09-a8f3-6a54f0acccc0",
      "name": "Post to #billing-saves",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        1540,
        380
      ],
      "credentials": {
        "slackApi": {
          "id": "replace-me",
          "name": "Slack (connect yours)"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "b1fc6c08-92a1-48c1-a774-58f558465a31",
              "name": "invoice_id",
              "value": "={{ $json.data.object.id }}",
              "type": "string"
            },
            {
              "id": "2fb7d6c3-ec66-48cb-a162-131d359e14c6",
              "name": "customer_email",
              "value": "={{ String($json.data.object.customer_email || '').trim().toLowerCase() }}",
              "type": "string"
            },
            {
              "id": "ac2cf2f1-701b-4397-ac60-6e26061f3bff",
              "name": "status",
              "value": "recovered",
              "type": "string"
            },
            {
              "id": "65fcc94e-e49a-41ae-a6e7-772f5147d4f0",
              "name": "recovered_at",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "9d08f645-d967-41ee-a17e-f2a168e09a80",
      "name": "Shape the save",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        440,
        700
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_YOUR_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Recovery",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [
            "invoice_id"
          ],
          "schema": []
        },
        "options": {}
      },
      "id": "e358b993-1d1a-46e8-a67f-42d929abe8c8",
      "name": "Mark recovered in the sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        660,
        700
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "replace-me",
          "name": "Google Sheets (connect yours)"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://a.klaviyo.com/api/profile-import",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "revision",
              "value": "2024-10-15"
            },
            {
              "name": "accept",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"data\": {\n    \"type\": \"profile\",\n    \"attributes\": {\n      \"email\": \"{{ $('Shape the save').item.json.customer_email }}\",\n      \"properties\": { \"payment_status\": \"recovered\" }\n    }\n  }\n}",
        "options": {}
      },
      "id": "509e38a7-f3d4-4fc7-ae33-00c8adfdbf86",
      "name": "Mark profile recovered in Klaviyo",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        700
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "replace-me",
          "name": "Klaviyo (Header Auth: Authorization = Klaviyo-API-Key <key>)"
        }
      }
    },
    {
      "parameters": {
        "content": "### In Klaviyo\nCreate a flow triggered by the Payment Failed metric: email now, wait, second email, final notice. Add a split on payment_status before every send so a customer who paid stops getting mail.\n\nSet backfill to true when you load old failures, or past customers get a dunning email.",
        "height": 240,
        "width": 460,
        "color": 6
      },
      "id": "7572c29f-93f3-4e92-a3aa-8910bb4936ff",
      "name": "Flow note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1080,
        620
      ]
    }
  ],
  "connections": {
    "Stripe invoice events": {
      "main": [
        [
          {
            "node": "Failed or paid?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Failed or paid?": {
      "main": [
        [
          {
            "node": "Renewal only?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Shape the save",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Renewal only?": {
      "main": [
        [
          {
            "node": "Shape the failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Shape the failure": {
      "main": [
        [
          {
            "node": "Log the failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log the failure": {
      "main": [
        [
          {
            "node": "Payment Failed event to Klaviyo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Payment Failed event to Klaviyo": {
      "main": [
        [
          {
            "node": "Above MRR threshold?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Above MRR threshold?": {
      "main": [
        [
          {
            "node": "Post to #billing-saves",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Shape the save": {
      "main": [
        [
          {
            "node": "Mark recovered in the sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark recovered in the sheet": {
      "main": [
        [
          {
            "node": "Mark profile recovered in Klaviyo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
