# 🚀 Production Deployment Checklist

## Pre-Deployment

### Environment Configuration
- [ ] Copy `.env.example` to `.env.production`
- [ ] Set `DEBUG=False`
- [ ] Generate new `SECRET_KEY` (use: `python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'`)
- [ ] Configure `ALLOWED_HOSTS` with production domains
- [ ] Set PostgreSQL credentials
- [ ] Configure SMTP email settings
- [ ] Update PayTech credentials (production keys)
- [ ] Set `PAYTECH_ENV=prod`
- [ ] Update all callback URLs to production domain

### PayTech Setup
- [ ] Register at [paytech.sn](https://paytech.sn)
- [ ] Complete KYC documentation
- [ ] Test in sandbox mode (`PAYTECH_ENV=test`)
- [ ] Submit production activation request to contact@paytech.sn
- [ ] Provide required documents (business registration, tax ID, bank details)
- [ ] Receive production API keys
- [ ] Update IPN URL: `https://sponsor.enfantsoleilmonde.org/payment/ipn/`
- [ ] Update Success URL: `https://sponsor.enfantsoleilmonde.org/payment/success/`
- [ ] Update Cancel URL: `https://sponsor.enfantsoleilmonde.org/payment/cancel/`
- [ ] Verify success/cancel pages require valid PayTech parameters (security protection)

### Server Setup
- [ ] Server running Ubuntu 20.04+ or similar
- [ ] Python 3.8+ installed
- [ ] PostgreSQL 12+ installed
- [ ] Nginx installed
- [ ] Certbot installed (for SSL)
- [ ] Sufficient disk space (min 10GB)
- [ ] Adequate RAM (min 2GB)

## Deployment Steps

### 1. Initial Setup
- [ ] Clone repository to `/var/www/enfantsoleilmonde`
- [ ] Create virtual environment
- [ ] Install dependencies from `requirements.txt`
- [ ] Install production packages: `gunicorn`, `psycopg2-binary`

### 2. Database
- [ ] Create PostgreSQL database
- [ ] Create database user with permissions
- [ ] Run `python manage.py migrate`
- [ ] Create superuser: `python manage.py createsuperuser`
- [ ] Test database connection

### 3. Static Files
- [ ] Run `python manage.py collectstatic`
- [ ] Verify static files in `staticfiles/` directory
- [ ] Check permissions on static directories

### 4. Gunicorn
- [ ] Create `/etc/systemd/system/gunicorn.service`
- [ ] Configure Gunicorn with 3+ workers
- [ ] Set correct user and group (`www-data`)
- [ ] Create logs directory
- [ ] Start service: `sudo systemctl start gunicorn`
- [ ] Enable on boot: `sudo systemctl enable gunicorn`
- [ ] Check status: `sudo systemctl status gunicorn`

### 5. Nginx
- [ ] Create site configuration in `/etc/nginx/sites-available/`
- [ ] Configure proxy to Gunicorn socket
- [ ] Set up static files serving
- [ ] Add security headers
- [ ] Enable site: `sudo ln -s /etc/nginx/sites-available/enfantsoleilmonde /etc/nginx/sites-enabled/`
- [ ] Test config: `sudo nginx -t`
- [ ] Reload Nginx: `sudo systemctl reload nginx`

### 6. SSL Certificate
- [ ] Run Certbot: `sudo certbot --nginx -d enfantsoleilmonde.org -d www.enfantsoleilmonde.org`
- [ ] Verify SSL certificate
- [ ] Test auto-renewal: `sudo certbot renew --dry-run`
- [ ] Verify HTTPS redirect works

### 7. Django Security
- [ ] Verify `SECURE_SSL_REDIRECT=True` (automatic when DEBUG=False)
- [ ] Verify `SESSION_COOKIE_SECURE=True`
- [ ] Verify `CSRF_COOKIE_SECURE=True`
- [ ] Run `python manage.py check --deploy`
- [ ] Review and fix any security warnings

## Post-Deployment Testing

### Functionality Tests
- [ ] Homepage loads correctly
- [ ] Static files (CSS, JS, images) load
- [ ] User registration works
- [ ] User login/logout works
- [ ] Password reset flow works
- [ ] Admin panel accessible at `/admin`
- [ ] Donation form appears
- [ ] PayTech payment request succeeds
- [ ] Redirect to PayTech works
- [ ] Success page displays after payment
- [ ] Cancel page displays on cancellation
- [ ] Email notifications sent

### PayTech Integration Tests
- [ ] Create test donation (small amount)
- [ ] Complete payment on PayTech page
- [ ] Verify redirect to success page (with token/ref_command parameters)
- [ ] Test direct access to success page (should redirect to home)
- [ ] Check IPN notification received (check logs)
- [ ] Verify donation status updated in database
- [ ] Confirm email sent to donor
- [ ] Test payment cancellation flow
- [ ] Verify cancel page displays (with token/ref_command parameters)
- [ ] Test direct access to cancel page (should redirect to home)
- [ ] Check logs for any errors

### Security Tests
- [ ] HTTPS enabled (no mixed content warnings)
- [ ] HTTP auto-redirects to HTTPS
- [ ] SSL certificate valid and trusted
- [ ] Security headers present (check with: https://securityheaders.com)
- [ ] Admin panel requires authentication
- [ ] CSRF protection active
- [ ] SQL injection tests pass
- [ ] XSS protection enabled

### Performance Tests
- [ ] Page load time < 3 seconds
- [ ] Static files cached properly
- [ ] Database queries optimized
- [ ] No memory leaks
- [ ] Server response time acceptable

## Monitoring & Maintenance

### Log Monitoring
- [ ] Setup log rotation
- [ ] Monitor Gunicorn logs: `/var/www/enfantsoleilmonde/logs/gunicorn-*.log`
- [ ] Monitor Nginx logs: `/var/log/nginx/access.log` and `error.log`
- [ ] Monitor Django logs (if configured)
- [ ] Setup error alerting

### Backup Schedule
- [ ] Database backup daily: `pg_dump projectesm_db > backup.sql`
- [ ] Static files backup weekly
- [ ] Environment file backup (`.env`)
- [ ] Test backup restoration procedure

### Update Procedure
- [ ] Document update process
- [ ] Test updates in staging first
- [ ] Create backup before updates
- [ ] Run migrations: `python manage.py migrate`
- [ ] Collect static: `python manage.py collectstatic`
- [ ] Restart services: `sudo systemctl restart gunicorn`
- [ ] Monitor for errors after update

### Regular Checks (Weekly)
- [ ] Check server disk space
- [ ] Review error logs
- [ ] Check PayTech transaction logs
- [ ] Verify email delivery
- [ ] Check SSL certificate expiry
- [ ] Review payment success rate
- [ ] Monitor server resources (CPU, RAM)

### Regular Checks (Monthly)
- [ ] Update Django and dependencies: `pip list --outdated`
- [ ] Security audit: `python manage.py check --deploy`
- [ ] Review and rotate API keys (if needed)
- [ ] Test backup restoration
- [ ] Review and optimize database
- [ ] Check for Django security updates

## Emergency Procedures

### Site Down
1. Check Gunicorn: `sudo systemctl status gunicorn`
2. Check Nginx: `sudo systemctl status nginx`
3. Check logs for errors
4. Restart services if needed
5. Verify database connection

### Payment Issues
1. Check PayTech API status
2. Verify API credentials in `.env`
3. Check IPN logs
4. Contact PayTech support: +221 77 125 57 99
5. Monitor PayTech dashboard

### Database Issues
1. Check PostgreSQL: `sudo systemctl status postgresql`
2. Verify database credentials
3. Check disk space
4. Review database logs
5. Restore from backup if needed

## Rollback Plan

If deployment fails:
1. Stop Gunicorn: `sudo systemctl stop gunicorn`
2. Restore database from backup: `psql projectesm_db < backup.sql`
3. Revert code: `git checkout <previous-commit>`
4. Reinstall dependencies: `pip install -r requirements.txt`
5. Restart Gunicorn: `sudo systemctl start gunicorn`
6. Verify site functionality

## Support Contacts

- **PayTech Support**: contact@paytech.sn | +221 77 125 57 99
- **Server Admin**: [Your contact info]
- **Django Documentation**: https://docs.djangoproject.com
- **PayTech Documentation**: https://doc.intech.sn/doc_paytech.php

---

## Completion Sign-Off

Deployed by: ___________________________

Date: ___________________________

Verified by: ___________________________

Production URL: https://enfantsoleilmonde.org

Admin URL: https://enfantsoleilmonde.org/admin

---

**Note**: Keep this checklist updated as deployment procedures evolve.
