{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://aha.is/schemas/woocommerce-plugin/order-create.request.schema.json",
  "title": "Aha -> plugin order create request",
  "description": "Body of POST wp-json/aha-is-for-woocommerce/v1/orders. The field list mirrors the payload that crons/woocommerce-orders/src/utils/transformer.js builds today (transformOrder), with two additions (aha_order_id, schema_version) and one change (line items travel as SKU).",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "aha_order_id",
    "payment_method",
    "currency",
    "billing",
    "line_items"
  ],
  "properties": {
    "schema_version": {
      "description": "Contract version of this payload. The plugin rejects a major version it does not know.",
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+$",
      "examples": ["1.0"]
    },
    "aha_order_id": {
      "description": "Stable Aha order identity. It is the idempotency key. Aha must not change it between retries of the same order. The plugin stores it in the UNIQUE column of {prefix}aha_is_woocommerce_order_map and also in order meta_data.",
      "type": "string",
      "minLength": 1,
      "maxLength": 64
    },
    "aha_order_increment_id": {
      "description": "Human readable Aha order number. Display only. Never used for dedup.",
      "type": "string",
      "maxLength": 64
    },
    "payment_method": {
      "description": "Payment method slug. The sync sends the constant \"aha\".",
      "type": "string",
      "minLength": 1
    },
    "payment_method_title": {
      "description": "Payment method label shown in WooCommerce admin.",
      "type": "string"
    },
    "set_paid": {
      "description": "True when Aha already took the money. Aha orders are paid before they reach the store.",
      "type": "boolean",
      "default": true
    },
    "currency": {
      "description": "ISO 4217 currency code. Aha is ISK only today.",
      "type": "string",
      "pattern": "^[A-Z]{3}$"
    },
    "billing": { "$ref": "#/definitions/addressWithContact" },
    "shipping": { "$ref": "#/definitions/address" },
    "line_items": {
      "description": "Order lines. Aha sends the SKU; the plugin resolves the SKU to a local product_id or variation_id. An unknown or ambiguous SKU gives 422 unknown_sku.",
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/definitions/lineItem" }
    },
    "shipping_lines": {
      "description": "Shipping cost. Aha sends at most one line. Free shipping is one line with total \"0.00\", not an empty array. More than one line is a contract error and gives 400 invalid_payload.",
      "type": "array",
      "maxItems": 1,
      "items": { "$ref": "#/definitions/shippingLine" }
    },
    "customer_note": {
      "description": "Free text note put on the order. Aha builds it from the company id, the Aha order id, the schedule date and the customer notes.",
      "type": "string"
    },
    "meta_data": {
      "description": "Extra WooCommerce order meta. The plugin always writes aha_order_id itself, so this array is optional.",
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["key", "value"],
        "properties": {
          "key": { "type": "string", "minLength": 1 },
          "value": { "type": ["string", "number", "boolean", "null"] }
        }
      }
    }
  },
  "definitions": {
    "address": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "first_name": {
          "description": "Masked customer first name. Aha keeps only the first 3 characters.",
          "type": "string"
        },
        "last_name": {
          "description": "Masked customer last name.",
          "type": "string"
        },
        "address_1": { "type": "string" },
        "city": { "type": "string" },
        "postcode": { "type": "string" },
        "country": {
          "description": "ISO 3166-1 alpha-2 country code. Aha defaults to IS.",
          "type": "string",
          "pattern": "^[A-Z]{2}$"
        }
      }
    },
    "addressWithContact": {
      "description": "Same as address, plus the Aha service contact. Only the billing address carries the contact.",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "first_name": { "type": "string" },
        "last_name": { "type": "string" },
        "address_1": { "type": "string" },
        "city": { "type": "string" },
        "postcode": { "type": "string" },
        "country": { "type": "string", "pattern": "^[A-Z]{2}$" },
        "email": {
          "description": "Aha service contact email, or the per-store email_override. Aha never sends the real customer email.",
          "type": "string",
          "format": "email"
        },
        "phone": {
          "description": "Aha service contact phone. Never the real customer phone.",
          "type": "string"
        }
      }
    },
    "lineItem": {
      "type": "object",
      "additionalProperties": false,
      "required": ["sku", "quantity", "subtotal", "total"],
      "properties": {
        "sku": {
          "description": "Store SKU of the product or variation. The plugin resolves it locally.",
          "type": "string",
          "minLength": 1
        },
        "product_id": {
          "description": "Optional, added in 1.1. The WooCommerce parent product id that Aha took from its own SKU. A Connect-signed request is resolved by this id only: the product must exist and be published or private, else 422 unknown_sku. Other requests ignore it and resolve the SKU.",
          "type": "integer",
          "minimum": 1
        },
        "variation_id": {
          "description": "Optional, added in 1.2. The WooCommerce variation id under product_id. Sent only for variable products. A Connect-signed request must name a published or private variation whose parent is product_id, else 422 unknown_sku.",
          "type": "integer",
          "minimum": 1
        },
        "quantity": {
          "description": "Integer quantity. Aha rounds fractional quantities and keeps a floor of 1.",
          "type": "integer",
          "minimum": 1
        },
        "subtotal": {
          "description": "Required. Line subtotal before discount, as a decimal string. Aha is the price authority; the plugin writes this value and does not let WooCommerce recalculate it. The Aha price can differ from the price the store shows today.",
          "type": "string",
          "pattern": "^-?[0-9]+(\\.[0-9]{1,4})?$"
        },
        "total": {
          "description": "Required. Line total after discount, as a decimal string. Same price authority rule.",
          "type": "string",
          "pattern": "^-?[0-9]+(\\.[0-9]{1,4})?$"
        },
        "name": {
          "description": "Optional product name for operator readability.",
          "type": "string"
        }
      }
    },
    "shippingLine": {
      "type": "object",
      "additionalProperties": false,
      "required": ["method_id", "total"],
      "properties": {
        "method_id": { "type": "string", "minLength": 1 },
        "method_title": { "type": "string" },
        "total": {
          "description": "Shipping amount as a decimal string with 2 decimals.",
          "type": "string",
          "pattern": "^-?[0-9]+(\\.[0-9]{1,4})?$"
        }
      }
    }
  }
}
