Skip to content

Accept a payment with checkout

View as Markdown

Overview

A checkout session is a page where your 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

Your key needs the checkout:write scope, and you use your secret key, shown below as sk_test_YOUR_SECRET_KEY. Use a sandbox key while you build, because sandbox payments move no money. Keep the secret key on your server and call TapPay only from there.

Steps

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-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.

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"}'
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
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);
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. 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 shows the full flow.

Pay in the sandbox

Open the url to see the page, or let the simulator pay the intent for the customer. This works only with a sandbox key.

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"}'
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
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);
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

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.

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"
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
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);
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

The session status changes from OPEN to COMPLETE when the payment succeeds. For a faster and more reliable signal, create a webhook endpoint and handle the payment.succeeded event, as described in the webhooks guide.

Return addresses

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 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 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 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 (sandbox only) → 400 VALIDATION_ERROR. The page checks again before it redirects.

Reaching either address is not proof of payment — confirm the session or wait for the webhook.

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

Listen for payments with the webhooks guide. To share a checkout link without code, see no-code checkout links. To save cards and start subscriptions, follow the customers and recurring guide. To take payments in an iOS or Android app, see the mobile app guide. See every field in Create a checkout session and Retrieve a checkout session.

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:

<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.