env > mysite > main > models.pyfrom django.db import models from django.contrib.auth.models import User from django.dispatch import receiver from django.db.models.signals import post_save
____________________________________________________________________env > mysite > main > views.pyfrom django.shortcuts import render, redirect from .models import Product, Profile
...
def checkout(request) if request.user.is_authenticated: cart = Profile.objects.get(user= request.user).cart total = cart.aggregate(Sum('product_price'))['product_price__sum'] return render(request,"main/checkout.html", {"cart":cart, "total":total}) else: redirect("main:homepage")
____________________________________________________________________env > mysite > main > templates > main > checkout.html{% extends "main/header.html" %}
{% block content %}
Checkout
{% for c in cart.all %}
{% endfor %}
Name
Price
{{ c.product_name }}
${{ c.product_price }}
Total
${{ total }}
{% endblock %}
pip install --upgrade stripe
env > mysite > main > views.pyfrom django.shortcuts import render, redirect from .models import Product, Profile from django.views.decorators.csrf import csrf_exempt import stripe import json from django.http import JsonResponse
...
def checkout(request) ...
@csrf_exempt def createpayment(request): if request.user.is_authenticated: cart = Profile.objects.get(user=request.user).products total = cart.aggregate(Sum('product_price'))['product_price__sum'] total = total * 100 stripe.api_key = 'your test secret key' if request.method=="POST":
data = json.loads(request.body) # Create a PaymentIntent with the order amount and currency intent = stripe.PaymentIntent.create( amount=total, currency=data['currency'], metadata={'integration_check': 'accept_a_payment'}, ) try: return JsonResponse({'publishableKey': 'your test publishable key', 'clientSecret': intent.client_secret}) except Exception as e: return JsonResponse({'error':str(e)},status= 403)
env > mysite > main > templates > main > checkout.html{% extends "main/header.html" %}{% block content %}