# Hold and finalize a payment with pre-auth

> Reserve an amount on the customer card, then finalize the final amount later, for example for a hotel or a rental.

Source: https://docs.tappay.mn/guides/pre-auth/

## Overview {#overview}

[Pre-auth](https://docs.tappay.mn/glossary.md#pre-auth) lets you reserve an amount when the final price is not known yet, such as a hotel stay. You create the [invoice](https://docs.tappay.mn/glossary.md#invoice) with `authorizationType` `PREAUTH`. Later you [finalize](https://docs.tappay.mn/glossary.md#finalize) the [payment](https://docs.tappay.mn/glossary.md#payment) with the final amount.

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

Your key needs the `payments:write` [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`. Pre-auth must be switched on for your account: the `preauth_enabled` setting is granted by your partner or by TapPay, and without it TapPay returns `422` `PRE-001`. Use a [sandbox](https://docs.tappay.mn/glossary.md#sandbox) key while you build.

## Steps {#steps}

### Create a pre-auth invoice {#create-a-pre-auth-invoice}

Send `authorizationType` `PREAUTH` when you create the invoice. The optional `preauthFinalizeDays` sets how many days you have to finalize. If you leave it out, the default of 7 days applies, and your partner sets the highest value.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/invoices" \
  -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":"Room deposit","orderId":"order-'"$(uuidgen)"'","authorizationType":"PREAUTH","preauthFinalizeDays":7}'
```

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

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const invoice = await tappay.invoices.create({
  body: {
    terminalId: 'YOUR_TERMINAL_ID',
    amount: 1000,
    description: 'Room deposit',
    orderId: `order-${crypto.randomUUID()}`,
    authorizationType: 'PREAUTH',
    preauthFinalizeDays: 7,
  },
});
console.log(JSON.stringify(invoice));
```

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

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$invoice = $tappay->invoices->create([
    'body' => [
        'terminalId' => 'YOUR_TERMINAL_ID',
        'amount' => 1000,
        'description' => 'Room deposit',
        'orderId' => 'order-' . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)) . '',
        'authorizationType' => 'PREAUTH',
        'preauthFinalizeDays' => 7,
    ],
]);
echo json_encode($invoice);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
invoice = client.invoices.create({
    "body": {
        "terminalId": "YOUR_TERMINAL_ID",
        "amount": 1000,
        "description": "Room deposit",
        "orderId": "order-" + str(uuid.uuid4()) + "",
        "authorizationType": "PREAUTH",
        "preauthFinalizeDays": 7,
    },
})
print(json.dumps(invoice))
```

### Authorize in the sandbox {#authorize-in-the-sandbox}

Let the [simulator](https://docs.tappay.mn/glossary.md#simulator) pay with a [test card](https://docs.tappay.mn/glossary.md#test-card). It works only with a sandbox key. The card here always succeeds, and any other number gives `422` [EVT-202](https://docs.tappay.mn/errors.md#EVT-202). The response has the payment `id`.

```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":"CARD","card":"4242424242424242"}'
```

```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: 'CARD', card: '4242424242424242' } });
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' => 'CARD', 'card' => '4242424242424242']]);
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": "CARD", "card": "4242424242424242"}})
print(json.dumps(result))
```

### Read the open payment {#read-the-open-payment}

Read the payment. Its `authorizationType` is `PREAUTH`, its `preauthStatus` is `OPEN`, and `finalizeDueAt` is the last time you can finalize. The `preauthMode` field shows how TapPay holds the money.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/payments/PAYMENT_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 payment = await tappay.payments.retrieve({ id: 'PAYMENT_ID' });
console.log(JSON.stringify(payment));
```

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

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$payment = $tappay->payments->retrieve(['id' => 'PAYMENT_ID']);
echo json_encode($payment);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
payment = client.payments.retrieve({"id": "PAYMENT_ID"})
print(json.dumps(payment))
```

TapPay holds the money in one of two modes.

| `preauthMode` | What happens |
| - | - |
| `CHARGE_REFUND` | TapPay charges the full amount, and the payment is `SUCCEEDED`. You see the full amount in your balance. |
| `HOLD` | TapPay only reserves the amount on the card, and the payment is `AUTHORIZED`. Nothing is added to your balance until you finalize. |

### Finalize with the final amount {#finalize-with-the-final-amount}

Send the final `amount` as a string, which must not be higher than the held amount. You can finalize only once. In `CHARGE_REFUND` mode TapPay [refunds](https://docs.tappay.mn/glossary.md#refund) the difference to the [customer](https://docs.tappay.mn/glossary.md#customer) from your balance, with the reason `PREAUTH_ADJUSTMENT`. In `HOLD` mode TapPay captures the final amount and releases the rest.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/payments/PAYMENT_ID/finalize" \
  -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":"600"}'
```

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

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const payment = await tappay.payments.finalize({ id: 'PAYMENT_ID', body: { amount: '600' } });
console.log(JSON.stringify(payment));
```

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

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$payment = $tappay->payments->finalize(['id' => 'PAYMENT_ID', 'body' => ['amount' => '600']]);
echo json_encode($payment);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
payment = client.payments.finalize({"id": "PAYMENT_ID", "body": {"amount": "600"}})
print(json.dumps(payment))
```

### Read the final payment {#read-the-final-payment}

Read the payment again. The `preauthStatus` is now `FINALIZED`, and `finalAmount` shows the amount that you finalized. In `CHARGE_REFUND` mode, `preauthRefundId` points to the refund of the difference.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/payments/PAYMENT_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 payment = await tappay.payments.retrieve({ id: 'PAYMENT_ID' });
console.log(JSON.stringify(payment));
```

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

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$payment = $tappay->payments->retrieve(['id' => 'PAYMENT_ID']);
echo json_encode($payment);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
payment = client.payments.retrieve({"id": "PAYMENT_ID"})
print(json.dumps(payment))
```

In the sandbox you can also call [Simulate automatic finalization](https://docs.tappay.mn/reference/sandbox/auto-finalize-payment.md) on a new pre-auth payment. It finalizes the full amount, as if the due time had passed. To try `HOLD` mode, send `preauthMode` `HOLD` to the simulator on a `PREAUTH` intent. The `holdCapture` field then sets the result of the capture.

## Verify {#verify}

When you finalize, TapPay sends the `payment.finalized` [event](https://docs.tappay.mn/glossary.md#event) to your [webhook endpoint](https://docs.tappay.mn/glossary.md#webhook-endpoint). In `HOLD` mode it sends `payment.authorized` first. Handle them as described in the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md#create-the-endpoint), then read the payment with the request in the last step.

## Common mistakes {#common-mistakes}

- Expecting a smaller final amount to lower the fee. The fee stays on the full amount, and the refund of the difference does not return any fee.
- Finalizing for more than the held amount. TapPay returns [PRE-008](https://docs.tappay.mn/errors.md#PRE-008), because the final amount must not be higher.
- Finalizing twice. The second call returns `409` [PRE-006](https://docs.tappay.mn/errors.md#PRE-006), because a pre-auth can be finalized only once.
- Never finalizing. After `preauthFinalizeDays` TapPay finalizes the full amount itself and sets `preauthStatus` to `AUTO_FINALIZED`, and a later call returns [PRE-007](https://docs.tappay.mn/errors.md#PRE-007).
- Having too little balance for the refund of the difference. TapPay takes the missing amount from your next incoming payments.
- Combining `PREAUTH` with `partialMode` `LINES`. TapPay rejects the invoice with `422` `PAY-021`.
- Expecting the tax [receipt](https://docs.tappay.mn/glossary.md#receipt) before you finalize. The eBarimt receipt is issued after finalize, for the final amount.
- Cancelling the intent of an authorized payment. TapPay returns `409` [PRE-015](https://docs.tappay.mn/errors.md#PRE-015), so finalize the payment instead.

## Next steps {#next-steps}

Listen for `payment.finalized` with the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md). Learn how payments move between states in [Statuses](https://docs.tappay.mn/concepts/statuses.md#preauth). See every field in [Finalize a held payment](https://docs.tappay.mn/reference/pre-auth/finalize.md), [Create an invoice](https://docs.tappay.mn/reference/invoices/create.md) and [Retrieve a payment](https://docs.tappay.mn/reference/payments/get.md).
