Skip to content

A Backend E-Commerce Application that utilizes REST APIs to send data seamlessly

Notifications You must be signed in to change notification settings

Atala2000/Backend-Commerce-Application

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Backend E-commerce Application

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

Requirements

  • Python 3.6+
  • Django 3.0+
  • Django Rest Framework 3.11+
  • Redis 5.0+
  • Postman 7.34+ (optional)

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/Backend-Commerce-Application.git
  2. Navigate to the project directory:

    cd Backend-Commerce-Application
  3. Create and activate a virtual environment:

    python -m venv env
    source env/bin/activate  # On Windows, use `env\Scripts\activate`
  4. Install the required packages:

    pip install -r requirements.txt
  5. Apply the migrations:

    python manage.py migrate
  6. Create a superuser:

    python manage.py createsuperuser
  7. Run the development server:

    python manage.py runserver

API Endpoints

Accounts Endpoints

  • /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",
      }
  • /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

Products Endpoints

Category View

Endpoint for listing all categories.

  • URL: /api/categories/
  • Method: GET
  • Permission: Allow any user.
  • Description: Returns a list of all categories.

Product View

Endpoint for listing all products.

  • URL: /api/products/
  • Method: GET
  • Permission: Allow any user.
  • Description: Returns a list of all products.

Update Product View

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.

Product Search View

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.

Authentication and Permissions

  • 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.

Usage with Postman

To test the endpoints using Postman:

  1. List Categories:

    • URL: http://localhost:8000/api/categories/
    • Method: GET
  2. List Products:

    • URL: http://localhost:8000/api/products/
    • Method: GET
  3. 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.
  4. Search Products:

    • URL: http://localhost:8000/api/products/search/?q=<search_term>
    • Method: GET

Cart Endpoints

View Cart

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.

List Cart Items

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.

Clear 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.

Payment Endpoints

Create Payment(Paypal)

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",
      }

Execute Payment

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",
      }

Payment History Endpoints

List Payment History

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.

Retrieve Payment History

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.

Mpesa Payment

Initiate payment

Initiate M-Pesa Payment

  • URL: /mpesa/
  • Method: POST
  • Description: This endpoint initiates a payment process using M-Pesa.
  • Name: initiate_payment

M-Pesa Callback

  • URL: /mpesa/callback/
  • Description: This endpoint handles the callback from M-Pesa after a payment is processed.
  • Name: mpesa_callback

Mpesa Order History

  • URL: /orders/history/
  • Method: GET
  • Description: This endpoint allows users to view their order history.
  • Name: order_history

Using Localtunnel or Ngrok for Public URLs

To test M-Pesa callbacks, you need a publicly accessible URL. You can use Localtunnel or Ngrok for this purpose.

Using Localtunnel

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).

Configure M-Pesa:

Use the provided URL as the callback URL in your M-Pesa configuration. For example, https://abcd.loca.lt/mpesa/callback/.

Using Ngrok

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).

Configure M-Pesa:

Use the provided URL as the callback URL in your M-Pesa configuration. For example, https://1234.ngrok.io/mpesa/callback/.

Usage

Initiate Payment:

Make a POST request to /mpesa/ with the required payment details in the request body.

Handle Callback:

The /mpesa/callback/ endpoint will automatically handle the response from M-Pesa after the payment process.

View Order History:

Navigate to /orders/history/ to view the order history.

License

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

Contributing

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/your-feature).
  5. Open a pull request.

Contact

For any inquiries, please contact: cyrilondanje@gmail.com. atalakidi@gmail.com.

About

A Backend E-Commerce Application that utilizes REST APIs to send data seamlessly

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages