# 激活

激活接口用于通过开发者参数、终端激活码（code）和设备信息来生成开放平台终端号（terminal_sn）和终端密钥（terminal_key），以用于后续支付业务。

对同一台终端，只需要激活一次。使用服务商序列号（vendor_sn）作为签名人序列号，服务商密钥（vendor_key）作为签名密钥。

## 接口说明

- 请求方式：`POST`
- 请求地址：`https://vsi-api.shouqianba.com/terminal/activate`
- Operation ID：`activate`

## 请求参数

无请求参数。

## 请求体

| 字段名 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| app_id | string | 是 | 应用ID |
| client_sn | string | 否 | 三方系统中的终端号必须保证在app id下唯一 |
| code | string | 是 | 激活码内容 |
| device_id | string | 是 | 设备指纹。内容自行定义，同一个app_id下唯一。建议格式：品牌名称+支付场景。例：ZARA001P01 |
| name | string | 否 | 终端名 |
| os_info | string | 否 | 当前系统信息，如：Android5.0 |
| sdk_version | string | 否 | SDK版本 |

## 请求示例

```json
{
  "app_id": "test_app",
  "code": "12345678",
  "device_id": "ZARA001P01"
}
```

## 响应体

| 字段名 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| biz_response | object | 否 | 业务响应数据。激活成功时包含终端号和终端密钥。 |
| biz_response.terminal_key | string | 是 | 终端密钥 |
| biz_response.terminal_sn | string | 是 | 终端号 |
| error_code | string | 否 | 通讯错误码。通讯失败时返回。见附录《公共错误码》 |
| error_message | string | 否 | 通讯错误信息描述。通讯失败时返回。 |
| result_code | string | 是 | 通讯响应码。200：通讯成功；400：客户端错误；500：服务端错误 |

## 响应示例

```json
{
  "biz_response": {
    "terminal_key": "68d499beda5f72116592f5c527465656",
    "terminal_sn": "10298371039"
  },
  "result_code": "200"
}
```

## 示例代码

### cURL

```bash
curl -X POST "https://vsi-api.shouqianba.com/terminal/activate" \
  -H "Authorization: 100000000000000000 0123456789abcdef0123456789abcdef" \
  -H "Content-Type: application/json" \
  -d '{"app_id":"test_app","code":"12345678","device_id":"ZARA001P01"}'
```

### Python

```python
import requests

url = "https://vsi-api.shouqianba.com/terminal/activate"
headers = {"Content-Type": "application/json",
    "Authorization": "100000000000000000 0123456789abcdef0123456789abcdef",
}
json_data = {
    "app_id": "test_app",
    "code": "12345678",
    "device_id": "ZARA001P01"
}

response = requests.post(
    url,
    headers=headers,
    json=json_data,
)

print(response.json())
```

### Java

```java
// 激活
OkHttpClient client = new OkHttpClient();
String json = "{\"app_id\":\"test_app\",\"code\":\"12345678\",\"device_id\":\"ZARA001P01\"}";
RequestBody body = RequestBody.create(json, MediaType.parse("application/json"));

Request request = new Request.Builder()
    .url("https://vsi-api.shouqianba.com/terminal/activate")
    .addHeader("Authorization", "100000000000000000 0123456789abcdef0123456789abcdef")
    .post(body)
    .build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
```

### Go

```go
package main

import (
    "fmt"
    "io"
    "net/http"
    "bytes"
)

func main() {
	jsonBody := []byte(`{"app_id":"test_app","code":"12345678","device_id":"ZARA001P01"}`)
	reqBody := bytes.NewBuffer(jsonBody)
	req, err := http.NewRequest("POST", "https://vsi-api.shouqianba.com/terminal/activate", reqBody)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "100000000000000000 0123456789abcdef0123456789abcdef")
	resp, err := (&http.Client{}).Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	respBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(respBytes))
}
```

### Node.js

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

const options = {
  method: "POST",
  hostname: "vsi-api.shouqianba.com",
  path: "/terminal/activate",
  headers: {
    "Authorization": "100000000000000000 0123456789abcdef0123456789abcdef",
    "Content-Type": "application/json"
  }
};

const postData = JSON.stringify({"app_id":"test_app","code":"12345678","device_id":"ZARA001P01"});

const req = https.request(options, (res) => {
  let data = "";
  res.on("data", (chunk) => { data += chunk; });
  res.on("end", () => { console.log(JSON.parse(data)); });
});
req.write(postData);
req.end();
```
