openapi: 3.1.0
info:
  title: BloomCore API - Order Webhooks
  version: 1.1.0
  description: |
    Шесть подписанных webhook-событий жизненного цикла заказа. Все CRM `id` и `*_id`
    являются UUID; внутренние bigint ID не отправляются. Payload содержит контактные,
    коммерческие и операционные данные. Проверяйте HMAC до JSON parsing, шифруйте
    данные получателя и не записывайте полное тело в открытые логи.
servers:
  - url: https://{tenant_subdomain}.crm.flowers/api/v1
    variables:
      tenant_subdomain:
        default: demo
        description: Поддомен магазина.
paths: {}
webhooks:
  orderCreated:
    post:
      summary: Создан заказ
      parameters: &webhookParameters
        - $ref: '#/components/parameters/webhookEvent'
        - $ref: '#/components/parameters/webhookEventId'
        - $ref: '#/components/parameters/webhookDeliveryId'
        - $ref: '#/components/parameters/webhookTimestamp'
        - $ref: '#/components/parameters/webhookSignature'
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/OrderCreatedWebhookEnvelope'}
      responses: &acceptedResponse
        '200': {description: Событие принято интеграцией.}
  orderStatusChanged:
    post:
      summary: Изменен статус заказа
      parameters: *webhookParameters
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/OrderStatusChangedWebhookEnvelope'}
      responses: *acceptedResponse
  orderPaid:
    post:
      summary: Заказ полностью оплачен
      parameters: *webhookParameters
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/OrderPaidWebhookEnvelope'}
      responses: *acceptedResponse
  orderClosed:
    post:
      summary: Заказ закрыт
      parameters: *webhookParameters
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/OrderClosedWebhookEnvelope'}
      responses: *acceptedResponse
  orderCancelled:
    post:
      summary: Заказ отменен
      parameters: *webhookParameters
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/OrderCancelledWebhookEnvelope'}
      responses: *acceptedResponse
  orderCorrectionApplied:
    post:
      summary: Применена корректировка состава или доставки заказа
      description: |
        Событие содержит только UUID цепочки ревизий, тип корректировки и финансовую
        дельту. Причина, состав заказа, адрес, контакты, снимки состояния и служебные
        метаданные намеренно не передаются.
      parameters: *webhookParameters
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/OrderCorrectionAppliedWebhookEnvelope'}
            example:
              event: order.correction_applied
              event_id: 019fbc00-1111-7000-8000-000000000001
              created_at: '2026-08-01T08:00:00Z'
              payload_schema_version: crm.webhook.v1
              tenant_id: 019fbc00-1111-7000-8000-000000000002
              store_id: 019fbc00-1111-7000-8000-000000000003
              retry_count: 0
              data:
                correction:
                  id: 019fbc00-1111-7000-8000-000000000004
                  tenant_id: 019fbc00-1111-7000-8000-000000000002
                  store_id: 019fbc00-1111-7000-8000-000000000003
                  order_id: 019fbc00-1111-7000-8000-000000000005
                  before_revision_id: 019fbc00-1111-7000-8000-000000000006
                  after_revision_id: 019fbc00-1111-7000-8000-000000000007
                  type: items
                  status: applied
                  applied_at: '2026-08-01T08:00:00Z'
                change:
                  action: correction_applied
                  correction_type: items
                  before_revision_no: 1
                  after_revision_no: 2
                  delivery_id: null
                financial:
                  currency_code: RUB
                  financial_delta: '150.00'
                  payment_due_delta: '150.00'
                  amount_to_collect: '150.00'
                  amount_available_to_refund: '0.00'
      responses: *acceptedResponse
