Intégration de Django et Stripe – La startup
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_saveclass Product(models.Model):
____________________________________________________________________env > mysite > main > views.pyfrom django.shortcuts import render, redirect
product_name = models.CharField(max_length=150)
product_type = models.CharField(max_length=25)
product_description = models.TextField()
product_price = models.IntegerField()
from .models import Product, Profile...
def checkout(request)
____________________________________________________________________env > mysite > main > templates > main > checkout.html{% extends "main/header.html" %}
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"){% 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 %}Checkout
...Billing
{% endblock%}
env> mysite> main> templates> main> checkout.html{% étend "main / header.html"%}{% block content%}
...
{% endblock%}
env > mysite > main > urls.pyfrom django.urls import path
from . import viewsapp_name = "main"
urlpatterns = [
...
path("create-payment-intent", views.createpayment, name="create-payment-intent"),]
env > mysite > main > urls.pyfrom django.urls import path
from . import viewsapp_name = "main"
urlpatterns = [
...
path("create-payment-intent", views.createpayment, name="create-payment-intent"),
path("payment-complete", views.paymentcomplete, name="payment-complete"),]
____________________________________________________________________env > mysite > main > views.pydef paymentcomplete(request):
if request.method=="POST":
data = json.loads(request.POST.get("payload"))
if data["status"] == "succeeded":
# save purchase here/ setup email confirmation
return render(request, "main/payment-complete.html")