openapi: 3.1.0
info:
  title: Walaay Public Merchant API
  version: "2026-10-01"
  description: |
    REST API for POS / ERP loyalty integrations.
    Authenticate with merchant API keys (`wl_live_*` or `wl_test_*`).
    Sandbox and live data are fully isolated.
  contact:
    name: Walaay Developers
    url: https://walaay.com.sa/developers/api
servers:
  - url: https://walaay.com.sa/api/v1
    description: Production
  - url: http://localhost:3000/api/v1
    description: Local development
security:
  - ApiKeyAuth: []
tags:
  - name: Identity
  - name: Customers
  - name: Transactions
  - name: Redemptions
  - name: Rewards
  - name: Branches
paths:
  /health:
    get:
      tags: [Identity]
      security: []
      summary: Health check
      operationId: getHealth
      responses:
        "200":
          description: OK
  /me:
    get:
      tags: [Identity]
      summary: Key and merchant context
      operationId: getMe
      responses:
        "200":
          description: Merchant + key metadata
        "401":
          $ref: "#/components/responses/Problem"
  /branches:
    get:
      tags: [Branches]
      summary: List store branches
      operationId: listBranches
      responses:
        "200":
          description: Branches
  /customers/lookup:
    get:
      tags: [Customers]
      summary: Lookup customer by phone
      operationId: lookupCustomer
      parameters:
        - name: phone
          in: query
          required: true
          schema:
            type: string
            example: "+966501234567"
      responses:
        "200":
          description: Customer
        "404":
          $ref: "#/components/responses/Problem"
  /customers:
    post:
      tags: [Customers]
      summary: Find or create customer by phone
      operationId: createCustomer
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [phone]
              properties:
                phone:
                  type: string
                display_name:
                  type: string
                consent:
                  type: boolean
                  default: true
      responses:
        "200":
          description: Existing customer
        "201":
          description: Created
  /customers/{id}:
    get:
      tags: [Customers]
      summary: Get customer
      operationId: getCustomer
      parameters:
        - $ref: "#/components/parameters/CustomerId"
      responses:
        "200":
          description: Customer
  /customers/{id}/balance:
    get:
      tags: [Customers]
      summary: Customer balance
      operationId: getBalance
      parameters:
        - $ref: "#/components/parameters/CustomerId"
      responses:
        "200":
          description: Balance
  /customers/{id}/transactions:
    get:
      tags: [Transactions]
      summary: Customer ledger history
      operationId: listCustomerTransactions
      parameters:
        - $ref: "#/components/parameters/CustomerId"
        - name: limit
          in: query
          schema:
            type: integer
            default: 25
            maximum: 100
      responses:
        "200":
          description: Transactions
  /transactions/earn:
    post:
      tags: [Transactions]
      summary: Earn points from an order
      operationId: earnPoints
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                customer:
                  type: object
                  properties:
                    phone:
                      type: string
                    id:
                      type: string
                branch_id:
                  type: string
                  nullable: true
                order:
                  type: object
                  properties:
                    reference:
                      type: string
                    total_halalas:
                      type: integer
                points:
                  type: number
      responses:
        "201":
          description: Posted
  /transactions/{id}/reverse:
    post:
      tags: [Transactions]
      summary: Reverse / refund an earn
      operationId: reverseTransaction
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: "#/components/parameters/IdempotencyKey"
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                reason:
                  type: string
                points:
                  type: number
                refund_reference:
                  type: string
      responses:
        "201":
          description: Reversal posted
  /rewards:
    get:
      tags: [Rewards]
      summary: Rewards catalog
      operationId: listRewards
      responses:
        "200":
          description: Rewards
  /otp/request:
    post:
      tags: [Redemptions]
      summary: Request OTP via redemption start
      operationId: requestOtp
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
      responses:
        "201":
          description: OTP challenge created (sandbox code 000000)
  /otp/verify:
    post:
      tags: [Redemptions]
      summary: Verify OTP
      operationId: verifyOtp
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
      responses:
        "200":
          description: Verified
  /redemptions:
    post:
      tags: [Redemptions]
      summary: Start redemption
      operationId: createRedemption
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
      responses:
        "201":
          description: Pending OTP or verified
  /redemptions/{id}/verify-otp:
    post:
      tags: [Redemptions]
      summary: Verify redemption OTP
      operationId: verifyRedemptionOtp
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: "#/components/parameters/IdempotencyKey"
      responses:
        "200":
          description: Verified
  /redemptions/{id}/confirm:
    post:
      tags: [Redemptions]
      summary: Confirm redemption after OTP
      operationId: confirmRedemption
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: "#/components/parameters/IdempotencyKey"
      responses:
        "200":
          description: Completed
  /redemptions/{id}/cancel:
    post:
      tags: [Redemptions]
      summary: Cancel pending redemption
      operationId: cancelRedemption
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
        - $ref: "#/components/parameters/IdempotencyKey"
      responses:
        "200":
          description: Cancelled
components:
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      description: wl_live_<id>.<secret> or wl_test_<id>.<secret>
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: true
      schema:
        type: string
    CustomerId:
      name: id
      in: path
      required: true
      schema:
        type: string
  responses:
    Problem:
      description: RFC 9457 problem+json
      content:
        application/problem+json:
          schema:
            type: object
            properties:
              type:
                type: string
              title:
                type: string
              status:
                type: integer
              code:
                type: string
              detail:
                type: string
              message_ar:
                type: string
              request_id:
                type: string
webhooks:
  pointsEarned:
    post:
      summary: points.earned
      requestBody:
        content:
          application/json:
            schema:
              type: object
      responses:
        "200":
          description: Acknowledged
