Skip to content

Brints/alx_travel_app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ALX Travel App - Django with Chapa Payment Integration

This is a travel application built using Django that allows users to browse listings, make bookings, and process payments through the Chapa payment gateway.

Features

  • User authentication and authorization
  • Browse and search travel listings
  • Create and manage bookings
  • Integrated Chapa payment gateway
  • Payment verification and status tracking
  • Review and rating system

Prerequisites

  • Python 3.8+
  • pip
  • PostgreSQL/SQLite (default)
  • Chapa API account

Setting Up the Project

1. Clone the Repository

git clone <repository-url>
cd alx-travel-app

2. Create a Virtual Environment

python -m venv venv
source venv/bin/activate  # On Windows use `venv\Scripts\activate`

3. Install Dependencies

pip install -r requirements.txt

4. Configure Environment Variables

Create a .env file in the project root and add the following variables:

SECRET_KEY=your-django-secret-key
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1

# Database Configuration (Optional - defaults to SQLite)
DATABASE_URL=postgresql://user:password@localhost:5432/alx_travel_db

# Chapa API Configuration
CHAPA_SECRET_KEY=your-chapa-secret-key
CHAPA_PUBLIC_KEY=your-chapa-public-key

# Email Configuration (for payment confirmations)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_HOST_USER=your-email@gmail.com
EMAIL_HOST_PASSWORD=your-email-password

5. Apply Migrations

python manage.py makemigrations
python manage.py migrate

6. Setup and Seed Database

python manage.py setup_db --seed

7. Create a Superuser

python manage.py createsuperuser

8. Run the Development Server

python manage.py runserver

9. Start the Celery Worker (for async tasks)

In a new terminal, activate the virtual environment and run:

celery -A config worker -l info

Documentation

The API documentation is available at http://localhost:8000/api/docs/ once the server is running.

API Endpoints

Authentication

  • POST /api/auth/register/ - Register a new user
  • POST /api/auth/login/ - Login user and obtain JWT token
  • POST /api/auth/logout/ - Logout user

Listings

  • GET /api/listings/ - Retrieve all listings
  • GET /api/listings/<id>/ - Retrieve a specific listing
  • POST /api/listings/ - Create a new listing (authenticated)
  • PUT /api/listings/<id>/ - Update a listing (authenticated)
  • DELETE /api/listings/<id>/ - Delete a listing (authenticated)

Bookings

  • POST /api/bookings/ - Create a new booking (authenticated)
  • GET /api/bookings/<id>/ - Retrieve a specific booking (authenticated)
  • GET /api/bookings/user/ - Retrieve bookings for the logged-in user (authenticated)
  • PUT /api/bookings/<id>/ - Update a booking (authenticated)
  • DELETE /api/bookings/<id>/ - Cancel a booking (authenticated)

Payments

  • POST /api/payments/initiate/ - Initiate a payment (authenticated)
{
  "booking_id": "uuid",
  "return_url": "http://yourapp.com/success"
}
  • GET /api/payments/verify/?tx_ref=BK-{booking_id} - Verify payment status (authenticated)
  • GET /api/payments/ - Retrieve payment history for the logged-in user (authenticated)
  • GET /api/payments/<id>/ - Retrieve a specific payment (authenticated)

Payment Workflow

  1. User creates a booking via the /api/bookings/ endpoint.
{
  "listing_id": "uuid",
  "check_in_date": "2024-01-15",
  "check_out_date": "2024-01-20"
}
  1. User initiates payment via the /api/payments/initiate/ endpoint.
{
  "booking_id": "booking-uuid",
  "return_url": "http://localhost:8000/payment/success"
}

Response

{
  "payment_url": "https://checkout.chapa.co/checkout/payment/...",
  "reference": "BK-booking-uuid",
  "status": "success"
}
  1. User is redirected to Chapa's payment page to complete the transaction.
  2. After payment, Chapa redirects the user to the specified return URL.
  3. The application verifies the payment status via the /api/payments/verify/ endpoint.
GET /api/payments/verify/?tx_ref=BK-booking-uuid

Response

{
  "reference": "BK-booking-uuid",
  "status": "completed",
  "amount": "5000.00"
}

Project Structure

alx_travel_app/
├── alx_travel_app/
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── listings/
│   ├── models.py          # Listing, Booking, Payment, Review models
│   ├── serializers.py     # DRF serializers
│   ├── views.py           # ViewSets and API endpoints
│   ├── urls.py            # URL routing
│   └── tests.py           # Unit tests
├── manage.py
├── requirement.txt
├── .env
└── README.md

About

Travel App that allows users to list properties, make bookings and securely pay for any property.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors