{
  "name": "TrustLoop + Signatrust: Customer Refund",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ai_refund_request",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "trigger-1",
      "name": "AI Refund Request",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.trustloop.live/api/intercept",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{$env.TRUSTLOOP_API_KEY}}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\"tool_name\": \"issue_refund\", \"arguments\": {{JSON.stringify($json.body)}}, \"agent_name\": \"n8n-refund-workflow\"}",
        "options": {}
      },
      "id": "trustloop-1",
      "name": "TrustLoop Policy Check",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.allowed}}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-1",
      "name": "Is Allowed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        400,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const input = $('AI Refund Request').item.json.body;\nreturn {\n  action: 'refund_processed',\n  amount: input.amount || 0,\n  currency: input.currency || 'USD',\n  customer_id: input.customer_id || 'unknown',\n  reason: input.reason || 'customer_request',\n  timestamp: new Date().toISOString()\n};"
      },
      "id": "execute-1",
      "name": "Process Stripe Refund",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        -100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://signatrust.net/api/v1/n8n/receipts",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{$env.SIGNATRUST_API_KEY}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{JSON.stringify({\"decision\": \"Approved refund of \" + ($json.currency || 'USD') + ' ' + ($json.amount || 0) + \" for customer \" + ($json.customer_id || 'unknown') + \" \u2014 reason: \" + ($json.reason || 'customer_request') + \" \u2014 governed by TrustLoop policy\", \"agent_id\": \"n8n-refund-workflow\", \"action\": \"customer_refund_issued\", \"metadata\": {\"governed_by\": \"TrustLoop\", \"amount\": $json.amount, \"customer_id\": $json.customer_id}})}}",
        "options": {}
      },
      "id": "signatrust-1",
      "name": "Signatrust Seal Receipt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        800,
        -100
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://signatrust.net/api/v1/n8n/receipts/{{$json.receipt_id}}/verify",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{$env.SIGNATRUST_API_KEY}}"
            }
          ]
        },
        "options": {}
      },
      "id": "verify-1",
      "name": "Verify Receipt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1000,
        -100
      ]
    },
    {
      "parameters": {
        "jsCode": "const receipt = $('Signatrust Seal Receipt').item.json;\nconst verification = $json;\nreturn {\n  status: 'completed',\n  receipt_id: receipt.receipt_id,\n  verified: verification.valid,\n  share_url: receipt.share_url,\n  hash: receipt.hash,\n  signature: receipt.signature\n};"
      },
      "id": "respond-1",
      "name": "Return Audit Trail",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1200,
        -100
      ]
    },
    {
      "parameters": {
        "jsCode": "return {\n  status: 'blocked',\n  reason: $('TrustLoop Policy Check').item.json.message || 'Policy violation detected',\n  action: 'refund_denied'\n};"
      },
      "id": "block-1",
      "name": "Handle Blocked Action",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        100
      ]
    }
  ],
  "connections": {
    "AI Refund Request": {
      "main": [
        [
          {
            "node": "TrustLoop Policy Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TrustLoop Policy Check": {
      "main": [
        [
          {
            "node": "Is Allowed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Allowed?": {
      "main": [
        [
          {
            "node": "Process Stripe Refund",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Blocked Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Stripe Refund": {
      "main": [
        [
          {
            "node": "Signatrust Seal Receipt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Signatrust Seal Receipt": {
      "main": [
        [
          {
            "node": "Verify Receipt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify Receipt": {
      "main": [
        [
          {
            "node": "Return Audit Trail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2.0"
}