# Refund a payment

> Refund all or part of a successful payment, then follow the refund until it finishes.

Source: https://docs.tappay.mn/guides/refunds/

## Overview {#overview}

A [refund](https://docs.tappay.mn/glossary.md#refund) returns all or part of a successful [payment](https://docs.tappay.mn/glossary.md#payment) to the payer. You create it from your server, and TapPay sends the money back the way it came. You can refund one payment several times, until the refunds add up to the paid amount.

## Before you begin {#before-you-begin}

Your key needs `refunds:write` and `checkout:write` in its [scope](https://docs.tappay.mn/glossary.md#scope), and you use your [secret key](https://docs.tappay.mn/glossary.md#secret-key), shown below as `sk_test_YOUR_SECRET_KEY`. Use a [sandbox](https://docs.tappay.mn/glossary.md#sandbox) key while you build. The refund amount comes out of your merchant balance, so keep enough balance for it.

## Steps {#steps}

### Create a payment to refund {#create-a-payment-to-refund}

You need a payment that has succeeded. Create a [checkout session](https://docs.tappay.mn/glossary.md#checkout-session) as in the [checkout guide](https://docs.tappay.mn/guides/checkout.md#create-the-session).

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/checkout/sessions" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"terminalId":"YOUR_TERMINAL_ID","amount":"1000","description":"Order 1","orderId":"order-'"$(uuidgen)"'","returnUrl":"https://example.com/return","cancelUrl":"https://example.com/cancel"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const checkoutSession = await tappay.checkoutSessions.create({
  body: {
    terminalId: 'YOUR_TERMINAL_ID',
    amount: '1000',
    description: 'Order 1',
    orderId: `order-${crypto.randomUUID()}`,
    returnUrl: 'https://example.com/return',
    cancelUrl: 'https://example.com/cancel',
  },
});
console.log(JSON.stringify(checkoutSession));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$checkoutSession = $tappay->checkoutSessions->create([
    'body' => [
        'terminalId' => 'YOUR_TERMINAL_ID',
        'amount' => '1000',
        'description' => 'Order 1',
        'orderId' => 'order-' . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)) . '',
        'returnUrl' => 'https://example.com/return',
        'cancelUrl' => 'https://example.com/cancel',
    ],
]);
echo json_encode($checkoutSession);
```

```python title="Python"
import json
import uuid
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
checkout_session = client.checkout_sessions.create({
    "body": {
        "terminalId": "YOUR_TERMINAL_ID",
        "amount": "1000",
        "description": "Order 1",
        "orderId": "order-" + str(uuid.uuid4()) + "",
        "returnUrl": "https://example.com/return",
        "cancelUrl": "https://example.com/cancel",
    },
})
print(json.dumps(checkout_session))
```

### Pay it in the sandbox {#pay-it-in-the-sandbox}

Let the [simulator](https://docs.tappay.mn/glossary.md#simulator) pay for the [customer](https://docs.tappay.mn/glossary.md#customer). The response has the `id` of the payment, and you use it in the refund.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/sandbox/intents/INTENT_ID/simulate" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"channel":"QR","outcome":"SUCCEEDED"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const result = await tappay.sandbox.simulateIntent({ id: 'INTENT_ID', body: { channel: 'QR', outcome: 'SUCCEEDED' } });
console.log(JSON.stringify(result));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$result = $tappay->sandbox->simulateIntent(['id' => 'INTENT_ID', 'body' => ['channel' => 'QR', 'outcome' => 'SUCCEEDED']]);
echo json_encode($result);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
result = client.sandbox.simulate_intent({"id": "INTENT_ID", "body": {"channel": "QR", "outcome": "SUCCEEDED"}})
print(json.dumps(result))
```

### Refund the payment {#refund-the-payment}

Send the `amount` as a string and an optional `reason`. This example refunds the full paid amount of 1000. A partial amount works only when the payment's rail supports partial refunds; otherwise TapPay returns [RFD-032](https://docs.tappay.mn/errors.md#RFD-032). To refund chosen [order lines](https://docs.tappay.mn/glossary.md#order-line), send `lines` with the `invoiceLineId` and `quantity` of each line. Send an `Idempotency-Key`, so that a retry does not refund twice. The response is `202`, because a refund finishes later, and its `status` starts as `PENDING`.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/payments/PAYMENT_ID/refunds" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"amount":"1000","reason":"Customer returned an item"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const refund = await tappay.refunds.create({ paymentId: 'PAYMENT_ID', body: { amount: '1000', reason: 'Customer returned an item' } });
console.log(JSON.stringify(refund));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$refund = $tappay->refunds->create([
    'paymentId' => 'PAYMENT_ID',
    'body' => [
        'amount' => '1000',
        'reason' => 'Customer returned an item',
    ],
]);
echo json_encode($refund);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
refund = client.refunds.create({"paymentId": "PAYMENT_ID", "body": {"amount": "1000", "reason": "Customer returned an item"}})
print(json.dumps(refund))
```

### Read the refund {#read-the-refund}

Read the refund until its `status` is `SUCCEEDED` or `FAILED`. A `FAILED` refund has a `failureCode`.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/refunds/REFUND_ID" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27"
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const refund = await tappay.refunds.retrieve({ id: 'REFUND_ID' });
console.log(JSON.stringify(refund));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$refund = $tappay->refunds->retrieve(['id' => 'REFUND_ID']);
echo json_encode($refund);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
refund = client.refunds.retrieve({"id": "REFUND_ID"})
print(json.dumps(refund))
```

