# Accept a payment with checkout

> Create a checkout session on your server, send the customer to pay, and confirm the result.

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

## Overview {#overview}

A [checkout session](https://docs.tappay.mn/glossary.md#checkout-session) is a page where your [customer](https://docs.tappay.mn/glossary.md#customer) pays. You create it from your server and TapPay returns a link. When the customer finishes, you confirm the result from your server.

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

Your key needs the `checkout: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`. Use a [sandbox](https://docs.tappay.mn/glossary.md#sandbox) key while you build, because sandbox [payments](https://docs.tappay.mn/glossary.md#payment) move no money. Keep the secret key on your server and call TapPay only from there.

## Steps {#steps}

### Create the session {#create-the-session}

Send the amount, a description and your own order id. With a merchant-level key the `terminalId` is required; with a [terminal](https://docs.tappay.mn/glossary.md#terminal)-level key it is not. The `Idempotency-Key` makes the call safe to repeat: if your server retries with the same key, TapPay returns the same session. The response has the `url` for the customer, the `intentId` and, for an embedded session, a `clientSecret`.

```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))
```

By default the customer is sent to the hosted page at `url`.

The `mode` decides what the session does. The default `PAYMENT` takes one payment. `SETUP` saves the card of a customer for later charges, and `SUBSCRIPTION` takes the first payment, saves the card and starts a [subscription](https://docs.tappay.mn/glossary.md#subscription). Both need a `customer`, and `SUBSCRIPTION` also needs a `subscription` with a `billingPlanId`. Saving cards and charging them later must be enabled for your merchant first. The [customers and recurring guide](https://docs.tappay.mn/guides/customers-recurring.md) shows the full flow.

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

Open the `url` to see the page, or let the [simulator](https://docs.tappay.mn/glossary.md#simulator) pay the intent for the customer. This works only with a sandbox key.

```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))
```

### Confirm the session {#confirm-the-session}

After the customer returns to your `returnUrl`, read the session from your server and check that its `status` is `COMPLETE`. Never mark an order as paid only because the customer reached your return page.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/checkout/sessions/SESSION_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 checkoutSession = await tappay.checkoutSessions.retrieve({ id: 'SESSION_ID' });
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->retrieve(['id' => 'SESSION_ID']);
echo json_encode($checkoutSession);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
checkout_session = client.checkout_sessions.retrieve({"id": "SESSION_ID"})
print(json.dumps(checkout_session))
```

## Verify {#verify}

The session `status` changes from `OPEN` to `COMPLETE` when the payment succeeds. For a faster and more reliable signal, create a [webhook endpoint](https://docs.tappay.mn/glossary.md#webhook-endpoint) and handle the `payment.succeeded` [event](https://docs.tappay.mn/glossary.md#event), as described in the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md#create-the-endpoint).

### Return addresses {#return-urls}

The page sends the customer to `returnUrl` with `?session_id=…` after a successful payment, and to `cancelUrl` when they go back without paying. An [invoice](https://docs.tappay.mn/glossary.md#invoice) has the same fields: `returnUrl` and `cancelUrl` on `POST /v1/invoices` apply to that invoice's hosted page; with no `returnUrl` the merchant's `default_return_url` is used.

- **Your app.** Give an app scheme (`myshop://paid`) or a universal link — the app opens when the page goes there. If you open checkout inside an in-app WebView, an https address opens in the system browser, so use an app scheme. The [mobile app guide](https://docs.tappay.mn/guides/mobile-app.md) shows the whole flow.
- **Return from a wallet.** When the customer pays in a wallet app, the wallet opens `https://pay.tappay.mn/q/{ref}?from=wallet`. The page reads the status from the server. Paid → `returnUrl`. Not paid, intent still open → a «Payment failed or cancelled» notice, the other [channels](https://docs.tappay.mn/glossary.md#channel) and a «Back» button (`cancelUrl`). Expired or cancelled → `cancelUrl`. If the address is empty, the page shows its result screen.
- **URL rules.** `https://` or an app scheme (`[a-z][a-z0-9+.-]*://`), at most 2048 characters, no spaces. `javascript:`, `data:`, `file:`, `vbscript:`, `blob:` and `about:` are rejected; `http://` is rejected in [production](https://docs.tappay.mn/glossary.md#production) (sandbox only) → `400 VALIDATION_ERROR`. The page checks again before it redirects.

Reaching either address is not proof of payment — [confirm the session](#confirm-the-session) or wait for the webhook.

## Common mistakes {#common-mistakes}

- Trusting the redirect to `returnUrl`. A customer can open that address without paying, so always confirm the session or wait for the webhook.
- Giving `returnUrl` or `cancelUrl` an `http://` address (production) or a `javascript:`-style address. TapPay answers `400` — use `https://` or an app scheme.
- Putting the secret key in a browser or a mobile app. Only the [publishable key](https://docs.tappay.mn/glossary.md#publishable-key) may be used there.
- Sending a POST request without an `Idempotency-Key`, or with a new key on every retry. A retry then creates a second session.

## Next steps {#next-steps}

Listen for payments with the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md). To share a [checkout link](https://docs.tappay.mn/glossary.md#checkout-link) without code, see [no-code checkout links](https://docs.tappay.mn/no-code/checkout-links.md). To save cards and start subscriptions, follow the [customers and recurring guide](https://docs.tappay.mn/guides/customers-recurring.md). To take payments in an iOS or Android app, see the [mobile app guide](https://docs.tappay.mn/guides/mobile-app.md). See every field in [Create a checkout session](https://docs.tappay.mn/reference/checkout-sessions/create.md) and [Retrieve a checkout session](https://docs.tappay.mn/reference/checkout-sessions/get.md).

To show the checkout inside your own page, set `uiMode` to `EMBEDDED`, send the `clientSecret` to your page, and mount the checkout with your [publishable key](https://docs.tappay.mn/glossary.md#publishable-key):

```html
<div id="checkout"></div>
<script src="https://pay.tappay.mn/v1/checkout.js"></script>
<script>
  TapPay.checkout.mount('#checkout', {
    clientSecret: 'CLIENT_SECRET_FROM_YOUR_SERVER',
    publishableKey: 'pk_test_YOUR_PUBLISHABLE_KEY',
  });
</script>
```

The publishable key is made for the browser. It works only for checkout, so it cannot create or read payments.
