{
  "openapi": "3.0.3",
  "info": {
    "title": "TezVerify REST API v1",
    "description": "Tactile Autonomous Payment Verification Engine API for Pakistan eCommerce & digital platforms. Verify Easypaisa, JazzCash, and Commercial Bank (Raast/1Link) payments instantly with bank-grade security and zero NTN required.",
    "version": "1.0.0",
    "contact": {
      "name": "TezVerify Integration Support",
      "url": "https://tezverify.com",
      "email": "support@tezverify.com"
    }
  },
  "servers": [
    {
      "url": "https://tezverify.com/api/v1",
      "description": "Production API Server"
    },
    {
      "url": "http://localhost:8000/api/v1",
      "description": "Local Development Server"
    }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Api-Key",
        "description": "Tenant API Key generated in Merchant Dashboard under Integrations & API tab."
      }
    },
    "schemas": {
      "PaymentSubmissionRequest": {
        "type": "object",
        "required": ["gateway", "transaction_reference", "amount"],
        "properties": {
          "gateway": {
            "type": "string",
            "enum": ["easypaisa", "jazzcash", "bank"],
            "example": "easypaisa",
            "description": "Payment channel used by customer."
          },
          "transaction_reference": {
            "type": "string",
            "example": "10293847561",
            "description": "Sender Transaction ID (TID) from customer SMS or bank reference number."
          },
          "amount": {
            "type": "number",
            "format": "float",
            "example": 1500.0,
            "description": "Total payable amount in PKR."
          },
          "sender_source": {
            "type": "string",
            "example": "03001234567",
            "description": "Optional customer phone number or sender identifier."
          },
          "callback_url": {
            "type": "string",
            "format": "uri",
            "example": "https://my-store.com/webhooks/tezverify",
            "description": "Optional custom callback URL for instant webhook notification."
          },
          "client_metadata": {
            "type": "object",
            "example": { "order_id": "ORD-9941", "customer_name": "Ali Khan" },
            "description": "Arbitrary key-value JSON dictionary passed through to webhook payload."
          }
        }
      },
      "PaymentSubmissionResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "message": { "type": "string", "example": "Payment submission registered successfully." },
          "data": {
            "type": "object",
            "properties": {
              "uuid": { "type": "string", "example": "7c4731f8-9fa4-4d87-8df1-4a11b6973e21" },
              "status": { "type": "string", "enum": ["pending", "verified", "failed"], "example": "pending" },
              "gateway": { "type": "string", "example": "easypaisa" },
              "transaction_reference": { "type": "string", "example": "10293847561" },
              "amount_expected": { "type": "number", "example": 1500.0 },
              "created_at": { "type": "string", "example": "2026-09-14T06:00:00Z" }
            }
          }
        }
      },
      "TransactionStatusResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "data": {
            "type": "object",
            "properties": {
              "uuid": { "type": "string", "example": "7c4731f8-9fa4-4d87-8df1-4a11b6973e21" },
              "gateway": { "type": "string", "example": "easypaisa" },
              "transaction_reference": { "type": "string", "example": "10293847561" },
              "amount_expected": { "type": "number", "example": 1500.0 },
              "amount_detected": { "type": "number", "nullable": true, "example": 1500.0 },
              "status": { "type": "string", "enum": ["pending", "verified", "manual_review", "failed"], "example": "verified" },
              "verified_at": { "type": "string", "nullable": true, "example": "2026-09-14T06:00:15Z" }
            }
          }
        }
      },
      "SmsIngestRequest": {
        "type": "object",
        "required": ["sender", "body"],
        "properties": {
          "sender": {
            "type": "string",
            "example": "3737",
            "description": "Originating shortcode or bank SMS header (e.g. 3737, 8558, MEEZAN, HBL)."
          },
          "body": {
            "type": "string",
            "example": "You have received Rs. 1,500.00 from ALI KHAN. Trans ID 10293847561. Balance Rs. 25,000.00",
            "description": "Full unedited SMS message text."
          },
          "received_at": {
            "type": "string",
            "format": "date-time",
            "example": "2026-09-14T06:00:00Z",
            "description": "Timestamp when SMS was received on merchant device."
          }
        }
      },
      "SystemHealthResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "example": "healthy" },
          "timestamp": { "type": "string", "example": "2026-09-14T06:00:00Z" },
          "services": {
            "type": "object",
            "properties": {
              "database": { "type": "string", "example": "ok" },
              "cache": { "type": "string", "example": "ok" },
              "queue": { "type": "string", "example": "ok" }
            }
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": false },
          "message": { "type": "string", "example": "Invalid or missing API key." }
        }
      }
    }
  },
  "paths": {
    "/payments/verify": {
      "post": {
        "summary": "Submit Pending Payment for Verification",
        "description": "Registers a customer checkout payment intent. TezVerify's autonomous engine reconciles against banking notifications and dispatches an HMAC-signed webhook upon match.",
        "security": [{ "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/PaymentSubmissionRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Payment registered or already verified",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PaymentSubmissionResponse" }
              }
            }
          },
          "401": {
            "description": "Unauthorized — Missing or Invalid API Key",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } }
            }
          },
          "402": {
            "description": "Payment Required — Tenant is suspended or unpaid cycle limit reached",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } }
            }
          },
          "422": {
            "description": "Validation Error"
          },
          "429": {
            "description": "Rate limit exceeded (60 requests per minute per tenant)"
          }
        }
      }
    },
    "/payments/status/{uuid}": {
      "get": {
        "summary": "Poll Payment Verification Status",
        "description": "Query live verification state for a transaction UUID (typically polled every 3-5 seconds by checkout thank-you pages).",
        "security": [{ "ApiKeyAuth": [] }],
        "parameters": [
          {
            "name": "uuid",
            "in": "path",
            "required": true,
            "description": "Transaction UUID returned during submission.",
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Current status of transaction",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/TransactionStatusResponse" }
              }
            }
          },
          "401": {
            "description": "Unauthorized"
          },
          "404": {
            "description": "Transaction UUID not found"
          }
        }
      }
    },
    "/sms/ingest": {
      "post": {
        "summary": "Direct SMS Gateway Ingest",
        "description": "Accepts raw SMS alerts forwarded directly from merchant Android forwarder apps or cloud SMS webhooks.",
        "security": [{ "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/SmsIngestRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "SMS successfully parsed and matched against pending order",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": { "type": "string", "example": "verified" },
                    "transaction_uuid": { "type": "string", "example": "7c4731f8-9fa4-4d87-8df1-4a11b6973e21" },
                    "reference": { "type": "string", "example": "10293847561" },
                    "amount": { "type": "number", "example": 1500.0 }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized"
          },
          "422": {
            "description": "Unrecognized SMS format or template"
          },
          "429": {
            "description": "Rate limit exceeded (120 requests per minute)"
          }
        }
      }
    },
    "/system/health": {
      "get": {
        "summary": "Public System Health Probe",
        "description": "Returns operational status of database, cache, and processing workers for uptime monitoring (UptimeRobot, Pingdom).",
        "responses": {
          "200": {
            "description": "Cluster is healthy and operational",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/SystemHealthResponse" }
              }
            }
          },
          "503": {
            "description": "Degraded service"
          }
        }
      }
    }
  }
}