### List the refunds of a payment {#list-the-refunds-of-a-payment}

List every refund of the payment. Add up their amounts to see how much of the payment is still left to refund.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/payments/PAYMENT_ID/refunds" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27"
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const page = await tappay.refunds.list({ paymentId: 'PAYMENT_ID' }).firstPage();
console.log(JSON.stringify(page));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$page = $tappay->refunds->list(['paymentId' => 'PAYMENT_ID'])->firstPage();
echo json_encode(['data' => $page->data, 'hasMore' => $page->hasMore, 'nextCursor' => $page->nextCursor]);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
page = client.refunds.list({"paymentId": "PAYMENT_ID"}).first_page()
print(json.dumps({"data": page.data, "has_more": page.has_more, "next_cursor": page.next_cursor}))
```

## Verify {#verify}

TapPay sends `refund.*` [events](https://docs.tappay.mn/glossary.md#event) when the refund changes, so you can wait for them instead of polling. The `status` goes from `PENDING` to `PROCESSING`, and then to `SUCCEEDED` or `FAILED`. Your own refunds have `reasonCode` `MERCHANT_REQUEST`. TapPay uses `PREAUTH_ADJUSTMENT` for the difference of a [pre-auth](https://docs.tappay.mn/glossary.md#pre-auth) payment, and `OVERPAID` for an [overpaid](https://docs.tappay.mn/glossary.md#overpaid) payment, which only TapPay refunds.

`requestedVia` tells you where the refund was requested. It is `API` for your keys, `PORTAL` for the merchant portal, and `BACKOFFICE` for TapPay staff. It is `POS` for a bank POS [terminal](https://docs.tappay.mn/glossary.md#terminal), and `SYSTEM` for refunds that TapPay makes automatically.

If a same-day cancellation (`kind` is `VOID`) is rejected, that refund becomes `FAILED` with `failureCode` `NOT_VOIDABLE`. TapPay then automatically creates a regular refund for the same amount with a new `id`. The new refund has `replacesRefundId` set to the rejected refund's `id`. Link the two and do not refund again.

The refunds of one payment add up to at most the paid amount. The money goes back through the same [channel](https://docs.tappay.mn/glossary.md#channel) that took it, and the fee is not returned. If your balance is too small, it goes negative and TapPay settles it from your next income. If the payment was [split](https://docs.tappay.mn/glossary.md#split), each [beneficiary](https://docs.tappay.mn/glossary.md#beneficiary) returns its share in the same ratio as the split.

TapPay also corrects the tax [receipt](https://docs.tappay.mn/glossary.md#receipt). A full refund returns the receipt. A partial refund returns it and issues a new receipt for the remaining amount, and `lines` chooses which lines return. Read more in the [receipts guide](https://docs.tappay.mn/guides/receipts.md).

## Common mistakes {#common-mistakes}

- Refunding more than is left. TapPay returns [RFD-030](https://docs.tappay.mn/errors.md#RFD-030) when the amount is above what can still be refunded.
- Treating the `202` response as a finished refund. The money moves later, so read the `status` or wait for the `refund.*` event.
- Expecting the fee back. TapPay keeps the fee of the original payment.
- Refunding after the refund window of the payment. TapPay returns [RFD-034](https://docs.tappay.mn/errors.md#RFD-034).
- Refunding a payment that is not eligible, or one that has no refund route. TapPay returns [RFD-022](https://docs.tappay.mn/errors.md#RFD-022) or [RFD-031](https://docs.tappay.mn/errors.md#RFD-031).
- Sending a partial amount for a payment that supports only a full refund. TapPay returns [RFD-032](https://docs.tappay.mn/errors.md#RFD-032).
- Reusing an `Idempotency-Key` with different parameters. TapPay returns [RFD-036](https://docs.tappay.mn/errors.md#RFD-036), so use a new key for each new refund.

## Next steps {#next-steps}

Return the tax receipt with the [receipts guide](https://docs.tappay.mn/guides/receipts.md), and follow refund events with the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md). See [Create a refund](https://docs.tappay.mn/reference/refunds/create.md), [Retrieve a refund](https://docs.tappay.mn/reference/refunds/get.md) and [List refunds of a payment](https://docs.tappay.mn/reference/refunds/list-by-payment.md).
