Integrate Stripe payment flow into django

I’m trying to integrate stripe custom payments flow into my Django ecom website as PayPal isn’t as good but the docs (https://stripe.com/docs/payments/quickstart?lang=python) for python are in the flask framework. Does anyone have boilerplate code for handling a simple transaction for this in Django (views, template etc.)

In theory, the only thing that needs to change is the flask part of that code and change it into Django view(s).
The rest, html+js+css, should be able to be copied + pasted (especially cause the html is dynamically created by the Stripe JS)

views.py

from django.shortcuts import render
from django.http import HttpResponse

# The GET checkout form
#
# urlpattern: 
#   path('checkout', views.checkout, name='checkout'),
def checkout(request):
    return render(request, 'checkout.html')

# The POST checkout form
#
# urlpattern: 
#   path('create-payment-intent', views.create_payment, name='create_payment'),
def create_payment(request):
    if request.method == 'POST':
        import json
        try:
            data = json.loads(request.POST)

            def calculate_order_amount(items):
                # Replace this constant with a calculation of the order's amount
                # Calculate the order total on the server to prevent
                # people from directly manipulating the amount on the client
                return 1400

            # this api_key could possibly go into settings.py like:
            #   STRIPE_API_KEY = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
            #
            # and fetched out with:
            #   from django.conf import settings
            #   stripe.api_key = settings.STRIPE_API_KEY

            import stripe
            stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'

            # Create a PaymentIntent with the order amount and currency
            intent = stripe.PaymentIntent.create(
                amount=calculate_order_amount(data['items']),
                currency='usd',
                automatic_payment_methods={
                    'enabled': True,
                },
            )
            return HttpResponse(
                json.dumps({'clientSecret': intent['client_secret']}),
                content_type='application/json'
            )
        except Exception as e:

            # Just return the 403 and NO error msg
            #   Not sure how to raise a 403 AND return the error msg
            from django.http import HttpResponseForbidden
            return HttpResponseForbidden()

            # OR you could return just the error msg
            #   but the js would need to be changed to handle this
            return HttpResponse(
                json.dumps({'error': str(e)}),
                content_type='application/json'
            )

    # POST only View, Raise Error
    from django.http import Http404
    raise Http404

Note: You might also have to change the two urls in the .js to match your django URLS. What they have "/create-payment-intent" + "http://localhost:4242/checkout.html" (not sure why that 2nd one is full url, but remember to get the port correct)


This would just be the barebones that your URL you included shows, you still have to figure out how to get the items into checkout.html, dynamically pass them to the page + eventually to the POST and then redo calculate_order_amount(items)

To understand working with Stripepayement I am sharing my GIT URL in which stripe payment is integrated you may refer

     https://github.com/KBherwani/BookManagement/ 
Back to Top