{
  "name": "09 · Stripe Invoice Paid → Credit Top-Up",
  "nodes": [
    {
      "name": "Stripe Invoice Paid",
      "type": "n8n-nodes-base.stripeTrigger",
      "typeVersion": 1,
      "position": [
        0,
        208
      ],
      "parameters": {
        "events": [
          "invoice.payment_succeeded"
        ]
      },
      "webhookId": "4742ec47-5058-492a-8e45-e04077f4a064",
      "credentials": {
        "stripeApi": {
          "id": "v8kDsMc8Q3lV9r9O",
          "name": "Stripe account"
        }
      }
    },
    {
      "name": "Extract Invoice Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        224,
        208
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "email",
              "value": "={{ $json.data.object.customer_email }}",
              "type": "string"
            },
            {
              "id": "a2",
              "name": "amount_cents",
              "value": "={{ $json.data.object.amount_paid }}",
              "type": "number"
            },
            {
              "id": "a3",
              "name": "credits",
              "value": "={{ Math.floor($json.data.object.amount_paid / 100) * 10 }}",
              "type": "number"
            },
            {
              "id": "a4",
              "name": "invoice_id",
              "value": "={{ $json.data.object.id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "name": "Find Client by Email",
      "type": "n8n-nodes-lindo.lindo",
      "typeVersion": 1,
      "position": [
        448,
        208
      ],
      "parameters": {
        "resource": "client",
        "operation": "list"
      },
      "credentials": {
        "lindoApi": {
          "id": "EZiHryOUuVuxcDYQ",
          "name": "Lindo account"
        }
      }
    },
    {
      "name": "Match Client",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        672,
        208
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "b1",
              "name": "client_id",
              "value": "={{ ($json.list || []).find(c => c.email === $(\"Extract Invoice Data\").first().json.email)?.client_id || \"\" }}",
              "type": "string"
            },
            {
              "id": "b2",
              "name": "email",
              "value": "={{ $(\"Extract Invoice Data\").first().json.email }}",
              "type": "string"
            },
            {
              "id": "b3",
              "name": "credits",
              "value": "={{ $(\"Extract Invoice Data\").first().json.credits }}",
              "type": "number"
            },
            {
              "id": "b4",
              "name": "invoice_id",
              "value": "={{ $(\"Extract Invoice Data\").first().json.invoice_id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "name": "Client Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        896,
        208
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "f1",
              "leftValue": "={{ $json.client_id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEquals",
                "name": "filter.operator.notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "name": "Allocate Credits",
      "type": "n8n-nodes-lindo.lindo",
      "typeVersion": 1,
      "position": [
        1120,
        112
      ],
      "parameters": {
        "resource": "credits",
        "operation": "allocate",
        "clientId": "={{ $json.client_id }}",
        "creditType": "purchased",
        "amount": "={{ $json.credits }}"
      },
      "credentials": {
        "lindoApi": {
          "id": "EZiHryOUuVuxcDYQ",
          "name": "Lindo account"
        }
      }
    },
    {
      "name": "Get Updated Balance",
      "type": "n8n-nodes-lindo.lindo",
      "typeVersion": 1,
      "position": [
        1344,
        112
      ],
      "parameters": {
        "resource": "credits",
        "operation": "getClient",
        "clientId": "={{ $(\"Match Client\").first().json.client_id }}"
      },
      "credentials": {
        "lindoApi": {
          "id": "EZiHryOUuVuxcDYQ",
          "name": "Lindo account"
        }
      }
    },
    {
      "name": "Send Confirmation Email",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1568,
        112
      ],
      "parameters": {
        "promptType": "define",
        "text": "=Send a credit top-up confirmation email.\n\nClient: {{ $(\"Match Client\").first().json.email }}\nCredits Added: {{ $(\"Match Client\").first().json.credits }}\nNew Balance: {{ $json.balance?.total || \"unknown\" }}\nInvoice: {{ $(\"Match Client\").first().json.invoice_id }}\n\nCompose a brief receipt-style email confirming the credit top-up. Send via Emailr.",
        "options": {
          "systemMessage": "Send a brief credit top-up confirmation email via Emailr."
        }
      }
    },
    {
      "name": "OpenAI",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        1584,
        336
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "Yuica5wlzDrhFTNk",
          "name": "OpenAi account"
        }
      }
    },
    {
      "name": "Emailr",
      "type": "n8n-nodes-emailr.emailrTool",
      "typeVersion": 1,
      "position": [
        1712,
        336
      ],
      "parameters": {
        "from": "lindo.ai <billing@lindo.ai>",
        "to": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('To', '') }}",
        "subject": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Subject', '') }}",
        "html": "=",
        "text": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Text_Body', ``, 'string') }}"
      },
      "credentials": {
        "emailrApi": {
          "id": "1QJ6NqiurNRi9d4C",
          "name": "Emailr account"
        }
      }
    },
    {
      "name": "No Client Found (Stop)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        304
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "x1",
              "name": "error",
              "value": "Client not found for this email",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "name": "Sticky Note b9b1791f",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        848,
        384
      ],
      "parameters": {
        "content": "### 09 · Stripe Invoice Paid → Credit Top-Up\n\n**How it works:** Stripe invoice.payment_succeeded → extracts payment data → calculates credits ($1 = 10 credits) → finds client by email → allocates purchased credits → fetches updated balance → sends confirmation email with receipt details.\n\n**Setup:** Connect Stripe webhook for invoice.payment_succeeded. Ensure clients exist in Lindo with matching emails. Configure Emailr credentials.\n\n**Customisation:** Adjust credit conversion rate (default $1 = 10 credits). Change email sender to billing@. Add Slack alert for large top-ups. Log transactions to a spreadsheet.",
        "color": 2,
        "width": 1868,
        "height": 392
      }
    },
    {
      "name": "Sticky Note 8f45a95e",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        848,
        384
      ],
      "parameters": {
        "content": "## Payment Processing\nExtracts invoice data and calculates credits at $1 = 10 credits conversion rate.",
        "color": 1,
        "width": 524,
        "height": 200
      }
    },
    {
      "name": "Sticky Note db5e6b7d",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        848,
        384
      ],
      "parameters": {
        "content": "## Client Lookup\nFinds matching client by email. If not found, stops gracefully.",
        "color": 1,
        "width": 972,
        "height": 296
      }
    },
    {
      "name": "Sticky Note 4bd11732",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        848,
        384
      ],
      "parameters": {
        "content": "## Credit Allocation & Confirmation\nAllocates credits, fetches new balance, sends receipt-style confirmation email.",
        "color": 1,
        "width": 748,
        "height": 200
      }
    }
  ],
  "connections": {
    "Stripe Invoice Paid": {
      "main": [
        [
          {
            "node": "Extract Invoice Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Invoice Data": {
      "main": [
        [
          {
            "node": "Find Client by Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Client by Email": {
      "main": [
        [
          {
            "node": "Match Client",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Match Client": {
      "main": [
        [
          {
            "node": "Client Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Client Found?": {
      "main": [
        [
          {
            "node": "Allocate Credits",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Client Found (Stop)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Allocate Credits": {
      "main": [
        [
          {
            "node": "Get Updated Balance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Updated Balance": {
      "main": [
        [
          {
            "node": "Send Confirmation Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "ai_languageModel": [
        [
          {
            "node": "Send Confirmation Email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Emailr": {
      "ai_tool": [
        [
          {
            "node": "Send Confirmation Email",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}