{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agentictradestandard.org/schemas/checkout-outcomes.schema.json",
  "title": "SACP checkout outcomes",
  "description": "Bodies a shop returns from POST /checkout/prepare and POST /checkout/{checkout_id}/confirm. Error bodies are problem+json (RFC 9457) members plus the members below. Added in SACP 1.1.0-draft, extended in 1.3.0-draft.",
  "oneOf": [
    {
      "$ref": "#/$defs/PrepareOk"
    },
    {
      "$ref": "#/$defs/TermsChanged"
    },
    {
      "$ref": "#/$defs/ApprovalRequired"
    },
    {
      "$ref": "#/$defs/AuthorityDenied"
    }
  ],
  "$defs": {
    "TermsHash": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "DiffEntry": {
      "type": "object",
      "required": [
        "path",
        "approved",
        "current",
        "change",
        "material"
      ],
      "additionalProperties": false,
      "properties": {
        "path": {
          "type": "string"
        },
        "approved": {},
        "current": {},
        "change": {
          "enum": [
            "increase",
            "decrease",
            "changed",
            "unavailable",
            "added",
            "removed"
          ]
        },
        "material": {
          "type": "boolean"
        }
      }
    },
    "PrepareOk": {
      "description": "HTTP 200. The checkout resource. For human_handoff the handoff is issued and status is awaiting_human. For autonomous the status is ready_to_confirm. Extended in 1.3.0-draft with checkout_id, status, terms and order.",
      "type": "object",
      "required": [
        "checkout_id",
        "status"
      ],
      "properties": {
        "checkout_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "status": {
          "enum": [
            "awaiting_human",
            "ready_to_confirm",
            "confirmed",
            "expired",
            "cancelled",
            "failed"
          ]
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        },
        "expires_at": {
          "type": "string",
          "format": "date-time"
        },
        "terms_hash": {
          "$ref": "#/$defs/TermsHash"
        },
        "terms": {
          "$ref": "terms-snapshot.schema.json",
          "description": "The current terms snapshot. Required when status is ready_to_confirm."
        },
        "order": {
          "type": "object",
          "required": [
            "order_id",
            "confirmed_at",
            "agent_reference"
          ],
          "additionalProperties": false,
          "properties": {
            "order_id": {
              "type": "string"
            },
            "confirmed_at": {
              "type": "string",
              "format": "date-time"
            },
            "agent_reference": {
              "$ref": "#/$defs/AgentReference"
            }
          }
        },
        "agent_reference": {
          "$ref": "#/$defs/AgentReference"
        },
        "failure": {
          "type": "object",
          "properties": {
            "code": {
              "type": "string"
            },
            "detail": {
              "type": "string"
            }
          }
        },
        "terms_check": {
          "type": "object",
          "required": [
            "status",
            "terms_hash"
          ],
          "properties": {
            "status": {
              "enum": [
                "match",
                "within_tolerance"
              ]
            },
            "terms_hash": {
              "$ref": "#/$defs/TermsHash"
            },
            "current_terms_hash": {
              "$ref": "#/$defs/TermsHash"
            },
            "changes": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/DiffEntry"
              }
            }
          }
        },
        "authority_check": {
          "type": "object",
          "required": [
            "status"
          ],
          "properties": {
            "status": {
              "const": "covered"
            },
            "mandate_id": {
              "type": "string"
            },
            "level": {
              "enum": [
                "consumer",
                "enterprise"
              ]
            },
            "approvals_verified": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "audit_event_id": {
              "type": "string"
            }
          }
        },
        "handoff": {
          "type": "object",
          "required": [
            "type",
            "url"
          ],
          "properties": {
            "type": {
              "type": "string"
            },
            "url": {
              "type": "string",
              "format": "uri"
            },
            "expires_at": {
              "type": "string",
              "format": "date-time"
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "awaiting_human"
              }
            },
            "required": [
              "status"
            ]
          },
          "then": {
            "required": [
              "handoff",
              "terms_hash"
            ],
            "properties": {
              "handoff": true,
              "terms_hash": true
            },
            "additionalProperties": true
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "ready_to_confirm"
              }
            },
            "required": [
              "status"
            ]
          },
          "then": {
            "required": [
              "terms",
              "terms_hash",
              "expires_at"
            ],
            "properties": {
              "terms": true,
              "terms_hash": true,
              "expires_at": true
            },
            "additionalProperties": true
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "confirmed"
              }
            },
            "required": [
              "status"
            ]
          },
          "then": {
            "required": [
              "order"
            ],
            "properties": {
              "order": true
            },
            "additionalProperties": true
          }
        }
      ]
    },
    "TermsChanged": {
      "description": "HTTP 409. A material change voided the approval. No handoff is issued.",
      "type": "object",
      "required": [
        "code",
        "approved_terms_hash",
        "current_terms_hash",
        "diff",
        "current_terms",
        "reconfirm_required"
      ],
      "properties": {
        "code": {
          "const": "terms_changed"
        },
        "approved_terms_hash": {
          "$ref": "#/$defs/TermsHash"
        },
        "current_terms_hash": {
          "$ref": "#/$defs/TermsHash"
        },
        "within_tolerance": {
          "const": false
        },
        "reconfirm_required": {
          "const": true
        },
        "diff": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/DiffEntry"
          }
        },
        "current_terms": {
          "$ref": "terms-snapshot.schema.json"
        },
        "type": {
          "type": "string",
          "format": "uri"
        },
        "title": {
          "type": "string"
        },
        "status": {
          "type": "integer"
        },
        "detail": {
          "type": "string"
        },
        "instance": {
          "type": "string",
          "format": "uri-reference"
        },
        "trace_id": {
          "type": "string"
        },
        "retryable": {
          "type": "boolean"
        }
      }
    },
    "ApprovalRequired": {
      "description": "HTTP 428. The amount is above an approval threshold and no sufficient approval is bound to the terms hash.",
      "type": "object",
      "required": [
        "code",
        "terms_hash",
        "required"
      ],
      "properties": {
        "code": {
          "const": "approval_required"
        },
        "terms_hash": {
          "$ref": "#/$defs/TermsHash"
        },
        "mandate_id": {
          "type": "string"
        },
        "threshold": {
          "type": "object",
          "properties": {
            "above": {
              "type": "string",
              "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$"
            }
          }
        },
        "required": {
          "type": "object",
          "required": [
            "approvals"
          ],
          "properties": {
            "approvals": {
              "type": "integer",
              "minimum": 1
            },
            "approver_roles": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "approver_ids": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        },
        "approval_request_uri": {
          "type": "string",
          "format": "uri"
        },
        "expires_at": {
          "type": "string",
          "format": "date-time"
        },
        "type": {
          "type": "string",
          "format": "uri"
        },
        "title": {
          "type": "string"
        },
        "status": {
          "type": "integer"
        },
        "detail": {
          "type": "string"
        },
        "instance": {
          "type": "string",
          "format": "uri-reference"
        },
        "trace_id": {
          "type": "string"
        },
        "retryable": {
          "type": "boolean"
        }
      }
    },
    "AuthorityDenied": {
      "description": "HTTP 403. The mandate does not cover the transaction.",
      "type": "object",
      "required": [
        "code",
        "reasons"
      ],
      "properties": {
        "code": {
          "const": "authority_denied"
        },
        "mandate_id": {
          "type": "string"
        },
        "reasons": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": [
              "mandate_missing",
              "mandate_signature_invalid",
              "issuer_untrusted",
              "mandate_not_yet_valid",
              "mandate_expired",
              "mandate_revoked",
              "status_unavailable",
              "delegation_invalid",
              "agent_mismatch",
              "scope_category",
              "scope_merchant",
              "scope_country",
              "scope_currency",
              "limit_per_transaction",
              "limit_per_period",
              "limit_count",
              "approval_invalid",
              "approval_unbound",
              "separation_of_duties",
              "terms_not_bound",
              "outside_standing_policy"
            ]
          }
        },
        "type": {
          "type": "string",
          "format": "uri"
        },
        "title": {
          "type": "string"
        },
        "status": {
          "type": "integer"
        },
        "detail": {
          "type": "string"
        },
        "instance": {
          "type": "string",
          "format": "uri-reference"
        },
        "trace_id": {
          "type": "string"
        },
        "retryable": {
          "type": "boolean"
        }
      }
    },
    "AgentReference": {
      "type": "object",
      "required": [
        "key_id",
        "checkout_id",
        "ref"
      ],
      "additionalProperties": false,
      "description": "The agent reference. A critical data element added in 1.3.0-draft. It ties an order to the agent key, the mandate and the checkout. The shop sets it, never changes it, and keeps it through settlement and reconciliation.",
      "properties": {
        "key_id": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{43}$",
          "description": "RFC 7638 thumbprint (SHA-256, base64url) of the key that signed the prepare call."
        },
        "mandate_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64,
          "description": "The mandate_id of the mandate presented with the call. Absent when no mandate was presented."
        },
        "checkout_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "ref": {
          "type": "string",
          "maxLength": 256,
          "pattern": "^[A-Za-z0-9_-]{43}/[^/\\s]+/[^/\\s]+$",
          "description": "Compact form: key_id/mandate_id/checkout_id, with - in place of an absent mandate_id. Fits a payment reference or metadata field."
        }
      }
    }
  }
}
