openapi: "3.0.0"
info:
  version: 1.0.0
  title: BharatX Web SDK
  description: BharatX Web SDK
  termsOfService: https://bharatx.tech/terms-and-conditions.pdf
  license:
    name: All Rights Reserved
servers:
  - url: https://web.bharatx.tech
  - url: http://localhost:8000
paths:
  /api/transaction:
    post:
      description: |
        Initiate a transaction
      operationId: post
      x-eov-operation-handler: routes/api/transaction
      parameters:
        - $ref: "#/components/parameters/partnerId"
        - $ref: "#/components/parameters/signature"
      requestBody:
        description: Transaction to initiate
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Transaction"
      responses:
        200:
          description: Successfully created the transaction
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TransactionResponse"
        400:
          $ref: "#/components/responses/400"
        401:
          $ref: "#/components/responses/401"
        default:
          description: Error in creating the transaction
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /api/transaction/status:
    get:
      description: |
        Get the status of a transaction
      operationId: get
      x-eov-operation-handler: routes/api/transaction/status
      parameters:
        - $ref: "#/components/parameters/partnerId"
        - name: "id"
          in: "query"
          required: true
          description: |
            Transaction ID to get the status of
          schema:
            type: string
      responses:
        200:
          description: Fetched the status of the transaction
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TransactionStatus"
        400:
          $ref: "#/components/responses/400"
        401:
          $ref: "#/components/responses/401"
        404:
          description: Transaction ID not found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
        default:
          description: Error in creating the transaction
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /api/abandoned-checkout:
    post:
      description: |
        Create an abandoned checkout entry
      operationId: create_abandoned_checkout
      x-eov-operation-handler: routes/api/abandoned-checkout
      parameters:
        - $ref: "#/components/parameters/partnerId"
        - $ref: "#/components/parameters/signature"
      requestBody:
        description: The abandoned checkout
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AbandonedCheckout"
      responses:
        201:
          description: Abandoned checkout created
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
        409:
          description: Abandoned checkout already exists
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
        default:
          description: Error in creating the transaction
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /api/abandoned-checkout/{checkoutId}:
    get:
      description: Get the status of abandoned checkout
      operationId: get_abandoned_checkout
      x-eov-operation-handler: routes/api/abandoned-checkout/get
      parameters:
        - $ref: "#/components/parameters/partnerId"
        - $ref: "#/components/parameters/checkoutId"
      responses:
        200:
          description: Fetched status of the checkout
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AbandonedCheckout"
        default:
          description: Error in fetching the checkout
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /api/abandoned-checkout/cancel:
    post:
      description: Cancel an abandoned checkout
      operationId: cancel_abandoned_checkout
      x-eov-operation-handler: routes/api/abandoned-checkout/cancel
      parameters:
        - $ref: "#/components/parameters/partnerId"
        - $ref: "#/components/parameters/signature"
      requestBody:
        description: The abandoned checkout
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - checkoutId
              properties:
                checkoutId:
                  type: string
                  description: The checkoutId for the abandoned cart that has to be cancelled

      responses:
        200:
          description: Checkout successfully cancelled
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    description: "ok"
        default:
          description: Error in cancelling the checkout
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"

components:
  schemas:
    Message:
      type: object
      required:
        - message
      properties:
        message:
          type: string
          description: Message
    Error:
      type: object
      required:
        - message
      properties:
        message:
          type: string
          description: Error Message
    AbandonedCheckout:
      type: object
      required:
        - checkoutId
        - userPhoneNumber
        - userName
        - userEmail
        - amount
        - cart
      properties:
        partnerId:
          type: string
          readOnly: true
        checkoutId:
          type: string
          description: a unique identifier for the abandoned cart
        userName:
          type: string
          description: The name of the user doing the transaction
        userPhoneNumber:
          type: string
          pattern: '^\+91\d{10}$'
          description: The phone number of the user doing the transaction. A whatsapp message will be triggered to this number on cart creation.
        userEmail:
          type: string
          description: The email of the user doing the transaction.
        amount:
          description: The total cart amount in paise.
          type: number
        redirectUri:
          type: string
          description: The user will be redirected to this page post transaction
          nullable: true
        webhookUri:
          type: string
          nullable: true
          description: The end-point to hit once the payment for the checkout is successful [AbandonedCart object is sent to this end-point]
        status:
          type: string
          description: status of the cart
          enum: [PENDING, SUCCESS, PARTNER_CANCELLED, EXPIRED, FAILURE]
          readOnly: true
        cartUri:
          type: string
          readOnly: true
        cart:
          description: The abandoned cart details
          type: object
          required:
            - items
          properties:
            items:
              description: The items in the cart
              type: array
              minItems: 1
              items:
                type: object
                required:
                  - itemName
                  - itemPrice
                properties:
                  itemName:
                    type: string
                    description: The name of the item
                  itemPrice:
                    type: number
                    description: The total price for the item in paise.

    Transaction:
      type: object
      required:
        - id
        - amount
        - user
      properties:
        id:
          type: string
          description: A transaction identifier unique across your requests
        amount:
          type: number
        user:
          type: object
          required:
            - name
            - phoneNumber
            - email
          properties:
            name:
              type: string
              description: The name of the user doing the transaction
            phoneNumber:
              type: string
              pattern: '^\+91\d{10}$'
              description: The phone number of the user doing the transaction
            email:
              type: string
              description: The email of the user doing the transaction
        redirect:
          type: object
          properties:
            url:
              type: string
              description: The URL to redirect to after the transaction completes. If not passed, it will redirect to your homepage
            logoOverride:
              type: string
              description: The logo URL to use instead of your default logo (don't use this unless necessary)
            colorOverride:
              type: string
              description: The hex color of the format \#xxxxxx to use instead of your default hex color (don't use this unless necessary)
    TransactionResponse:
      type: object
      required:
        - message
      properties:
        message:
          type: string
          description: Message
        redirectUrl:
          type: string
          description: The URL to redirect your user to for completing the transaction
    TransactionStatus:
      type: object
      required:
        - status
      properties:
        status:
          type: string
          description: The status of the transaction. Can be PENDING, SUCCESS, FAILURE or CANCELLED.
          enum: [PENDING, SUCCESS, FAILURE, CANCELLED]
        signature:
          type: string
          description: Used to verify the authenticity of the status update.
  responses:
    400:
      description: Bad request
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
    401:
      description: Unauthorized
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Error"
  parameters:
    partnerId:
      name: "X-Partner-Id"
      in: header
      required: true
      description: The partner id assigned to you. You can use 'testPartnerId' for testing purposes.
      schema:
        type: string
    signature:
      name: "X-Signature"
      in: header
      required: true
      description: |
        Base-64 encoded SHA256 hash of request body + request URL without https:// and domain name 
        _(For example, in http://example.org/api/example1/?a=b, you should consider only
        /api/example1/?a=b)_ + privateApiKey.
        You can use 'testPrivateKey' as privateApiKey for testing purposes.
      schema:
        type: string
    checkoutId:
      name: "checkoutId"
      in: path
      required: true
      description: The checkoutId
      schema:
        type: string
