{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:orbiplex:schema:gateway-receipt:v1",
  "title": "GatewayReceipt v1",
  "description": "Machine-readable schema for a signed fiat-to-credit or credit-to-fiat crossing performed by a trusted prepaid gateway node. The signed surface uses `orbiplex-gateway-receipt-v1\\x00 || deterministic_cbor(payload_without_signature)`.",
  "type": "object",
  "additionalProperties": true,
  "x-dia-workflow": "project",
  "x-dia-status": "draft",
  "x-dia-basis": [
    "doc/project/40-proposals/016-supervised-prepaid-gateway-and-escrow-mvp.md",
    "doc/project/50-requirements/requirements-007.md",
    "doc/project/50-requirements/requirements-008.md"
  ],
  "required": [
    "schema/v",
    "receipt/id",
    "gateway/node-id",
    "direction",
    "external/amount",
    "external/currency",
    "internal/amount",
    "internal/currency",
    "account/id",
    "ts",
    "external/payment-ref",
    "gateway-policy/ref",
    "signature"
  ],
  "properties": {
    "schema/v": {
      "const": 1,
      "description": "Schema version."
    },
    "receipt/id": {
      "type": "string",
      "minLength": 1,
      "description": "Stable identifier of the gateway receipt."
    },
    "gateway/node-id": {
      "type": "string",
      "minLength": 1,
      "description": "Trusted gateway node that performed the external settlement crossing."
    },
    "direction": {
      "type": "string",
      "enum": ["inbound", "outbound"],
      "description": "Direction of value crossing the protocol boundary. `inbound` credits a local account, `outbound` debits it for payout."
    },
    "external/amount": {
      "type": "number",
      "exclusiveMinimum": 0,
      "description": "Gross amount observed on the external payment rail before any explicit ingress fee is applied."
    },
    "external/currency": {
      "type": "string",
      "minLength": 2,
      "maxLength": 16,
      "description": "External settlement currency or tender symbol."
    },
    "internal/amount": {
      "type": "integer",
      "minimum": 1,
      "description": "Net amount credited or debited to `account/id` in internal minor units. For `ORC`, the value uses ORC minor units with fixed scale `2`."
    },
    "internal/currency": {
      "const": "ORC",
      "description": "Internal settlement unit used by the supervised ledger in MVP. `ORC` uses fixed decimal scale `2`."
    },
    "account/id": {
      "type": "string",
      "minLength": 1,
      "description": "Local supervised account affected by the gateway event."
    },
    "ts": {
      "type": "string",
      "format": "date-time",
      "description": "Timestamp when the gateway event was committed for audit."
    },
    "external/payment-ref": {
      "type": "string",
      "minLength": 1,
      "description": "Gateway-side payment reference such as a PSP transaction id or bank transfer reference."
    },
    "gateway-policy/ref": {
      "type": "string",
      "pattern": "^gateway-policy:[a-z0-9][a-z0-9:-]*$",
      "description": "Gateway policy under which this boundary crossing was executed."
    },
    "fee/external-amount": {
      "type": "number",
      "minimum": 0,
      "description": "Explicit fee amount deducted from the gross external amount."
    },
    "fee/rate": {
      "type": "number",
      "minimum": 0,
      "maximum": 1,
      "description": "Fee rate applied to the gross external amount."
    },
    "fee/destination-account-id": {
      "type": "string",
      "minLength": 1,
      "description": "Ledger account that received the internal fee portion, typically the `community-pool`."
    },
    "net/external-amount": {
      "type": "number",
      "exclusiveMinimum": 0,
      "description": "External amount remaining after explicit fee deduction."
    },
    "rate/applied": {
      "type": "number",
      "exclusiveMinimum": 0,
      "description": "Applied conversion rate from one external unit into internal minor units. For `ORC`, one unit of this rate equals one ORC minor unit at fixed scale `2`."
    },
    "internal/fee-amount": {
      "type": "integer",
      "minimum": 0,
      "description": "Internal minor-unit amount credited to the fee destination account. For `ORC`, the value uses ORC minor units with fixed scale `2`."
    },
    "external/provider": {
      "type": "string",
      "minLength": 1,
      "description": "Payment service provider or banking rail label used by the gateway."
    },
    "exchange-policy/ref": {
      "type": "string",
      "minLength": 1,
      "description": "Optional reference to the gateway-side pricing or exchange policy in force for this event."
    },
    "notes": {
      "type": "string",
      "description": "Optional human-readable notes."
    },
    "signature": {
      "$ref": "#/$defs/signature",
      "description": "Gateway node signature over the receipt payload."
    },
    "policy_annotations": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "$defs": {
    "signature": {
      "type": "object",
      "required": ["alg", "value"],
      "properties": {
        "alg": {
          "type": "string",
          "enum": ["ed25519"]
        },
        "value": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "direction": {
            "const": "inbound"
          }
        },
        "required": ["direction"]
      },
      "then": {
        "required": [
          "fee/external-amount",
          "fee/rate",
          "fee/destination-account-id",
          "net/external-amount",
          "rate/applied",
          "internal/fee-amount"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "direction": {
            "const": "outbound"
          }
        },
        "required": ["direction"]
      },
      "then": {
        "required": ["gateway-policy/ref"]
      }
    }
  ]
}
