{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agenthandelstandard.se/schemas/checkout-outcomes.schema.json",
  "title": "SACP checkout outcomes",
  "description": "Bodies a shop returns from POST /checkout/prepare when it advertises terms.binding or authority.mandate. Added in SACP 1.1.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 handoff is issued.",
      "type": "object",
      "required": [
        "handoff"
      ],
      "properties": {
        "handoff": {
          "type": "object",
          "required": [
            "type",
            "url"
          ],
          "properties": {
            "type": {
              "type": "string"
            },
            "url": {
              "type": "string",
              "format": "uri"
            }
          }
        },
        "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"
            }
          }
        }
      }
    },
    "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"
        }
      }
    },
    "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"
        }
      }
    },
    "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"
            ]
          }
        }
      }
    }
  }
}
