API Danh sách Voucher
API này cho phép lấy danh sách voucher sàn và voucher shop với đầy đủ thông tin chi tiết, validation và phân trang.
Ví dụ minh họa: Danh sách voucher sàn và shop
Ví dụ minh họa: Danh sách voucher sàn và shop
GET https://api.socdo.vn/v1/voucher_list
Các tham số:
- type: Loại voucher - 'platform' (sàn) hoặc 'shop' (shop) - mặc định 'platform'
- shop_id: ID shop (bắt buộc khi type='shop')
- user_id: ID người dùng (tùy chọn, để check số lần sử dụng)
- product_id: ID sản phẩm (tùy chọn, để lọc voucher áp dụng cho SP cụ thể)
- page: Trang hiện tại (mặc định 1)
- limit: Số lượng voucher mỗi trang (mặc định 10, tối đa 100)
Ví dụ lấy voucher sàn bằng cURL:
curl -X GET "https://api.socdo.vn/v1/voucher_list?type=platform&page=1&limit=10" -H "Authorization: Bearer <token>"
Ví dụ lấy voucher shop bằng cURL:
curl -X GET "https://api.socdo.vn/v1/voucher_list?type=shop&shop_id=123&user_id=456&page=1&limit=5" -H "Authorization: Bearer <token>"
Ví dụ kết nối bằng PHP:
<?php
// Lấy voucher sàn
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.socdo.vn/v1/voucher_list?" . http_build_query([
"type" => "platform",
"user_id" => 456,
"page" => 1,
"limit" => 10
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer <token>"
]);
$response = curl_exec($ch);
curl_close($ch);
// Lấy voucher shop
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://api.socdo.vn/v1/voucher_list?" . http_build_query([
"type" => "shop",
"shop_id" => 123,
"user_id" => 456,
"product_id" => 789,
"page" => 1,
"limit" => 5
]));
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HTTPHEADER, [
"Authorization: Bearer <token>"
]);
$response2 = curl_exec($ch2);
curl_close($ch2);
echo $response;
?>
Dữ liệu trả về mẫu (Voucher Sàn):
{
"success": true,
"message": "Lấy danh sách voucher thành công",
"data": {
"voucher": [
{
"id": "1",
"ma": "SOCDO10",
"loai": "phantram",
"kieu": "all",
"giam": 10,
"giam_toi_da": 50000,
"min_price": 100000,
"max_price": 0,
"start": 1703721600,
"expired": 1704326400,
"max_global_uses": 1000,
"current_uses": 245,
"max_uses_per_user": 1,
"allow_combination": 0,
"sanpham": "",
"shop": 0,
"remaining_uses": 755,
"giam_formatted": "10%",
"start_formatted": "28/12/2023 00:00",
"expired_formatted": "04/01/2024 00:00",
"min_price_formatted": "100,000đ",
"max_price_formatted": "",
"giam_toi_da_formatted": "50,000đ",
"can_apply": true,
"error_message": "",
"applicable_products": [],
"is_all_products": true,
"shop_info": null,
"button_text": "Sử dụng ngay",
"button_class": "active"
},
{
"id": "2",
"ma": "FREESHIP20",
"loai": "tru",
"kieu": "sanpham",
"giam": 20000,
"giam_toi_da": 0,
"min_price": 200000,
"max_price": 0,
"start": 1703721600,
"expired": 1704326400,
"max_global_uses": 500,
"current_uses": 450,
"max_uses_per_user": 2,
"allow_combination": 1,
"sanpham": "123,456,789",
"shop": 0,
"remaining_uses": 50,
"giam_formatted": "20,000đ",
"start_formatted": "28/12/2023 00:00",
"expired_formatted": "04/01/2024 00:00",
"min_price_formatted": "200,000đ",
"max_price_formatted": "",
"giam_toi_da_formatted": "",
"can_apply": true,
"error_message": "",
"applicable_products": [
{
"id": "123",
"tieu_de": "iPhone 15 Pro Max",
"minh_hoa": "https://api.socdo.vn/uploads/minh-hoa/iphone15.jpg"
},
{
"id": "456",
"tieu_de": "Samsung Galaxy S24",
"minh_hoa": "https://api.socdo.vn/uploads/minh-hoa/samsung-s24.jpg"
}
],
"is_all_products": false,
"shop_info": null,
"button_text": "Sử dụng ngay",
"button_class": "active"
}
],
"pagination": {
"current_page": 1,
"total_pages": 5,
"total_records": 47,
"per_page": 10,
"has_next": true,
"has_prev": false
},
"filters": {
"type": "platform",
"shop_id": 0,
"product_id": 0
}
}
}
Dữ liệu trả về mẫu (Voucher Shop):
{
"success": true,
"message": "Lấy danh sách voucher thành công",
"data": {
"voucher": [
{
"id": "15",
"ma": "SHOP50K",
"loai": "tru",
"kieu": "all",
"giam": 50000,
"giam_toi_da": 0,
"min_price": 300000,
"max_price": 1000000,
"start": 1703721600,
"expired": 1704326400,
"max_global_uses": 100,
"current_uses": 25,
"max_uses_per_user": 1,
"allow_combination": 0,
"sanpham": "",
"shop": 123,
"remaining_uses": 75,
"giam_formatted": "50,000đ",
"start_formatted": "28/12/2023 00:00",
"expired_formatted": "04/01/2024 00:00",
"min_price_formatted": "300,000đ",
"max_price_formatted": "1,000,000đ",
"giam_toi_da_formatted": "",
"can_apply": true,
"error_message": "",
"applicable_products": [],
"is_all_products": true,
"shop_info": {
"name": "Shop Điện Tử ABC",
"avatar": "https://api.socdo.vn/uploads/avatar/shop123.jpg"
},
"button_text": "Sử dụng ngay",
"button_class": "active"
}
],
"pagination": {
"current_page": 1,
"total_pages": 1,
"total_records": 3,
"per_page": 5,
"has_next": false,
"has_prev": false
},
"filters": {
"type": "shop",
"shop_id": 123,
"product_id": 0
}
}
}
Tính năng chi tiết:
- Phân loại voucher: Platform (sàn) và Shop với logic riêng biệt
- Validation đầy đủ: Thời gian, số lần sử dụng, điều kiện áp dụng
- Lọc theo sản phẩm: Chỉ lấy voucher áp dụng cho sản phẩm cụ thể
- Phân trang: Hỗ trợ pagination với thông tin chi tiết
- Format dữ liệu: Tự động format giá tiền, thời gian, phần trăm
- Check user usage: Kiểm tra số lần sử dụng của user cụ thể
- Thông tin shop: Lấy tên và avatar shop cho voucher shop
- Sản phẩm áp dụng: Danh sách sản phẩm được áp dụng voucher
Các bảng database liên quan:
- coupon: Bảng chính chứa thông tin voucher
- voucher_usage: Lịch sử sử dụng voucher của user
- sanpham: Thông tin sản phẩm áp dụng voucher
- user_info: Thông tin shop (name, avatar)
Logic phân biệt voucher:
- Voucher sàn: shop = 0, do platform quản lý
- Voucher shop: shop = shop_id, do shop cụ thể tạo
- Voucher tất cả SP: kieu = 'all'
- Voucher SP cụ thể: kieu = 'sanpham', danh sách trong trường 'sanpham'
Validation voucher:
- Thời gian: start <= hiện tại <= expired
- Trạng thái: status = 2 (đang hoạt động)
- Số lần dùng: current_uses < max_global_uses
- User limit: Kiểm tra bảng voucher_usage
- Sản phẩm: Kiểm tra FIND_IN_SET nếu có product_id
Lưu ý:
- Bạn cần lấy token xác thực trước khi gọi API này
- shop_id bắt buộc khi type='shop'
- API tự động validate tất cả điều kiện voucher
- Trả về thông tin đầy đủ: giá trị, điều kiện, trạng thái
- Hỗ trợ phân trang với limit tối đa 100 voucher/page
- Format sẵn tất cả giá trị để hiển thị trực tiếp
- Kiểm tra số lần sử dụng của user nếu có user_id
- Lọc voucher theo sản phẩm cụ thể nếu có product_id