# Run a refund

Run a referenced refund with the Tap to Pay on iPhone SDK using the uniqueRef of the original sale.

**Prerequisites:** [Authentication](/api/authentication) · [Run a sale](/guides/payments/tap-to-pay-on-iphone/run-a-sale)

**Important:** You can't run an unreferenced refund with the Tap to Pay on iPhone SDK. For more information about refund types, go to [Refunds and reversals](/knowledge/payments/refunds-and-reversals).

A referenced refund returns funds to the same card that the customer used for the original sale. You don't need the customer to tap their card again. If you refund a sale that hasn't settled yet, our gateway reverses the sale instead.

Before you begin, you need the `uniqueRef` of the sale that you want to refund. If you don't have it, go to [Retrieve transactions](/guides/payments/tap-to-pay-on-iphone/retrieve-transactions).

<a id="integration-steps"></a>
## Integration steps

1. [Register the refund listener](#step-1-register-the-refund-listener).
2. [Run the refund](#step-2-run-the-refund).

<a id="step-1-register-the-refund-listener"></a>
## Step 1. Register the refund listener

The SDK returns refund results to `onRefundResponse:` on your `CoreAPIRefundListener`. To register your refund listener when your view appears, and unregister it when the view disappears, use the following code:

**`Swift`**

```swift
// viewWillAppear:
terminal.register(self as CoreAPIRefundListener)

// viewWillDisappear:
terminal.unRegister(self as CoreAPIRefundListener)
```

**`Objective-C`**

```objectivec
// viewWillAppear:
[[WTPSTerminal singleton] registerCoreAPIRefundListener:self];

// viewWillDisappear:
[[WTPSTerminal singleton] unRegisterCoreAPIRefundListener:self];
```

<a id="step-2-run-the-refund"></a>
## Step 2. Run the refund

To run a refund, create a `CoreRefund`, and then call `processRefund:`.

`CoreRefund` includes the following properties:

| **Property** | **Description** | **Required or optional** |
| --- | --- | --- |
| `uniqueRef` | Unique ID of the sale that you want to refund. | Required |
| `reason` | Reason for the refund. | Required |
| `amount` | Amount that you want to refund. | Required |
| `orderId` | Your identifier for the order. | Optional |
| `previousTxnDateTime` | The date and time of the original transaction. | Optional |
| `autoCapture` | Whether to capture the refund automatically. The default value is `true`. | Optional |

The following example refunds a sale:

**`Swift`**

```swift
let refund = CoreRefund()
refund.uniqueRef = "ORIGINAL_UNIQUE_REF"
refund.reason = "Customer request"
refund.amount = 42.00

terminal.processRefund(refund)
```

**`Objective-C`**

```objectivec
CoreRefund *refund = [[CoreRefund alloc] init];
refund.uniqueRef = @"ORIGINAL_UNIQUE_REF";
refund.reason = @"Customer request";
refund.amount = [NSNumber numberWithDouble:42.00];

[[WTPSTerminal singleton] processRefund:refund];
```

The SDK returns the result to `onRefundResponse:`.

**Note:** If the SDK reversed the sale instead of refunding it, `onRefundResponse:` returns a `CoreSaleResponse` instead of a `CoreRefundResponse`.

<a id="refund-errors"></a>
### Refund errors

`CoreAPIRefundListener` uses the same `onError:withDescription:` signature as the sale listener. For more information, go to [Handle errors](/guides/payments/tap-to-pay-on-iphone/handle-errors).
