{
 "openapi": "3.1.0",
 "info": {
  "title": "رصد ← البنك المركزي السعودي والقطاع المصرفي",
  "version": "1.0.0",
  "description": "عقد الربط بين رصد (مقدم خدمة تتبع الأموال) والبنك المركزي السعودي والقطاع المصرفي. الأساس النظامي: المواد 15/3، 18/2، 20/2، 27، 33/2 من نظام التنفيذ. الدور: بيانات الحسابات والأرصدة وتنفيذ الحجز المصرفي. لا يُرسل طلب إلا بأمر قضائي نافذ يسمّي الجهة وفي حدود أطرافه وبياناته وفترته، وأرقام الهوية تُرسل مع الطلب ولا تحفظها رصد.",
  "x-agency-code": "SAMA_BANKING",
  "x-contract-version": "rasd.agency.v1"
 },
 "servers": [
  {
   "url": "https://sandbox.agency.example/rasd/v1",
   "description": "بيئة اختبار الجهة (يحددها الاتفاق)"
  },
  {
   "url": "https://api.agency.example/rasd/v1",
   "description": "التشغيل الحي (بعد الترخيص والاتفاقية والموافقة الأمنية)"
  }
 ],
 "security": [
  {
   "mutualTLS": [],
   "oauth2": []
  }
 ],
 "paths": {
  "/disclosure": {
   "post": {
    "operationId": "SAMA_BANKING_DISCLOSURE",
    "summary": "الإفصاح عن الحسابات والأرصدة والحركات",
    "description": "نوع العملية: استعلام. الأمر المطلوب: أمر تتبع الأموال. مهلة الرد: 3 أيام عمل من إبلاغ الجهة.",
    "x-rasd-enabled": true,
    "x-rasd-order-types": [
     "tracing",
     "disclosure",
     "access_enablement"
    ],
    "x-rasd-reply-business-days": 3,
    "parameters": [
     {
      "name": "Idempotency-Key",
      "in": "header",
      "required": true,
      "description": "يساوي request_reference؛ إعادة الطلب نفسه بالمفتاح نفسه تعيد الرد نفسه، وطلب مختلف بالمفتاح نفسه يُرفض بالرمز 409.",
      "schema": {
       "type": "string",
       "maxLength": 120
      }
     },
     {
      "name": "X-Request-Reference",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string",
       "maxLength": 120
      }
     },
     {
      "name": "X-Attempt",
      "in": "header",
      "required": false,
      "description": "رقم المحاولة؛ 1 للإرسال الأول، وما بعده استعلام عن رد معلق.",
      "schema": {
       "type": "integer",
       "minimum": 1
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/AgencyRequest"
       },
       "example": {
        "request_reference": "RSD-SAMA_BANKING-1d3f7c2a-0000-4000-8000-000000000001",
        "operation": "DISCLOSURE",
        "court_order_id": "رقم أمر المحكمة",
        "authorized_scope": {
         "parties": [
          "party-ref"
         ],
         "period": {
          "from": "2025-01-01",
          "to": "2026-11-27"
         },
         "fields": [
          "accounts",
          "balances",
          "statements"
         ]
        },
        "requested_fields": [
         "accounts",
         "balances",
         "statements"
        ],
        "legal_purpose": "تتبع أموال المنفذ ضده بموجب أمر المحكمة وفي حدود نطاقه",
        "subjects": [
         {
          "ref": "party-ref",
          "id_type": "cr",
          "id_number": "SBX-7001234567"
         }
        ]
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "الرد بالعقد الموحد؛ يُهمل أي مفتاح خارج العقد قبل دخوله الملف.",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "required": [
          "reference"
         ],
         "properties": {
          "reference": {
           "type": "string",
           "maxLength": 120,
           "description": "مرجع الجهة"
          },
          "data": {
           "type": "object",
           "additionalProperties": true,
           "required": [
            "accounts",
            "balances",
            "statements"
           ],
           "properties": {
            "accounts": {
             "type": "array",
             "items": {
              "type": "object",
              "additionalProperties": false,
              "description": "الحسابات البنكية",
              "properties": {
               "ref": {
                "type": "string",
                "maxLength": 1000,
                "title": "الحساب (مقنّع)"
               },
               "holder": {
                "type": "string",
                "maxLength": 1000,
                "title": "صاحب الحساب"
               },
               "balance": {
                "type": "number",
                "description": "مبلغ بالريال السعودي",
                "title": "الرصيد"
               },
               "status": {
                "type": "string",
                "maxLength": 1000,
                "title": "الحالة"
               }
              }
             },
             "description": "الحسابات البنكية"
            },
            "balances": {
             "type": "object",
             "additionalProperties": false,
             "description": "الأرصدة",
             "properties": {
              "total": {
               "type": "number",
               "description": "مبلغ بالريال السعودي",
               "title": "إجمالي الأرصدة"
              }
             }
            },
            "statements": {
             "type": "array",
             "items": {
              "type": "object",
              "additionalProperties": false,
              "description": "كشوف الحركات",
              "properties": {
               "date": {
                "type": "string",
                "format": "date",
                "title": "التاريخ"
               },
               "debit": {
                "type": "number",
                "description": "مبلغ بالريال السعودي",
                "title": "مدين"
               },
               "credit": {
                "type": "number",
                "description": "مبلغ بالريال السعودي",
                "title": "دائن"
               },
               "to": {
                "type": "string",
                "maxLength": 1000,
                "title": "المستفيد"
               },
               "memo": {
                "type": "string",
                "maxLength": 1000,
                "title": "البيان"
               }
              }
             },
             "description": "كشوف الحركات"
            }
           }
          }
         },
         "additionalProperties": true
        },
        "example": {
         "reference": "SAMA_BANKING-REF-0001",
         "data": {
          "accounts": [
           {
            "ref": "DEMO-BANK-ACC-4471",
            "holder": "P-DEBTOR",
            "balance": 120000,
            "status": "محجوز"
           }
          ],
          "balances": {
           "total": 120000
          },
          "statements": [
           {
            "date": "2026-10-20",
            "debit": 480000,
            "credit": 0,
            "to": "P-JUSOOR",
            "memo": "دفعة توريد، بحسب البيان"
           }
          ]
         }
        }
       }
      }
     },
     "202": {
      "description": "قُبل الطلب وسيرد الجواب لاحقًا بإشعار موقّع إلى رصد أو عند الاستعلام التالي.",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Accepted"
        }
       }
      }
     },
     "400": {
      "$ref": "#/components/responses/Problem"
     },
     "401": {
      "$ref": "#/components/responses/Problem"
     },
     "403": {
      "$ref": "#/components/responses/Problem"
     },
     "409": {
      "$ref": "#/components/responses/Problem"
     },
     "422": {
      "$ref": "#/components/responses/Problem"
     },
     "429": {
      "$ref": "#/components/responses/Problem"
     },
     "500": {
      "$ref": "#/components/responses/Problem"
     },
     "503": {
      "$ref": "#/components/responses/Problem"
     }
    },
    "callbacks": {
     "agencyReply": {
      "https://{rasd-origin}/api/agency-callbacks/SAMA_BANKING": {
       "post": {
        "summary": "رد غير متزامن من الجهة إلى رصد",
        "description": "يُوقَّع بـ HMAC-SHA256 على «SAMA_BANKING.<الطابع الزمني>.<بايتات الجسم الخام>» بالسر المتفق عليه لهذه الجهة وبيئتها، ويُرفض ما يزيد فارق وقته على خمس دقائق، ولا يُقبل التوقيع نفسه مرتين. يُقبل الرد مرة واحدة لكل طلب؛ والطلب المغلق في رصد يُرد بالرمز 409.",
        "parameters": [
         {
          "name": "X-Rasd-Timestamp",
          "in": "header",
          "required": true,
          "schema": {
           "type": "string",
           "pattern": "^\\d{10}$",
           "description": "ثوانٍ منذ 1970-01-01 UTC"
          }
         },
         {
          "name": "X-Rasd-Signature",
          "in": "header",
          "required": true,
          "schema": {
           "type": "string",
           "pattern": "^sha256=[a-f0-9]{64}$"
          }
         }
        ],
        "requestBody": {
         "required": true,
         "content": {
          "application/json": {
           "schema": {
            "type": "object",
            "required": [
             "request_reference",
             "data"
            ],
            "properties": {
             "request_reference": {
              "type": "string"
             },
             "reference": {
              "type": "string",
              "maxLength": 120
             },
             "data": {
              "type": "object",
              "additionalProperties": true,
              "required": [
               "accounts",
               "balances",
               "statements"
              ],
              "properties": {
               "accounts": {
                "type": "array",
                "items": {
                 "type": "object",
                 "additionalProperties": false,
                 "description": "الحسابات البنكية",
                 "properties": {
                  "ref": {
                   "type": "string",
                   "maxLength": 1000,
                   "title": "الحساب (مقنّع)"
                  },
                  "holder": {
                   "type": "string",
                   "maxLength": 1000,
                   "title": "صاحب الحساب"
                  },
                  "balance": {
                   "type": "number",
                   "description": "مبلغ بالريال السعودي",
                   "title": "الرصيد"
                  },
                  "status": {
                   "type": "string",
                   "maxLength": 1000,
                   "title": "الحالة"
                  }
                 }
                },
                "description": "الحسابات البنكية"
               },
               "balances": {
                "type": "object",
                "additionalProperties": false,
                "description": "الأرصدة",
                "properties": {
                 "total": {
                  "type": "number",
                  "description": "مبلغ بالريال السعودي",
                  "title": "إجمالي الأرصدة"
                 }
                }
               },
               "statements": {
                "type": "array",
                "items": {
                 "type": "object",
                 "additionalProperties": false,
                 "description": "كشوف الحركات",
                 "properties": {
                  "date": {
                   "type": "string",
                   "format": "date",
                   "title": "التاريخ"
                  },
                  "debit": {
                   "type": "number",
                   "description": "مبلغ بالريال السعودي",
                   "title": "مدين"
                  },
                  "credit": {
                   "type": "number",
                   "description": "مبلغ بالريال السعودي",
                   "title": "دائن"
                  },
                  "to": {
                   "type": "string",
                   "maxLength": 1000,
                   "title": "المستفيد"
                  },
                  "memo": {
                   "type": "string",
                   "maxLength": 1000,
                   "title": "البيان"
                  }
                 }
                },
                "description": "كشوف الحركات"
               }
              }
             }
            }
           }
          }
         }
        },
        "responses": {
         "200": {
          "description": "استُلم الرد، أو استُلم من قبل (duplicate)"
         },
         "400": {
          "description": "مرجع طلب غير معروف"
         },
         "401": {
          "description": "لم يُقبل الإشعار"
         },
         "409": {
          "description": "الطلب مغلق في رصد"
         },
         "422": {
          "description": "الرد يخالف العقد الموحد"
         }
        }
       }
      }
     }
    }
   }
  },
  "/attachment": {
   "post": {
    "operationId": "SAMA_BANKING_ATTACHMENT",
    "summary": "إبلاغ البنوك بأمر الحجز على الحسابات الصادر من المحكمة",
    "description": "غير مفعّلة في رصد: إبلاغ هذا الأمر من عمل المحكمة والجهة المشرفة، لا من عمل مقدم خدمة تتبع الأموال (م27/1 من النظام، م30/2 و53 من اللائحة). نوع العملية: إبلاغ أمر قضائي. الأمر المطلوب: أمر الحجز. مهلة الرد: 3 أيام عمل من إبلاغ الجهة. يُرسل بموجب أمر الحجز الصادر من المحكمة، وبعد اعتماد مراجع مختص.",
    "x-rasd-enabled": false,
    "x-rasd-order-types": [
     "attachment"
    ],
    "x-rasd-reply-business-days": 3,
    "parameters": [
     {
      "name": "Idempotency-Key",
      "in": "header",
      "required": true,
      "description": "يساوي request_reference؛ إعادة الطلب نفسه بالمفتاح نفسه تعيد الرد نفسه، وطلب مختلف بالمفتاح نفسه يُرفض بالرمز 409.",
      "schema": {
       "type": "string",
       "maxLength": 120
      }
     },
     {
      "name": "X-Request-Reference",
      "in": "header",
      "required": true,
      "schema": {
       "type": "string",
       "maxLength": 120
      }
     },
     {
      "name": "X-Attempt",
      "in": "header",
      "required": false,
      "description": "رقم المحاولة؛ 1 للإرسال الأول، وما بعده استعلام عن رد معلق.",
      "schema": {
       "type": "integer",
       "minimum": 1
      }
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "$ref": "#/components/schemas/AgencyRequest"
       },
       "example": {
        "request_reference": "RSD-SAMA_BANKING-1d3f7c2a-0000-4000-8000-000000000001",
        "operation": "ATTACHMENT",
        "court_order_id": "رقم أمر المحكمة",
        "authorized_scope": {
         "parties": [
          "party-ref"
         ],
         "period": {
          "from": "2025-01-01",
          "to": "2026-11-27"
         },
         "fields": [
          "attachment_execution"
         ]
        },
        "requested_fields": [
         "attachment_execution"
        ],
        "legal_purpose": "تتبع أموال المنفذ ضده بموجب أمر المحكمة وفي حدود نطاقه",
        "subjects": [
         {
          "ref": "party-ref",
          "id_type": "cr",
          "id_number": "SBX-7001234567"
         }
        ]
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "الرد بالعقد الموحد؛ يُهمل أي مفتاح خارج العقد قبل دخوله الملف.",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "required": [
          "reference"
         ],
         "properties": {
          "reference": {
           "type": "string",
           "maxLength": 120,
           "description": "مرجع الجهة"
          },
          "data": {
           "type": "object",
           "additionalProperties": true,
           "required": [
            "attachment_execution"
           ],
           "properties": {
            "attachment_execution": {
             "type": "array",
             "items": {
              "type": "object",
              "additionalProperties": false,
              "description": "نتيجة تنفيذ الحجز",
              "properties": {
               "reference": {
                "type": "string",
                "maxLength": 1000,
                "title": "مرجع الحجز"
               },
               "status": {
                "type": "string",
                "maxLength": 1000,
                "title": "الحالة"
               },
               "amount": {
                "type": "number",
                "description": "مبلغ بالريال السعودي",
                "title": "المبلغ المحجوز"
               },
               "executed_at": {
                "type": "string",
                "format": "date",
                "title": "تاريخ التنفيذ"
               }
              }
             },
             "description": "نتيجة تنفيذ الحجز"
            }
           }
          }
         },
         "additionalProperties": true
        },
        "example": {
         "reference": "SAMA_BANKING-REF-0001",
         "data": {
          "attachment_execution": [
           {
            "reference": "DEMO-ATT-0091",
            "status": "منفذ",
            "amount": 120000,
            "executed_at": "2026-11-12"
           }
          ]
         }
        }
       }
      }
     },
     "202": {
      "description": "قُبل الطلب وسيرد الجواب لاحقًا بإشعار موقّع إلى رصد أو عند الاستعلام التالي.",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/Accepted"
        }
       }
      }
     },
     "400": {
      "$ref": "#/components/responses/Problem"
     },
     "401": {
      "$ref": "#/components/responses/Problem"
     },
     "403": {
      "$ref": "#/components/responses/Problem"
     },
     "409": {
      "$ref": "#/components/responses/Problem"
     },
     "422": {
      "$ref": "#/components/responses/Problem"
     },
     "429": {
      "$ref": "#/components/responses/Problem"
     },
     "500": {
      "$ref": "#/components/responses/Problem"
     },
     "503": {
      "$ref": "#/components/responses/Problem"
     }
    },
    "callbacks": {
     "agencyReply": {
      "https://{rasd-origin}/api/agency-callbacks/SAMA_BANKING": {
       "post": {
        "summary": "رد غير متزامن من الجهة إلى رصد",
        "description": "يُوقَّع بـ HMAC-SHA256 على «SAMA_BANKING.<الطابع الزمني>.<بايتات الجسم الخام>» بالسر المتفق عليه لهذه الجهة وبيئتها، ويُرفض ما يزيد فارق وقته على خمس دقائق، ولا يُقبل التوقيع نفسه مرتين. يُقبل الرد مرة واحدة لكل طلب؛ والطلب المغلق في رصد يُرد بالرمز 409.",
        "parameters": [
         {
          "name": "X-Rasd-Timestamp",
          "in": "header",
          "required": true,
          "schema": {
           "type": "string",
           "pattern": "^\\d{10}$",
           "description": "ثوانٍ منذ 1970-01-01 UTC"
          }
         },
         {
          "name": "X-Rasd-Signature",
          "in": "header",
          "required": true,
          "schema": {
           "type": "string",
           "pattern": "^sha256=[a-f0-9]{64}$"
          }
         }
        ],
        "requestBody": {
         "required": true,
         "content": {
          "application/json": {
           "schema": {
            "type": "object",
            "required": [
             "request_reference",
             "data"
            ],
            "properties": {
             "request_reference": {
              "type": "string"
             },
             "reference": {
              "type": "string",
              "maxLength": 120
             },
             "data": {
              "type": "object",
              "additionalProperties": true,
              "required": [
               "attachment_execution"
              ],
              "properties": {
               "attachment_execution": {
                "type": "array",
                "items": {
                 "type": "object",
                 "additionalProperties": false,
                 "description": "نتيجة تنفيذ الحجز",
                 "properties": {
                  "reference": {
                   "type": "string",
                   "maxLength": 1000,
                   "title": "مرجع الحجز"
                  },
                  "status": {
                   "type": "string",
                   "maxLength": 1000,
                   "title": "الحالة"
                  },
                  "amount": {
                   "type": "number",
                   "description": "مبلغ بالريال السعودي",
                   "title": "المبلغ المحجوز"
                  },
                  "executed_at": {
                   "type": "string",
                   "format": "date",
                   "title": "تاريخ التنفيذ"
                  }
                 }
                },
                "description": "نتيجة تنفيذ الحجز"
               }
              }
             }
            }
           }
          }
         }
        },
        "responses": {
         "200": {
          "description": "استُلم الرد، أو استُلم من قبل (duplicate)"
         },
         "400": {
          "description": "مرجع طلب غير معروف"
         },
         "401": {
          "description": "لم يُقبل الإشعار"
         },
         "409": {
          "description": "الطلب مغلق في رصد"
         },
         "422": {
          "description": "الرد يخالف العقد الموحد"
         }
        }
       }
      }
     }
    }
   }
  }
 },
 "components": {
  "securitySchemes": {
   "oauth2": {
    "type": "oauth2",
    "description": "OAuth 2.0 بيانات اعتماد العميل (RFC 6749 §4.4)؛ مدة الرمز 300 ثانية على الأقل.",
    "flows": {
     "clientCredentials": {
      "tokenUrl": "https://agency.example/oauth2/token",
      "scopes": {}
     }
    }
   },
   "mutualTLS": {
    "type": "mutualTLS",
    "description": "شهادة عميل صادرة لرصد، وTLS 1.2 فأعلى."
   }
  },
  "schemas": {
   "AgencyRequest": {
    "type": "object",
    "additionalProperties": false,
    "required": [
     "request_reference",
     "operation",
     "court_order_id",
     "authorized_scope",
     "requested_fields",
     "legal_purpose",
     "subjects"
    ],
    "properties": {
     "request_reference": {
      "type": "string",
      "maxLength": 120
     },
     "operation": {
      "type": "string",
      "enum": [
       "DISCLOSURE",
       "ATTACHMENT"
      ]
     },
     "court_order_id": {
      "type": "string"
     },
     "authorized_scope": {
      "type": "object",
      "properties": {
       "parties": {
        "type": "array",
        "items": {
         "type": "string"
        }
       },
       "period": {
        "type": [
         "object",
         "null"
        ],
        "properties": {
         "from": {
          "type": "string",
          "format": "date"
         },
         "to": {
          "type": "string",
          "format": "date"
         }
        }
       },
       "fields": {
        "type": "array",
        "items": {
         "type": "string"
        }
       }
      }
     },
     "requested_fields": {
      "type": "array",
      "minItems": 1,
      "items": {
       "type": "string",
       "enum": [
        "accounts",
        "balances",
        "statements",
        "attachment_execution"
       ]
      }
     },
     "legal_purpose": {
      "type": "string",
      "minLength": 8,
      "maxLength": 500
     },
     "subjects": {
      "type": "array",
      "minItems": 1,
      "maxItems": 20,
      "items": {
       "type": "object",
       "additionalProperties": false,
       "required": [
        "ref",
        "id_type",
        "id_number"
       ],
       "properties": {
        "ref": {
         "type": "string"
        },
        "id_type": {
         "type": "string",
         "enum": [
          "national_id",
          "iqama",
          "cr",
          "unified_number",
          "passport"
         ]
        },
        "id_number": {
         "type": "string",
         "pattern": "^[A-Za-z0-9-]{4,30}$"
        }
       }
      }
     }
    }
   },
   "Accepted": {
    "type": "object",
    "required": [
     "status"
    ],
    "properties": {
     "status": {
      "const": "processing"
     },
     "reference": {
      "type": "string"
     },
     "retry_after_seconds": {
      "type": "integer",
      "minimum": 1,
      "maximum": 86400
     }
    }
   },
   "Problem": {
    "type": "object",
    "required": [
     "error"
    ],
    "properties": {
     "error": {
      "type": "object",
      "required": [
       "code"
      ],
      "properties": {
       "code": {
        "type": "string"
       },
       "message": {
        "type": "string"
       }
      }
     }
    }
   }
  },
  "responses": {
   "Problem": {
    "description": "خطأ بنموذج موحد؛ 429 و5xx مؤقتة ويُعاد الطلب بالمرجع نفسه.",
    "content": {
     "application/json": {
      "schema": {
       "$ref": "#/components/schemas/Problem"
      }
     }
    }
   }
  }
 }
}
