> Create and manage payment requests

Source: https://docs.apipay.vn/en/api/payment-requests

# Payment Requests API

Each payment request has a payment link (`payUrl`) and a bank-transfer QR code. The basic flow:

1. Your server creates a payment request.
2. Send the customer to `payUrl`, or show the `qrUrl` image on your own page.
3. Receive the result through the `transaction.in` [webhook](/en/api/webhooks), or turn on `payment_request.completed` to be told which request was paid.

Every request needs the header `Authorization: Bearer `, where `` is the base64 of `accessKey:secretKey`. See [API Authentication](/en/api/authentication). Successful responses have the shape `{"data": ..., "statusCode": ..., "timestamp": ...}`.

## Create a payment request

```bash
POST /v1/client/payment-requests
```

| Parameter        | Type   | Required | Description                                                                                                  |
| ---------------- | ------ | -------- | ------------------------------------------------------------------------------------------------------------ |
| `bankPublicId`   | string | Yes      | Receiving account. Get it from the [Banks API](/en/api/banks)                                                |
| `amount`         | string | No       | Amount in VND, max `300000000`. Omit to let the customer enter it. Do not send with `priceId`                |
| `priceId`        | string | No       | A [Commerce](/en/api/commerce) price. The amount is taken from the price at creation time. Do not send with `amount` |
| `content`        | string | No       | Transfer description, max 140 characters. ApiPay strips diacritics and special characters; match on the `content` in the response |
| `title`          | string | No       | Title shown on the payment page                                                                              |
| `expiresAt`      | string | No       | Expiry time (ISO 8601). Default 90 days                                                                      |
| `redirectUrl`    | string | No       | Where the customer returns after paying. See [Verify the redirect](#verify-the-redirect)                     |
| `customDomainId` | string | No       | Show the payment page on your [custom domain](/en/custom-domains)                                            |
| `customer`       | object | No       | Buyer details when creating from `priceId`: `name`, `email`, `taxCode`, `address`                            |

```bash
curl -X POST https://app.apipay.vn/v1/client/payment-requests \
  -H "Authorization: Bearer " \
  -H "Content-Type: application/json" \
  -d '{
    "bankPublicId": "bnk_abc123",
    "amount": "100000",
    "content": "ORDER-12345",
    "title": "Order 12345",
    "redirectUrl": "https://your-domain.com/payment/result"
  }'
```

```json
{
  "data": {
    "publicId": "APIPAYJSCAF9H23M74K",
    "payUrl": "https://pay.apipay.vn/APIPAYJSCAF9H23M74K",
    "qrUrl": "https://app.apipay.vn/v1/payment-requests/APIPAYJSCAF9H23M74K/qr-image",
    "bankCode": "MB",
    "accountNumber": "0123456789",
    "accountName": "NGUYEN VAN A",
    "amount": "100000",
    "content": "ORDER-12345",
    "expiresAt": "2026-11-10T10:00:00.000Z",
    "createdAt": "2026-08-12T10:00:00.000Z",
    "redirectSecret": "a1b2c3d4e5f6..."
  },
  "statusCode": 201,
  "timestamp": "2026-08-12T10:00:00.000Z"
}
```

Store `redirectSecret` on your server to [verify the redirect](#verify-the-redirect).

## Get a payment request

```bash
GET /v1/client/payment-requests/:publicId
```

```json
{
  "data": {
    "publicId": "APIPAYJSCAF9H23M74K",
    "status": "ACTIVE",
    "payUrl": "https://pay.apipay.vn/APIPAYJSCAF9H23M74K",
    "qrUrl": "https://app.apipay.vn/v1/payment-requests/APIPAYJSCAF9H23M74K/qr-image",
    "bankCode": "MB",
    "accountNumber": "0123456789",
    "accountName": "NGUYEN VAN A",
    "amount": "100000",
    "content": "ORDER-12345",
    "title": "Order 12345",
    "redirectUrl": "https://your-domain.com/payment/result",
    "expiresAt": "2026-11-10T10:00:00.000Z",
    "createdAt": "2026-08-12T10:00:00.000Z"
  },
  "statusCode": 200,
  "timestamp": "2026-08-12T10:05:00.000Z"
}
```

### Status

| Status      | Description        |
| ----------- | ------------------ |
| `ACTIVE`    | Waiting for payment |
| `COMPLETED` | Paid               |
| `EXPIRED`   | Expired            |
| `CANCELLED` | Cancelled          |

## List payment requests

```bash
GET /v1/client/payment-requests
```

| Parameter  | Type     | Required | Description                                                    |
| ---------- | -------- | -------- | -------------------------------------------------------------- |
| `page`     | number   | No       | Default `1`                                                    |
| `limit`    | number   | No       | Default `20`, max `100`                                        |
| `search`   | string   | No       | Search by ID, description, title or account number             |
| `status`   | string[] | No       | Filter by [status](#status). Repeat to filter several values   |
| `dateFrom` | string   | No       | Created from date, `YYYY-MM-DD` (Vietnam time)                 |
| `dateTo`   | string   | No       | Created through date, `YYYY-MM-DD` (Vietnam time)              |

```bash
curl -G https://app.apipay.vn/v1/client/payment-requests \
  -H "Authorization: Bearer " \
  --data-urlencode "status=COMPLETED" \
  --data-urlencode "dateFrom=2026-08-01"
```

The list is at `data.data`, pagination at `data.pagination` (`page`, `limit`, `total`, `totalPages`).

## Cancel a payment request

Only `ACTIVE` requests can be cancelled. After cancelling, the status is `CANCELLED`.

```bash
DELETE /v1/client/payment-requests/:publicId
```

## QR image

Returns the bank-transfer QR code as a PNG. No API key needed, so you can use it directly in an `<img>` tag. This is the URL in the `qrUrl` field.

```bash
GET /v1/payment-requests/:publicId/qr-image
```

## List Bank Apps

Use this when you build your own mobile payment page and want buttons that open a banking app with the transfer details prefilled. No API key needed. The ApiPay payment page already shows these buttons.

```bash
GET /v1/payment-requests/:publicId/bank-apps
```

```json
{
  "data": [
    {"bankBin": "970436", "bankCode": "VCB", "name": "Vietcombank"},
    {"bankBin": "970422", "bankCode": "MB", "name": "MB Bank"}
  ],
  "statusCode": 200
}
```

The most used banks come first. The list is empty when the payment request is no longer `ACTIVE`.

### Create a bank app link

```bash
POST /v1/payment-requests/:publicId/bank-apps/link
```

| Parameter  | Type   | Required | Description                                                                 |
| ---------- | ------ | -------- | --------------------------------------------------------------------------- |
| `bankBin`  | string | Yes      | `bankBin` from the bank app list                                            |
| `platform` | string | No       | Customer's phone: `android` or `ios`. Most banks need it. Send it when you call from your server; from the phone it is read automatically. |

```json
{
  "data": {"url": "<bank app link>"},
  "statusCode": 201
}
```

Open `url` on the customer's phone, for example `window.location.assign(url)`. Opening the app does not mean the customer paid; use the webhook to confirm.

## Simulate a sandbox payment

Marks a sandbox payment request (`test_pr_*`) as paid, so you can test the webhook. Sandbox redirects are not signed. No API key, no body.

```bash
curl -X POST https://app.apipay.vn/v1/payment-requests/test_pr_abc123/pay
```

See [Sandbox & Live Test](/en/sandbox).

## Verify the redirect

If you send `redirectUrl`, a customer who has paid returns to:

```
https://your-domain.com/payment/result?data=<base64url>&signature=<hmac>
```

`data` is base64url-encoded JSON:

```json
{
  "status": "success",
  "transactionId": "550e8400-e29b-41d4-a716-446655440000",
  "amount": "100000",
  "content": "ORDER-12345",
  "timestamp": 1755000000000
}
```

`signature` is the HMAC-SHA256 (hex) of the decoded JSON string, signed with the `redirectSecret` of that payment request (each request has its own). `timestamp` is in milliseconds.

When the customer cancels or the link expires, the URL only carries `?status=cancel` or `?status=expired`, with no signature. Treat it as not paid.

The redirect is only for showing the result to the customer. Confirm the order with the webhook.

```javascript
const crypto = require("crypto");

// redirectSecret: the secret you stored with the order when creating the payment request
function verifyRedirect({data, signature}, redirectSecret) {
  const json = Buffer.from(String(data), "base64url").toString("utf8");
  const expected = Buffer.from(crypto.createHmac("sha256", redirectSecret).update(json).digest("hex"), "hex");
  const received = Buffer.from(String(signature || ""), "hex");

  if (received.length !== expected.length || !crypto.timingSafeEqual(received, expected)) {
    throw new Error("Invalid signature");
  }
  return JSON.parse(json);
}

app.get("/payment/result", async (req, res) => {
  if (!req.query.data) return res.send("Not paid");

  try {
    const json = Buffer.from(String(req.query.data), "base64url").toString("utf8");
    const order = await findOrderByContent(JSON.parse(json).content); // your own lookup
    verifyRedirect(req.query, order.redirectSecret);
    res.send("Payment successful");
  } catch {
    res.status(400).send("Invalid redirect");
  }
});
```

```python
import base64
import hashlib
import hmac
import json

def verify_redirect(data: str, signature: str, redirect_secret: str) -> dict:
    padded = data + "=" * (-len(data) % 4)
    payload_json = base64.urlsafe_b64decode(padded).decode("utf-8")
    expected = hmac.new(redirect_secret.encode(), payload_json.encode(), hashlib.sha256).hexdigest()

    if not hmac.compare_digest(expected, signature or ""):
        raise ValueError("Invalid signature")
    return json.loads(payload_json)
```

```php
<?php

function verifyRedirect(string $data, string $signature, string $redirectSecret): array
{
  $json = base64_decode(strtr($data, '-_', '+/'));
  $expected = hash_hmac('sha256', $json, $redirectSecret);

  if (!hash_equals($expected, $signature)) {
    throw new RuntimeException('Invalid signature');
  }
  return json_decode($json, true);
}
```
