# Sellkit Checkout Integration Guide

Tích hợp checkout Sellkit vào website bên ngoài. Không cần API key cho các endpoint public.

**API Base:** `https://sk-api.vibery.app`

## Cách 1: Embed Widget (30 giây)

Dán vào bất kỳ trang HTML nào:

```html
<div data-sellkit-checkout="your-tenant/your-product"></div>
<script src="https://sk-api.vibery.app/embed/checkout.js"></script>
```

Tùy chọn:

| Attribute | Mô tả |
|-----------|-------|
| `data-sellkit-checkout` | `tenant-slug/product-slug` (bắt buộc) |
| `data-sellkit-ref` | Mã giới thiệu affiliate |
| `data-sellkit-coupon` | Mã giảm giá điền sẵn |
| `data-sellkit-lang` | `vi` hoặc `en` |

## Cách 2: SDK (Custom UI)

```html
<script src="https://sk-api.vibery.app/embed/sdk.js"></script>
```

```js
var sk = new Sellkit('https://sk-api.vibery.app');

// 1. Lấy sản phẩm
var data = await sk.getProduct('tenant-slug', 'product-slug');

// 2. Xem trước giá (gọi khi nhập email — tính giá theo membership)
var pricing = await sk.previewPricing({
  tenant_slug: 'tenant-slug',
  variant_ids: ['variant-uuid-1', 'variant-uuid-2'],
  email: 'customer@example.com',
  coupon_code: 'GIAM20'
});

// 3. Tạo đơn hàng
var order = await sk.checkout({
  tenant_slug: 'tenant-slug',
  variant_ids: ['variant-uuid-1'],
  name: 'Nguyễn Văn A',
  email: 'customer@example.com',
  phone: '0901234567',
  coupon_code: 'GIAM20',
  ref: 'affiliate-code',
  sale_id: 'sale-uuid',
  tax_info: { company_name: 'Công ty ABC', tax_code: '0123456789', address: '123 Đường XYZ' }
});

// 4. Hiển thị thanh toán INLINE (không redirect sang domain khác!)
if (order.is_free) {
  showSuccess(order);
} else {
  // QR code: hiển thị ngay trên trang
  var isQR = order.checkout_url.includes('qr') || order.checkout_url.includes('sepay');
  if (isQR) document.getElementById('qr').src = order.checkout_url;
  // Stripe: mở tab mới
  else window.open(order.checkout_url, '_blank');

  // Polling — chờ thanh toán, cập nhật trạng thái
  sk.pollStatus(order.order_id, function(status) {
    if (status.status === 'paid') showSuccess(status);
    // status.fulfillment = { license_key, download_url, ticket_code }
  });
}

// Helper
Sellkit.formatPrice(299000, 'vnd') // "299.000 ₫"
```

## Cách 3: Sale Page Template (Multi-product)

Download template: [sale-page.html](https://sk-api.vibery.app/embed/sale-page.html)

Sửa 3 dòng config:

```js
const CONFIG = {
  apiBase: 'https://sk-api.vibery.app',
  tenantSlug: 'your-tenant',
  saleSlug: 'your-sale-slug',
  checkoutBase: 'https://your-checkout-domain.com',
};
```

Template tự động: load sản phẩm, checkbox chọn, tính giá theo membership, coupon, thanh toán.

## API Endpoints

| Method | Endpoint | Mô tả |
|--------|----------|-------|
| GET | `/api/v1/public/{tenant}/{product}` | Lấy sản phẩm + variants |
| GET | `/api/v1/public/{tenant}/sale/{slug}` | Lấy sale + tất cả sản phẩm |
| POST | `/api/v1/public/pricing-preview` | Tính giá (email-based, bundle) |
| POST | `/api/v1/public/checkout` | Tạo đơn hàng + thanh toán |
| GET | `/api/v1/public/orders/{id}/status` | Theo dõi trạng thái |

## Pricing Preview Request

```json
{
  "tenant_slug": "your-tenant",
  "variant_ids": ["uuid-1", "uuid-2"],
  "email": "customer@example.com",
  "coupon_code": "GIAM20",
  "quantity": 1
}
```

Response:
```json
{
  "subtotal": 59800000,
  "total": 50000000,
  "discount": 9800000,
  "adjustments": [{"rule_name": "Bundle discount", "amount": 9800000}],
  "tax_amount": 4545454,
  "tax_inclusive": true,
  "currency": "vnd"
}
```

## Checkout Request

```json
{
  "tenant_slug": "your-tenant",
  "variant_ids": ["uuid-1", "uuid-2"],
  "name": "Nguyễn Văn A",
  "email": "customer@example.com",
  "phone": "0901234567",
  "coupon_code": "GIAM20",
  "ref": "affiliate-code",
  "sale_id": "sale-uuid",
  "tax_info": {
    "company_name": "Công ty ABC",
    "tax_code": "0123456789",
    "address": "123 Đường XYZ"
  }
}
```

Response:
```json
{
  "order_id": "uuid",
  "order_number": 42,
  "total": 50000000,
  "currency": "vnd",
  "status": "pending",
  "is_free": false,
  "checkout_url": "https://qr.sepay.vn/img?...",
  "simulate": false
}
```

## Giá trị tiền

Tất cả giá trị tiền lưu theo đơn vị nhỏ nhất của tiền tệ:
- VND: lưu nguyên. 299.000₫ = `299000`
- USD: lưu cents. $9.99 = `999`
- Miễn phí = `0`

## CORS & Rate Limit

- CORS: mở cho mọi domain
- Pricing preview: không giới hạn
- Checkout: 10 request/phút per IP

---
*Sellkit — API-first digital commerce engine*
