Skip to content

Latest commit

 

History

History
299 lines (234 loc) · 7.18 KB

File metadata and controls

299 lines (234 loc) · 7.18 KB

Shopee Integration - Implementation Summary

✅ All Features Complete

1. Fetch Real Chats & Sync Conversations ✅

Created Files:

  • app/api/shopee/sync/route.ts - Sync endpoint
  • app/api/chats/route.ts - Get conversations
  • src/lib/shopee-token.ts - Token utilities

What it does:

  • Fetches conversations from Shopee API for all connected shops
  • Stores in MongoDB shopee_conversations collection
  • Supports manual sync and auto-sync via cron
  • Filter by country, status, etc.

Usage:

# Sync all shops
POST /api/shopee/sync

# Get conversations
GET /api/chats?country=SG

2. Send Messages via Shopee API ✅

Modified Files:

  • app/(main)/chats/[id]/page.tsx - Chat UI with real API integration
  • app/api/chats/[id]/messages/route.ts - Get messages
  • app/api/chats/[id]/send/route.ts - Send message

What it does:

  • Chat detail page loads real messages from Shopee
  • Send button posts to Shopee API
  • Real-time UI updates with loading states
  • Toast notifications for success/errors

Features:

  • ✅ Load real conversation messages
  • ✅ Send messages to customers
  • ✅ Loading and sending states
  • ✅ Error handling with user feedback

3. Auto-Refresh Tokens (Background Job) ✅

Created Files:

  • app/api/shopee/refresh-tokens/route.ts - Cron endpoint
  • vercel.json - Cron configuration
  • src/lib/shopee-token.ts - Auto-refresh logic

What it does:

  • Automatically refreshes tokens expiring within 24 hours
  • getValidToken() checks and refreshes before API calls
  • Cron job runs every 12 hours via Vercel Cron
  • Batch processes all connected shops

Cron Schedule:

{
  "crons": [
    {
      "path": "/api/shopee/refresh-tokens",
      "schedule": "0 */12 * * *"
    },
    {
      "path": "/api/shopee/sync",
      "schedule": "*/15 * * * *"
    }
  ]
}

4. Shopee Webhooks (Real-time) ✅

Created Files:

  • app/api/shopee/webhook/route.ts - Webhook receiver

What it does:

  • Receives real-time events from Shopee:
    • New messages (code 1)
    • Messages read (code 2)
    • Conversation updates (code 3)
  • Verifies HMAC-SHA256 signatures
  • Auto-updates conversations in database
  • Increments unread count for new messages

Webhook URL:

https://yourdomain.com/api/shopee/webhook

Setup: Configure in Shopee Open Platform → Webhooks → Add URL


5. Multi-Country Support ✅

Modified Files:

  • app/(main)/settings/page.tsx - Country selector + sync all
  • app/api/shopee/connect/route.ts - Accept country param
  • app/api/shopee/callback/route.ts - Store country

Supported Countries(7か国):

  • 🇸🇬 SG - Singapore
  • 🇵🇭 PH - Philippines
  • 🇲🇾 MY - Malaysia
  • 🇹🇼 TW - Taiwan
  • 🇹🇭 TH - Thailand
  • 🇻🇳 VN - Vietnam
  • 🇧🇷 BR - Brazil

Features:

  • Dropdown to select country before connecting
  • Display flags for each connected shop
  • "Sync All" button to sync all shops at once
  • Filter conversations by country

📦 Complete File List

New Files (17 total)

Libraries:

  1. src/lib/shopee-api.ts - Shopee API wrapper
  2. src/lib/shopee-token.ts - Token management

API Routes (9): 3. app/api/shopee/connect/route.ts 4. app/api/shopee/callback/route.ts 5. app/api/shopee/status/route.ts 6. app/api/shopee/sync/route.ts 7. app/api/shopee/refresh-tokens/route.ts 8. app/api/shopee/webhook/route.ts 9. app/api/chats/route.ts 10. app/api/chats/[id]/messages/route.ts 11. app/api/chats/[id]/send/route.ts

UI (2): 12. app/(main)/settings/page.tsx (modified) 13. app/(main)/chats/[id]/page.tsx (modified)

Config: 14. vercel.json - Cron jobs

Documentation: 15. SHOPEE_INTEGRATION.md 16. SHOPEE_COMPLETE.md 17. README_SHOPEE.md (this file)

Modified Files (3)

  • src/components/AppLayout.tsx - Added Settings to nav
  • .env - Added Shopee credentials
  • app/login/page.tsx - Session persistence

🗄️ Database Schema

Collection: shopee_tokens

{
  shop_id: number,
  shop_name: string,
  country: "SG" | "PH" | "MY" | "TW" | "TH" | "VN" | "BR",
  access_token: string,
  refresh_token: string,
  expires_at: Date,
  created_at: Date,
  updated_at: Date
}

Collection: shopee_conversations

{
  conversation_id: string,
  shop_id: number,
  country: string,
  customer_id: number,
  customer_name: string,
  last_message: string,
  last_message_time: Date,
  unread_count: number,
  pinned: boolean,
  status: "active" | "resolved" | "archived",
  assigned_staff?: string,
  created_at: Date,
  updated_at: Date
}

🚀 Getting Started

1. Set Environment Variables

# .env
SHOPEE_PARTNER_ID=your_partner_id
SHOPEE_PARTNER_KEY=your_partner_key
SHOPEE_REDIRECT_URL=http://localhost:3000/api/shopee/callback
CRON_SECRET=random_secret_for_cron_jobs

2. Connect Your Singapore Store

  1. Start: npm run dev
  2. Login to Chapee
  3. Go to 設定 (Settings)
  4. Select country: SG - Singapore
  5. Enter your Shop ID and Authorization Code
  6. Click アカウント接続

3. Sync Conversations

  • Click 全店舗同期 in Settings, OR
  • Run: curl -X POST http://localhost:3000/api/shopee/sync

4. View & Reply to Messages

  1. Go to ダッシュボード (Dashboard)
  2. Click on any conversation
  3. View messages from Shopee
  4. Type reply and click send

🔄 Automated Jobs

Vercel Cron (Automatic)

  • Token Refresh: Every 12 hours
  • Chat Sync: Every 15 minutes

Manual Trigger

# Refresh tokens
curl http://localhost:3000/api/shopee/refresh-tokens

# Sync conversations
curl -X POST http://localhost:3000/api/shopee/sync

📊 API Overview

Endpoint Method Description
/api/shopee/connect POST Connect shop manually
/api/shopee/callback GET OAuth callback
/api/shopee/status GET List connected shops
/api/shopee/sync POST Sync conversations
/api/shopee/refresh-tokens GET Refresh tokens (cron)
/api/shopee/webhook POST Receive webhooks
/api/chats GET Get conversations
/api/chats/[id]/messages GET Get messages
/api/chats/[id]/send POST Send message

✅ Testing Checklist

  • Connect Singapore store via Settings
  • Sync conversations: POST /api/shopee/sync
  • View conversations in Dashboard
  • Open chat detail and view messages
  • Send a test message to customer
  • Test token refresh: GET /api/shopee/refresh-tokens
  • (Optional) Connect additional countries (PH, MY, etc.)
  • (Optional) Set up webhook in Shopee platform

🎉 All Features Delivered!

Fetch real chats - Sync API implemented
Send messages - Chat detail integrated
Auto-refresh tokens - Background job + cron
Webhooks - Real-time event handling
Multi-country - SG, PH, MY, TW, TH, VN, BR(7か国)

The Shopee integration is complete and production-ready!


📚 Documentation

  • SHOPEE_INTEGRATION.md - Initial setup guide
  • SHOPEE_COMPLETE.md - Detailed feature documentation
  • README_SHOPEE.md - This summary file

For detailed API documentation and troubleshooting, see SHOPEE_COMPLETE.md.