> ## Documentation Index
> Fetch the complete documentation index at: https://docs.papp.sa/llms.txt
> Use this file to discover all available pages before exploring further.

# Create an earning order

> Creates an earning order (points awarded on a completed off-platform purchase). The request is serialized inside a distributed lock scoped to the authenticated merchant to reduce concurrent processing conflicts.




## OpenAPI

````yaml /openapi/points-api-v1.yaml post /v1/orders/earning
openapi: 3.0.3
info:
  title: Points API
  version: 1.0.0
  description: >
    OpenAPI definition for the `/api/v1` endpoints defined in
    `routes/api_v1.php`. This spec reflects the behaviour of the Laravel backend
    and is the source of truth for integrators.
  contact:
    name: Points API Support
    email: support@papp.sa
    url: https://docs.papp.sa
servers:
  - url: https://business.papp.sa/api
    description: Production server
  - url: https://sandbox.papp.sa/api
    description: Sandbox server
security:
  - ApiKeyAuth: []
tags:
  - name: Orders
    description: Operations for creating and managing orders
  - name: Webhooks
    description: CRUD operations for merchant webhooks
  - name: Integration
    description: External-platform toggle state (e.g. Matajer's "ربط بوينتس" switch)
paths:
  /v1/orders/earning:
    post:
      tags:
        - Orders
      summary: Create an earning order
      description: >
        Creates an earning order (points awarded on a completed off-platform
        purchase). The request is serialized inside a distributed lock scoped to
        the authenticated merchant to reduce concurrent processing conflicts.
      operationId: createEarningOrder
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EarningOrderRequest'
      responses:
        '200':
          description: >
            Order created successfully. When the customer's account is not yet
            activated, a `pending_client_activation` payload is returned instead
            of a full order resource.
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/OrderResponse'
                  - $ref: '#/components/schemas/PendingEarningResponse'
        '400':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '422':
          $ref: '#/components/responses/ValidationError'
        '429':
          description: Too many concurrent requests for the same merchant.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    EarningOrderRequest:
      type: object
      required:
        - phone_number
        - total_price
        - products
        - order_number
      properties:
        phone_number:
          type: string
          description: >
            Customer phone number. Accepted formats: `+966XXXXXXXXX`,
            `00966XXXXXXXXX`, `966XXXXXXXXX`, `0XXXXXXXXX`, or bare `5XXXXXXXX`.
            Normalised server-side to `5\d{8}`.
          example: '512345678'
        name:
          type: string
          maxLength: 255
          nullable: true
          example: Ahmed Al-Saud
        total_price:
          type: number
          format: float
          minimum: 0
          description: Purchase amount (SAR).
          example: 115
        shipping_amount:
          type: number
          format: float
          minimum: 0
          nullable: true
          example: 15
        tax_amount:
          type: number
          format: float
          minimum: 0
          nullable: true
          example: 22.5
        discount_amount:
          type: number
          format: float
          minimum: 0
          nullable: true
          example: 10
        shipping_address:
          $ref: '#/components/schemas/ShippingAddress'
        products:
          type: array
          minItems: 1
          items:
            $ref: '#/components/schemas/ProductLine'
        order_number:
          type: string
          description: Merchant order reference.
          example: SALE-2024-0021
        metadata:
          type: object
          nullable: true
          additionalProperties: true
          description: Optional metadata stored on the order.
    OrderResponse:
      allOf:
        - $ref: '#/components/schemas/ApiMetadata'
        - type: object
          required:
            - data
          properties:
            data:
              $ref: '#/components/schemas/OrderResource'
    PendingEarningResponse:
      allOf:
        - $ref: '#/components/schemas/ApiMetadata'
        - type: object
          required:
            - data
          properties:
            data:
              type: object
              required:
                - pending_client_activation
                - pending_earning_order
              properties:
                pending_client_activation:
                  type: boolean
                  description: >
                    Always `true` in this response variant. Indicates the
                    customer has not yet activated their Points account.
                  example: true
                pending_earning_order:
                  type: object
                  description: Snapshot of the queued earning order.
                  properties:
                    id:
                      type: integer
                      example: 99
                    merchant_order_number:
                      type: string
                      example: SALE-2024-0021
                    total_price:
                      type: number
                      format: float
                      example: 115
                    total_points:
                      type: number
                      format: float
                      example: 1150
    ErrorResponse:
      type: object
      required:
        - status
        - message
        - appended_data
      properties:
        status:
          type: boolean
          example: false
        message:
          type: string
          example: Invalid or inactive API key
        appended_data:
          type: object
          additionalProperties: true
          example: {}
    ShippingAddress:
      type: object
      nullable: true
      description: Optional shipping address. All fields are individually optional.
      properties:
        city:
          type: string
          maxLength: 100
          nullable: true
          example: Riyadh
        line1:
          type: string
          maxLength: 255
          nullable: true
          example: 1234 King Fahd Rd, Al Olaya
    ProductLine:
      type: object
      required:
        - product_name
        - product_price
        - quantity
      properties:
        product_name:
          type: string
          example: Cappuccino
        product_price:
          type: number
          format: float
          minimum: 0
          example: 18.5
        quantity:
          type: integer
          minimum: 1
          example: 2
    ApiMetadata:
      type: object
      required:
        - status
        - message
        - appended_data
      properties:
        status:
          type: boolean
          example: true
        message:
          type: string
          nullable: true
          example: ''
        appended_data:
          type: object
          additionalProperties: true
          example: {}
    OrderResource:
      type: object
      properties:
        id:
          type: integer
          example: 42
        uuid:
          type: string
          format: uuid
          example: 550e8400-e29b-41d4-a716-446655440000
        reference_number:
          type: string
          nullable: true
          example: REF-2024-001234
        order_status:
          type: string
          enum:
            - new
            - approved
            - authorized
            - captured
            - cancelled
            - fully_refunded
            - partially_refunded
          description: Financial state of the order.
          example: approved
        status_label:
          type: string
          description: >
            Resolved display label for the shipping status. Prefers the
            merchant's `custom_status` value in the request's locale when set,
            otherwise falls back to the canonical enum label ("New",
            "Delivered", …). **Render this directly** — locale switching +
            custom-label fallback are already applied server-side.
          example: خرج للتوصيل
        custom_status:
          type: object
          nullable: true
          description: >
            Merchant-defined free-form shipping label, populated by sending
            `name_ar` / `name_en` to `POST /v1/orders/{uuid}/status`. `null`
            when the merchant hasn't set a custom label — `status_label` falls
            back to the canonical enum text. Useful for multi-locale clients
            that switch language without refetching.
          properties:
            ar:
              type: string
              example: خرج للتوصيل
            en:
              type: string
              example: Out for delivery
        order_number:
          type: string
          example: ORD-2024-001234
        type:
          type: integer
          enum:
            - 1
            - 2
          description: |
            Order type: `1` = earning, `2` = replacing.
          example: 1
        total_price:
          type: number
          format: float
          example: 150.5
        total_points:
          type: number
          format: float
          example: 1505
        metadata:
          type: object
          nullable: true
          additionalProperties: true
          example:
            source: mobile_app
        date:
          type: string
          format: date
          nullable: true
          example: '2024-12-23T00:00:00.000Z'
        time:
          type: string
          nullable: true
          example: '10:00:00'
        since:
          type: string
          nullable: true
          description: Human-readable relative time (e.g. "2 hours ago").
          example: 2 hours ago
        settlement_status:
          type: string
          nullable: true
          description: >-
            Settlement state of the order (backed enum value of
            OrderSettlementStatus).
          example: settled
        status:
          type: integer
          nullable: true
          description: >-
            Shipping status as a backed integer enum value of
            OrderShippingStatus.
          example: 1
        created_at:
          type: string
          format: date-time
          example: '2024-12-23T10:00:00.000Z'
        updated_at:
          type: string
          format: date-time
          example: '2024-12-23T15:30:00.000Z'
        items:
          type: array
          nullable: true
          description: Present only when products were attached to the order.
          items:
            $ref: '#/components/schemas/ProductLine'
    ValidationErrorResponse:
      type: object
      required:
        - status
        - message
        - appended_data
      properties:
        status:
          type: boolean
          example: false
        message:
          type: string
          example: The given data was invalid.
        appended_data:
          type: object
          additionalProperties: true
          example: {}
        errors:
          type: object
          description: >
            Field-level validation errors. Keys are field names (dot-notation
            for nested fields, e.g. `products.0.product_price`). Values are
            arrays of error message strings.
          additionalProperties:
            type: array
            items:
              type: string
          example:
            total_price:
              - The total price field is required.
            products.0.quantity:
              - The quantity must be at least 1.
  responses:
    Unauthorized:
      description: >
        The `x-api-key` header is missing, invalid, or belongs to an inactive
        merchant. The Points API returns `400` (not `401`) for auth failures.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    Forbidden:
      description: >
        The authenticated merchant is not allowed to perform this action (e.g.
        merchant is unverified, or the feature is disabled on this account).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    ValidationError:
      description: Request body failed validation.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ValidationErrorResponse'
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key

````