{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agentictradestandard.org/schemas/mandate.schema.json",
  "title": "SACP mandate",
  "description": "Authority to commit a buyer to a financial obligation, separate from the technical ability to call the shop API. Carried as the payload of a signed JWS. Added in SACP 1.1.0-draft.",
  "type": "object",
  "required": [
    "sacp_mandate_version",
    "mandate_id",
    "level",
    "issuer",
    "principal",
    "agent",
    "scope",
    "limits",
    "revocation",
    "validity"
  ],
  "additionalProperties": false,
  "properties": {
    "sacp_mandate_version": {
      "const": "1.1"
    },
    "mandate_id": {
      "type": "string",
      "minLength": 1
    },
    "level": {
      "enum": [
        "consumer",
        "enterprise"
      ]
    },
    "issuer": {
      "type": "object",
      "required": [
        "id"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "format": "uri"
        },
        "kid": {
          "type": "string"
        },
        "jwks_uri": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "principal": {
      "type": "object",
      "required": [
        "type",
        "id"
      ],
      "additionalProperties": false,
      "properties": {
        "type": {
          "enum": [
            "person",
            "organization"
          ]
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        }
      }
    },
    "delegated_by": {
      "type": "object",
      "required": [
        "id"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "role": {
          "type": "string"
        }
      }
    },
    "delegation_chain": {
      "type": "array",
      "maxItems": 5,
      "items": {
        "type": "object",
        "required": [
          "delegator",
          "delegate"
        ],
        "additionalProperties": false,
        "properties": {
          "delegator": {
            "type": "string"
          },
          "delegate": {
            "type": "string"
          },
          "max_per_transaction": {
            "type": "object",
            "required": [
              "amount",
              "currency"
            ],
            "additionalProperties": false,
            "properties": {
              "amount": {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
              },
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              }
            }
          },
          "not_after": {
            "type": "string",
            "format": "date-time"
          }
        }
      }
    },
    "agent": {
      "type": "object",
      "required": [
        "id",
        "key_thumbprint"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string"
        },
        "key_thumbprint": {
          "type": "string",
          "description": "RFC 7638 JWK thumbprint of the key the agent signs requests with."
        }
      }
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "categories": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "merchants": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "countries": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[A-Z]{2}$"
          }
        },
        "currencies": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[A-Z]{3}$"
          }
        }
      }
    },
    "limits": {
      "type": "object",
      "required": [
        "per_transaction"
      ],
      "additionalProperties": false,
      "properties": {
        "per_transaction": {
          "type": "object",
          "required": [
            "amount",
            "currency"
          ],
          "additionalProperties": false,
          "properties": {
            "amount": {
              "type": "string",
              "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$"
            }
          }
        },
        "per_period": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "period",
              "amount",
              "currency"
            ],
            "additionalProperties": false,
            "properties": {
              "period": {
                "enum": [
                  "day",
                  "week",
                  "month",
                  "quarter",
                  "year"
                ]
              },
              "amount": {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
              },
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              }
            }
          }
        },
        "max_transactions_per_period": {
          "type": "integer",
          "minimum": 1
        },
        "counter_uri": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "approval_policy": {
      "type": "object",
      "required": [
        "ref"
      ],
      "additionalProperties": false,
      "properties": {
        "ref": {
          "type": "string",
          "format": "uri"
        },
        "sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "thresholds": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "above",
              "currency",
              "require"
            ],
            "additionalProperties": false,
            "properties": {
              "above": {
                "type": "string",
                "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
              },
              "currency": {
                "type": "string",
                "pattern": "^[A-Z]{3}$"
              },
              "require": {
                "type": "object",
                "required": [
                  "approvals"
                ],
                "additionalProperties": false,
                "properties": {
                  "approvals": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "approver_roles": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "approver_ids": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "revocation": {
      "type": "object",
      "required": [
        "status_uri"
      ],
      "additionalProperties": false,
      "properties": {
        "status_uri": {
          "type": "string",
          "format": "uri"
        },
        "max_check_age_seconds": {
          "type": "integer",
          "minimum": 0,
          "default": 300
        }
      }
    },
    "validity": {
      "type": "object",
      "required": [
        "not_before",
        "not_after"
      ],
      "additionalProperties": false,
      "properties": {
        "not_before": {
          "type": "string",
          "format": "date-time"
        },
        "not_after": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "standing": {
      "type": "object",
      "required": [
        "mode",
        "policy",
        "policy_approval",
        "outside_policy",
        "audit_log_uri"
      ],
      "additionalProperties": false,
      "description": "Added in 1.3.0-draft. A standing mandate or a batch approval: a person approves a policy once, and checkouts inside the policy may run autonomous. Anything outside the policy escalates to human_handoff.",
      "properties": {
        "mode": {
          "enum": [
            "standing",
            "batch"
          ],
          "description": "standing: the policy applies for the whole validity window. batch: the policy applies to a bounded set of orders (policy.batch)."
        },
        "policy": {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": false,
          "properties": {
            "per_order": {
              "$ref": "#/$defs/Money"
            },
            "per_day": {
              "$ref": "#/$defs/Money"
            },
            "max_orders_per_day": {
              "type": "integer",
              "minimum": 1
            },
            "per_supplier_per_day": {
              "$ref": "#/$defs/Money"
            },
            "per_category": {
              "type": "array",
              "items": {
                "type": "object",
                "required": [
                  "category",
                  "per_day"
                ],
                "additionalProperties": false,
                "properties": {
                  "category": {
                    "type": "string"
                  },
                  "per_day": {
                    "$ref": "#/$defs/Money"
                  }
                }
              }
            },
            "batch": {
              "type": "object",
              "required": [
                "max_orders",
                "max_total"
              ],
              "additionalProperties": false,
              "properties": {
                "max_orders": {
                  "type": "integer",
                  "minimum": 1
                },
                "max_total": {
                  "$ref": "#/$defs/Money"
                }
              }
            }
          }
        },
        "policy_approval": {
          "type": "object",
          "required": [
            "approval_id",
            "approved_at"
          ],
          "additionalProperties": false,
          "description": "The signed approval of the policy itself (approval.schema.json), given by a person entitled to bind the principal.",
          "properties": {
            "approval_id": {
              "type": "string"
            },
            "approved_at": {
              "type": "string",
              "format": "date-time"
            }
          }
        },
        "outside_policy": {
          "const": "human_handoff",
          "description": "What happens to a checkout outside the policy."
        },
        "review": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "mode": {
              "enum": [
                "exception_only",
                "sample",
                "all"
              ]
            },
            "digest_interval": {
              "enum": [
                "daily",
                "weekly"
              ]
            }
          }
        },
        "audit_log_uri": {
          "type": "string",
          "format": "uri",
          "description": "Per-mandate audit log. Every decision under this mandate, inside the policy or not."
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "level": {
            "const": "enterprise"
          }
        }
      },
      "then": {
        "required": [
          "delegated_by",
          "approval_policy"
        ],
        "properties": {
          "limits": {
            "required": [
              "per_transaction",
              "per_period"
            ]
          }
        }
      }
    }
  ],
  "$defs": {
    "MandateStatus": {
      "description": "Body returned by revocation.status_uri.",
      "type": "object",
      "required": [
        "mandate_id",
        "status",
        "as_of"
      ],
      "properties": {
        "mandate_id": {
          "type": "string"
        },
        "status": {
          "enum": [
            "active",
            "suspended",
            "revoked"
          ]
        },
        "as_of": {
          "type": "string",
          "format": "date-time"
        },
        "revoked_at": {
          "type": "string",
          "format": "date-time"
        },
        "reason": {
          "type": "string"
        }
      }
    },
    "Money": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "type": "string",
          "pattern": "^[0-9]+(\\.[0-9]{1,6})?$"
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        }
      }
    }
  }
}
