Skip to content

XusanDev07/python_stripe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Django Stripe Shop

A Django web application integrated with Stripe payment processing system for handling item purchases.

πŸš€ Features

  • Django Item Model: Store items with name, description, and price
  • Stripe Integration: Secure payment processing using Stripe Checkout
  • Admin Panel: Django admin interface for managing items
  • API Endpoints: RESTful endpoints for payment processing
  • Responsive UI: Clean HTML interface with buy functionality
  • Docker Support: Containerized deployment ready

πŸ“‹ Requirements

  • Python 3.8+
  • Django 4.2+
  • Stripe account (for API keys)

πŸ› οΈ Installation & Setup

Method 1: Local Development

  1. Clone the repository
git clone https://github.com/XusanDev07/python_stripe.git
cd python_stripe
  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Environment setup
cp .env.example .env
# Edit .env file with your Stripe keys

5Run the server

python manage.py runserver

Method 2: Docker

Build and run with Docker Compose

cp .env.example .env
# Edit .env file with your Stripe keys
docker-compose up --build

πŸ”‘ Stripe Configuration

  1. Create a Stripe account at stripe.com
  2. Get your API keys from the Stripe Dashboard
  3. Add them to your .env file:
STRIPE_PUBLISHABLE_KEY=pk_test_your_publishable_key_here
STRIPE_SECRET_KEY=sk_test_your_secret_key_here

πŸ“š API Endpoints

  • GET /item/{id}/ - Display item page with buy button
  • GET /buy/{id}/ - Create Stripe checkout session (returns session ID)
  • GET /admin/ - Django admin panel
  • GET /success/ - Payment success page
  • GET /cancel/ - Payment cancelled page

🎯 Usage Examples

View an item and make purchase:

curl -X GET http://localhost:8000/item/1/

Get Stripe session ID for payment:

curl -X GET http://localhost:8000/buy/1/

Response:

{
  "id": "cs_test_stripe_session_id_here"
}

πŸ‘¨β€πŸ’Ό Admin Access

Default credentials:

  • Username: admin
  • Password: admin
  • URL: http://localhost:8000/admin/

πŸ—οΈ Project Structure

django-stripe-shop/
β”œβ”€β”€ config/                   # Django project settings
β”‚   β”œβ”€β”€ settings.py
β”‚   β”œβ”€β”€ urls.py
β”‚   └── wsgi.py
β”œβ”€β”€ items/                  # Django app
β”‚   β”œβ”€β”€ models.py          # Item model
β”‚   β”œβ”€β”€ views.py           # API views
β”‚   β”œβ”€β”€ admin.py           # Admin configuration
β”‚   └── urls.py            # App URLs
β”œβ”€β”€ templates/             # HTML templates
β”‚   β”œβ”€β”€ base.html
β”‚   β”œβ”€β”€ home.html
β”‚   └── items/
β”‚       β”œβ”€β”€ item_detail.html
β”‚       β”œβ”€β”€ success.html
β”‚       └── cancel.html
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ requirements.txt
└── .env.example          # Environment variables template

πŸ§ͺ Testing

  1. Access the application: http://localhost:8000
  2. Create items via admin panel: http://localhost:8000/admin/
  3. View item page: http://localhost:8000/item/1/
  4. Test purchase flow using Stripe's test card numbers:
    • Card: 4242 4242 4242 4242
    • Expiry: Any future date
    • CVC: Any 3 digits
    • ZIP: Any 5 digits

🎁 Bonus Features Implemented

  • βœ… Docker support - Full containerization with docker-compose
  • βœ… Environment variables - Secure configuration management
  • βœ… Django Admin - Full admin panel for item management
  • βœ… Production ready - Ready for deployment with proper settings
  • βœ… Error handling - Comprehensive error handling and user feedback

πŸ”§ Advanced Features (Optional Extensions)

Multiple Currency Support

# Add to Item model
currency = models.CharField(max_length=3, default='USD', choices=[
    ('USD', 'US Dollar'),
    ('EUR', 'Euro'),
    ('GBP', 'British Pound'),
])

Order Management

# Additional models for orders
class Order(models.Model):
    items = models.ManyToManyField(Item, through='OrderItem')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    stripe_session_id = models.CharField(max_length=255, blank=True)


class OrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)

Discount and Tax Support

class Discount(models.Model):
    name = models.CharField(max_length=100)
    percentage = models.DecimalField(max_digits=5, decimal_places=2)
    stripe_coupon_id = models.CharField(max_length=255)


class Tax(models.Model):
    name = models.CharField(max_length=100)
    percentage = models.DecimalField(max_digits=5, decimal_places=2)
    stripe_tax_rate_id = models.CharField(max_length=255)

πŸ› Troubleshooting

Common Issues

  1. Stripe keys not working

    • Ensure you're using test keys (starting with pk_test_ and sk_test_)
    • Check that keys are properly set in .env file
  2. CSRF errors

    • The /buy/{id}/ endpoint is GET-based to avoid CSRF issues
    • For POST endpoints, ensure CSRF tokens are included
  3. Database issues

    • Run python manage.py migrate to apply migrations
  4. Static files not loading

    • Run python manage.py collectstatic
    • Ensure STATIC_ROOT is properly configured

πŸ“± API Response Examples

Successful purchase session creation:

{
  "id": "cs_test_a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R8s9T0"
}

Error response:

{
  "error": "Item not found or Stripe configuration error"
}

πŸ“ Testing Checklist

  • Items can be created via admin panel
  • Item detail page loads correctly
  • Buy button triggers Stripe checkout
  • Payment success/cancel flows work
  • Admin panel is accessible
  • API endpoints return correct responses
  • Docker containers build and run
  • Environment variables are loaded

πŸ†˜ Support

For issues and questions:

  1. Check the troubleshooting section
  2. Review Stripe documentation
  3. Create an issue in the repository

Happy coding! πŸš€

About

This task was completed for the vacancy.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published