{
  "openapi": "3.1.0",
  "info": {
    "title": "UPI Gateway Pro · Merchant API",
    "version": "1.0.0",
    "description": "Create UPI payment orders, read their status, inspect payment accounts and test webhooks. Authenticate with X-Api-Key and X-Api-Secret headers (or HTTP Basic). Live keys (pk_live_) create real orders; sandbox keys (pk_test_) create simulated orders."
  },
  "servers": [{ "url": "/api/v1" }],
  "security": [{ "apiKey": [], "apiSecret": [] }, { "basic": [] }],
  "components": {
    "securitySchemes": {
      "apiKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key" },
      "apiSecret": { "type": "apiKey", "in": "header", "name": "X-Api-Secret" },
      "basic": { "type": "http", "scheme": "basic" }
    },
    "parameters": {
      "IdempotencyKey": { "name": "Idempotency-Key", "in": "header", "required": false, "schema": { "type": "string", "maxLength": 128 }, "description": "Replays the original response for a repeated key + body within 24h." }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "const": false },
          "error": { "type": "object", "properties": { "code": { "type": "string" }, "message": { "type": "string" }, "details": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } } }, "required": ["code", "message"] }
        }
      },
      "Order": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "example": "ORD7K2M9QX3P1BZ8RT4Y" },
          "client_order_id": { "type": "string" },
          "environment": { "type": "string", "enum": ["live", "sandbox"] },
          "status": { "type": "string", "enum": ["pending", "review", "paid", "expired", "cancelled", "failed"] },
          "amount": { "type": "string", "example": "499.00" },
          "currency": { "type": "string", "example": "INR" },
          "payment_url": { "type": "string", "format": "uri" },
          "upi_intent": { "type": ["string", "null"] },
          "payment_account": { "type": ["object", "null"], "properties": { "id": { "type": "integer" }, "label": { "type": "string" }, "provider": { "type": "string" }, "upi_id": { "type": ["string", "null"] } } },
          "utr": { "type": ["string", "null"] },
          "provider_txn_id": { "type": ["string", "null"] },
          "verified_by": { "type": ["string", "null"], "enum": ["provider", "manual", "admin", "sandbox", null] },
          "customer": { "type": "object", "properties": { "name": { "type": ["string", "null"] }, "email": { "type": ["string", "null"] }, "mobile": { "type": ["string", "null"] }, "ref": { "type": ["string", "null"] } } },
          "note": { "type": ["string", "null"] },
          "metadata": { "type": ["object", "null"] },
          "redirect_url": { "type": ["string", "null"] },
          "paid_at": { "type": ["string", "null"], "format": "date-time" },
          "expires_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" }
        }
      },
      "CreateOrder": {
        "type": "object",
        "required": ["amount"],
        "properties": {
          "amount": { "type": "number", "minimum": 1, "multipleOf": 0.01 },
          "client_order_id": { "type": "string", "maxLength": 64, "pattern": "^[A-Za-z0-9_\\-.]+$" },
          "payment_account_id": { "type": "integer", "description": "Live only. Pins the order to a specific account; strictly validated." },
          "customer_name": { "type": "string", "maxLength": 120 },
          "customer_email": { "type": "string", "format": "email" },
          "customer_mobile": { "type": "string", "maxLength": 20 },
          "customer_ref": { "type": "string", "maxLength": 64 },
          "note": { "type": "string", "maxLength": 120 },
          "metadata": { "type": "object", "maxProperties": 20, "additionalProperties": { "type": ["string", "number", "boolean", "null"] } },
          "redirect_url": { "type": "string", "format": "uri", "description": "Host must be in the merchant's allowed redirect domains." },
          "expiry_minutes": { "type": "integer", "minimum": 3, "maximum": 1440 }
        }
      },
      "Account": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" }, "label": { "type": "string" }, "provider": { "type": "string" }, "upi_id": { "type": ["string", "null"] },
          "is_active": { "type": "boolean" }, "is_default": { "type": "boolean" }, "health": { "type": "string" },
          "priority": { "type": "integer" }, "weight": { "type": "integer" },
          "daily_limit": { "type": ["string", "null"] }, "used_today": { "type": "string" }, "remaining_today": { "type": ["string", "null"] },
          "per_txn_min": { "type": ["string", "null"] }, "per_txn_max": { "type": ["string", "null"] }
        }
      }
    },
    "responses": {
      "Unauthorized": { "description": "Invalid credentials", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
      "Validation": { "description": "Validation failed", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
    }
  },
  "paths": {
    "/me": { "get": { "summary": "Identify key and merchant", "responses": { "200": { "description": "OK" }, "401": { "$ref": "#/components/responses/Unauthorized" } } } },
    "/accounts": { "get": { "summary": "List payment accounts and rotation policy", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "success": { "type": "boolean" }, "data": { "type": "array", "items": { "$ref": "#/components/schemas/Account" } }, "environment": { "type": "string" }, "rotation": { "type": "object", "properties": { "enabled": { "type": "boolean" }, "strategy": { "type": ["string", "null"] } } } } } } } } } } },
    "/orders": {
      "get": {
        "summary": "List orders",
        "parameters": [
          { "name": "status", "in": "query", "schema": { "type": "string" } },
          { "name": "client_order_id", "in": "query", "schema": { "type": "string" } },
          { "name": "customer_ref", "in": "query", "schema": { "type": "string" } },
          { "name": "from", "in": "query", "schema": { "type": "string", "format": "date" } },
          { "name": "to", "in": "query", "schema": { "type": "string", "format": "date" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 } },
          { "name": "cursor", "in": "query", "schema": { "type": "integer" } }
        ],
        "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "success": { "type": "boolean" }, "data": { "type": "array", "items": { "$ref": "#/components/schemas/Order" } }, "pagination": { "type": "object", "properties": { "limit": { "type": "integer" }, "has_more": { "type": "boolean" }, "next_cursor": { "type": ["integer", "null"] } } } } } } } } }
      },
      "post": {
        "summary": "Create order",
        "parameters": [{ "$ref": "#/components/parameters/IdempotencyKey" }],
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateOrder" } } } },
        "responses": { "201": { "description": "Created", "content": { "application/json": { "schema": { "type": "object", "properties": { "success": { "type": "boolean" }, "data": { "$ref": "#/components/schemas/Order" } } } } } }, "422": { "$ref": "#/components/responses/Validation" } }
      }
    },
    "/orders/{id}": {
      "get": {
        "summary": "Fetch order by gateway id or client_order_id",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "success": { "type": "boolean" }, "data": { "$ref": "#/components/schemas/Order" } } } } } }, "404": { "description": "Not found" } }
      }
    },
    "/orders/{id}/cancel": {
      "post": {
        "summary": "Cancel a pending or in-review order",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }, { "$ref": "#/components/parameters/IdempotencyKey" }],
        "requestBody": { "content": { "application/json": { "schema": { "type": "object", "properties": { "reason": { "type": "string", "maxLength": 160 } } } } } },
        "responses": { "200": { "description": "Cancelled" }, "422": { "$ref": "#/components/responses/Validation" } }
      }
    },
    "/webhooks/ping": { "post": { "summary": "Send a ping event to active endpoints", "responses": { "202": { "description": "Queued" } } } }
  },
  "webhooks": {
    "order.paid": {
      "post": {
        "summary": "Order events",
        "description": "Events: order.created, order.paid, order.expired, order.cancelled, order.review_pending, order.review_rejected, order.failed, ping. Signed with X-Webhook-Signature: t=<unix>,v1=<hmac_sha256(secret, t + '.' + raw_body)>.",
        "requestBody": { "content": { "application/json": { "schema": { "type": "object", "properties": { "event_id": { "type": "string", "format": "uuid" }, "event": { "type": "string" }, "created_at": { "type": "string", "format": "date-time" }, "environment": { "type": "string" }, "data": { "$ref": "#/components/schemas/Order" } } } } } },
        "responses": { "200": { "description": "Return any 2xx to acknowledge." } }
      }
    }
  }
}
