# Create orders from your own system

> Create an order from your website or app in the merchant's name, send the customer to the payment page and follow the order until it is confirmed.

Source: https://docs.tappay.mn/guides/external-orders/

## Overview {#overview}

Your website or app can create an order in the merchant's name. TapPay creates the [payment](https://docs.tappay.mn/glossary.md#payment), returns an `order_page_url` that shows the payment and the progress to the [customer](https://docs.tappay.mn/glossary.md#customer), and keeps the status of the order. You create the order with one request and then follow it with `GET` or the `order.updated` [event](https://docs.tappay.mn/glossary.md#event).

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

Your key needs the `commerce:write` [scope](https://docs.tappay.mn/glossary.md#scope) and must cover all [terminals](https://docs.tappay.mn/glossary.md#terminal). Choose “All terminals” when you connect your system, or create the key in Developers › API keys & webhooks. Use your [secret key](https://docs.tappay.mn/glossary.md#secret-key), shown below as `sk_test_YOUR_SECRET_KEY`, and a [sandbox](https://docs.tappay.mn/glossary.md#sandbox) key while you build. The example also pays the order in the sandbox, so the key needs `payments:write` too. You need the `location_id` of a branch that has an order terminal. Set it in the shell as `LOCATION_ID`.

## Steps {#steps}

### Create the order {#create-the-order}

Send `location_id`, a unique `external_ref` from your own system, the `lines` (`name`, `quantity`, `unit_price`), the `fulfillment` mode (`PICKUP`, `AT_MERCHANT`, `DIGITAL` or `DELIVERY`) and the `customer`. `DELIVERY` also needs `fulfillment.address`. The response has the `order_id`, the `order_page_url` to open for the customer and the `intent_id` of the payment. Send the same `external_ref` only once.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/commerce/orders" \
  -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 '{"location_id":"'"$LOCATION_ID"'","external_ref":"web-'"$(uuidgen)"'","lines":[{"name":"Coffee","quantity":2,"unit_price":4000},{"name":"Cake","quantity":1,"unit_price":6000}],"fulfillment":{"mode":"PICKUP"},"customer":{"name":"Bat","phone":"+97699000000"}}'
```

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

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const order = await tappay.commerce.create({
  body: {
    location_id: process.env.LOCATION_ID,
    external_ref: `web-${crypto.randomUUID()}`,
    lines: [
      { name: 'Coffee', quantity: 2, unit_price: 4000 },
      { name: 'Cake', quantity: 1, unit_price: 6000 },
    ],
    fulfillment: { mode: 'PICKUP' },
    customer: { name: 'Bat', phone: '+97699000000' },
  },
});
console.log(JSON.stringify(order));
```

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

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$order = $tappay->commerce->create([
    'body' => [
        'location_id' => getenv('LOCATION_ID'),
        'external_ref' => 'web-' . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)),
        'lines' => [
            ['name' => 'Coffee', 'quantity' => 2, 'unit_price' => 4000],
            ['name' => 'Cake', 'quantity' => 1, 'unit_price' => 6000],
        ],
        'fulfillment' => ['mode' => 'PICKUP'],
        'customer' => ['name' => 'Bat', 'phone' => '+97699000000'],
    ],
]);
echo json_encode($order);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
order = client.commerce.create({
    "body": {
        "location_id": os.environ["LOCATION_ID"],
        "external_ref": "web-" + str(uuid.uuid4()),
        "lines": [
            {"name": "Coffee", "quantity": 2, "unit_price": 4000},
            {"name": "Cake", "quantity": 1, "unit_price": 6000},
        ],
        "fulfillment": {"mode": "PICKUP"},
        "customer": {"name": "Bat", "phone": "+97699000000"},
    },
})
print(json.dumps(order))
```

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

The [simulator](https://docs.tappay.mn/glossary.md#simulator) pays the payment instead of the customer. It works only with a sandbox key. Use the `intent_id` from the previous response.

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

### Read the order {#read-the-order}

Read the order with `GET`. After the payment it has the status `CONFIRMED`. Before the payment it is `AWAITING_PAYMENT`.

```bash title="curl"
curl -sS "https://api.tappay.mn/api/v1/commerce/orders/ORDER_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 order = await tappay.commerce.retrieve({ order_id: 'ORDER_ID' });
console.log(JSON.stringify(order));
```

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

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$order = $tappay->commerce->retrieve(['order_id' => 'ORDER_ID']);
echo json_encode($order);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
order = client.commerce.retrieve({"order_id": "ORDER_ID"})
print(json.dumps(order))
```

## Verify {#verify}

TapPay sends the `order.updated` event to your [webhook endpoint](https://docs.tappay.mn/glossary.md#webhook-endpoint) each time the order status changes. Handle it as described in the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md#create-the-endpoint) and read the order to get the new status.

## Common mistakes {#common-mistakes}

- Sending the same `external_ref` twice. TapPay answers `409 ORD-019`, so create the order once and read it afterwards.
- Sending no lines or a total of zero. TapPay answers `422 ORD-020`. A delivery that the dispatcher handles is also not supported here and gives the same error.
- Sending `DELIVERY` without `fulfillment.address`. TapPay answers `400 VAL-001`.
- Using the id of a branch that does not exist (`CAT-008`) or a branch that is not accepting orders (`ORD-007`). Use the id of a branch that takes orders.
- Using a key that is limited to one terminal. The key must cover all terminals.
- Sending tracking for a delivery that you handle yourself without `address.location`. TapPay answers `DLV-004`, so send the coordinates when you create the order.

## Next steps {#next-steps}

Show `order_page_url` to the customer. Send delivery progress as described in the [fulfillment](https://docs.tappay.mn/glossary.md#fulfillment) [guide](https://docs.tappay.mn/guides/fulfillment.md) and read how orders work in the [orders guide](https://docs.tappay.mn/guides/orders.md). Look up each field in the [API reference](https://docs.tappay.mn/reference.md).
