/home/awneajlw/public_html/codestechvista.com/TWILIO_SETUP_GUIDE.md
# 📱 Twilio SMS Notifications Setup Guide
## 🚀 Quick Setup Steps
### 1. **Twilio Account Setup**
1. Go to [Twilio Console](https://console.twilio.com/)
2. Sign up for free account (gets $15 trial credit)
3. Get your credentials:
- Account SID
- Auth Token
- Phone Number
### 2. **Configure Twilio Settings**
Open `config/twilio_config.php` and update:
```php
// Replace with your actual Twilio credentials
define('TWILIO_ACCOUNT_SID', 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('TWILIO_AUTH_TOKEN', 'your_auth_token_here');
define('TWILIO_PHONE_NUMBER', '+1234567890'); // Your Twilio number
// Replace with admin phone number
define('ADMIN_PHONE_NUMBER', '+923001234567'); // Admin's phone number
```
### 3. **Database Setup**
Run this SQL command in phpMyAdmin:
```sql
-- Run the SMS notifications table setup
SOURCE database_sms_notifications.sql;
```
Or manually run:
```sql
CREATE TABLE IF NOT EXISTS sms_notifications (
id INT AUTO_INCREMENT PRIMARY KEY,
notification_type ENUM('new_order', 'order_completed', 'status_update', 'daily_summary') NOT NULL,
order_id INT DEFAULT NULL,
customer_name VARCHAR(255) DEFAULT NULL,
phone_number VARCHAR(20) DEFAULT NULL,
message_text TEXT,
status ENUM('sent', 'failed', 'pending') DEFAULT 'sent',
twilio_sid VARCHAR(255) DEFAULT NULL,
sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
### 4. **Test SMS Functionality**
#### Enable Debug Mode (for testing):
```php
// In config/twilio_config.php
define('SMS_DEBUG', true); // Set to true for testing
```
#### Test SMS Service:
```php
// Create test file: test_sms.php
<?php
require_once 'includes/sms_service.php';
$smsService = new SMSService();
// Test new order notification
$smsService->sendNewOrderNotification(
123, // Order ID
'John Doe', // Customer name
'5000', // Amount
'+923001234567' // Phone number
);
echo "Test SMS sent!";
?>
```
## 📱 **SMS Notification Types**
### 1. **New Order Notification**
```
🆕 NEW ORDER ALERT!
Order ID: #123
Customer: John Doe
Amount: PKR 5000
Status: New Order
📱 OPTI SLIP - Eye Clinic Management
```
### 2. **Order Completed Notification**
```
✅ ORDER COMPLETED!
Order ID: #123
Customer: John Doe
Amount: PKR 5000
Status: Completed
📱 OPTI SLIP - Eye Clinic Management
```
### 3. **Status Update Notification**
```
🔄 ORDER STATUS UPDATE!
Order ID: #123
Customer: John Doe
Status: Pending → In Progress
📱 OPTI SLIP - Eye Clinic Management
```
## 🎯 **Features**
✅ **Auto SMS on New Orders**
✅ **SMS on Status Updates**
✅ **SMS on Order Completion**
✅ **Admin Notifications**
✅ **Customer Notifications (optional)**
✅ **SMS Logging & Tracking**
✅ **Error Handling**
✅ **Debug Mode for Testing**
## 🔧 **API Usage**
### Update Order Status with SMS:
```javascript
// POST to update_order_status.php
fetch('update_order_status.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
order_id: 123,
status: 'Completed'
})
});
```
## 📊 **SMS Logs**
Check SMS activity in:
- Database table: `sms_notifications`
- Log file: `logs/sms_log.txt`
- PHP error log
## 💰 **Twilio Pricing**
- **Free Trial:** $15 credit
- **SMS Cost:** ~$0.0075 per SMS
- **Monthly Phone:** $1/month
- **Pakistan SMS:** ~$0.05 per SMS
## 🚨 **Troubleshooting**
### Common Issues:
1. **SMS not sending:**
- Check Twilio credentials
- Verify phone number format (+923001234567)
- Check account balance
2. **Permission errors:**
- Ensure `logs/` directory is writable
- Check file permissions
3. **Database errors:**
- Run database setup SQL
- Check table exists
## 🎛️ **Settings**
### Disable SMS (if needed):
```php
// In config/twilio_config.php
define('SMS_ENABLED', false);
```
### Debug Mode:
```php
define('SMS_DEBUG', true); // Logs messages without sending
```
Ready to use! 🔥