# Create orders with line items

> Create an invoice with order lines, let customers pay some of the lines, and track what is paid.

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

## Overview {#overview}

An order is an [invoice](https://docs.tappay.mn/glossary.md#invoice) with [order lines](https://docs.tappay.mn/glossary.md#order-line), such as items in a basket. You decide whether a [customer](https://docs.tappay.mn/glossary.md#customer) pays the whole order, any amount or chosen lines. TapPay tracks what is paid and what is still open.

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

Your key needs the `orders: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. A key with `payments:write` or `checkout:write` can also create invoices.

## Steps {#steps}

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

Send `lines`, each with a `name`, `quantity`, `unitPrice` and `totalAmount`, and make the line totals add up to the `amount`. Set `partialMode` to `NONE`, `AMOUNT` or `LINES` to choose how customers may pay. The `ebarimt` object sets who issues the tax [receipt](https://docs.tappay.mn/glossary.md#receipt) for this order: `NODE`, `MERCHANT` or `NONE`. With `LINES` or `AMOUNT` no intent exists yet, so `intentId` is `null`.

```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":10000,"description":"Order 1","orderId":"order-'"$(uuidgen)"'","partialMode":"LINES","lines":[{"name":"Coffee","quantity":"2","unitPrice":"3000","totalAmount":"6000"},{"name":"Cake","quantity":"1","unitPrice":"4000","totalAmount":"4000"}],"ebarimt":{"issuer":"NONE"}}'
```

```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: 10000,
    description: 'Order 1',
    orderId: `order-${crypto.randomUUID()}`,
    partialMode: 'LINES',
    lines: [
      {
        name: 'Coffee',
        quantity: '2',
        unitPrice: '3000',
        totalAmount: '6000',
      },
      {
        name: 'Cake',
        quantity: '1',
        unitPrice: '4000',
        totalAmount: '4000',
      },
    ],
    ebarimt: {
      issuer: 'NONE',
    },
  },
});
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' => 10000,
        'description' => 'Order 1',
        'orderId' => 'order-' . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)) . '',
        'partialMode' => 'LINES',
        'lines' => [
            [
                'name' => 'Coffee',
                'quantity' => '2',
                'unitPrice' => '3000',
                'totalAmount' => '6000',
            ],
            [
                'name' => 'Cake',
                'quantity' => '1',
                'unitPrice' => '4000',
                'totalAmount' => '4000',
            ],
        ],
        'ebarimt' => [
            'issuer' => 'NONE',
        ],
    ],
]);
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": 10000,
        "description": "Order 1",
        "orderId": "order-" + str(uuid.uuid4()) + "",
        "partialMode": "LINES",
        "lines": [
            {
                "name": "Coffee",
                "quantity": "2",
                "unitPrice": "3000",
                "totalAmount": "6000",
            },
            {
                "name": "Cake",
                "quantity": "1",
                "unitPrice": "4000",
                "totalAmount": "4000",
            },
        ],
        "ebarimt": {
            "issuer": "NONE",
        },
    },
})
print(json.dumps(invoice))
```

### Create a payment intent for lines {#create-a-payment-intent}

A [payment intent](https://docs.tappay.mn/glossary.md#payment-intent) is one share of the invoice that a customer pays on a [channel](https://docs.tappay.mn/glossary.md#channel), such as a QR code. In `LINES` mode, send the `invoiceLineId` and the `quantity` to pay. The `invoiceLineId` is the line number, starting at 1, and TapPay works out the amount. In `AMOUNT` mode, send an `amount` instead.

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/invoices/INVOICE_ID/intents" \
  -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 '{"lines":[{"invoiceLineId":1,"quantity":"2"}]}'
```

```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.createIntent({ id: 'INVOICE_ID', body: { lines: [{ invoiceLineId: 1, quantity: '2' }] } });
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->createIntent(['id' => 'INVOICE_ID', 'body' => ['lines' => [['invoiceLineId' => 1, 'quantity' => '2']]]]);
echo json_encode($invoice);
```

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

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
invoice = client.invoices.create_intent({"id": "INVOICE_ID", "body": {"lines": [{"invoiceLineId": 1, "quantity": "2"}]}})
print(json.dumps(invoice))
```

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

Let the [simulator](https://docs.tappay.mn/glossary.md#simulator) pay the intent for the customer. It works only with a sandbox key. Use the `intentId` of the intent that you just created, not the invoice 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":"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 invoice to see what is paid. The `status` is `PARTIALLY_PAID`, `amountPaid` shows the paid amount, and each line has `quantityPaid` and `quantityReserved`. The invoice becomes `PAID` when every line 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))
```

To cancel an invoice that nobody has paid, call [Cancel an invoice](https://docs.tappay.mn/reference/invoices/cancel.md). It also cancels the open intents. A paid, expired or cancelled invoice cannot be cancelled.

## Verify {#verify}

TapPay sends the `invoice.partially_paid` [event](https://docs.tappay.mn/glossary.md#event) when part of the invoice is paid. It sends `invoice.paid` when the last part is paid and `invoice.expired` when the invoice runs out of time. Handle them as described in the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md#create-the-endpoint), then read the invoice with the request above.

## Common mistakes {#common-mistakes}

- Using `partialMode` `LINES` on an invoice without `lines`. TapPay rejects it with `422`, because there are no lines to choose from.
- Sending line totals that do not add up to the `amount`. TapPay rejects the invoice with `422`.
- Mixing `AMOUNT` and `LINES` intents on one invoice. The two modes cannot be combined, and TapPay returns `422`.
- Reusing an `orderId` on the same [terminal](https://docs.tappay.mn/glossary.md#terminal). The pair of `terminalId` and `orderId` is unique, so the second invoice is rejected as a duplicate.
- Expecting open intents to stay reserved forever. An unpaid intent that expires gives its amount or lines back to the invoice.
- Expecting a new intent after the invoice expires. Paid parts stay paid and are not refunded, and you decide what to do with the rest.

## Next steps {#next-steps}

Show the intent to the customer with the [QR payments guide](https://docs.tappay.mn/guides/qr-payments.md) or the [channels guide](https://docs.tappay.mn/guides/channels.md). Listen for payments with the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md). See every field in [Create an invoice](https://docs.tappay.mn/reference/invoices/create.md), [Create a payment intent](https://docs.tappay.mn/reference/intents/create.md) and [Retrieve an invoice](https://docs.tappay.mn/reference/invoices/get.md).