components:
  parameters:
    webhookEvent:
      name: X-CRM-Webhook-Event
      in: header
      required: true
      schema:
        type: string
        enum: [order.created, order.status_changed, order.paid, order.closed, order.cancelled, order.correction_applied]
    webhookEventId:
      name: X-CRM-Webhook-Id
      in: header
      required: true
      schema: {type: string, format: uuid}
    webhookDeliveryId:
      name: X-CRM-Webhook-Delivery
      in: header
      required: true
      schema: {type: string, format: uuid}
    webhookTimestamp:
      name: X-CRM-Webhook-Timestamp
      in: header
      required: true
      schema: {type: string, pattern: '^[0-9]{10}$'}
    webhookSignature:
      name: X-CRM-Webhook-Signature
      in: header
      required: true
      schema: {type: string, pattern: '^sha256=[a-f0-9]{64}$'}
  schemas:
    OrderCreatedWebhookEnvelope:
      type: object
      additionalProperties: false
      required: &envelopeRequired [event, event_id, created_at, payload_schema_version, tenant_id, store_id, retry_count, data]
      properties:
        event: {type: string, const: order.created}
        event_id:
          type: string
          format: uuid
        created_at: {type: string, format: date-time}
        payload_schema_version: {type: string, const: crm.webhook.v1}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/OrderOnlyWebhookData'}
    OrderStatusChangedWebhookEnvelope:
      type: object
      additionalProperties: false
      required: *envelopeRequired
      properties:
        event: {type: string, const: order.status_changed}
        event_id:
          type: string
          format: uuid
        created_at: {type: string, format: date-time}
        payload_schema_version: {type: string, const: crm.webhook.v1}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/OrderStatusWebhookData'}
    OrderPaidWebhookEnvelope:
      type: object
      additionalProperties: false
      required: *envelopeRequired
      properties:
        event: {type: string, const: order.paid}
        event_id:
          type: string
          format: uuid
        created_at: {type: string, format: date-time}
        payload_schema_version: {type: string, const: crm.webhook.v1}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/OrderOnlyWebhookData'}
    OrderClosedWebhookEnvelope:
      type: object
      additionalProperties: false
      required: *envelopeRequired
      properties:
        event: {type: string, const: order.closed}
        event_id:
          type: string
          format: uuid
        created_at: {type: string, format: date-time}
        payload_schema_version: {type: string, const: crm.webhook.v1}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/OrderStatusWebhookData'}
    OrderCancelledWebhookEnvelope:
      type: object
      additionalProperties: false
      required: *envelopeRequired
      properties:
        event: {type: string, const: order.cancelled}
        event_id:
          type: string
          format: uuid
        created_at: {type: string, format: date-time}
        payload_schema_version: {type: string, const: crm.webhook.v1}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/OrderStatusWebhookData'}
    OrderCorrectionAppliedWebhookEnvelope:
      type: object
      additionalProperties: false
      required: *envelopeRequired
      properties:
        event: {type: string, const: order.correction_applied}
        event_id:
          type: string
          format: uuid
        created_at: {type: string, format: date-time}
        payload_schema_version: {type: string, const: crm.webhook.v1}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/OrderCorrectionAppliedWebhookData'}
    OrderCorrectionAppliedWebhookData:
      type: object
      additionalProperties: false
      required: [correction, change, financial]
      properties:
        correction: {$ref: '#/components/schemas/OrderCorrectionAppliedWebhookCorrection'}
        change: {$ref: '#/components/schemas/OrderCorrectionAppliedWebhookChange'}
        financial: {$ref: '#/components/schemas/OrderCorrectionAppliedWebhookFinancial'}
    OrderCorrectionAppliedWebhookCorrection:
      type: object
      additionalProperties: false
      required: [id, tenant_id, store_id, order_id, before_revision_id, after_revision_id, type, status, applied_at]
      properties:
        id:
          type: string
          format: uuid
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        order_id:
          type: string
          format: uuid
        before_revision_id:
          type: string
          format: uuid
        after_revision_id:
          type: string
          format: uuid
        type: {type: string, enum: [items, delivery]}
        status: {type: string, const: applied}
        applied_at: {type: string, format: date-time}
    OrderCorrectionAppliedWebhookChange:
      type: object
      additionalProperties: false
      required: [action, correction_type, before_revision_no, after_revision_no, delivery_id]
      properties:
        action: {type: string, const: correction_applied}
        correction_type: {type: string, enum: [items, delivery]}
        before_revision_no: {type: integer, minimum: 1}
        after_revision_no: {type: integer, minimum: 2}
        delivery_id:
          type: [string, 'null']
          format: uuid
    OrderCorrectionAppliedWebhookFinancial:
      type: object
      additionalProperties: false
      required: [currency_code, financial_delta, payment_due_delta, amount_to_collect, amount_available_to_refund]
      properties:
        currency_code: {type: string, pattern: '^[A-Z]{3}$'}
        financial_delta: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        payment_due_delta: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        amount_to_collect: {type: string, pattern: '^[0-9]+\.[0-9]{2}$'}
        amount_available_to_refund: {type: string, pattern: '^[0-9]+\.[0-9]{2}$'}
    OrderOnlyWebhookData:
      type: object
      additionalProperties: false
      required: [order]
      properties:
        order: {$ref: '#/components/schemas/OrderWebhookOrder'}
    OrderStatusWebhookData:
      type: object
      additionalProperties: false
      required: [order, status_change]
      properties:
        order: {$ref: '#/components/schemas/OrderWebhookOrder'}
        status_change: {$ref: '#/components/schemas/OrderWebhookStatusChange'}
    OrderWebhookOrder:
      type: object
      additionalProperties: false
      required: [id, number, tenant_id, store_id, customer_id, recipient_customer_id, manager_id, florist_id, courier_id, source, type, status, payment_status, delivery_status, customer_name, customer_phone, customer_email, recipient_same_as_customer, recipient_name, recipient_phone, is_surprise, do_not_call_recipient, desired_at, subtotal, discount_total, delivery_price, total, paid_total, refunded_total, payment_due, completed_at, cancelled_at, created_at, updated_at, items]
      properties:
        id:
          type: string
          format: uuid
        number: {type: string}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        customer_id:
          type: [string, 'null']
          format: uuid
        recipient_customer_id:
          type: [string, 'null']
          format: uuid
        manager_id:
          type: [string, 'null']
          format: uuid
        florist_id:
          type: [string, 'null']
          format: uuid
        courier_id:
          type: [string, 'null']
          format: uuid
        source: {type: [string, 'null']}
        type: {type: [string, 'null']}
        status: {type: string}
        payment_status: {type: [string, 'null']}
        delivery_status: {type: [string, 'null']}
        customer_name: {type: [string, 'null']}
        customer_phone: {type: [string, 'null']}
        customer_email: {type: [string, 'null'], format: email}
        recipient_same_as_customer: {type: boolean}
        recipient_name: {type: [string, 'null']}
        recipient_phone: {type: [string, 'null']}
        is_surprise: {type: boolean}
        do_not_call_recipient: {type: boolean}
        desired_at: {type: [string, 'null'], format: date-time}
        subtotal: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        discount_total: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        delivery_price: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        total: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        paid_total: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        refunded_total: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        payment_due: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        completed_at: {type: [string, 'null'], format: date-time}
        cancelled_at: {type: [string, 'null'], format: date-time}
        created_at: {type: string, format: date-time}
        updated_at: {type: string, format: date-time}
        items:
          type: array
          items: {$ref: '#/components/schemas/OrderWebhookOrderItem'}
    OrderWebhookOrderItem:
      type: object
      additionalProperties: false
      required: [id, product_id, inventory_batch_id, type, name, qty, unit_price, discount_total, total, is_stock_tracked]
      properties:
        id:
          type: string
          format: uuid
        product_id:
          type: [string, 'null']
          format: uuid
        inventory_batch_id:
          type: [string, 'null']
          format: uuid
        type: {type: string}
        name: {type: string}
        qty: {type: string, pattern: '^-?[0-9]+\.[0-9]{3}$'}
        unit_price: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        discount_total: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        total: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        is_stock_tracked: {type: boolean}
    OrderWebhookStatusChange:
      type: object
      additionalProperties: false
      required: [from_status, to_status, reason]
      properties:
        from_status: {type: [string, 'null']}
        to_status: {type: string, minLength: 1}
        reason: {type: [string, 'null']}
