{
  "openapi": "3.1.0",
  "info": {
    "title": "PayIran API",
    "version": "1.0.0",
    "description": "Payments, accounting, settlements, payout requests and signed webhooks."
  },
  "servers": [
    {
      "url": "https://api.payiran.org/v1"
    }
  ],
  "components": {
    "securitySchemes": {
      "PayIranHMAC": {
        "type": "apiKey",
        "in": "header",
        "name": "X-PIS-Key",
        "description": "HMAC authentication also requires X-PIS-Timestamp, X-PIS-Nonce, X-PIS-Signature-Version: 2 and X-PIS-Signature."
      }
    },
    "schemas": {
      "Payment": {
        "type": "object",
        "properties": {
          "payment_id": {
            "type": "string"
          },
          "order_id": {
            "type": "string"
          },
          "amount": {
            "type": "integer"
          },
          "status_code": {
            "type": "integer"
          },
          "status": {
            "type": "string"
          },
          "reference": {
            "type": [
              "string",
              "null"
            ]
          },
          "expected_card": {
            "type": [
              "string",
              "null"
            ]
          },
          "paid_card": {
            "type": [
              "string",
              "null"
            ]
          },
          "card_match": {
            "type": [
              "boolean",
              "null"
            ]
          },
          "review_reason": {
            "type": [
              "string",
              "null"
            ]
          },
          "review_action": {
            "type": [
              "string",
              "null"
            ]
          },
          "environment": {
            "type": "string"
          },
          "created_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "paid_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean",
            "const": false
          },
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            },
            "required": [
              "code",
              "message"
            ]
          },
          "request_id": {
            "type": "string"
          }
        },
        "required": [
          "ok",
          "error",
          "request_id"
        ]
      }
    }
  },
  "security": [
    {
      "PayIranHMAC": []
    }
  ],
  "paths": {
    "/payments": {
      "post": {
        "summary": "Create payment",
        "description": "Requires Idempotency-Key. Scope: payments:write.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 128
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "order_id",
                  "amount",
                  "card",
                  "callback"
                ],
                "properties": {
                  "order_id": {
                    "type": "string"
                  },
                  "amount": {
                    "type": "integer",
                    "minimum": 1000
                  },
                  "card": {
                    "type": "string",
                    "pattern": "^[0-9]{16}$"
                  },
                  "callback": {
                    "type": "string",
                    "format": "uri"
                  },
                  "return_url": {
                    "type": "string",
                    "format": "uri"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Payment created"
          },
          "409": {
            "description": "Idempotency or duplicate conflict"
          },
          "422": {
            "description": "Validation error"
          }
        }
      }
    },
    "/payments/verify": {
      "post": {
        "summary": "Verify payment",
        "description": "Scope: payments:read.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "order_id"
                ],
                "properties": {
                  "order_id": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "$ref": "#/components/schemas/Payment"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/payments/{orderId}": {
      "get": {
        "summary": "Get payment",
        "description": "Scope: payments:read.",
        "parameters": [
          {
            "name": "orderId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "$ref": "#/components/schemas/Payment"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/transactions": {
      "get": {
        "summary": "List transactions",
        "description": "Scope: payments:read. Filters: status, order_id, reference, environment, from, to, min_amount, max_amount, per_page.",
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "order_id",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "reference",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "environment",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "live",
                "sandbox"
              ]
            }
          },
          {
            "name": "from",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "to",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "min_amount",
            "in": "query",
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "max_amount",
            "in": "query",
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "per_page",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/balance": {
      "get": {
        "summary": "Get settlement balance",
        "description": "Scope: accounting:read.",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/daily-summary": {
      "get": {
        "summary": "Get daily summary",
        "description": "Scope: accounting:read.",
        "parameters": [
          {
            "name": "date",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/settlements": {
      "get": {
        "summary": "List settlements",
        "description": "Scope: accounting:read.",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/payout-accounts": {
      "get": {
        "summary": "List payout accounts",
        "description": "Scope: payouts:read.",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create payout account",
        "description": "Scope: payouts:write.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "type",
                  "address"
                ],
                "properties": {
                  "type": {
                    "type": "string",
                    "enum": [
                      "usdt_trc20",
                      "usdt_bep20"
                    ]
                  },
                  "label": {
                    "type": "string"
                  },
                  "address": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created"
          }
        }
      }
    },
    "/payout-accounts/{id}": {
      "delete": {
        "summary": "Disable payout account",
        "description": "Scope: payouts:write.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/payout-requests": {
      "get": {
        "summary": "List payout requests",
        "description": "Scope: payouts:read.",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create payout request",
        "description": "Scope: payouts:write.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "method",
                  "amount_toman"
                ],
                "properties": {
                  "method": {
                    "type": "string",
                    "enum": [
                      "irt",
                      "usdt_trc20",
                      "usdt_bep20"
                    ]
                  },
                  "amount_toman": {
                    "type": "integer"
                  },
                  "payout_account_id": {
                    "type": "integer"
                  },
                  "note": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created"
          }
        }
      }
    },
    "/webhooks/deliveries": {
      "get": {
        "summary": "List webhook deliveries",
        "description": "Scope: webhooks:read.",
        "parameters": [
          {
            "name": "order_id",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/webhooks/retry": {
      "post": {
        "summary": "Retry current webhook event",
        "description": "Scope: webhooks:write.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "order_id"
                ],
                "properties": {
                  "order_id": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "data": {
                      "type": "object"
                    },
                    "request_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/sandbox/payments": {
      "post": {
        "summary": "Create sandbox payment",
        "description": "Scope: payments:write. Does not enter live accounting.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "order_id",
                  "amount",
                  "card",
                  "callback",
                  "scenario"
                ],
                "properties": {
                  "order_id": {
                    "type": "string"
                  },
                  "amount": {
                    "type": "integer"
                  },
                  "card": {
                    "type": "string"
                  },
                  "paid_card": {
                    "type": "string"
                  },
                  "callback": {
                    "type": "string",
                    "format": "uri"
                  },
                  "scenario": {
                    "type": "string",
                    "enum": [
                      "success",
                      "card_mismatch",
                      "failed",
                      "canceled",
                      "pending"
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Sandbox event created"
          }
        }
      }
    }
  }
}