# Hướng dẫn sử dụng Haravan webhook để trigger Mailjet automation flow
**Target:** Kích hoạt Mailjet automation flow để gửi ra email tới user khi có event order fullfilled từ hệ thống Haravan
Webhook event trên Haravan: **orders/fulfilled**
Mình cần thực hiện các bước sau:
1) Cần 1 server để chờ webhook event từ Haravan khi có order thực hiện thành công gọi tới
2) Cài đặt webhook trên Haravan admin của store
3) Setup workflow trên Mailjet
## 1. Thiết lập server xử lý webhook event từ Haravan
### a. Tạo app trên trên Heroku
Tạo tài khoản tại Heroku (Tốt hơn là bên mình có thể chọn các server prodiver khác nếu được)
Mình vào http://heroku.com/ và tạo 1 account

Sau đó mình cần 1 app như hình

Vào phần settings của app để biết URL của app:

### b. Tạo app trên [Haravan partner](https://developers.haravan.com/apps)
Mình vào https://developers.haravan.com/apps để đăng ký Heroku application. Sau khi tạo thành công, sẽ cần ghi nhận App_ID, App_Secret và thiết lập Redirect URL như hình

Mình làm theo hướng dẫn sau để đẩy source code về Heroku

Clone source code [tại đây](https://drive.google.com/file/d/1CBXXq2cVIHH8Bixet7IGOoMGtugUq8P2/view?usp=sharing):
Cấu hình Redirect Url như hình:
> - <URL của Server>/install/login
> - <URL của Server>/install/grandservice
Thay thế các thông tin sau trong file index.js tương ứng với app vừa tạo
- APP_ID
- APP_SECRET
- HEROKU_DOMAIN
**Chú ý:** Vui lòng không chia sẻ source ở đây, vì trong link đã gồm các các thông tin credentials của account Mailjet của phía Hconnect.
```javascript=
$ cd my-project/
$ git init
$ heroku git:remote -a <ten-app>
```
Sau khi làm xong bước này, mình sẽ có application trên Heroku
### b. Cấu hình scopes


### c. Cài đặt app trên Haravan Store
Sau khi đẩy app lên Heroku server, mình sẽ vào đường dẫn host app để cài đặt app.
```javascript=
$ git add .
$ git commit -m "upload to heroku"
$ git push heroku master
```
## 2.Cài đặt webhook trên Haravan admin của store
Bước này mình cần cài đặt webhook để đẩy event **order fullfill** về Heroku app
App của mình bên cạnh đó, cần phải được setup webhook trên Haravan Admin như hình sau:

Sample format:
- Event: order - fullfill
- URL: <URL-Tới Heroku app/webhooks>
References:
1. https://github.com/Haravan/Omni_OAuth2_nodejs
2. https://docs.haravan.com/blogs/api-reference/1000017858-webhook
## 3. Setup workflow trên Mailjet
Với mỗi webhook event **order fullfill**, bên Haravan sẽ gửi về payload chứa order_id và email của khách hàng đó trong hệ thống Haravan.
Do đó, điều kiện để gửi 1 (rating) email ra sẽ là khi order_id của user đó được cập nhập