Tổng đài hỗ trợ - 
1900 6750
( 7:00 - 22:00 )

Webhook

Một Webhook là một công cụ để truy vấn và lưu trữ dữ liệu của một Event xác định. Bạn có thể đăng ký đường dẫn http:// hoặc https:// - nơi mà dữ liệu của Event được lưu trữ dưới dạng XML hoặc JSON. Webhook có thể được đăng ký cho các Event sau:

App
uninstalled/charge
Collection
create/update/delete
Customer
create/delete/disable/enable/update
Fulfillment
create/update
Order
create/delete/updated/paid/cancelled/fulfilled/partially_fulfilled
Order_Transactions
create
Product
create/update/delete
Refund
create
Store
update

Bạn có thể làm gì với Webhook?

Sapo API cho phép bạn thực hiện các thao tác sau với tài nguyên Product. Các phiên bản chi tiết hơn của những thao tác này có thể có:

Các thuộc tính của Webhook

address
{ "address" : "http://apple.com/uninstall" }

Địa chỉ URI mà Webhook sẽ gửi một POST Request đến khi có Event xảy ra.

created_on
{ "created_on" : "2012-09-28T11:50:07-04:00" }

Thời gian Webhook được tạo. API trả về kết quả theo định dạng chuẩn ISO 8601.

format
{ "format" : "json" }

Định dạng kiểu dữ liệu mà Webhook trả về. Các giá trị hợp lệ là json và xml.

id
{ "id" : 901431826 }

Số duy nhất định danh Webhook.

topic
{ "topic" : "app/uninstalled" }

Event mà khi xảy ra sẽ thực hiện lời gọi Webhook. Các giá trị hợp lệ là: orders/create, orders/delete, orders/updated, orders/paid, orders/cancelled, orders/fulfilled, orders/partially_fulfilled, order_transactions/create, carts/create, carts/update, refunds/create, products/create, products/update, products/delete, collections/create, collections/update, collections/delete, customer_groups/delete, customers/create, customers/enable, customers/disable, customers/update, customers/delete, fulfillments/create, fulfillments/update, store/update, app/uninstalled

modified_on
{ "modified_on" : "2012-09-28T11:50:07-04:00" }

Thời gian Webhook được cập nhật. API trả về kết quả theo định dạng chuẩn ISO 8601.

Endpoints

GET/admin/webhooks.json
address

Sử dụng tham số này để lấy ra những Webhook sẽ thực hiện một POST Request tới địa chỉ URI này khi có Event xảy ra.

created_on_max

Lấy ra những Webhook được tạo trước ngày này (định dạng: 2008-12-31 03:00).

created_on_min

Lấy ra những Webhook được tạo sau ngày này (định dạng: 2008-12-31 03:00).

fields

Danh sách các thuộc tín trong kết quả trả về, cách nhau bởi dấu phẩy.

limit

Số lượng webhook trả về.

(mặc định: 50) (tối đa: 250)
page

Số trang.

(mặc định: 1)
since_id

Lấy ra các Webhook có Id lớn hơn hoặc bằng Id xác định.

topic

Lấy ra các Webhook với topic xác định. Các topic hợp lệ là: orders/create, orders/delete, orders/updated, orders/paid, orders/cancelled, orders/fulfilled, orders/partially_fulfilled, order_transactions/create, carts/create, carts/update, refunds/create, products/create, products/update, products/delete, collections/create, collections/update, collections/delete, customers/create, customers/enable, customers/disable, customers/update, customers/delete, fulfillments/create, fulfillments/update, store/update, app/uninstalled

modified_on_min

Lấy ra các Webhook được cập nhật trước ngày này (định dạng: 2008-12-31 03:00).

modified_on_max

Lấy ra các Webhook được cập nhật sau ngày này (định dạng: 2008-12-31 03:00).

Lấy danh sách tất cả các Webhook.

GET /admin/webhooks.json
Xem kết quả
HTTP/1.1 200 OK
{
  "webhooks": [
    {
      "id": 4759306,
      "address": "http://apple.com",
      "topic": "orders/create",
      "created_on": "2016-01-20T13:00:56Z",
      "modified_on": "2016-01-20T13:00:56Z",
      "format": "json"
      
    },
    {
      "id": 901431826,
      "address": "http://apple.com/uninstall",
      "topic": "app/uninstalled",
      "created_on": "2016-01-20T13:00:56Z",
      "modified_on": "2016-01-20T13:00:56Z",
      "format": "json"
      
    }
  ]
}

Lấy danh sách tất cả Webhook sau một ID xác định

