{
  "id": "demo8dd281561f32",
  "name": "Billing Operations \u2014 Local Demo",
  "active": false,
  "nodes": [
    {
      "id": "start",
      "name": "Run local demo",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        50,
        220
      ],
      "parameters": {}
    },
    {
      "id": "fixtures",
      "name": "Load fictional scenarios",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        280,
        220
      ],
      "parameters": {
        "jsCode": "return [{\"json\": {\"name\": \"Fees reconcile exactly\", \"input\": {\"currency\": \"usd\", \"rows\": [{\"id\": \"t1\", \"gross\": 10000, \"fee\": 300, \"currency\": \"usd\"}], \"payout\": 9700, \"invoice\": {\"id\": \"INV-1\", \"status\": \"open\", \"amountDue\": 10000}}, \"expected\": {\"status\": \"matched\", \"difference\": 0, \"followUp\": \"draft-only\"}}}, {\"json\": {\"name\": \"Paid invoice suppresses follow-up\", \"input\": {\"currency\": \"usd\", \"rows\": [], \"payout\": 0, \"invoice\": {\"id\": \"INV-2\", \"status\": \"paid\", \"amountDue\": 0}}, \"expected\": {\"status\": \"matched\", \"followUp\": \"suppressed\", \"draft\": null}}}, {\"json\": {\"name\": \"Payout mismatch routes to review\", \"input\": {\"currency\": \"usd\", \"rows\": [{\"id\": \"t1\", \"gross\": 10000, \"fee\": 300, \"currency\": \"usd\"}], \"payout\": 9000}, \"expected\": {\"status\": \"review\", \"reason\": \"payout-mismatch\", \"difference\": -700}}}, {\"json\": {\"name\": \"Duplicate ledger rows require review\", \"input\": {\"currency\": \"usd\", \"rows\": [{\"id\": \"t1\", \"gross\": 100, \"fee\": 0, \"currency\": \"usd\"}, {\"id\": \"t1\", \"gross\": 100, \"fee\": 0, \"currency\": \"usd\"}], \"payout\": 200}, \"expected\": {\"reason\": \"duplicate-transaction\"}}}, {\"json\": {\"name\": \"Mixed currencies cannot reconcile\", \"input\": {\"currency\": \"usd\", \"rows\": [{\"id\": \"t1\", \"gross\": 100, \"fee\": 0, \"currency\": \"eur\"}], \"payout\": 100}, \"expected\": {\"reason\": \"currency-mismatch\"}}}, {\"json\": {\"name\": \"Fractional minor units rejected\", \"input\": {\"currency\": \"usd\", \"rows\": [{\"id\": \"t1\", \"gross\": 100.5, \"fee\": 0, \"currency\": \"usd\"}], \"payout\": 100}, \"expected\": {\"reason\": \"invalid-amount\"}}}, {\"json\": {\"name\": \"Refund is included as a negative ledger amount\", \"input\": {\"currency\": \"usd\", \"rows\": [{\"id\": \"sale\", \"gross\": 10000, \"fee\": 300, \"currency\": \"usd\"}, {\"id\": \"refund\", \"gross\": -2000, \"fee\": 0, \"currency\": \"usd\"}], \"payout\": 7700}, \"expected\": {\"status\": \"matched\", \"difference\": 0}}}];"
      }
    },
    {
      "id": "stage0",
      "name": "Validate ledger inputs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        570,
        220
      ],
      "parameters": {
        "jsCode": "function step({input}) {\n const seen=new Set();let error=null;\n for(const row of input.rows){if(seen.has(row.id)){error='duplicate-transaction';break;}seen.add(row.id);\n if(row.currency!==input.currency){error='currency-mismatch';break;}\n if(!Number.isSafeInteger(row.gross)||!Number.isSafeInteger(row.fee)||row.fee<0){error='invalid-amount';break;}}\n if(!Number.isSafeInteger(input.payout))error='invalid-amount';\n return {error};\n}\nreturn $input.all().map(({json},index)=>({json:{...json,...step(json)},pairedItem:{item:index}}));"
      }
    },
    {
      "id": "stage1",
      "name": "Reconcile payout totals",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        820,
        340
      ],
      "parameters": {
        "jsCode": "function step({input,error}) {\n const expectedNet=error?null:input.rows.reduce((s,r)=>s+r.gross-r.fee,0);\n return {reconciliation:{status:error?'review':expectedNet===input.payout?'matched':'review',reason:error??(expectedNet===input.payout?'balanced':'payout-mismatch'),difference:expectedNet===null?null:input.payout-expectedNet}};\n}\nreturn $input.all().map(({json},index)=>({json:{...json,...step(json)},pairedItem:{item:index}}));"
      }
    },
    {
      "id": "stage2",
      "name": "Draft unpaid invoice follow-up",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1110,
        220
      ],
      "parameters": {
        "jsCode": "function step({input,reconciliation}) {\n const unpaid=input.invoice?.status==='open' && Number.isSafeInteger(input.invoice.amountDue) && input.invoice.amountDue>0;\n const draft=unpaid?'Please review your outstanding invoice '+input.invoice.id+'. Contact our team if you need help.':null;\n return {result:{...reconciliation,followUp:unpaid?'draft-only':'suppressed',draft}};\n}\nreturn $input.all().map(({json},index)=>({json:{...json,...step(json)},pairedItem:{item:index}}));"
      }
    },
    {
      "id": "assert",
      "name": "Verify expected outcomes",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1370,
        220
      ],
      "parameters": {
        "jsCode": "const checks=$input.all().map(({json})=>{const failed=Object.entries(json.expected).filter(([k,v])=>JSON.stringify(json.result[k])!==JSON.stringify(v));return {scenario:json.name,passed:failed.length===0,expected:json.expected,output:json.result,failures:failed.map(([k])=>k)};});\nconst failures=checks.filter(c=>!c.passed);if(failures.length)throw new Error(JSON.stringify(failures));\nreturn [{json:{demo:\"billing-operations\",mode:'local-simulation',checkedAt:new Date().toISOString(),passed:checks.length,failed:0,externalActionsPerformed:0,checks}}];"
      }
    },
    {
      "id": "section-1",
      "name": "Section 1",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 480,
        "height": 420,
        "color": 7,
        "content": "### 1. Load billing fixtures\n\nUse fictional payout ledgers and invoice states, including mismatched totals, duplicate entries, and currencies."
      }
    },
    {
      "id": "section-2",
      "name": "Section 2",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        520,
        0
      ],
      "parameters": {
        "width": 520,
        "height": 550,
        "color": 7,
        "content": "### 2. Reconcile the ledger\n\nValidate input records and compare integer totals in minor currency units. Surface explicit reconciliation exceptions."
      }
    },
    {
      "id": "section-3",
      "name": "Section 3",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1080,
        0
      ],
      "parameters": {
        "width": 510,
        "height": 420,
        "color": 7,
        "content": "### 3. Draft and verify\n\nPrepare fixed follow-up copy only for unpaid invoices and verify the expected results. No billing provider or mail service is called."
      }
    }
  ],
  "connections": {
    "Run local demo": {
      "main": [
        [
          {
            "node": "Load fictional scenarios",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load fictional scenarios": {
      "main": [
        [
          {
            "node": "Validate ledger inputs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate ledger inputs": {
      "main": [
        [
          {
            "node": "Reconcile payout totals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reconcile payout totals": {
      "main": [
        [
          {
            "node": "Draft unpaid invoice follow-up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft unpaid invoice follow-up": {
      "main": [
        [
          {
            "node": "Verify expected outcomes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
