openapi: 3.1.0
info:
  title: Kadja Pay API
  version: 0.1.0
  description: |
    Kadja Pay merchant test gateway. Amounts are integers in the smallest
    currency unit (5000 = $50.00). Test mode only: no real card is charged.
servers:
  - url: http://localhost:3000
    description: Local development
security:
  - bearerAuth: []
paths:
  /api/v1/transactions/initialize:
    post:
      operationId: initializeTransaction
      summary: Initialize a transaction
      tags: [Transactions]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/InitializeRequest'
      responses:
        '200':
          description: Authorization URL created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InitializeResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '409':
          $ref: '#/components/responses/Conflict'
        '429':
          $ref: '#/components/responses/RateLimited'
  /api/v1/transactions/verify/{reference}:
    get:
      operationId: verifyTransaction
      summary: Verify a transaction
      tags: [Transactions]
      parameters:
        - name: reference
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Verification result
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/VerifyResponse'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
  /api/v1/transactions/refund:
    post:
      operationId: refundTransaction
      summary: Refund a successful transaction (full or partial)
      tags: [Refunds]
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RefundRequest'
      responses:
        '200':
          description: Refund processed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RefundResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          $ref: '#/components/responses/Conflict'
        '429':
          $ref: '#/components/responses/RateLimited'
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Use a test secret key (`sk_test_...`) from the dashboard.
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      schema:
        type: string
        minLength: 4
        maxLength: 120
      description: |
        Same key + same body replays the original response. Same key with a
        different body returns 409.
  schemas:
    InitializeRequest:
      type: object
      required: [email, amount]
      properties:
        email:
          type: string
          format: email
        amount:
          type: integer
          minimum: 100
          maximum: 100000000
          description: Amount in the smallest currency unit.
        currency:
          type: string
          enum: [USD, SDG, EGP, EUR, GBP]
          default: USD
        reference:
          type: string
          pattern: '^[A-Za-z0-9_-]{6,64}$'
        callback_url:
          type: string
          format: uri
        metadata:
          type: object
          additionalProperties: true
    InitializeResponse:
      type: object
      properties:
        status:
          type: boolean
        message:
          type: string
        data:
          type: object
          properties:
            authorization_url:
              type: string
              format: uri
            access_code:
              type: string
            reference:
              type: string
    VerifyResponse:
      type: object
      properties:
        status:
          type: boolean
        message:
          type: string
        data:
          $ref: '#/components/schemas/Transaction'
    RefundRequest:
      type: object
      required: [transaction]
      properties:
        transaction:
          type: string
          description: Transaction reference.
        amount:
          type: integer
          minimum: 1
          description: Optional amount in minor units. Defaults to the remaining refundable amount.
        reason:
          type: string
          maxLength: 200
    RefundResponse:
      type: object
      properties:
        status:
          type: boolean
        message:
          type: string
        data:
          type: object
          properties:
            reference:
              type: string
            status:
              type: string
              enum: [success, refunded]
            amount:
              type: integer
              description: Cumulative refunded amount in minor units.
            refunded_now:
              type: integer
            refund_count:
              type: integer
            remaining:
              type: integer
            currency:
              type: string
            refunded_at:
              type: string
              format: date-time
    Transaction:
      type: object
      properties:
        reference:
          type: string
        amount:
          type: integer
        currency:
          type: string
        status:
          type: string
          enum: [pending, success, failed, refunded, abandoned, disputed, chargeback]
        environment:
          type: string
          enum: [test, live]
        payment_method:
          type: string
          nullable: true
          enum: [card, bank-transfer, mobile-wallet, null]
        fee_amount:
          type: integer
          nullable: true
        net_amount:
          type: integer
          nullable: true
        settlement_id:
          type: string
          nullable: true
        paid_at:
          type: string
          format: date-time
          nullable: true
        expires_at:
          type: string
          format: date-time
        refunded_at:
          type: string
          format: date-time
          nullable: true
        refund_amount:
          type: integer
          nullable: true
        refund_count:
          type: integer
        customer:
          type: object
          properties:
            email:
              type: string
        metadata:
          type: object
          nullable: true
          additionalProperties: true
    Error:
      type: object
      properties:
        status:
          type: boolean
          example: false
        message:
          type: string
  responses:
    BadRequest:
      description: Invalid request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    Unauthorized:
      description: Missing or invalid API key
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    Forbidden:
      description: Email not verified, live keys disabled, or KYC required
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotFound:
      description: Transaction not found
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    Conflict:
      description: Reference exists, invalid idempotency reuse, or final transaction
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    RateLimited:
      description: Too many requests
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
