# ⚡ WhatsApp Custom API - Quick Setup Checklist

## 🎯 30-Second Setup

### Your API Credentials
```
App Key:  1a68fcd1-3746-4be7-9cc0-334423e4e1d5
Auth Key: yoQ5tAx2MjtnOM2JCu8HU5IuL14DDkKesmGnCVOLRzuyVbu1qv
Endpoint: https://whatsapp.thechintanpatel.co.in/api/create-message
```

---

## ✅ Setup Steps (Copy-Paste)

### Step 1: Edit `backend/.env`

Add these lines (or update if they exist):

```bash
# WhatsApp Configuration
WHATSAPP_API_URL=https://whatsapp.thechintanpatel.co.in/api/create-message
WHATSAPP_APP_KEY=1a68fcd1-3746-4be7-9cc0-334423e4e1d5
WHATSAPP_AUTH_KEY=yoQ5tAx2MjtnOM2JCu8HU5IuL14DDkKesmGnCVOLRzuyVbu1qv
```

### Step 2: Start Backend

```bash
cd backend
npm start
```

### Step 3: Test Login with WhatsApp OTP

```bash
# Open browser
http://localhost:3000/login

# Enter your phone number
# 9876543210

# You'll receive OTP on WhatsApp instantly! ✅
```

**That's it! You're done.** 🎉

---

## 📋 Complete Checklist

- [ ] Credentials copied from above
- [ ] `backend/.env` updated with API keys
- [ ] Backend restarted (`npm start`)
- [ ] Tested login on http://localhost:3000
- [ ] Received WhatsApp OTP
- [ ] Verified OTP works

---

## 🧪 Test Commands

### Quick Test (Using curl)
```bash
curl -X POST http://localhost:5000/api/auth/send-otp \
  -H "Content-Type: application/json" \
  -d '{"mobile": "9876543210"}'
```

**Expected Response:**
```json
{
  "success": true,
  "message": "OTP sent via WhatsApp",
  "method": "whatsapp"
}
```

Check your WhatsApp for the OTP! 📱

---

## 📊 Message Examples

### OTP Message
```
Your OTP for Trust Tax Advisor is: 123456. Valid for 10 minutes. DO NOT SHARE WITH ANYONE.
```

### Commission Notification
```
💰 Congratulations! You earned ₹500 commission on Order #1234.
💰 Total Pending: ₹500
Check your dashboard for details.
```

### Order Status Update
```
✅ Your order #1234 has been completed!
📋 Service: Income Tax Return
💰 Amount: ₹5000
Thank you for using Trust Tax Advisor! 🙏
```

---

## 🔄 File Changes Made

| File | Change |
|------|--------|
| `backend/config/whatsapp.js` | ✅ Updated to use custom API |
| `backend/.env.example` | ✅ Updated with new credentials |
| `backend/controllers/AuthController.js` | ✅ Already compatible |
| `backend/models/OtpLog.js` | ✅ Already compatible |

**No other files need to be changed!**

---

## 💡 Different Message Types

### 1. Text OTP (Automatic)
```javascript
// Automatically happens during login
sendWhatsAppOTP('9876543210', '123456')
// User receives: "Your OTP for Trust Tax Advisor is: 123456..."
```

### 2. Text Alert Message
```javascript
// Send notification to connector
sendWhatsAppAlert(
  '9876543210', 
  '💰 You earned ₹500 commission!'
)
```

### 3. Message with File
```javascript
// Send invoice with message
sendWhatsAppAlert(
  '9876543210',
  'Your invoice is attached',
  'https://yourdomain.com/invoice.pdf'
)
```

---

## 🚀 What Happens Now

| Event | Action | Result |
|-------|--------|--------|
| **User sends OTP** | Automatically sends via WhatsApp | ✅ OTP on phone |
| **Order completed** | System sends alert | ✅ User notified |
| **Commission earned** | System sends notification | ✅ Connector informed |
| **Admin action needed** | System sends alert to admin | ✅ Admin alerted |

---

## ⚠️ If Something Goes Wrong

### Problem: "Invalid credentials"
```bash
# Check your .env file has exact values:
WHATSAPP_APP_KEY=1a68fcd1-3746-4be7-9cc0-334423e4e1d5
WHATSAPP_AUTH_KEY=yoQ5tAx2MjtnOM2JCu8HU5IuL14DDkKesmGnCVOLRzuyVbu1qv

# Restart backend
npm start
```

### Problem: No OTP received
```bash
# 1. Check terminal for error messages
# 2. Verify phone number is correct (10 digits)
# 3. Check WhatsApp is active on phone
# 4. Try again after 1 minute
```

### Problem: Slow delivery
```bash
# Normal: 1-5 seconds
# If longer: Check internet connection
# Restart: npm start
```

---

## 🎯 Next: Integration Examples

| Use Case | File | Function |
|----------|------|----------|
| **Login OTP** | `AuthController.js` | `sendOtp()` |
| **Commission Alert** | `CommissionController.js` | `notifyCommissionCreated()` |
| **Order Update** | `OrderController.js` | `sendUpdate()` |
| **Admin Alert** | `AdminController.js` | `sendAdminAlert()` |

See `WHATSAPP_IMPLEMENTATION_EXAMPLES.md` for detailed code.

---

## 📞 Support

| Question | Answer |
|----------|--------|
| **Lost credentials?** | Check `.env` file |
| **Need to change API?** | Edit `backend/.env` |
| **Want to customize message?** | Edit `AuthController.js` |
| **Need help?** | Check `WHATSAPP_CUSTOM_API_SETUP.md` |

---

## ✨ Features Enabled

✅ WhatsApp OTP login (main auth method)  
✅ Commission notifications  
✅ Order status updates  
✅ Invoice attachments  
✅ Admin alerts  
✅ Fallback to email  
✅ Rate limiting (3/min)  
✅ Error handling  

---

## 🎊 You're All Set!

**Your Trust Tax Advisor now has WhatsApp integration!**

### Start using it:

1. **Run backend:**
   ```bash
   cd backend && npm start
   ```

2. **Run frontend:**
   ```bash
   cd frontend && npm run dev
   ```

3. **Test login:**
   - Go to http://localhost:3000
   - Enter phone number
   - Get OTP on WhatsApp
   - Login successful! ✅

---

## 📚 Documentation

| Document | Purpose |
|----------|---------|
| `WHATSAPP_CUSTOM_API_SETUP.md` | Full API documentation |
| `WHATSAPP_IMPLEMENTATION_EXAMPLES.md` | Code examples |
| `README.md` | Project overview |
| `AUTHENTICATION_GUIDE.md` | Auth flow details |

---

**Done! WhatsApp OTP is now active.** 🚀

Questions? Check the documentation files listed above. 📖
