This is a backend application for an e-commerce website. It is built using Django and Django Rest Framework. The application has the following features:
- User authentication
- Product management
- Order management
- Payment processing
- User profile management
- Python 3.6+
- Django 3.0+
- Django Rest Framework 3.11+
- Redis 5.0+
- Postman 7.34+ (optional)
-
Clone the repository:
git clone https://github.com/yourusername/Backend-Commerce-Application.git
-
Navigate to the project directory:
cd Backend-Commerce-Application
-
Create and activate a virtual environment:
python -m venv env source env/bin/activate # On Windows, use `env\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Apply the migrations:
python manage.py migrate
-
Create a superuser:
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
-
/accounts/register/
- Register a new user- Method:
POST
{ "email": "required_field", "password": "required_field", "first_name": "optional_field", "last_name": "optional_field", "phone_no": "optional_field", "address": "optional_field", }
- Method:
-
/accounts/login/
- Login a user{ "email": "test@example.com", "password": "testpassword" }
-
/accounts/logout/
- Logout a user- For it to work needs a user to have logged in
-
/accounts/user/
- Get the user profile
Endpoint for listing all categories.
- URL:
/api/categories/
- Method:
GET
- Permission: Allow any user.
- Description: Returns a list of all categories.
Endpoint for listing all products.
- URL:
/api/products/
- Method:
GET
- Permission: Allow any user.
- Description: Returns a list of all products.
Endpoint for updating or deleting a product by ID. Only accessible by admin users.
- URL:
/api/products/<id>/
- Methods:
GET
,PUT
,PATCH
,DELETE
- Permission: Admin users only.
- Description: Retrieve, update, or delete a product by its ID.
Endpoint for searching products by name.
- URL:
/api/products/search/
- Method:
GET
- Permission: Allow any user.
- Description: Returns a list of products matching the search query. If no query is provided, returns all products.
- Query Parameters:
q
: The search term to filter products by name.
- CSRF Protection: Enabled by default for all views except for views that are explicitly exempted.
- Admin Access: Only admin users can update or delete products.
To test the endpoints using Postman:
-
List Categories:
- URL:
http://localhost:8000/api/categories/
- Method:
GET
- URL:
-
List Products:
- URL:
http://localhost:8000/api/products/
- Method:
GET
- URL:
-
Update Product (Admin only):
- URL:
http://localhost:8000/api/products/<id>/
- Methods:
GET
,PUT
,PATCH
,DELETE
- Headers: Include the CSRF token in the
X-CSRFToken
header if CSRF protection is enabled.
- URL:
-
Search Products:
- URL:
http://localhost:8000/api/products/search/?q=<search_term>
- Method:
GET
- URL:
Endpoint for viewing the contents of the user's cart.
- URL:
/cart/
- Method:
GET
- Permission: Allow any user.
- Description: Returns the items in the user's cart.
Adding items to the cart
- URL:
/cart/items/
- Method:
POST
- Permission: Allow any user.
- Description: Returns a list of all items in the user's cart.
Endpoint for clearing the user's cart.
- URL:
/cart/clear/
- Method:
POST
- Permission: Allow any user.
- Description: Clears all items from the user's cart.
Endpoint for creating a payment.
- URL:
/payments/process/
- Method:
POST
- Permission: Allow any user.
- Description: Creates a payment for the user's cart.
-
{ "payment_method": "required_field", "total": "required_field", "price": "required_field", }
Endpoint for executing a payment.
- URL:
/payments/execute/
- Method:
POST
- Permission: Allow any user.
- Description: Executes a payment for the user's cart.
-
{ "payment_method": "required_field", "total": "required_field", "price": "required_field", }
Endpoint for listing the user's payment history.
- URL:
/history/
- Method:
GET
- Permission: Logged in user.
- Description: Returns a list of all payments made by the user.
Endpoint for retrieving a payment by ID.
- URL:
/history/<str:transaction_id>/
- Method:
GET
- Permission: Logged in user.
- Description: Returns a payment by its transaction ID.
Initiate M-Pesa Payment
- URL: /mpesa/
- Method: POST
- Description: This endpoint initiates a payment process using M-Pesa.
- Name: initiate_payment
- URL: /mpesa/callback/
- Description: This endpoint handles the callback from M-Pesa after a payment is processed.
- Name: mpesa_callback
- URL: /orders/history/
- Method: GET
- Description: This endpoint allows users to view their order history.
- Name: order_history
To test M-Pesa callbacks, you need a publicly accessible URL. You can use Localtunnel or Ngrok for this purpose.
Install Localtunnel:
bash
npm install -g localtunnel
Start Localtunnel:
bash
lt --port 8000
Replace 8000 with your Django server's port if different. Note the URL provided by Localtunnel (e.g., https://abcd.loca.lt).
Use the provided URL as the callback URL in your M-Pesa configuration. For example, https://abcd.loca.lt/mpesa/callback/.
Install Ngrok: Download and install Ngrok from ngrok.com.
Start Ngrok: bash
ngrok http 8000
Replace 8000 with your Django server's port if different. Note the URL provided by Ngrok (e.g., https://1234.ngrok.io).
Use the provided URL as the callback URL in your M-Pesa configuration. For example, https://1234.ngrok.io/mpesa/callback/.
Make a POST request to /mpesa/ with the required payment details in the request body.
The /mpesa/callback/ endpoint will automatically handle the response from M-Pesa after the payment process.
Navigate to /orders/history/ to view the order history.
This project is licensed under the MIT License. See the LICENSE file for details.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature
). - Open a pull request.
For any inquiries, please contact: cyrilondanje@gmail.com. atalakidi@gmail.com.