# 创建订单

{% hint style="info" %}
说明

创建订单后接口会返回收款链接，用户通过链接进入收银台进行支付。

若商户设置了白标应用，前端代码还需接入JS-SDK，查看[SDK](https://www.npmjs.com/package/@ttpay/payment_modal)
{% endhint %}

### 接口地址 <a href="#jie-kou-di-zhi" id="jie-kou-di-zhi"></a>

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

### 接口参数 <a href="#jie-kou-can-shu" id="jie-kou-can-shu"></a>

| 名称             | 位置     | 类型      | 必选 | 说明                                                                                                   |
| -------------- | ------ | ------- | -- | ---------------------------------------------------------------------------------------------------- |
| app\_id        | header | string  | 是  | 应用 ID，示例值：`8e4b8c2e7cxxxxxxxx1a1cbd3d59e0bd`                                                         |
| mch\_id        | body   | string  | 是  | 商户 ID，示例值：`12345678`                                                                                 |
| description    | body   | string  | 否  | 订单描述，示例值：`充值`                                                                                        |
| out\_trade\_no | body   | string  | 是  | 商户订单号，商家自行生成唯一订单号                                                                                    |
| expire\_second | body   | integer | 是  | 有效期(秒)，示例值：`3600`                                                                                    |
| amount         | body   | number  | 是  | 订单金额，示例值：`15000000`                                                                                  |
| chain          | body   | string  | 是  | 所属公链，示例值：`TRON`                                                                                      |
| currency       | body   | string  | 是  | 币种，示例值：`TRX` [查看支持币种](https://apidoc.tokenpay.me/chinese/huo-bi/cha-xun-bi-zhong-xin-xi)             |
| to\_address    | body   | string  | 否  | 收款钱包（注：收款钱包为`监听订单`的应用必填）                                                                             |
| attach         | body   | string  | 否  | 自定义参数(如用户ID等唯一标识)，在查询 API 和支付通知中原样返回，实际情况下只有支付完成状态才会返回该字段                                            |
| locale         | body   | string  | 否  | 语言：`zh_cn`中文、 `en`英文，使用收银台将根据此设置显示默认语言。                                                              |
| notify\_url    | body   | string  | 否  | 回调地址，示例值：`https://xxx/xxx` [建议使用 https](https://apidoc.tokenpay.me/chinese/shuo-ming/an-quan-xu-zhi) |
| return\_url    | body   | string  | 否  | 返回地址，当支付完成或失败，点击返回按钮的地址，可以是 https 或 app 的路由。                                                         |
| order\_type    | body   | string  | 是  | 支付方式，示例值：`platform_order` 平台代收                                                                       |

#### 参数示例 <a href="#can-shu-shi-li" id="can-shu-shi-li"></a>

```json
{
  "app_id": "d549a3ac0e4641f998c6675fc539ba21",
  "mch_id": "12345678",
  "description": "充值",
  "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" %}
说明

app\_id 可通过商户后台创建应用获取。
{% endhint %}

### 接口返回 <a href="#jie-kou-fan-hui" id="jie-kou-fan-hui"></a>

| 名称           | 类型      | 说明                                                                        | 说明                                                                                        |
| ------------ | ------- | ------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| code         | integer | [业务状态码](https://apidoc.tokenpay.me/chinese/cuo-wu-ma/ye-wu-zhuang-tai-ma) |                                                                                           |
| msg          | string  | 状态描述                                                                      |                                                                                           |
| request\_id  | string  | 请求 ID                                                                     |                                                                                           |
| data         | object  | 数据对象                                                                      |                                                                                           |
| prepay\_id   | string  | 预支付 ID                                                                    | 预支付交易会话标识。用于后续接口调用中使用，该值有效期为创建订单时设置的 `expire_second` 参数，也可直接访问`payment_url`完成支付           |
| payment\_url | string  | 支付收银台 URL                                                                 | `payment_url`为拉起支付收银台的中间页面，可通过访问该 url 完成支付，`payment_url`的有效期为创建订单时设置的 `expire_second` 参数。 |

### 返回示例 <a href="#fan-hui-shi-li" id="fan-hui-shi-li"></a>

{% code overflow="wrap" %}

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

{% endcode %}

### 代码示例 <a href="#dai-ma-shi-li" id="dai-ma-shi-li"></a>

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

{% code overflow="wrap" %}

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

{% endcode %}
{% endtab %}

{% tab title="Go" %}

{% code overflow="wrap" %}

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

{% endcode %}
{% endtab %}

{% tab title="PHP" %}

{% code overflow="wrap" %}

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

{% endcode %}

<br>
{% endtab %}

{% tab title="Python" %}

{% code overflow="wrap" %}

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

{% endcode %}
{% endtab %}

{% tab title="Java" %}

{% code overflow="wrap" %}

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

{% endcode %}
{% endtab %}
{% endtabs %}
