{
  "name": "Refund with Guardian",
  "nodes": [
    {
      "parameters": {},
      "id": "4245a4a9-b21c-4480-955d-605bc7fa89ff",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// One refund request, sample data. Replace this node with your own trigger\n// (form, helpdesk, webhook) and map its fields to these names.\nreturn [{ json: {\n  order_id: 'ORD-1001',\n  refund_request_id: 'R1',      // one ID per refund request: part of the key\n  amount: 40,\n  currency: 'EUR',\n  reason_code: 'damaged_item',\n  requested_by: 'support-agent-1',\n} }];"
      },
      "id": "44a93f9d-53c9-4821-9e89-58307cc6c73f",
      "name": "Refund request",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        300
      ],
      "notesInFlow": true,
      "notes": "Sample data. Replace with your own trigger and map the fields."
    },
    {
      "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": "1cc61d62-3f9e-40bf-ab88-6c4f4194409a",
      "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": "226b955c-6348-4cd4-b465-70bc826a7a64",
      "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": "1de717b5-d6e3-4f34-b73e-b18602270026",
              "leftValue": "={{ $json.decision === 'ALLOW' && !!$json.intentRunId && $json._guardian?.duplicateBlocked !== true }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "f880f53c-71b6-4cc9-915b-cf445b10f7e5",
      "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": "228b401f-7866-404f-9ea4-a78c78309fad",
      "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": "70011f99-44c4-4d79-b768-28e4c5dbd663",
      "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": "d7ffa59e-d42b-4ca1-aa84-1e51cb100856",
              "leftValue": "={{ $json._guardian?.decision === 'ERROR' }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "0d20cab9-8ce9-4ca4-a2e7-3088aa18117a",
      "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": "8f5ba5e3-70ff-402e-bda0-acfa67c8e77b",
      "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": "0f4b86ca-beab-4748-9bd1-ad906381e3f1",
      "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": "7700947d-dc4a-49a3-9478-14643027f6dd",
      "name": "Waiting for a person",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        600
      ]
    },
    {
      "parameters": {
        "content": "## Refund with Guardian\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.",
        "height": 300,
        "width": 560,
        "color": 4
      },
      "id": "c52f22e8-6ebd-43eb-8473-12ac19bf03b2",
      "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": []
}