{
  "name": "Refund with Guardian - approvals (publish, never Execute)",
  "nodes": [
    {
      "parameters": {
        "includeRejected": false,
        "actionTypeFilter": "payment.refund"
      },
      "id": "7633e208-5fa8-4b05-b7d1-ce66ab499a65",
      "name": "Guardian Approval Trigger",
      "type": "n8n-nodes-guardian.guardianApprovalTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ],
      "webhookId": "0650e213-0c68-418a-a32e-e709ea01b061",
      "notesInFlow": true,
      "notes": "Starts once per approval of a payment.refund request. Publish this workflow; never press Execute here."
    },
    {
      "parameters": {
        "resource": "enforce",
        "enforceOperation": "verifyOnly",
        "enforceIntentRunId": "={{ $('Guardian Approval Trigger').item.json.intentRunId }}",
        "expectedActionType": "payment.refund",
        "expectedProjectSlug": "",
        "expectedIdempotencyKey": "",
        "enforceTestMode": false
      },
      "id": "95960d8f-92fe-424c-a2f0-07f5b0b117f2",
      "name": "Read the approved refund",
      "type": "n8n-nodes-guardian.guardian",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "onError": "continueRegularOutput",
      "notesInFlow": true,
      "notes": "Verify only, no claim yet. Returns the approved payload and, if a reviewer edited it, the original."
    },
    {
      "parameters": {
        "jsCode": "// The workflow's own check, not Guardian's: an edit may lower the amount, never raise it,\n// and may not change the order or the currency. Guardian re-checks an edit against the rules,\n// not against the original request.\nconst approved = $json.payloadJson || {};\nconst original = $json.originalPayloadJson || approved;   // null when nobody edited it\nconst problems = [];\nif (approved.amount_valid !== true || typeof approved.amount !== 'number' || !(approved.amount > 0)) problems.push('no valid amount');\nif (typeof original.amount === 'number' && typeof approved.amount === 'number' && approved.amount > original.amount) problems.push(`amount raised from ${original.amount} to ${approved.amount}`);\nif (approved.order_id !== original.order_id) problems.push('order changed');\nif (approved.currency !== original.currency) problems.push('currency changed');\nreturn [{ json: { ...$json, edit_ok: problems.length === 0, edit_problems: problems, approved_amount: approved.amount ?? null, original_amount: original.amount ?? null } }];"
      },
      "id": "f4a4bddd-af3a-4d77-b421-6ff565ecbf1d",
      "name": "Compare with the original request",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "c946a0ed-5802-479a-aa70-bb40e71c8517",
              "leftValue": "={{ $json.edit_ok === true }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "4515b321-7807-49f8-a62b-53841bd50b0a",
      "name": "Edit within the request?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        720,
        200
      ]
    },
    {
      "parameters": {
        "resource": "enforce",
        "enforceOperation": "claim",
        "enforceIntentRunId": "={{ $('Guardian Approval Trigger').item.json.intentRunId }}",
        "expectedActionType": "payment.refund",
        "expectedProjectSlug": "",
        "expectedIdempotencyKey": "",
        "enforceTestMode": false
      },
      "id": "df970e35-6835-4fd0-9cf4-3b4a9ab94b39",
      "name": "Guardian enforce (claim once)",
      "type": "n8n-nodes-guardian.guardian",
      "typeVersion": 1,
      "position": [
        960,
        100
      ],
      "onError": "continueRegularOutput",
      "notesInFlow": true,
      "notes": "Claims the approval once. Payload field left empty: it is taken from the item's payloadJson."
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "ae7c32ca-e2b0-4f1c-8eeb-df0879e59ff2",
              "leftValue": "={{ $json._guardian?.decision === 'ALLOW' && $json._guardian?.executed === true && $json._guardian?.verified !== false }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "45bab53b-6926-4bd5-960b-57a24b84aa93",
      "name": "Real go-ahead? (approval)",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1200,
        40
      ]
    },
    {
      "parameters": {
        "jsCode": "// STUB: nothing is refunded. Replace with your payment step; it must read the amount from payloadJson (the approved version).\nreturn [{ json: { step: 'would refund now (stub, nothing moves)', order_id: $json.payloadJson?.order_id ?? null, amount: $json.payloadJson?.amount ?? null, currency: $json.payloadJson?.currency ?? null, edited: $json.payloadEdited === true, original_amount: $json.originalPayloadJson?.amount ?? null, intentRunId: $('Guardian Approval Trigger').item.json.intentRunId } }];"
      },
      "id": "479ba72c-cbcf-499f-a928-f9819b871eda",
      "name": "Refund approved amount (stub)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1440,
        -40
      ],
      "notesInFlow": true,
      "notes": "Stub. Reads the approved payload (payloadJson), including an edit."
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { step: \"not refunded: claim failed or Guardian error\", intentRunId: $('Guardian Approval Trigger').item.json.intentRunId, decision: $json._guardian?.decision ?? null, status: $json._guardian?.status ?? $json.status ?? null, edit_problems: $json.edit_problems ?? null, error: $json.error?.message ?? $json.error ?? null } }];"
      },
      "id": "8a2f460b-6254-43dc-ba72-f2bb0dfb2a16",
      "name": "Not refunded (claim failed or error)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1440,
        160
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { step: \"not refunded: the approved edit is outside the original request (record stays approved, not executed)\", intentRunId: $('Guardian Approval Trigger').item.json.intentRunId, decision: $json._guardian?.decision ?? null, status: $json._guardian?.status ?? $json.status ?? null, edit_problems: $json.edit_problems ?? null, error: $json.error?.message ?? $json.error ?? null } }];"
      },
      "id": "fa53c7da-992e-486d-8631-bbcf124feda0",
      "name": "Not refunded: edit outside the request",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        960,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { step: \"not refunded: not approved or Guardian error\", intentRunId: $('Guardian Approval Trigger').item.json.intentRunId, decision: $json._guardian?.decision ?? null, status: $json._guardian?.status ?? $json.status ?? null, edit_problems: $json.edit_problems ?? null, error: $json.error?.message ?? $json.error ?? null } }];"
      },
      "id": "dfd67778-c084-41cb-9c14-6759fb58eb95",
      "name": "Not refunded (not approved or error)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        460
      ]
    },
    {
      "parameters": {
        "content": "## Refund with Guardian, approvals\n\nPublish this workflow and leave it published. Never press Execute here: a test run in the editor can switch off the approval registration (node versions up to 1.0.11).\n\nEvery approval in the Guardian dashboard starts one run. The workflow reads the approved refund, checks that an edit only lowered the amount, claims the approval once, then reaches the refund stub. Nothing moves money.",
        "height": 260,
        "width": 560,
        "color": 4
      },
      "id": "2858c2a7-788c-42a8-a564-4685a935d76f",
      "name": "How it works",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -260
      ]
    }
  ],
  "connections": {
    "Guardian Approval Trigger": {
      "main": [
        [
          {
            "node": "Read the approved refund",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read the approved refund": {
      "main": [
        [
          {
            "node": "Compare with the original request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not refunded (not approved or error)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not refunded (not approved or error)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compare with the original request": {
      "main": [
        [
          {
            "node": "Edit within the request?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit within the request?": {
      "main": [
        [
          {
            "node": "Guardian enforce (claim once)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not refunded: edit outside the request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Guardian enforce (claim once)": {
      "main": [
        [
          {
            "node": "Real go-ahead? (approval)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not refunded (claim failed or error)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not refunded (claim failed or error)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Real go-ahead? (approval)": {
      "main": [
        [
          {
            "node": "Refund approved amount (stub)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Not refunded (claim failed or error)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "pinData": {},
  "tags": []
}