DEV Community

Cover image for Django PayPal Payment API
PEMPEME MOHAMED CHAMSOUDINE
PEMPEME MOHAMED CHAMSOUDINE

Posted on

Django PayPal Payment API

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

  1. Clone the repository:
git clone https://github.com/pemochamdev/PayPal-Payment-using-django.git
cd PayPal-Payment-using-django
Enter fullscreen mode Exit fullscreen mode
  1. Create and activate a virtual environment:
python -m venv env
source env/bin/activate  # On Windows: env\Scripts\activate
Enter fullscreen mode Exit fullscreen mode
  1. Install dependencies:
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Run migrations:
python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

πŸš€ Usage

Starting the Server

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

API Endpoints

Create Payment

POST /api/payments/
Enter fullscreen mode Exit fullscreen mode

Request body:

{
    "amount": 10.00,
    "description": "Payment description"
}
Enter fullscreen mode Exit fullscreen mode

Response:

{
    "id": "uuid",
    "paypal_payment_id": "PAYID-...",
    "approval_url": "https://www.sandbox.paypal.com/..."
}
Enter fullscreen mode Exit fullscreen mode

Execute Payment

POST /api/payments/execute/
Enter fullscreen mode Exit fullscreen mode

Request body:

{
    "paymentId": "PAYID-...",
    "PayerID": "BUYER-ID"
}
Enter fullscreen mode Exit fullscreen mode

Get Payment Details

GET /api/payments/{payment_id}/
Enter fullscreen mode Exit fullscreen mode

Request Refund

POST /api/payments/{payment_id}/refund/
Enter fullscreen mode Exit fullscreen mode

Request body:

{
    "amount": 10.00,
    "reason": "Customer request"
}
Enter fullscreen mode Exit fullscreen mode

πŸ“ Project Structure

paypal-payment-using-django/
    -config(project name)
    -payments(app)
    -requirements.txt
    -READMED.md
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Implementation Details

Models

The project uses two main models:

  1. Payment: Tracks payment information and status
  2. 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
Enter fullscreen mode Exit fullscreen mode

Production

PAYPAL_MODE = "live"
DEBUG = False
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Testing

Run the test suite:

python manage.py test
Enter fullscreen mode Exit fullscreen mode

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘₯ Author

Your Name

πŸ™ Acknowledgments

  • PayPal Developer Documentation
  • Django Documentation
  • Django REST Framework Documentation

πŸ“ž Support

For support, email pemochamdev@gmail.com or open an issue on GitHub.

Logo du Projet
Documentation avec Swagger

Top comments (0)