Django PayPal Payment API
A robust Django REST API for handling PayPal payments with comprehensive error handling, logging, and proper separation of concerns.
π Features
- Complete PayPal payment integration
- RESTful API endpoints
- Comprehensive error handling
- Detailed logging system
- Database tracking of payment status
- Support for payment execution and refunds
- Clean architecture with separation of concerns
π Technology Stack
- Python 3.8+
- Django 4.2+
- Django REST Framework
- PayPal REST SDK
- PostgreSQL (recommended) / SQLite
π Prerequisites
Before you begin, ensure you have:
- Python 3.8 or higher installed
- A PayPal Developer account
- PayPal API credentials (Client ID and Secret)
- PostgreSQL (recommended) or SQLite
βοΈ Installation
- Clone the repository:
git clone https://github.com/pemochamdev/PayPal-Payment-using-django.git
cd PayPal-Payment-using-django
- Create and activate a virtual environment:
python -m venv env
source env/bin/activate # On Windows: env\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Create environment variables file (.env):
DJANGO_SECRET_KEY=your_secret_key
PAYPAL_CLIENT_ID=your_paypal_client_id
PAYPAL_CLIENT_SECRET=your_paypal_client_secret
PAYPAL_MODE=sandbox # or 'live' for production
- Run migrations:
python manage.py makemigrations
python manage.py migrate
π Usage
Starting the Server
python manage.py runserver
API Endpoints
Create Payment
POST /api/payments/
Request body:
{
"amount": 10.00,
"description": "Payment description"
}
Response:
{
"id": "uuid",
"paypal_payment_id": "PAYID-...",
"approval_url": "https://www.sandbox.paypal.com/..."
}
Execute Payment
POST /api/payments/execute/
Request body:
{
"paymentId": "PAYID-...",
"PayerID": "BUYER-ID"
}
Get Payment Details
GET /api/payments/{payment_id}/
Request Refund
POST /api/payments/{payment_id}/refund/
Request body:
{
"amount": 10.00,
"reason": "Customer request"
}
π Project Structure
paypal-payment-using-django/
-config(project name)
-payments(app)
-requirements.txt
-READMED.md
π‘ Implementation Details
Models
The project uses two main models:
-
Payment
: Tracks payment information and status -
PaymentRefund
: Handles refund information
Service Layer
The PayPalService
class handles all PayPal-related operations:
- Payment creation
- Payment execution
- Refund processing
- Error handling
- Logging
Error Handling
Custom exceptions for different scenarios:
-
PaymentError
: Base payment exception -
PaymentValidationError
: For validation issues -
PaymentProcessError
: For processing errors -
RefundError
: For refund-related issues
π Security Considerations
- All sensitive credentials are stored in environment variables
- Input validation for all API endpoints
- CSRF protection enabled
- Proper error handling to prevent information leakage
- Logging of sensitive data is sanitized
π Logging
The application implements comprehensive logging:
- Payment creation attempts
- Execution status
- Error tracking
- PayPal API responses
- Database operations
π Environment Configuration
Development
PAYPAL_MODE = "sandbox"
DEBUG = True
Production
PAYPAL_MODE = "live"
DEBUG = False
π§ Testing
Run the test suite:
python manage.py test
π€ Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π₯ Author
Your Name
- GitHub: @pemochamdev
- LinkedIn: pempeme mohamed chamsoudine
π Acknowledgments
- PayPal Developer Documentation
- Django Documentation
- Django REST Framework Documentation
π Support
For support, email pemochamdev@gmail.com or open an issue on GitHub.
Top comments (0)