# API thiết lập webhook

### Trước khi bắt đầu

Một số lưu ý trước khi bắt đầu với các API liên quan tới webhook:

* Một tài khoản [Casso](https://my.casso.vn) đã liên kết một tài khoản ngân hàng. Để test với API này **có thể** sử dụng [tài khoản demo.](broken://pages/-McjWDNXLnazsGil8Lw0)
* Bạn cần có một endpoint/API để nhận sự kiện từ Casso đây được gọi là [Webhook](https://en.wikipedia.org/wiki/Webhook).
* Endpoint/API này phải public ra ngoài Internet. Nếu như bạn đang ở **local** bạn có thể [xem hương dẫn này](/webhook/gia-lap-giao-dich-den.md#su-dung-ngrok-de-public-webhook-o-local) để biết cách public endpoint của bạn.
* Bạn cần có [API Key](/casso-api/chung-thuc/tao-api-key-thu-cong.md) để thiết lập ở trường Authorization HTTP Header.

## Tạo webhook

<mark style="color:green;">`POST`</mark> `https://oauth.casso.vn/v2/webhooks`

Thực hiện tạo webhook tới server của bạn

#### Headers

| Name          | Type   | Description                                                               |
| ------------- | ------ | ------------------------------------------------------------------------- |
| Authorization | string | `Bearer <"access token từ OAuth2">` **hoặc** `Apikey <"API key của bạn">` |

#### Request Body

| Name          | Type    | Description                                                                      |
| ------------- | ------- | -------------------------------------------------------------------------------- |
| income\_only | boolean | Giá trị được thiết lập để gửi bắn sự kiện tới webhook đối với giao dịch tiền vào |
| secure\_token | string  | Mã bảo mật để mỗi lần gửi Event từ Casso sẽ được đính kèm trên header.           |
| webhook       | string  | Đường dẫn(endpoint/API) nhận event(phát sinh giao dịch mới) từ Casso             |

{% tabs %}
{% tab title="200 Response thông tin webhook đã tạo thành công." %}

```
{
    "error": 0,
    "message": "success",
    "data": {
        "id": 114,
        "channel": "webhook",
        "param1": "https://ten-mien-cua-ban.com/wc/handler-bank-transfer.php",
        "param2": "",
        "send_only_income": 1
    }
}
```

{% endtab %}

{% tab title="401 Access-Token không đúng hoặc đã hết hạn." %}

```
{
    "error": 401,
    "message": "Unauthorized Access",
    "data": null
}
```

{% endtab %}
{% endtabs %}

#### Ví dụ:&#x20;

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

```javascript
curl --location --request POST 'https://oauth.casso.vn/v2/webhooks' \
--header 'Authorization: Apikey <API Key của bạn> hoặc Bearer <access token từ OAuth2>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "webhook": "https://ten-mien-cua-ban.com/wc/handler-bank-transfer.php",
    "secure_token": "@123#abc",
    "income_only": true
}'
```

{% endtab %}

{% tab title="PHP" %}

```php
$curl = curl_init();

$data = array(
  'webhook' => 'https://ten-mien-cua-ban.com/wc/handler-bank-transfer.php',
  'secure_token' => '@123#abc',
  'income_only' => true
);
$postdata = json_encode($data);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://oauth.casso.vn/v2/webhooks",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $postdata),
  CURLOPT_HTTPHEADER => array(
    "Authorization: Apikey <"API Key của bạn"> Hoặc Bearer <"access token từ OAuth2">",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
```

{% endtab %}

{% tab title="JAVA" %}

```java
OkHttpClient client = new OkHttpClient();

RequestBody formBody = new FormBody.Builder()
  .add("webhook", "https://ten-mien-cua-ban.com/wc/handler-bank-transfer.php")
  .add("secure_token", "@123#abc")
  .add("income_only", true)
  .build();

Request request = new Request.Builder()
  .url("https://oauth.casso.vn/v2/webhooks")
  .post(formBody)
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">")
  .build();

Response response = client.newCall(request).execute();
```

{% endtab %}
{% endtabs %}

## &#x20;Xem chi tiết webhook

<mark style="color:blue;">`GET`</mark> `https://oauth.casso.vn/v2/webhooks/:id`

Xem chi tiết các thông về webhook của bạn theo dựa theo `webhook Id`

#### Path Parameters

| Name | Type   | Description                        |
| ---- | ------ | ---------------------------------- |
| id   | string | `id webhook` bạn muốn xem chi tiết |

#### Headers

| Name          | Type   | Description                                                               |
| ------------- | ------ | ------------------------------------------------------------------------- |
| Authorization | string | `Bearer <"access token từ Oauth2">` **hoặc** `Apikey <"API key của bạn">` |

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

```
{
    "error": 0,
    "message": "success",
    "data": {
        "id": 111,
        "channel": "webhook",
        "param1": "https://ten-mien-cua-ban.com.vn/wc/handler-bank-transfer.php",
        "param2": "",
        "send_only_income": 1
    }
}
```

{% endtab %}

{% tab title="401 " %}

```
{
    "error": 401,
    "message": "Unauthorized Access",
    "data": null
}
```

{% endtab %}
{% endtabs %}

#### Ví dụ:&#x20;

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

```javascript
curl --location --request GET 'https://oauth.casso.vn/v2/webhooks/134' \
--header 'Authorization: Bearer <"Access token nhận được từ OAuth 2.0 của Casso">'
```

{% endtab %}

{% tab title="PHP" %}

```php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://oauth.casso.vn/v2/webhooks/134",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
```

{% endtab %}

{% tab title="JAVA" %}

```java
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url("https://oauth.casso.vn/v2/webhooks/134")
  .get()
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">")
  .build();

Response response = client.newCall(request).execute();
```

{% endtab %}
{% endtabs %}

## &#x20;Cập nhật một webhook

<mark style="color:orange;">`PUT`</mark> `https://oauth.casso.vn/v2/webhooks/:id`

Cập nhật các thông tin trong webhook đã được thiết lập trước đó

#### Path Parameters

| Name | Type   | Description  |
| ---- | ------ | ------------ |
| id   | string | `id webhook` |

#### Headers

| Name          | Type   | Description                                                               |
| ------------- | ------ | ------------------------------------------------------------------------- |
| Authorization | string | `Bearer <"access token từ OAuth2">` **hoặc** `Apikey <"API key của bạn">` |

#### Request Body

| Name          | Type    | Description                                                          |
| ------------- | ------- | -------------------------------------------------------------------- |
| income\_only  | boolean | Xác nhận gửi webhook đối với tiền vào                                |
| secure\_token | string  | mã bảo mật                                                           |
| webhook       | string  | Đường dẫn(endpoint/API) nhận event(phát sinh giao dịch mới) từ Casso |

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

```
{
    "error": 0,
    "message": "success",
    "data": {
        "id": 111,
        "channel": "webhook",
        "param1": "https://webhook-cua-ban.com.vn",
        "param2": "sdf",
        "send_only_income": 1
    }
}
```

{% endtab %}

{% tab title="401 " %}

```
{
    "error": 401,
    "message": "Unauthorized Access",
    "data": null
}
```

{% endtab %}
{% endtabs %}

#### Ví dụ:&#x20;

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

```javascript
curl --location --request PUT 'https://oauth.casso.vn/v2/webhooks/111' \
--header 'Authorization: Apikey <API Key của bạn> Hoặc Bearer <access token từ OAuth2>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "webhook": "https://ten-mien-cua-ban.com/api/bank",
    "secure_token": "@xyz@123",
    "income_only": "false"
}'
```

{% endtab %}

{% tab title="PHP" %}

```php
$curl = curl_init();

$data = array(
  'webhook' => 'https://ten-mien-cua-ban.com/api/bank'
);
$postdata = json_encode($data);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://oauth.casso.vn/v2/webhooks/111",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => $postdata),
  CURLOPT_HTTPHEADER => array(
    "Authorization: Apikey <"API Key của bạn"> Hoặc Bearer <"access token từ OAuth2">",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
```

{% endtab %}

{% tab title="JAVA" %}

```java
OkHttpClient client = new OkHttpClient();

RequestBody formBody = new FormBody.Builder()
  .add("webhook", "https://ten-mien-cua-ban-new.com/wc-new/handler-bank-transfer.php")
  .add("secure_token", "@123#abc-new")
  .build();

Request request = new Request.Builder()
  .url("https://oauth.casso.vn/v2/webhooks/111")
  .post(formBody)
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">")
  .build();

Response response = client.newCall(request).execute();
```

{% endtab %}
{% endtabs %}

## &#x20;Xoá một webhook

<mark style="color:red;">`DELETE`</mark> `https://oauth.casso.vn/v2/webhooks/:id`

Thực hiện xóa một webhook bằng `id webhook` &#x20;

#### Path Parameters

| Name | Type   | Description  |
| ---- | ------ | ------------ |
| id   | string | `id webhook` |

#### Headers

| Name          | Type   | Description                                                               |
| ------------- | ------ | ------------------------------------------------------------------------- |
| Authorization | string | `Bearer <"access token từ OAuth2">` **hoặc** `Apikey <"API key của bạn">` |

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

```
{
    "error": 0,
    "message": "success",
    "data": {
        "id": 111,
        "channel": "webhook",
        "param1": "https://khanh-dep-trai.com.vn",
        "param2": "sdf",
        "send_only_income": 1
    }
}
```

{% endtab %}

{% tab title="401 " %}

```
{
    "error": 401,
    "message": "Unauthorized Access",
    "data": null
}
```

{% endtab %}
{% endtabs %}

#### Ví dụ:&#x20;

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

```javascript
curl --location --request DELETE 'https://oauth.casso.vn/v2/webhooks/85' \
--header 'Authorization: Bearer <"Access token nhận được từ OAuth 2.0 của Casso">'
```

{% endtab %}

{% tab title="PHP" %}

```php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://oauth.casso.vn/v2/webhooks/85",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_CUSTOMREQUEST => "DELETE",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
```

{% endtab %}

{% tab title="JAVA" %}

```java
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url("https://oauth.casso.vn/v2/webhooks/85")
  .delete()
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">")
  .build();

Response response = client.newCall(request).execute();
```

{% endtab %}
{% endtabs %}

## &#x20;Xoá tất cả webhook trong đường dẫn &#x20;

<mark style="color:red;">`DELETE`</mark> `https://oauth.casso.vn/v2/webhooks`

Xóa tất cả các webhook đang tồn tại trong doanh nghiệp của bạn trên Casso tương ứng với giá trị webhook mà bạn yêu cầu.

#### Query Parameters

| Name    | Type   | Description                                                          |
| ------- | ------ | -------------------------------------------------------------------- |
| webhook | string | Đường dẫn(endpoint/API) nhận event(phát sinh giao dịch mới) từ Casso |

#### Headers

| Name          | Type   | Description                                                               |
| ------------- | ------ | ------------------------------------------------------------------------- |
| Authorization | string | `Bearer <"access token từ OAuth2">` **hoặc** `Apikey <"API key của bạn">` |

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

```
{
    "error": 0,
    "message": "success",
    "data": [
        {
            "id": 108,
            "channel": "webhook",
            "param1": "https://ten-mien-cua-ban.com/wc/handler.php",
            "param2": "",
            "send_only_income": 1
        },
        {
            "id": 109,
            "channel": "webhook",
            "param1": "https://ten-mien-cua-ban.com/wc/handler.php",
            "param2": "",
            "send_only_income": 1
        },
        {
            "id": 110,
            "channel": "webhook",
            "param1": "https://ten-mien-cua-ban.com/wc/handler.php",
            "param2": "",
            "send_only_income": 1
        },
    ]
}
```

{% endtab %}

{% tab title="401 " %}

```
{
    "error": 401,
    "message": "Unauthorized Access",
    "data": null
}
```

{% endtab %}

{% tab title="404 " %}

```
{
    "error": 12,
    "message": "Webhook not exists",
    "data": null
}
```

{% endtab %}
{% endtabs %}

#### Ví dụ:&#x20;

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

```javascript
curl --location --request DELETE 'https://oauth.casso.vn/v2/webhooks?webhook=https://websitecuaban.com/api/webhook' \
--header 'Authorization: Bearer <"Access token nhận được từ OAuth 2.0 của Casso">'
```

{% endtab %}

{% tab title="PHP" %}

```php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://oauth.casso.vn/v2/webhooks?webhook=https://websitecuaban.com/api/webhook",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_CUSTOMREQUEST => "DELETE",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">",
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
```

{% endtab %}

{% tab title="JAVA" %}

```java
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url("https://oauth.casso.vn/v2/webhooks?webhook=https://websitecuaban.com/api/webhook")
  .delete()
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Apikey <"API Key của bạn"> hoặc Bearer <"access token từ OAuth2">")
  .build();

Response response = client.newCall(request).execute();
```

{% endtab %}
{% endtabs %}

{% hint style="danger" %}
Lưu ý: Khi bạn gọi API có thể sẽ xóa đi những webhook cũ mà bạn đã thiết lập trên hệ thống của Casso. Cân nhắc trước khi dùng tới API này.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.casso.vn/casso-api/api/thiet-lap-webhook.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