GET /admin/webhooks.json?since_id=4759306
Xem kết quả
HTTP/1.1 200 OK
{
  "webhooks": [
    {
      "id": 901431826,
      "address": "http:/\/apple.com/uninstall",
      "topic": "app/uninstalled",
      "created_on": "2016-01-20T13:00:56Z",
      "modified_on": "2016-01-20T13:00:56Z",
      "format": "json"
      
    }
  ]
}
GET/admin/webhooks/count.json
address

Sử dụng tham số này để lấy ra những Webhook sẽ thực hiện một POST Request tới địa chỉ URI này khi có Event xảy ra.

topic

Hiển thị các Webhook với topic xác định. Các topic hợp lệ là: orders/create, orders/delete, orders/updated, orders/paid, orders/cancelled, orders/fulfilled, orders/partially_fulfilled, order_transactions/create, carts/create, carts/update, refunds/create, products/create, products/update, products/delete, collections/create, collections/update, collections/delete, customers/create, customers/enable, customers/disable, customers/update, customers/delete, fulfillments/create, fulfillments/update, store/update, app/uninstalled

Tổng số tất cả Webhook.

GET /admin/webhooks/count.json
Xem kết quả
HTTP/1.1 200 OK
{
  "count": 2
}

Tổng số tất cả Webhook có topic 'orders/create'

GET /admin/webhooks/count.json?topic=orders/create
Xem kết quả
HTTP/1.1 200 OK
{
  "count": 1
}
GET/admin/webhooks/4759306.json

Lấy một Webhook

fields

Danh sách các thuộc tính trong kết quả trả về, cách nhau bởi dấu phẩy.

Lấy một Webhook bằng Id.

GET /admin/webhooks/#{id}.json
Xem kết quả
HTTP/1.1 200 OK
{
  "webhook": {
    "id": 4759306,
    "address": "http:/\/apple.com",
    "topic": "orders/create",
    "created_on": "2016-01-20T13:00:56Z",
    "modified_on": "2016-01-20T13:00:56Z",
    "format": "json"
    
  }
}
POST/admin/webhooks.json

Mỗi webhook đều cần một topic và một địa chỉ URL. Cố gắng tạo một webhook không có topic hoặc địa chỉ URL sẽ dẫn đến lỗi 422 - Unprocessable Entity error.

format

Định dạng dữ liệu trả về. Các giá trị hợp lệ là json và xml.

(mặc định: json)

Sau khi cài đặt một Sapo App, bạn có thể tạo webhook dành cho app bằng cách thực hiện API sau.

POST /admin/webhooks.json
{
  "webhook": {
    "topic": "orders/create",
    "address": "http://whatever.hostname.com/",
    "format": "json"
  }
}
Xem kết quả
HTTP/1.1 201 Created
{
  "webhook": {
    "id": 987911590,
    "address": "http://whatever.hostname.com/",
    "topic": "orders/create",
    "created_on": "2016-01-20T13:01:10Z",
    "modified_on": "2016-01-20T13:01:10Z",
    "format": "json"
    
  }
}

Cố gắng tạo một webhook không có topic hoặc địa chỉ URL sẽ trả về kết quả lỗi

POST /admin/webhooks.json
{
  "webhook": {
    "body": "foobar"
  }
}
Xem kết quả
HTTP/1.1 422 Unprocessable Entity
{
  "errors": {
    "topic": [
      "can't be blank",
      "Invalid topic specified. Topics allowed: orders\/create, orders\/delete, orders\/updated, orders\/paid, orders\/cancelled, orders\/fulfilled, orders\/partially_fulfilled, order_transactions\/create, carts\/create, carts\/update, refunds\/create, products\/create, products\/update, products\/delete, collections\/create, collections\/update, collections\/delete, customers\/create, customers\/enable, customers\/disable, customers\/update, customers\/delete, fulfillments\/create, fulfillments\/update, store\/update, app\/uninstalled"
    ],
    "address": [
      "can't be blank"
    ]
  }
}
PUT/admin/webhooks/4759306.json

Cập nhật topic hoặc địa chỉ URI của webhook.

Thay đổi một webhook để nó thực hiện POST request tới một địa chỉ khác:

PUT /admin/webhooks/#{id}.json
{
  "webhook": {
    "id": 4759306,
    "address": "http://somewhere-else.com/"
  }
}
Xem kết quả
HTTP/1.1 200 OK
{
  "webhook": {
    "id": 4759306,
    "address": "http://somewhere-else.com/",
    "topic": "orders/create",
    "created_on": "2016-01-20T13:00:56Z",
    "modified_on": "2016-01-20T13:01:10Z",
    "format": "json"
    
  }
}
DELETE/admin/webhooks/4759306.json

Xóa một webhook

Xóa một webhook khỏi Shop

DELETE /admin/webhooks/#{id}.json
Xem kết quả
HTTP/1.1 200 OK
{}

Bài viết trên có hữu ích cho bạn không?

Hữu ích Không hữu ích
Số lượt đánh giá hữu ích: