How to Create a Fully Functional E-commerce Website with Django

Andika Pratama
Analytics Vidhya
Published in
3 min readFeb 13, 2020

Step 5 of 5 : Payment Handling with Stripe

https://www.shuup.com/wp-content/uploads/2017/12/python-plus-django-1.jpg

In this part of the tutorial we will process payments using stripe.

What is Stripe ?

Stripe is a payment processor, which means it supports the electronic transfer of money from a customer’s bank (issuing bank), into a merchant’s bank (acquiring bank) as payment for goods or services bought with a credit card. full information regarding the stripe can be seen on the following link :

Prerequisites

  • Django Latest Version
  • Anaconda (Optional) to make Virtual Environment
  • Visual Studio Code (Optional) as Code Editor
  • Stripe

Before starting the tutorial don’t forget to activate your Virtual Environment, and the project we created in the previous tutorial. if you don’t have it downloaded here :

Installing Stripe

To install the stripe you can do this using the command below :

$ pip install stripe

And now sripe is instaling in your device. In this tutorial we will use the stripe API to handle payments. how to use Stripe API can be seen in the Stripe API documentation by using the following link :

Make sure you use python language

Models

Open your models.py file and we will create a Payment model class to save payment information using stripe :

This model will store information relating to payments using stripe such as the ID of the stripe, who pays, how much and when the payment is made

After create a Payment model do not forget migrate your model database with the command below :

$ python manage.py migrate
$ python manage.py makemigrations

Complete models.py code can be seen in the following link:

Views

In the view replace the CheckoutView class that we created earlier using the code below :

This time the Checkout View Class will redirect to payment using stripe if the stripe option is selected

Then take the payment.html template in the following link and place it in the templates directory

suggestion: replace all the files in the templates directory with the files in the link

after that we will create a PaymentView class with a stripe in it. for that follow the following tutorial :

  1. Import
...
from .models import (
...,
Payment
)
import stripe
stripe.api_key = settings.STRIPE_KEY

In the import column, import Payment model that we made earlier and don’t forget to import the stripe because we will use it here

2. Settings

Open your settings.py file in your project name directory and add this line code in your bottom line :

STRIPE_KEY = '<your_api_key_here>'

Enter your Stripe API secret code to that constanta variable

3. Create PaymentView

The main part of Payment view is the try section, while error handling can be seen in the following documentation:

The post function will store payment information using the stripe to the Payment model we made earlier

Complete views.py code can be seen in the following link:

Urls

As usual after the view is ready to use, now we will create the url address used to carry out the function

Add PaymentView in Import

from .views import (
...,
PaymentView
)

Add path url to view in PaymentView classes

urlpatterns = [
...,
path('payment/<payment_option>/',
PaymentView.as_view(), name='payment')
]

And now you can use payments with stripe, you can check the payment.html file in the templates directory to find out how I can call and post on the payment form with stripe

Complete Source Code this Part :

Closing remarks

Thank you for following this tutorial to the end. with this tutorial “How to Create a Fully Functional E-commerce Website with Django” has been completed. Thank you very much for those of you who read this article

--

--

Andika Pratama
Analytics Vidhya

Fresh Graduate of Computer Science at Universitas Syiah Kuala, Software Engineer. Check my github on github.com/Andika7