> For the complete documentation index, see [llms.txt](https://apidoc.tokenpay.me/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://apidoc.tokenpay.me/zh-hk/ding-dan/ding-dan-chuang-jian.md).

# 訂單創建

{% hint style="info" %}
TIP

提示 (TIP)

訂單創建後，介面將返回支付連結，用戶可透過該連結進入收銀台進行支付。 如果商戶已設置白標應用程式，前端代碼還需要連接至 JS-SDK。查看 [SDK](https://www.npmjs.com/package/@ttpay/payment_modal)
{% endhint %}

#### 介面地址 (Interface address)

```
POST https://api.tokenpay.me/v1/transaction/prepayment
```

#### 介面參數 (Interface parameters)

| 參數名稱           | 位置     | 類型      | 必填 | 描述                                                                            |
| -------------- | ------ | ------- | -- | ----------------------------------------------------------------------------- |
| app\_id        | header | string  | 是  | 應用程式 ID，示例值：`8e4b8c2e7cxxxxxxxx1a1cbd3d59e0bd`                                |
| mch\_id        | body   | string  | 是  | 商戶 ID，示例值：`12345678`                                                          |
| description    | body   | string  | 否  | 訂單描述，示例值：`recharge`                                                           |
| out\_trade\_no | body   | string  | 是  | 商戶訂單號，商戶自行生成的唯一訂單號                                                            |
| expire\_second | body   | integer | 是  | 過期時間（秒），示例值：`3600`                                                            |
| amount         | body   | number  | 是  | 訂單金額，示例值：`15000000`                                                           |
| chain          | body   | string  | 是  | 所屬公鏈，示例值：`TRON`                                                               |
| currency       | body   | string  | 是  | 幣種，示例值：`TRX`。[查看支援的幣種](/zh-hk/bi-zhong/bi-zhong-xin-xi.md)                    |
| to\_address    | body   | string  | 否  | 收款錢包（註：`監控訂單`應用需要提供收款錢包）                                                      |
| attach         | body   | string  | 否  | 自定義參數（如用戶 ID 等唯一標識），在查詢 API 和支付通知中原樣返回。實際上，僅在支付完成狀態下會返回此欄位。                   |
| locale         | body   | string  | 否  | 語言：`zh_cn` 中文、`en` 英文。使用收銀台時將根據此設置顯示預設語言。                                     |
| notify\_url    | body   | string  | 否  | 回調 URL，示例值：`https://xxx/xxx`。[建議使用 https](/zh-hk/shuo-ming/an-quan-zi-xun.md) |
| return\_url    | body   | string  | 否  | 返回地址，當支付完成或失敗時，點擊地址上的返回按鈕跳轉。可以是 https 連結或 APP 的路由。                            |
| order\_type    | body   | string  | 是  | 支付方式，示例值：`platform_order` 平台代收                                                |

#### 參數示例 (Parameters example)

```json
{
  "app_id": "d549a3ac0e4641f998c6675fc539ba21",
  "mch_id": "12345678",
  "description": "recharge",
  "out_trade_no": "fb72xxxx-xxxx-xxxx-xxxx-xxxx8a7b52cb",
  "expire_second": 600,
  "amount": 9,
  "chain": "TRON",
  "currency": "USDT",
  "to_address": "TQjxEW2Z3p9wjoxxxxxxxxgJUrWXBun91w",
  "attach": "anim dolore",
  "locale": "zh_cn",
  "notify_url": "https://xxx/xxx",
  "return_url": "https://xxxx/xxx?id=xxxx",
  "order_type": "platform_order"
}
```

{% hint style="info" %}
提示 (TIP)

`app_id` 可透過商戶後台 [創建應用程式](https://ttpay.io/console/pages/app-list/pay-app) 獲取。
{% endhint %}

#### 介面返回 (Interface return)

| 參數名稱          | 類型      | 描述                                       | 說明                                                                                          |
| ------------- | ------- | ---------------------------------------- | ------------------------------------------------------------------------------------------- |
| `code`        | integer | [狀態碼](/zh-hk/cuo-wu-ma/zhuang-tai-ma.md) |                                                                                             |
| `msg`         | string  | 狀態描述                                     |                                                                                             |
| `request_id`  | string  | 請求 ID                                    |                                                                                             |
| `data`        | object  | 數據物件                                     |                                                                                             |
| `prepay_id`   | string  | 預支付 ID                                   | 預支付交易會話 ID。用於後續介面調用，其值在創建訂單時設置的 `expire_second` 參數時間內有效，您也可以直接訪問 `payment_url` 完成支付。        |
| `payment_url` | string  | 支付收銀台 URL                                | `payment_url` 用於拉起支付收銀台的中間頁，訪問此 URL 即可完成支付。`payment_url` 在創建訂單時設置的 `expire_second` 參數時間內有效。 |

#### 返回示例 (Return example)

```json
{
    "code": 0,
    "msg": "ok",
    "request_id": "9b9e08ab-48e5-4efa-83e7-97e5e3fe3d0c",
    "data": {
        "prepay_id": "15809074c5bbc36bce27exxxxxxxxxxxxxxxxxxxxa4ca0a281d7e1260624a1c2",
        "payment_url": "/pay/order?prepay_id=15809074c5bbc36bce27exxxxxxxxxxxxxxxxxxxxa4ca0a281d7e1260624a1c2"
    }
}
```

#### 示例代碼 (Example code)

{% tabs %}
{% tab title="Shell" %}

```sh
curl --location --request POST 'https://api.tokenpay.me/v1/transaction/prepayment' \
--header 'Authorization: <Authorization>' \
--header 'User-Agent: tokenpay API (https://tokenpay.me)' \
--header 'Content-Type: application/json' \
--data-raw '<body data here>'
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
   "fmt"
   "strings"
   "net/http"
   "io/ioutil"
)

func main() {

   url := "https://api.tokenpay.me/v1/transaction/prepayment"
   method := "POST"

   payload := strings.NewReader(`<body data here>`)

   client := &http.Client {
   }
   req, err := http.NewRequest(method, url, payload)

   if err != nil {
      fmt.Println(err)
      return
   }
   req.Header.Add("Authorization", "<Authorization>")
   req.Header.Add("User-Agent", "tokenpay API (https://tokenpay.me)")
   req.Header.Add("Content-Type", "application/json")

   res, err := client.Do(req)
   if err != nil {
      fmt.Println(err)
      return
   }
   defer res.Body.Close()

   body, err := ioutil.ReadAll(res.Body)
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println(string(body))
}
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
   CURLOPT_URL => 'https://api.tokenpay.me/v1/transaction/prepayment',
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => '',
   CURLOPT_MAXREDIRS => 10,
   CURLOPT_TIMEOUT => 0,
   CURLOPT_FOLLOWLOCATION => true,
   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
   CURLOPT_CUSTOMREQUEST => 'POST',
   CURLOPT_POSTFIELDS =>'<body data here>',
   CURLOPT_HTTPHEADER => array(
      'Authorization: <Authorization>',
      'User-Agent: tokenpay API (https://tokenpay.me)',
      'Content-Type: application/json'
   ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
```

<br>
{% endtab %}

{% tab title="Python" %}

```python
import http.client
import json

conn = http.client.HTTPSConnection("https://api.tokenpay.me")
payload = "<body data here>"
headers = {
   'Authorization': '<Authorization>',
   'User-Agent': 'tokenpay API (https://tokenpay.me)',
   'Content-Type': 'application/json'
}
conn.request("POST", "/v1/transaction/prepayment", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
```

{% endtab %}

{% tab title="Java" %}

```java
OkHttpClient client = new OkHttpClient().newBuilder()
   .build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "<body data here>");
Request request = new Request.Builder()
   .url("https://api.tokenpay.me/v1/transaction/prepayment")
   .method("POST", body)
   .addHeader("Authorization", "<Authorization>")
   .addHeader("User-Agent", "tokenpay API (https://tokenpay.me)")
   .addHeader("Content-Type", "application/json")
   .build();
Response response = client.newCall(request).execute();
```

{% endtab %}
{% endtabs %}
