# 🚀 Quick Setup Guide - Paydunya Integration

## ⚡ Quick Start (5 minutes)

### Step 1: Install Paydunya SDK
```bash
source venv/bin/activate
pip install paydunya
```

### Step 2: Configure Environment Variables
Add these to your `.env` file:

```bash
# Paydunya Test Configuration
PAYDUNYA_MASTER_KEY=wQzk9ZwR-Qq9m-0hD0-zpud-je5coGC3FHKW
PAYDUNYA_PRIVATE_KEY=test_private_rMIdJM3PLLhLjyArx9tF3VURAF5
PAYDUNYA_TOKEN=IivOiOxGJuWhc5znlIiK
PAYDUNYA_MODE=test

# Store Info
PAYDUNYA_STORE_NAME=ONG Enfants Soleil Sénégal
PAYDUNYA_STORE_TAGLINE=Different Make Me Amazing!
PAYDUNYA_STORE_PHONE=+221336530583
PAYDUNYA_STORE_POSTAL_ADDRESS=Dakar, Sénégal
PAYDUNYA_STORE_WEBSITE=http://localhost:8000
PAYDUNYA_STORE_LOGO=http://localhost:8000/static/theme/assets/img/ONG-Enfants-Soleil-Senegal-LOGO.jpeg

# Local Development URLs
PAYDUNYA_CALLBACK_URL=http://localhost:8000/paydunya/ipn/
PAYDUNYA_RETURN_URL=http://localhost:8000/paydunya/success/
PAYDUNYA_CANCEL_URL=http://localhost:8000/paydunya/cancel/
```

### Step 3: Migrations Already Applied ✅
```bash
# Already done! But if needed:
python manage.py migrate
```

### Step 4: Start Server and Test
```bash
python manage.py runserver
```

Then visit: http://localhost:8000

---

## 🧪 Testing Checklist

### Test 1: Anonymous Donation
- [ ] Go to homepage
- [ ] Select "Paydunya" radio button
- [ ] Enter amount: 1000 FCFA
- [ ] Click "Faire un DON"
- [ ] Should redirect to Paydunya payment page
- [ ] Complete payment (test mode charges 100-150 XOF)
- [ ] Should return to success page
- [ ] Check receipt PDF download link

### Test 2: Authenticated Donation
- [ ] Login to your account
- [ ] Select "Paydunya" radio button
- [ ] Enter amount: 5000 FCFA
- [ ] Complete payment
- [ ] Verify donation in admin panel

### Test 3: Payment Cancellation
- [ ] Start donation process
- [ ] On Paydunya page, click "Annuler"
- [ ] Should return to cancel page
- [ ] Verify donation status = 'rejected' in admin

### Test 4: PayTech Still Works
- [ ] Select "PayTech" radio button
- [ ] Verify PayTech flow still works normally

---

## 📊 Admin Panel Verification

1. Go to: http://localhost:8000/admin/
2. Check `appsponsor > Donations`
3. Verify new fields visible:
   - **Payment Provider** (PayTech/Paydunya)
   - **Invoice Token** (Paydunya token)
   - **Receipt URL** (PDF link)

---

## 🔧 Troubleshooting

### Error: "ModuleNotFoundError: No module named 'paydunya'"
```bash
pip install paydunya
```

### Error: "KeyError: 'PAYDUNYA_MASTER_KEY'"
Add Paydunya configuration to `.env` file (see Step 2)

### Error: "OperationalError: no such column"
```bash
python manage.py migrate
```

### Payment stuck on "pending"
- Check logs: `python manage.py shell` → `from appsponsor.models import Donation` → `Donation.objects.last()`
- Verify IPN URL is accessible (use ngrok for local testing)

---

## 🎨 What Users See

### Homepage Changes
- **Before:** Only amount input
- **After:** Provider selector (PayTech/Paydunya) + amount input

### Success Page
- Payment status (Completed/Pending)
- Amount display
- **PDF receipt download button** (Paydunya feature)
- Action buttons (return home, make another donation)

### Cancel Page
- Clear cancellation message
- Retry payment button
- Help/contact information

---

## 📝 Files Changed Summary

```
✅ appsponsor/models.py                     - Added Paydunya fields
✅ appsponsor/paydunya_service.py          - New service layer
✅ appsponsor/views_paydunya.py            - New views
✅ appsponsor/urls.py                      - Added Paydunya endpoints
✅ projectesm/settings.py                  - Paydunya configuration
✅ .env.example                            - Environment variables
✅ templates/appsponsor/index.html         - Provider selector
✅ templates/appsponsor/paydunya_success.html - Success page
✅ templates/appsponsor/paydunya_cancel.html  - Cancel page
✅ appsponsor/migrations/0004_*.py         - Database migration
✅ PAYDUNYA_INTEGRATION.md                 - Full documentation
```

---

## 🔜 Next Steps (Optional)

### 1. Activate IPN Handler (Later)
When ready to use IPN notifications:
1. Uncomment `paydunya_ipn_view` in [views_paydunya.py](appsponsor/views_paydunya.py)
2. Uncomment URL pattern in [urls.py](appsponsor/urls.py)
3. Configure IPN URL with ngrok for testing

### 2. Production Deployment
- Replace test keys with production keys
- Set `PAYDUNYA_MODE=live`
- Update callback URLs to HTTPS
- Test with real payments

### 3. User Profile Page (Future)
- Create dashboard for users
- Show donation history
- Download receipts
- Manage subscriptions

---

## 📞 Support

**Documentation:** See [PAYDUNYA_INTEGRATION.md](PAYDUNYA_INTEGRATION.md) for full details

**Paydunya Docs:** https://developers.paydunya.com/doc/FR/Python

**Issues?** Check logs in Django admin → Audits → Logs

---

**Status:** ✅ **Ready to Test**  
**Date:** April 3, 2026  
**Next:** Test the donation flow!
