# Take payments in a mobile app

> Let your iOS or Android app take payments through your own server, with the TapPay page or wallet buttons.

Source: https://docs.tappay.mn/guides/mobile-app/

## Overview {#overview}

Your iOS or Android app takes [payments](https://docs.tappay.mn/glossary.md#payment) through your own server, and only the server calls TapPay. The app opens the TapPay payment page or a wallet app, and the [customer](https://docs.tappay.mn/glossary.md#customer) pays there. When the app opens again, it asks your server for the result.

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

Your key needs the `checkout:write` [scope](https://docs.tappay.mn/glossary.md#scope) for the payment page, and `payments:write` for wallet buttons. 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 secret key stays on your server. The app talks only to your server, and your server calls TapPay. There is no mobile SDK.

```text
App  ──►  your server  ──►  TapPay
App  ◄──  your server  ◄──  webhook or GET
```

Register a return address for your app: an app scheme such as `myshop://paid`, or a universal link. TapPay sends the customer there after the payment, and the app opens.

## Steps {#steps}

### Path 1: create a checkout session {#create-a-checkout-session}

The TapPay payment page shows every [channel](https://docs.tappay.mn/glossary.md#channel) of the payment in one place. Your server creates a [checkout session](https://docs.tappay.mn/glossary.md#checkout-session) and sets `returnUrl` and `cancelUrl` to your app scheme or universal link. Send the `url` from the response to the app. The app opens it in the system browser, or in an in-app browser: `SFSafariViewController` on iOS and Custom Tabs on Android. If you open it in your own WebView, use an app scheme, because an `https` return address would open in the system browser.

```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":"myshop://paid","cancelUrl":"myshop://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: 'myshop://paid',
    cancelUrl: 'myshop://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' => 'myshop://paid',
        'cancelUrl' => 'myshop://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": "myshop://paid",
        "cancelUrl": "myshop://cancel",
    },
})
print(json.dumps(checkout_session))
```

### Path 2: create an invoice with wallet links {#create-an-invoice-with-wallet-links}

Use this path to show wallet buttons on your own screen. Your server creates an [invoice](https://docs.tappay.mn/glossary.md#invoice) with `expand[]=walletLinks` and the same `returnUrl` and `cancelUrl`. Each item of `walletLinks` has an `appName`, a `logoUrl` that can be `null`, and a `link`. The app shows a button for each wallet and opens its `link` with the system: `open` on iOS, `ACTION_VIEW` on Android. After the payment the wallet goes back through the TapPay page, and the page sends the customer on to your `returnUrl` or `cancelUrl`.

```bash title="curl"
curl -sS -g -X POST "https://api.tappay.mn/api/v1/invoices?expand[]=walletLinks" \
  -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":"myshop://paid","cancelUrl":"myshop://cancel"}'
```

```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({
  query: { expand: ['walletLinks'] },
  body: {
    terminalId: 'YOUR_TERMINAL_ID',
    amount: 1000,
    description: 'Order 1',
    orderId: `order-${crypto.randomUUID()}`,
    returnUrl: 'myshop://paid',
    cancelUrl: 'myshop://cancel',
  },
});
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([
    'query' => ['expand' => ['walletLinks']],
    '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' => 'myshop://paid',
        'cancelUrl' => 'myshop://cancel',
    ],
]);
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({
    "expand": ["walletLinks"],
    "body": {
        "terminalId": "YOUR_TERMINAL_ID",
        "amount": 1000,
        "description": "Order 1",
        "orderId": "order-" + str(uuid.uuid4()) + "",
        "returnUrl": "myshop://paid",
        "cancelUrl": "myshop://cancel",
    },
})
print(json.dumps(invoice))
```

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

Let the [simulator](https://docs.tappay.mn/glossary.md#simulator) pay the invoice for the customer, as if they paid in a wallet. 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 on your server {#confirm-on-your-server}

When the app opens again, it asks your server for the result. Your server reads the invoice and checks that its `status` is `PAID`. For path 1, read the session and check that its `status` is `COMPLETE`, as in the [checkout guide](https://docs.tappay.mn/guides/checkout.md#confirm-the-session). Then the server tells the app whether the order is paid.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/invoices/INVOICE_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 invoice = await tappay.invoices.retrieve({ id: 'INVOICE_ID' });
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->retrieve(['id' => 'INVOICE_ID']);
echo json_encode($invoice);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
invoice = client.invoices.retrieve({"id": "INVOICE_ID"})
print(json.dumps(invoice))
```

## Verify {#verify}

For a faster signal, handle the `payment.succeeded` [event](https://docs.tappay.mn/glossary.md#event) on your [webhook endpoint](https://docs.tappay.mn/glossary.md#webhook-endpoint), as described in the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md#create-the-endpoint). Your server stores the result, and the app reads it from your server. Opening the app at `myshop://paid` is not proof of payment, because anyone can open that address.

### Code in the app {#code-in-the-app}

These short examples open TapPay from the app and handle the return. Replace `refreshOrderFromServer` with a call to your own server.

```swift
import SafariServices
import SwiftUI
import UIKit

// Path 1: open the checkout url in an in-app browser.
func openCheckout(_ url: URL, from viewController: UIViewController) {
    viewController.present(SFSafariViewController(url: url), animated: true)
}

// Path 2: open a wallet link with the system, never in a WebView.
func openWallet(_ link: URL) {
    UIApplication.shared.open(link)
}

// myshop://paid opens the app again. Do not trust it: ask your server.
struct ShopApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { _ in refreshOrderFromServer() } // also dismiss the SFSafariViewController you presented
        }
    }
}
```

```kotlin
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent

// Path 1: open the checkout url in a Custom Tab.
fun openCheckout(context: Context, url: String) {
    CustomTabsIntent.Builder().build().launchUrl(context, Uri.parse(url))
}

// Path 2: open a wallet link with the system, never in a WebView.
fun openWallet(context: Context, link: String) {
    context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link)))
}

// myshop://paid opens your activity again (an intent-filter in AndroidManifest.xml).
// Do not trust it: ask your server.
override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    refreshOrderFromServer()
}
```

## Common mistakes {#common-mistakes}

- Putting the secret key in the app. Anyone can take it out of the app package, so keep it on your server.
- Trusting the return address. Mark the order as paid only after your server reads the session or the invoice, or gets the webhook.
- Opening a wallet link in a WebView. Open it with the system, so that the wallet app opens and its return reaches your app.
- Opening the checkout `url` in your own WebView with an `https` return address. The return then opens in the system browser instead of your app, so use an app scheme.
- Giving an `http://` return address in [production](https://docs.tappay.mn/glossary.md#production). TapPay answers `400`, so use `https://` or an app scheme.

## Next steps {#next-steps}

Read the rules for return addresses in the [checkout guide](https://docs.tappay.mn/guides/checkout.md#return-urls), and listen for payments with the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md). See every field in [Create a checkout session](https://docs.tappay.mn/reference/checkout-sessions/create.md), [Create an invoice](https://docs.tappay.mn/reference/invoices/create.md) and [Retrieve an invoice](https://docs.tappay.mn/reference/invoices/get.md).
