openapi: 3.1.0
info:
  title: CPAPI bet refund
  version: cpapi-refund.v1
  description: |
    Candidate optional profile for a studio to refund a full bet in a fiat or crypto
    session opened with this module enabled. SC and GC use the Sweepstakes profile.
    Use the registered Money origin and the credential for the session environment.
    Sign the exact UTF-8 I-JSON request bytes using the CPAPI v1 Money profile.
security:
  - Signature: []
    SignatureInput: []
paths:
  /v1/transactions/refund:
    post:
      operationId: refundBet
      summary: Refund the full amount of a bet
      x-cpapi-covered-components: ['@method', '@authority', '@path', content-digest]
      description: |
        Send a new refund transaction_id and Idempotency-Key. Set bet_transaction_id
        to the original studio bet ID. Repeat the bet's player, session, round,
        currency and amount. A new refund that requires wallet delivery is eligible
        for 604800 seconds (seven days) from the platform's receipt of the bet.
        An existing refund can finish delivery or recovery after that deadline.

        A missing or unsent bet is closed without money movement. A later bet with
        that ID gets 409 transaction_key_conflict and its status shows declined,
        terminal, refunded. A bet already declined by the wallet also closes without
        money movement. For a bet with an unknown wallet outcome, the first refund
        delivery requires verification of the specific wallet bound to that operator,
        session and environment. That wallet returns an actual debit once, or keeps
        the balance unchanged and rejects a late bet. If verification is missing
        when the refund is submitted, the platform returns 403 before recording
        the refund. Contact support to reconcile the bet. If verification lapses
        after the refund is recorded but before first delivery, the same refund
        stays pending. No wallet request or delivery attempt is made until the
        platform verifies the original session's wallet again.

        After a timeout or 500, read GET /v1/transactions/{transaction_id} for the
        refund ID. A terminal status is authoritative even when there is no balance
        receipt. A first confirmed wallet refusal (402) ends this refund with
        wallet_refused and keeps wins on the original bet blocked. A wallet 5xx
        or a connection lost before response headers schedules redelivery with the
        original wallet request bytes and ID while attempts remain. A refusal on
        redelivery or an ambiguous response ends automatic delivery, keeps wins
        blocked and calls for reconciliation.

        A pending, non-terminal refund can be waiting for its first wallet delivery,
        a scheduled redelivery or finalization. Resume with the same transaction ID,
        key and body, signed with a fresh nonce. For indeterminate, stop and contact
        support with both transaction IDs. If status returns 404 transaction_not_found
        after a timeout, send the original request again with the same identity.
      parameters:
        - name: Idempotency-Key
          in: header
          required: true
          schema: { type: string, minLength: 1, maxLength: 255 }
          description: The original business key for this refund. Money binds the key to the typed command.
        - name: Content-Digest
          in: header
          required: true
          schema: { type: string }
          description: RFC 9530 sha-256 of the exact request bytes, covered by the Money signature.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: './wire.schema.json#/$defs/Refund' }
            example:
              tenant_id: operator-1
              integration_id: studio-1
              environment: certification
              player_ref: player-1
              session_id: session-1
              round_id: round-1
              transaction_id: refund-42
              bet_transaction_id: bet-17
              amount: { currency: EUR, amount_minor: 150 }
      responses:
        '201':
          description: |
            Terminal committed or declined receipt with a balance in the bet currency.
            A refund for an absent or unsent bet commits with unknown_original. A refund
            of a declined bet commits with original_declined. Replaying either returns
            the same outcome with the current balance. For a refund delivered to the
            wallet, an exact replay returns the stored receipt when available.
          content:
            application/json:
              schema: { $ref: './wire.schema.json#/$defs/Result' }
              example:
                transaction_id: refund-42
                outcome: committed
                outcome_finality: terminal
                balance: { currency: EUR, amount_minor: 10150 }
        '400':
          description: An invalid request or an ordinary session opened without this module returns validation_failed (never). Malformed JSON or money uses the existing v1 problem.
          content:
            application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } }
        '403':
          description: |
            session_binding_mismatch (never) for a different amount, currency, player,
            session, round or transaction type, an expired window for a new monetary
            refund, or an unverified wallet for a bet with an unknown outcome.
          content:
            application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } }
        '409':
          description: |
            A bet still in flight during its initial 60 seconds gives unknown_bet_reference
            (after_backoff). No refund is recorded. Resend the same request after backoff.
            An active or committed win, another refund for the bet, or a changed
            key for the same transaction gives transaction_key_conflict (poll_status).
            A definitively declined win permits a refund if the other conditions hold.
            Reusing the same key for a changed command gives idempotency_fingerprint_conflict (never).
          content:
            application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } }
        '500':
          description: |
            internal_error (poll_status). This includes a missing or invalid balance
            receipt after a wallet decision. For a first confirmed 402 without a valid
            receipt, both the original and repeated POST return 500. Replaying that
            request makes no further wallet call. After finalization, GET for the
            refund shows declined, terminal, wallet_refused.
          content:
            application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } }
        default:
          description: CPAPI v1 Money problem with the registered retry_class.
          content:
            application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } }
components:
  securitySchemes:
    Signature: { type: apiKey, in: header, name: Signature }
    SignatureInput: { type: apiKey, in: header, name: Signature-Input }
  schemas:
    Problem:
      $ref: '../../../p0/machine-schemas/schemas/problem.schema.json#/$defs/Problem'
