A Django web application integrated with Stripe payment processing system for handling item purchases.
- 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
- Python 3.8+
- Django 4.2+
- Stripe account (for API keys)
- Clone the repository
git clone https://github.com/XusanDev07/python_stripe.git
cd python_stripe- Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Environment setup
cp .env.example .env
# Edit .env file with your Stripe keys5Run the server
python manage.py runserverBuild and run with Docker Compose
cp .env.example .env
# Edit .env file with your Stripe keys
docker-compose up --build- Create a Stripe account at stripe.com
- Get your API keys from the Stripe Dashboard
- Add them to your
.envfile:
STRIPE_PUBLISHABLE_KEY=pk_test_your_publishable_key_here
STRIPE_SECRET_KEY=sk_test_your_secret_key_hereGET /item/{id}/- Display item page with buy buttonGET /buy/{id}/- Create Stripe checkout session (returns session ID)GET /admin/- Django admin panelGET /success/- Payment success pageGET /cancel/- Payment cancelled page
curl -X GET http://localhost:8000/item/1/curl -X GET http://localhost:8000/buy/1/Response:
{
"id": "cs_test_stripe_session_id_here"
}Default credentials:
- Username:
admin - Password:
admin - URL:
http://localhost:8000/admin/
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
- Access the application:
http://localhost:8000 - Create items via admin panel:
http://localhost:8000/admin/ - View item page:
http://localhost:8000/item/1/ - 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
- Card:
- β 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
# Add to Item model
currency = models.CharField(max_length=3, default='USD', choices=[
('USD', 'US Dollar'),
('EUR', 'Euro'),
('GBP', 'British Pound'),
])# 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)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)-
Stripe keys not working
- Ensure you're using test keys (starting with
pk_test_andsk_test_) - Check that keys are properly set in
.envfile
- Ensure you're using test keys (starting with
-
CSRF errors
- The
/buy/{id}/endpoint is GET-based to avoid CSRF issues - For POST endpoints, ensure CSRF tokens are included
- The
-
Database issues
- Run
python manage.py migrateto apply migrations
- Run
-
Static files not loading
- Run
python manage.py collectstatic - Ensure
STATIC_ROOTis properly configured
- Run
{
"id": "cs_test_a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R8s9T0"
}{
"error": "Item not found or Stripe configuration error"
}- 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
For issues and questions:
- Check the troubleshooting section
- Review Stripe documentation
- Create an issue in the repository
Happy coding! π