{
  "openapi": "3.0.3",
  "info": {
    "title": "SabPaisa PG 2.0 API",
    "version": "2.4.0",
    "description": "SabPaisa Payment Gateway 2.0 — create payments, process refunds, and manage transactions.",
    "contact": { "email": "techsupport@sabpaisa.in" }
  },
  "servers": [
    { "url": "https://staging-sb-merchant-api.sabpaisa.in", "description": "Staging" },
    { "url": "https://merchant-api.sabpaisa.in", "description": "Production" }
  ],
  "security": [{ "ApiKey": [] }],
  "paths": {
    "/api/v2/payments": {
      "post": {
        "summary": "Create Payment Session",
        "operationId": "createPayment",
        "tags": ["Payments"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreatePaymentRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "Payment session created", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreatePaymentResponse" } } } },
          "400": { "description": "Validation error" },
          "401": { "description": "Unauthorized" },
          "429": { "description": "Rate limited" }
        }
      },
      "get": {
        "summary": "Transaction Enquiry",
        "operationId": "enquirePayment",
        "tags": ["Payments"],
        "parameters": [
          { "name": "merchantId", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "merchantTxnId", "in": "query", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Transaction details", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/TransactionResponse" } } } }
        }
      }
    },
    "/api/v2/refunds": {
      "post": {
        "summary": "Create Refund",
        "operationId": "createRefund",
        "tags": ["Refunds"],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateRefundRequest" } } }
        },
        "responses": {
          "200": { "description": "Refund initiated" }
        }
      },
      "get": {
        "summary": "List Refunds",
        "operationId": "listRefunds",
        "tags": ["Refunds"],
        "parameters": [
          { "name": "merchantId", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "sabpaisaTxnId", "in": "query", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "List of refunds" } }
      }
    },
    "/api/v2/refunds/{refundId}": {
      "get": {
        "summary": "Get Refund Status",
        "operationId": "getRefund",
        "tags": ["Refunds"],
        "parameters": [{ "name": "refundId", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "Refund details" } }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key" }
    },
    "schemas": {
      "CreatePaymentRequest": {
        "type": "object",
        "required": ["merchantId", "merchantTxnId", "amount", "currency", "returnUrl", "checksum", "timestamp"],
        "properties": {
          "merchantId": { "type": "string" },
          "merchantTxnId": { "type": "string", "maxLength": 50 },
          "amount": { "type": "integer", "description": "Amount in paise" },
          "currency": { "type": "string", "enum": ["INR"] },
          "customerName": { "type": "string" },
          "customerEmail": { "type": "string", "format": "email" },
          "customerPhone": { "type": "string", "pattern": "^[0-9]{10}$" },
          "returnUrl": { "type": "string", "format": "uri" },
          "description": { "type": "string" },
          "checksum": { "type": "string" },
          "timestamp": { "type": "integer" },
          "metadata": { "type": "object" }
        }
      },
      "CreatePaymentResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string" },
          "sabpaisaTxnId": { "type": "string" },
          "checkoutUrl": { "type": "string", "format": "uri" },
          "expiresAt": { "type": "string", "format": "date-time" }
        }
      },
      "TransactionResponse": {
        "type": "object",
        "properties": {
          "sabpaisaTxnId": { "type": "string" },
          "merchantTxnId": { "type": "string" },
          "status": { "type": "string", "enum": ["SUCCESS", "FAILED", "PENDING", "EXPIRED"] },
          "amount": { "type": "integer" },
          "paidAmount": { "type": "number" },
          "paymentMode": { "type": "string" },
          "customerName": { "type": "string" },
          "customerEmail": { "type": "string" }
        }
      },
      "CreateRefundRequest": {
        "type": "object",
        "required": ["merchantId", "sabpaisaTxnId", "refundAmount"],
        "properties": {
          "merchantId": { "type": "string" },
          "sabpaisaTxnId": { "type": "string" },
          "refundAmount": { "type": "integer" },
          "reason": { "type": "string" }
        }
      }
    }
  }
}
