# SMS Functionality - DISABLED

**Status:** 🔴 **SMS CURRENTLY DISABLED**  
**Disabled On:** November 11, 2026  
**Reason:** Prevent SMS from being sent during data cleanup and manual loan re-entry

---

## What Was Changed

SMS functionality has been **completely disabled** by commenting out the API key initialization in the SMS service.

### Files Modified:

1. **`payments/sms_service.py`** - Main SMS service file
   - Function `_get_sms_client()` modified to raise an exception
   - All africastalking API calls are blocked
   - API key and username initialization commented out

---

## SMS Will NOT Be Sent For:

✋ **All SMS notifications are blocked, including:**

- ❌ Loan disbursement notifications
- ❌ Loan approval notifications
- ❌ Payment confirmation SMS
- ❌ Processing fee confirmations
- ❌ Registration fee confirmations
- ❌ Repayment confirmations
- ❌ Overdue payment reminders
- ❌ STK push notifications
- ❌ Unknown payment alerts
- ❌ Any admin notifications

---

## How to Re-Enable SMS

When you're ready to turn SMS back on, follow these steps:

### Step 1: Edit the SMS Service File

Open `payments/sms_service.py` and find the `_get_sms_client()` function (around line 20).

### Step 2: Uncomment the Code

**BEFORE (Current - SMS DISABLED):**
```python
def _get_sms_client():
    """
    Initialise and return the Africa's Talking SMS object.
    
    🔴 SMS DISABLED - API Key commented out to prevent all SMS sending
    This was disabled on 2026-11-11 to stop SMS during data cleanup/re-entry.
    
    TO RE-ENABLE SMS:
    1. Uncomment the lines marked with "# SMS_DISABLED:"
    2. Comment out the "raise Exception" line
    3. Test on one loan/repayment to verify SMS works
    4. Deploy to production
    """
    # SMS_DISABLED: Prevent all SMS from being sent
    raise Exception("SMS functionality is currently disabled. See _get_sms_client() docstring to re-enable.")
    
    # SMS_DISABLED: import africastalking
    # SMS_DISABLED: username = getattr(settings, 'AT_USERNAME', 'Uzuriapps')
    # SMS_DISABLED: api_key  = getattr(settings, 'AT_API_KEY', '')
    # SMS_DISABLED: africastalking.initialize(username, api_key)
    # SMS_DISABLED: return africastalking.SMS
```

**AFTER (SMS RE-ENABLED):**
```python
def _get_sms_client():
    """Initialise and return the Africa's Talking SMS object."""
    import africastalking
    username = getattr(settings, 'AT_USERNAME', 'Uzuriapps')
    api_key  = getattr(settings, 'AT_API_KEY', '')
    africastalking.initialize(username, api_key)
    return africastalking.SMS
```

### Step 3: Test Before Production

**On localhost/development:**

1. Create a test loan or repayment
2. Verify you receive SMS to the test numbers
3. Check the SMS Logs page to confirm SMS was sent
4. Make sure admin numbers receive copies

### Step 4: Deploy to Production

Once tested locally:

```bash
# Upload the modified file
scp payments/sms_service.py user@server:/home/xygbfpsg/phingrazuri/payments/

# Restart the application (if needed)
# Method depends on your server setup (systemctl, supervisor, etc.)
```

### Step 5: Verify on Production

1. Process one test repayment
2. Check if SMS is received
3. Monitor the SMS Logs page at: https://grazuri.uzuriapps.xyz/payments/sms-logs/
4. Verify no errors in server logs

---

## Current SMS Configuration

The system uses **Africa's Talking** API:

- **Username:** `Uzuriapps`
- **API Key:** `atsk_e810a145ccd3a1089769cd17887da70ae76124ff044fa0ab949429e6b388954e6db55009`
- **Sender ID:** `HavGrazuri`

These credentials are stored in:
- `.env` file (production)
- Environment variables

---

## Admin Phone Numbers

SMS copies are sent to these admin numbers:
- `+254115451752`
- `+254114457516`

---

## Testing SMS Locally

If you want to test SMS on localhost:

```python
# In Django shell
python manage.py shell

from payments.sms_service import send_sms

# Send test SMS
send_sms(
    recipients='+254712345678',  # Your test number
    message='Test SMS from Grazuri',
    sms_type='other'
)
```

---

## SMS Toggle Button

There's also an **SMS Toggle Button** on the repayments page that allows admins to enable/disable SMS system-wide:

- **Location:** https://grazuri.uzuriapps.xyz/loans/repayments/
- **Function:** Toggles `SystemSettings.sms_notifications_enabled`
- **Note:** This toggle checks the setting, but with SMS disabled at the API level, no SMS will be sent regardless

The code-level disable (what we just did) **overrides** the toggle button setting.

---

## Troubleshooting

### SMS Not Sending After Re-enabling?

1. **Check API credentials** in `.env` file
2. **Verify Africa's Talking account** has credit
3. **Check server logs** for error messages:
   ```bash
   tail -f /path/to/logs/django.log
   ```
4. **Test the API directly** using Africa's Talking dashboard
5. **Verify phone number format** (must be E.164: +2547xxxxxxxx)

### How to Know If SMS Is Disabled?

Look for this error in logs:
```
SMS functionality is currently disabled. See _get_sms_client() docstring to re-enable.
```

Or check the `_get_sms_client()` function - if it has `raise Exception(...)`, SMS is disabled.

---

## Questions?

If you encounter issues re-enabling SMS:

1. Check this document first
2. Review the SMS Logs page for errors
3. Test with a single phone number first
4. Verify API credentials are correct

---

**Last Updated:** November 11, 2026  
**Modified By:** System Admin  
**Next Action:** Re-enable SMS after data cleanup is complete
