{
  "openapi": "3.1.0",
  "info": {
    "title": "Paisaify payments API",
    "version": "1.0.0",
    "summary": "Create a UPI payment from your own server, send the shopper to the pay page, and get a signed message when it is paid.",
    "description": "Paisaify confirms UPI payments by reading the bank credit SMS on your phone. Payments go straight to your own UPI ID. Each open payment of the same price gets its own extra paise (up to 99), so the shopper must pay the exact amount in the reply. Guide: https://pay.flyingstar.in/guide",
    "contact": { "name": "Flying Star", "email": "support@flyingstar.in", "url": "https://paisaify.flyingstar.in/developers.html" }
  },
  "servers": [
    {
      "url": "{baseUrl}",
      "description": "Your own Paisaify server",
      "variables": { "baseUrl": { "default": "https://pay.example.com", "description": "The address where you host the Paisaify server" } }
    }
  ],
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/api/v1/payments": {
      "post": {
        "operationId": "createPayment",
        "summary": "Create a payment",
        "description": "Returns a pay_url. Redirect the shopper there. Keep the API key on your server, never in a browser or app.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreatePayment" },
              "example": {
                "amount": "499",
                "reference": "ORDER-1042",
                "description": "Starter kit",
                "customer": { "email": "buyer@example.com" },
                "callback_url": "https://yoursite.example/hooks/paisaify",
                "return_url": "https://yoursite.example/thanks",
                "valid_minutes": 15
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Payment created",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Payment" } } }
          },
          "400": { "$ref": "#/components/responses/Error400" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "409": {
            "description": "A payment with this reference already exists",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          },
          "503": {
            "description": "Too many open payments of this amount. Retry shortly. Also returned if the server has no UPI ID configured.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    },
    "/api/v1/payments/{id}": {
      "get": {
        "operationId": "getPayment",
        "summary": "Get a payment's status",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "description": "The id returned when the payment was created", "schema": { "type": "integer" } }
        ],
        "responses": {
          "200": {
            "description": "Current status",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/PaymentStatus" } } }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": {
            "description": "No payment with this id",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    }
  },
  "webhooks": {
    "paymentPaid": {
      "post": {
        "operationId": "paymentPaidWebhook",
        "summary": "Payment paid (sent to your callback_url)",
        "description": "Paisaify POSTs this JSON to the callback_url you gave when creating the payment. Verify it before you trust it: compute HMAC-SHA256 as lowercase hex over the raw request body followed by the value of X-Paisaify-Timestamp (plain string concatenation), using your merchant API key as the key, and compare it with X-Paisaify-Signature. Reject the call if the timestamp is more than 5 minutes old. Answer with any 2xx status. Other answers or timeouts are retried with growing gaps for about a day.",
        "security": [],
        "parameters": [
          { "name": "X-Paisaify-Signature", "in": "header", "required": true, "description": "HMAC-SHA256 hex of (raw body + timestamp) with your merchant API key", "schema": { "type": "string" } },
          { "name": "X-Paisaify-Timestamp", "in": "header", "required": true, "description": "Unix time in seconds when the call was signed", "schema": { "type": "string", "pattern": "^[0-9]+$" } }
        ],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/PaymentPaidEvent" } } }
        },
        "responses": { "200": { "description": "Received. Any 2xx status stops the retries." } }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Your MERCHANT_API_KEY, sent as: Authorization: Bearer MERCHANT_API_KEY"
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Missing or wrong API key",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "Error400": {
        "description": "Invalid request, for example a missing reference, an amount outside the allowed range, or a callback_url or return_url that is not http(s)",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    },
    "schemas": {
      "CreatePayment": {
        "type": "object",
        "required": ["amount", "reference"],
        "properties": {
          "amount": { "type": ["string", "number"], "description": "Rupees with up to two decimals. At least 1 and at most the server's MAX_AMOUNT (default 100000). The reply may add up to 99 paise." },
          "reference": { "type": "string", "maxLength": 80, "description": "Your order id. Must be unique." },
          "description": { "type": "string", "maxLength": 120 },
          "customer": {
            "type": "object",
            "properties": {
              "name": { "type": "string", "maxLength": 120 },
              "phone": { "type": "string", "maxLength": 20 },
              "email": { "type": "string", "maxLength": 120 }
            }
          },
          "callback_url": { "type": "string", "format": "uri", "description": "http(s) address that receives the signed payment.paid call" },
          "return_url": { "type": "string", "format": "uri", "description": "http(s) address the shopper is sent to after paying" },
          "valid_minutes": { "type": "integer", "minimum": 3, "maximum": 1440, "description": "How long the payment can be paid. Defaults to the server's payment window (10 minutes unless changed)." }
        }
      },
      "Payment": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "reference": { "type": "string" },
          "amount": { "type": "string", "pattern": "^[0-9]+\\.[0-9]{2}$", "description": "The exact amount the shopper must pay, with paise" },
          "status": { "type": "string", "enum": ["pending"] },
          "pay_url": { "type": "string", "format": "uri", "description": "Send the shopper here" },
          "upi": { "type": "string", "description": "A upi://pay link for the same payment" },
          "expires_at": { "type": "string", "format": "date-time" }
        }
      },
      "PaymentStatus": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "reference": { "type": "string" },
          "amount": { "type": "string", "pattern": "^[0-9]+\\.[0-9]{2}$" },
          "status": { "type": "string", "enum": ["pending", "paid", "expired"] },
          "utr": { "type": ["string", "null"], "description": "UPI reference from the bank SMS, once paid" },
          "paid_at": { "type": ["string", "null"], "format": "date-time" }
        }
      },
      "PaymentPaidEvent": {
        "type": "object",
        "required": ["event", "id", "reference", "amount", "utr", "paid_at"],
        "properties": {
          "event": { "type": "string", "enum": ["payment.paid"] },
          "id": { "type": "integer" },
          "reference": { "type": "string" },
          "amount": { "type": "string", "pattern": "^[0-9]+\\.[0-9]{2}$" },
          "utr": { "type": ["string", "null"] },
          "paid_at": { "type": "string", "format": "date-time" }
        }
      },
      "Error": {
        "type": "object",
        "properties": { "ok": { "type": "boolean", "const": false }, "error": { "type": "string" } }
      }
    }
  }
}
