# Unreferenced Refund

An **unreferenced refund** is a card-present credit that is **not tied to any original sale**. Instead of pointing at a previous payment, you start a brand new transaction and the customer presents their card at the terminal to receive the money back.

Use this when you need to return funds but don't have, or don't want to use, the original payment's transaction reference. If you do have the original payment, prefer a [Referenced Refund](./refund-a-payment.md), which links the credit back to the initial sale for cleaner reconciliation.

```admonition
icon: fas fa-exclamation-triangle
content: |
  Unreferenced refunds is currently not open to new applications. You may implement and test but not board any live customers.
```

```admonition
icon: fas fa-exclamation-triangle
content: |
  Referenced refunds are strongly recommended wherever possible.
  An unreferenced refund sends a credit to a card without any link to a prior payment. Because there is no original transaction to validate against, these refunds are subject to additional fraud screening. As a result:
    - Some merchants may not be eligible to process unreferenced refunds at all.
    - An individual unreferenced refund may be declined even where the same refund would succeed as a referenced refund.
    - Eligibility cannot be reliably predicted, so building exclusively on unreferenced refunds risks unexplained refusals for merchants.
To use referenced refunds, the ePOS stores the `gateway_payment_id` returned when the original payment succeeds - the identifier of the original payment on a successful payment request. This single stored field unlocks automatic validation, lower decline rates, prevention of over-refunds and duplicate refunds, and cleaner reconciliation, since each refund maps directly to its original payment.
  Use unreferenced refunds only where the original `gateway_payment_id` is genuinely unavailable.
```  

## Make an unreferenced refund

Call the `makeUnreferencedRefund` function on the `transactionsApi`. It reuses the same state machine as [`makePayment`](./make-payment.md), so it returns a `PaymentStateSubscription` rather than the `RefundResultSubscription` used by referenced refunds. In the coroutines API it likewise returns a `PaymentHandle`.

```tabs
    tab:
      title: Kotlin (callbacks)
      content:
        ```kotlin
        val refundSubscription = teyaPosLinkSDK.transactionsApi.makeUnreferencedRefund(
            transactionId = UUID.randomUUID().toString(), // or pass whatever identifier you already have for this refund. Must be unique within the linked store.
            amount = amountToRefund, // The amount to refund, in the smallest unit of the currency (e.g., cents).
            currency = "GBP" // The ISO 4217 currency code (e.g., "GBP", "EUR").
        )
        ```
    tab:
      title: Kotlin (coroutines)
      content:
        ```kotlin
        val refundHandle = teyaPosLinkSDK.transactions.makeUnreferencedRefund(
            transactionId = UUID.randomUUID().toString(), // or pass whatever identifier you already have for this refund. Must be unique within the linked store.
            amount = amountToRefund, // The amount to refund, in the smallest unit of the currency (e.g., cents).
            currency = "GBP" // The ISO 4217 currency code (e.g., "GBP", "EUR").
        )
        ```
    tab:
      title: Java
      content:
        ```java
        PaymentStateSubscription refundSubscription = teyaPosLinkSDK.getTransactionsApi().makeUnreferencedRefund(
                UUID.randomUUID().toString(), // transactionId: or pass whatever identifier you already have for this refund. Must be unique within the linked store.
                amountToRefund, // amount: The amount to refund, in the smallest unit of the currency (e.g., cents).
                "GBP"  // currency: The ISO 4217 currency code (e.g., "GBP", "EUR").
        );
        ```
```

```admonition
icon: fas fa-info-circle
content: |
    Unreferenced refunds are **not available for tab, or Pay at Table, payments**.
```

## Subscribe to the refund status

Since `makeUnreferencedRefund` returns a `PaymentStateSubscription`, you track its progress exactly like a payment. Subscribe a `PaymentStateChangeListener` and read the same `PaymentStateDetails` states described in [Make Payment](./make-payment.md), for example `In_Progress`, `Successful` and `Processing_Failed`.

```tabs
    tab:
      title: Kotlin (callbacks)
      content:
        ```kotlin
        val listener = object : PaymentStateSubscription.PaymentStateChangeListener {
            override fun onPaymentStateChanged(state: PaymentStateSubscription.PaymentStateDetails) {
                println("new state = $state, is it a final state = $state.isFinal")
            }
        }
        refundSubscription.subscribe(listener)
        ```
    tab:
      title: Kotlin (coroutines)
      content:
        ```kotlin
        refundHandle.stateUntilCompletion().collect { state ->
            println("new state = $state, is it a final state = ${state.isFinal}")
        }
        ```
    tab:
      title: Java
      content:
        ```java
        PaymentStateSubscription.PaymentStateChangeListener listener = state -> {
            System.out.println("new state = " + state + ", is it a final state = " + state.isFinal());
        };

        refundSubscription.subscribe(listener);
        ```
```

## Transaction type

The `PaymentStateSubscription` exposes a `transactionType` property so you can tell, at runtime, whether it tracks a payment or a refund. The coroutines API's `PaymentHandle` exposes the same property.

```kotlin
public enum class TransactionType {
    Payment, // Money charged to the customer.
    Refund,  // Money returned to the customer.
}
```

For an unreferenced refund, `refundSubscription.transactionType` (or `refundHandle.transactionType`) is `TransactionType.Refund`. The id of the created refund is available on `PaymentStateDetails.gatewayRefundId` once it reaches the `Successful` state.

## Re-attach to a refund in progress

If your app loses track of a refund it started, you can re-attach to it with `subscribeToPayment` and the ePOS `transactionId` you started it with - see [Subscribe to an existing payment](./make-payment.md#subscribe-to-an-existing-payment).

```admonition
icon: fas fa-warning-triangle
title: Availability
content: |
    Contact us at partnersupport@teya.com to have unreferenced refunds enabled for your account. Until the feature is activated, the refund reaches a failed state whose `reason` is `PaymentStateReason.FeatureNotActivatedUnreferencedRefunds`. You can read `state.reason` on the latest `PaymentStateDetails`.
```
