# SMS Troubleshooting Guide

## Issue: Customer Not Receiving SMS

### Quick Checklist

1. **Verify SMS Provider is Selected**
   - Go to Admin Dashboard → Settings → SMS Configuration
   - Check that "SMS Provider" is set to "SMS Online GH"
   - Ensure it's not set to "BulkSMS Ghana" if you're using SMS Online GH

2. **Verify API Key is Configured**
   - Check that "SMS API Key" field has your SMS Online GH API key
   - The key should not be empty
   - Make sure there are no extra spaces before/after the key

3. **Verify Sender ID**
   - Check "Sender ID" field (max 11 characters)
   - Default is "RMBXchange"
   - Must be approved by SMS Online GH

4. **Test SMS Configuration**
   - In Admin Settings → SMS Configuration section
   - Enter your admin phone number in "Admin Alert Phone Number"
   - Click "Send Test SMS" button
   - Check if you receive the test message

5. **Check User Phone Number**
   - Verify the customer has a phone number in their profile
   - Phone should be in format: +233XXXXXXXXX or 0XXXXXXXXX
   - System automatically converts to +233 format

### SMS Online GH Configuration

#### API Endpoint
```
https://api.smsonlinegh.com/v4/message/sms/send
```

#### Request Format
```json
{
  "sender": "RMBXchange",
  "recipient": ["+233XXXXXXXXX"],
  "message": "Your message here",
  "type": 0
}
```

#### Headers
```
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
```

#### Expected Response (Success)
```json
{
  "code": 200,
  "message": "Message sent successfully",
  "data": {
    "id": "message_id",
    "status": "sent"
  }
}
```

### Common Issues and Solutions

#### 1. SMS Not Sending at All

**Symptoms:**
- No SMS received by customer
- No SMS received by admin

**Solutions:**
- Check Laravel logs: `storage/logs/laravel.log`
- Look for entries with "SMS" in them
- Check for error messages

**Log Location:**
```bash
tail -f storage/logs/laravel.log | grep SMS
```

#### 2. Invalid API Key Error

**Symptoms:**
- Log shows "401 Unauthorized" or "Invalid API key"

**Solutions:**
- Verify API key is correct
- Check if API key has expired
- Regenerate API key from SMS Online GH dashboard
- Ensure no extra spaces in the key

#### 3. Invalid Phone Number Format

**Symptoms:**
- Log shows "Invalid recipient" or "Invalid phone number"

**Solutions:**
- Check phone number format in user profile
- Should be: +233XXXXXXXXX or 0XXXXXXXXX
- System auto-converts, but check logs for conversion issues

#### 4. Insufficient SMS Credits

**Symptoms:**
- Log shows "Insufficient balance" or "Low credits"

**Solutions:**
- Check SMS balance in SMS Online GH dashboard
- Top up your SMS credits
- Use the balance check feature (if implemented)

#### 5. Sender ID Not Approved

**Symptoms:**
- SMS sends but shows different sender ID
- Or SMS fails with "Invalid sender ID"

**Solutions:**
- Verify sender ID is approved in SMS Online GH dashboard
- Use default sender ID provided by SMS Online GH
- Request sender ID approval if using custom ID

### Debugging Steps

#### Step 1: Check Settings
```php
// In Laravel Tinker or debug code
use App\Models\Setting;

echo "Provider: " . Setting::get('sms_provider') . "\n";
echo "API Key: " . (Setting::get('bulksms_api_key') ? 'Set' : 'Not Set') . "\n";
echo "Sender ID: " . Setting::get('bulksms_sender_id') . "\n";
echo "Admin Phone: " . Setting::get('admin_alert_phone') . "\n";
```

#### Step 2: Test SMS Service Directly
```php
// In Laravel Tinker
$sms = new \App\Services\SmsService();
$result = $sms->send('+233XXXXXXXXX', 'Test message');
print_r($result);
```

#### Step 3: Check Logs
```bash
# View recent SMS logs
tail -100 storage/logs/laravel.log | grep -i sms

# Watch logs in real-time
tail -f storage/logs/laravel.log | grep -i sms
```

#### Step 4: Test API Directly
```bash
# Using curl to test SMS Online GH API
curl -X POST https://api.smsonlinegh.com/v4/message/sms/send \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "sender": "RMBXchange",
    "recipient": ["+233XXXXXXXXX"],
    "message": "Test message",
    "type": 0
  }'
```

### Log Messages to Look For

#### Success Messages
```
SMS Online GH - Success
SMS sent successfully
```

#### Error Messages
```
SMS API key not configured
SMS Online GH API error
Invalid recipient
Insufficient balance
401 Unauthorized
```

### Withdrawal SMS Flow

When a customer makes a withdrawal request:

1. **Customer SMS** (sent to customer's phone)
   ```
   Flat Line: Your withdrawal request of ¥X.XX (Ref: WTH-XXXXX) has been submitted. You'll be notified once reviewed.
   ```

2. **Admin SMS** (sent to admin alert phone)
   ```
   RMBXchange Alert: [Customer Name] requested withdrawal of ¥X.XX. Ref: WTH-XXXXX. Alipay: [Account]
   ```

### Code Locations

- **SMS Service**: `app/Services/SmsService.php`
- **Withdrawal Logic**: `app/Http/Controllers/DashboardController.php` (withdraw method)
- **Admin Settings**: `resources/views/admin/settings.blade.php`
- **Settings Storage**: Database `settings` table

### Getting Help

1. **Check Laravel Logs**
   - Location: `storage/logs/laravel.log`
   - Look for SMS-related errors

2. **Enable Debug Mode**
   - Set `APP_DEBUG=true` in `.env`
   - Restart application
   - Try sending SMS again
   - Check detailed error messages

3. **Contact SMS Provider**
   - SMS Online GH Support: https://smsonlinegh.com
   - Provide: API key, phone number, error message
   - Check account status and credits

4. **Test with Different Phone**
   - Try sending to a different phone number
   - Verify if issue is phone-specific or system-wide

### Prevention

1. **Monitor SMS Credits**
   - Check balance regularly
   - Set up low balance alerts
   - Auto-top up if available

2. **Test After Changes**
   - Use "Send Test SMS" button after any configuration change
   - Verify SMS works before going live

3. **Keep Logs**
   - Regularly check logs for SMS errors
   - Set up log monitoring/alerts

4. **Backup Provider**
   - Consider having BulkSMS Ghana as backup
   - Can switch providers if one fails

### Quick Fix Checklist

- [ ] SMS provider is set to "SMS Online GH"
- [ ] API key is entered correctly
- [ ] Sender ID is configured
- [ ] Admin alert phone number is set
- [ ] Test SMS button works
- [ ] Customer has phone number in profile
- [ ] Phone number format is correct
- [ ] SMS credits are sufficient
- [ ] Sender ID is approved
- [ ] No errors in Laravel logs
