Intégration de Django et Stripe – La startup

Table des matières

Intégration de Django et Stripe – La startup

Codeurs ordinaires
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

class Product(models.Model):
product_name = models.CharField(max_length=150)
product_type = models.CharField(max_length=25)
product_description = models.TextField()
product_price = models.IntegerField()

____________________________________________________________________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 %}





NamePrice
{{ 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







{% csrf_token %}






{% 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 views

app_name = "main"

urlpatterns = [
...
path("create-payment-intent", views.createpayment, name="create-payment-intent"),

]

env > mysite > main > urls.pyfrom django.urls import path
from . import views

app_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")