{
  "name": "Refund with Guardian - TEST",
  "nodes": [
    {
      "parameters": {},
      "id": "dddda59b-c3b3-4ffc-9b86-c05e2d373434",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// TEST: pick ONE case per run. Sample data only: made-up orders, no customer data, nothing is refunded.\n// RUN: raise it for a fresh set of keys (a used key gets Guardian's earlier answer).\nconst RUN = 1;\nconst CASE = 't1';\n// t10 to t13 use 150, so they wait for a person with either policy (the 24h total or the single-refund rule above 100).\n// The 24h total counts allowed refunds in this project. Run t1, t2, t3 first, in that order, on the same day.\nconst cases = {\n  t1:            { order_id: 'ORD-1001', amount: 40 },       // allowed (total 0 + 40)\n  t2:            { order_id: 'ORD-1002', amount: 40 },       // allowed (total 40 + 40)\n  t3:            { order_id: 'ORD-1003', amount: 40 },       // waits for a person (80 + 40 = 120 over 100)\n  t6_same_key:   { order_id: 'ORD-1001', amount: 45, request: 't1' }, // t1's key with another amount: refused (409), alert\n  t7_fourth:     { order_id: 'ORD-1004', amount: 40 },       // after approving t3: see the README\n  t8_over_250:   { order_id: 'ORD-1005', amount: 300 },      // denied: over the single-refund ceiling\n  t9_no_amount:  { order_id: 'ORD-1006' },                   // amount missing: denied (no valid amount)\n  t9_text:       { order_id: 'ORD-1007', amount: 'forty' },  // amount as text: denied (no valid amount)\n  t10_edit_down: { order_id: 'ORD-1008', amount: 150 },      // waits; Edit & Approve 150 to 100\n  t11_edit_up:   { order_id: 'ORD-1009', amount: 150 },      // waits; Edit & Approve 150 to 200\n  t12_edit_400:  { order_id: 'ORD-1010', amount: 150 },      // waits; Edit & Approve 150 to 400\n  t13_reject:    { order_id: 'ORD-1011', amount: 150 },      // waits; Reject\n  t14_down:      { order_id: 'ORD-1012', amount: 40 },       // wrong Base URL in a copy of the credential\n  t15_kill:      { order_id: 'ORD-1013', amount: 40 },       // project kill switch on\n};\nconst c = cases[CASE];\nif (!c) throw new Error('Unknown CASE ' + CASE);\nconst out = { case: CASE, order_id: c.order_id, refund_request_id: `R${RUN}-${c.request || CASE}`, currency: 'EUR', reason_code: 'damaged_item', requested_by: 'support-agent-1' };\nif ('amount' in c) out.amount = c.amount;\nreturn [{ json: out }];"
      },
      "id": "3d29f4ee-5f62-4bd7-b6a1-b2051899fca6",
      "name": "Refund request",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        300
      ],
      "notesInFlow": true,
      "notes": "TEST: set CASE and RUN here."
    },
    {
      "parameters": {
        "jsCode": "// Builds exactly what Guardian checks. Every field a rule reads is always sent, under the same name:\n// a missing field would make Guardian skip that rule.\nconst r = $json;\nconst raw = r.amount;\nlet num = NaN;\nif (typeof raw === 'number') num = raw;\nelse if (typeof raw === 'string' && /^\\s*\\d+(\\.\\d+)?\\s*$/.test(raw)) num = Number(raw);\nconst amount_valid = Number.isFinite(num) && num > 0;\nconst order_id = String(r.order_id ?? '').trim();\nconst refund_request_id = String(r.refund_request_id ?? '').trim();\nreturn [{ json: {\n  order_id,\n  refund_request_id,\n  amount: amount_valid ? num : 0,        // always a number, so the amount rules never skip\n  amount_valid,                          // false = missing, text or not above 0; a rule denies it\n  currency: String(r.currency || 'EUR'),\n  reason_code: String(r.reason_code || 'unknown'),\n  requested_by: String(r.requested_by || 'unknown'),\n  // One key per refund request. Same key + same content = Guardian's earlier answer.\n  key: `refund-${order_id || 'no-order'}-${refund_request_id || 'no-request'}`,\n} }];"
      },
      "id": "62ed1581-44be-49ea-a095-5fef84834820",
      "name": "Prepare Guardian check",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ],
      "notesInFlow": true,
      "notes": "Builds the fields Guardian checks and the key. The refund step reads the amount from here."
    },
    {
      "parameters": {
        "resource": "check",
        "operation": "evaluateAndExecute",
        "actionType": "payment.refund",
        "payload": "={{ {\n  order_id: $('Prepare Guardian check').item.json.order_id,\n  amount: $('Prepare Guardian check').item.json.amount,\n  amount_valid: $('Prepare Guardian check').item.json.amount_valid,\n  currency: $('Prepare Guardian check').item.json.currency,\n  reason_code: $('Prepare Guardian check').item.json.reason_code,\n  requested_by: $('Prepare Guardian check').item.json.requested_by\n} }}",
        "requester": "refund-workflow",
        "projectSlug": "",
        "idempotencyKey": "={{ $('Prepare Guardian check').item.json.key }}",
        "options": {
          "testMode": false
        }
      },
      "id": "c2f65236-9b15-4416-b3a1-1b9a02ed1dfb",
      "name": "Guardian check",
      "type": "n8n-nodes-guardian.guardian",
      "typeVersion": 1,
      "position": [
        660,
        300
      ],
      "onError": "continueRegularOutput",
      "notesInFlow": true,
      "notes": "Credential: \"Guardian API key\". On Error = Continue: if Guardian can't be reached or answers with an error, the item goes to Denied with decision ERROR."
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "0af014c2-dd49-45ee-8664-2bc51baf7cf0",
              "leftValue": "={{ $json.decision === 'ALLOW' && !!$json.intentRunId && $json._guardian?.duplicateBlocked !== true }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "d3ef1e30-0553-4fe9-a399-68ddfb096955",
      "name": "Real go-ahead?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        140
      ],
      "notesInFlow": true,
      "notes": "Only a real ALLOW with a record ID and no duplicate reaches the refund step."
    },
    {
      "parameters": {
        "jsCode": "// STUB: nothing is refunded. Replace with your payment step; it must read the amount from \"Prepare Guardian check\".\nreturn [{ json: { step: 'would refund now (stub, nothing moves)', order_id: $('Prepare Guardian check').item.json.order_id, amount: $('Prepare Guardian check').item.json.amount, currency: $('Prepare Guardian check').item.json.currency, intentRunId: $json.intentRunId } }];"
      },
      "id": "a2c6d465-38fb-4295-954e-0a178c33c0b0",
      "name": "Refund (stub)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        60
      ],
      "notesInFlow": true,
      "notes": "Stub. Reads what Guardian checked, from \"Prepare Guardian check\"."
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { step: \"alert: allowed output without a real go-ahead, nothing refunded\", order_id: $('Prepare Guardian check').item.json.order_id, amount: $('Prepare Guardian check').item.json.amount, amount_valid: $('Prepare Guardian check').item.json.amount_valid, key: $('Prepare Guardian check').item.json.key, decision: $json._guardian?.decision ?? $json.decision ?? null, duplicateBlocked: $json._guardian?.duplicateBlocked ?? false, intentRunId: $json.intentRunId ?? $json._guardian?.intentRunId ?? null, reason: $json.reason ?? null, policyHits: (Array.isArray($json.policyHits) ? $json.policyHits.map(h => h.decision + ': ' + h.reason) : null), error: $json.error?.message ?? $json.error ?? null } }];"
      },
      "id": "dd121535-fb2d-450b-a2e1-995e5d17ab9e",
      "name": "Alert: no real go-ahead",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        220
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "4bdcc3d4-8911-461d-8ac3-f3b802f52891",
              "leftValue": "={{ $json._guardian?.decision === 'ERROR' }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "05054b60-6411-4a37-93ce-c80d8ed1fed0",
      "name": "Denied or error?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        360
      ],
      "notesInFlow": true,
      "notes": "ERROR (Guardian unreachable, wrong key, a used key with another amount) goes to the alert; the rest is a rule's DENY or a blocked repeat."
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { step: \"alert: Guardian error, nothing refunded\", order_id: $('Prepare Guardian check').item.json.order_id, amount: $('Prepare Guardian check').item.json.amount, amount_valid: $('Prepare Guardian check').item.json.amount_valid, key: $('Prepare Guardian check').item.json.key, decision: $json._guardian?.decision ?? $json.decision ?? null, duplicateBlocked: $json._guardian?.duplicateBlocked ?? false, intentRunId: $json.intentRunId ?? $json._guardian?.intentRunId ?? null, reason: $json.reason ?? null, policyHits: (Array.isArray($json.policyHits) ? $json.policyHits.map(h => h.decision + ': ' + h.reason) : null), error: $json.error?.message ?? $json.error ?? null } }];"
      },
      "id": "2acd0410-8447-4f96-825a-1bf551f94cd9",
      "name": "Alert: Guardian error",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        320
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { step: \"not refunded: denied by a rule or a repeat of an earlier request\", order_id: $('Prepare Guardian check').item.json.order_id, amount: $('Prepare Guardian check').item.json.amount, amount_valid: $('Prepare Guardian check').item.json.amount_valid, key: $('Prepare Guardian check').item.json.key, decision: $json._guardian?.decision ?? $json.decision ?? null, duplicateBlocked: $json._guardian?.duplicateBlocked ?? false, intentRunId: $json.intentRunId ?? $json._guardian?.intentRunId ?? null, reason: $json.reason ?? null, policyHits: (Array.isArray($json.policyHits) ? $json.policyHits.map(h => h.decision + ': ' + h.reason) : null), error: $json.error?.message ?? $json.error ?? null } }];"
      },
      "id": "8436f3d4-1ba2-4495-9bc2-375594ff6362",
      "name": "Not refunded (denied or repeat)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        460
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { step: \"waiting for a person in the Guardian dashboard\", order_id: $('Prepare Guardian check').item.json.order_id, amount: $('Prepare Guardian check').item.json.amount, amount_valid: $('Prepare Guardian check').item.json.amount_valid, key: $('Prepare Guardian check').item.json.key, decision: $json._guardian?.decision ?? $json.decision ?? null, duplicateBlocked: $json._guardian?.duplicateBlocked ?? false, intentRunId: $json.intentRunId ?? $json._guardian?.intentRunId ?? null, reason: $json.reason ?? null, policyHits: (Array.isArray($json.policyHits) ? $json.policyHits.map(h => h.decision + ': ' + h.reason) : null), error: $json.error?.message ?? $json.error ?? null } }];"
      },
      "id": "cd639221-b4ce-4a07-bc1a-33324f0b62ff",
      "name": "Waiting for a person",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        600
      ]
    },
    {
      "parameters": {
        "content": "## Refund with Guardian (TEST)\n\nSample data. The refund step is a stub: nothing moves money.\n\nRefund request, then Prepare Guardian check, then one Guardian check (action type payment.refund).\n- Allowed: an IF lets only a real go-ahead through, then the refund stub.\n- Denied: ERROR goes to an alert, a rule's DENY or a repeat ends in \"Not refunded\".\n- Needs approval: waits for a person in the Guardian dashboard. The approved refund runs in the separate approvals workflow.\n\nSet CASE and RUN in \"Refund request\".",
        "height": 300,
        "width": 560,
        "color": 4
      },
      "id": "84822178-07ab-477b-8272-06c3831c7ceb",
      "name": "How it works",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -260
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Refund request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Refund request": {
      "main": [
        [
          {
            "node": "Prepare Guardian check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Guardian check": {
      "main": [
        [
          {
            "node": "Guardian check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Guardian check": {
      "main": [
        [
          {
            "node": "Real go-ahead?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Denied or error?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Waiting for a person",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Real go-ahead?": {
      "main": [
        [
          {
            "node": "Refund (stub)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Alert: no real go-ahead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Denied or error?": {
      "main": [
        [
          {
            "node": "Alert: Guardian error",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not refunded (denied or repeat)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "pinData": {},
  "tags": []
}