Learn Django

Install & Configure Stripe

In this lesson you'll learn how to setup Stripe. This includes installing its Python package, adding the Stripe keys as environment variables, and updating the settings file to extract those variables into the Django project.

Step 1: Create a Stripe Account

Go to Stripe's site now to create an account.

Step 2: Install the Stripe Library

Stripe offers a pre-built library to aid in Python development. Follow these steps to install it.

  1. Open a terminal, navigate to the project folder /.../crmeasy, and activate the virtualenv
  2. Execute the sudo pip install --index-url https://code.stripe.com --upgrade stripe command

Step 3: Add the Stripe Key to Dev and Prod

Sticking with our plan to keep keys outside of our code files, the next step is to set the Stripe key environment variables in the dev and prod environments. Follow these steps to do so.

Set the Dev Stripe Key Environment Variable

Use the terminal to perform the following steps. For this step use your Stripe account Test Secret Key, which can be found on Stripe's Account page.

On a Mac
  1. Type vi ~/.bash_profile
  2. Press i to allow for editing of the file
  3. Below the existing exports, add export STRIPE_SECRET_KEY=<your test secret key>
  4. Below that, type export STRIPE_PUBLISHABLE_KEY=<your publishable key>
  5. Type :wq to save the file
  6. Close the terminal and reopen it
  7. Navigate to the /.../crmeasy/ directory and activate virtualenv
On Windows
  1. Open a command prompt
  2. Type setx STRIPE_SECRET_KEY=<your test secret key>
  3. Type setx STRIPE_PUBLISHABLE_KEY=<your publishable key>

Import The Keys in the Settings.py File

We'll need to use these keys in the project code. Therefore we need to import them into the settings file.

  1. Open the /.../crmeasy/crmapp/settings.py file
  2. Add the following code to the end of the file:
1
2
3
4
5
6
# Stripe Key Settings
STRIPE_SECRET_KEY = get_env_variable('STRIPE_SECRET_KEY')
STRIPE_PUBLISHABLE_KEY = get_env_variable('STRIPE_PUBLISHABLE_KEY')

# Current Subscription Price
SUBSCRIPTION_PRICE = 1500

Set the Production Stripe Key Environment Variable (on Heroku)

In this step you'll set the environment variable on Heroku. In production you'd usually use Stripe's Live Secret and Publishable Keys. However, since this application is still in testing, use the Test keys.

  1. With a terminal open and virtualenv activated, type in heroku config:add STRIPE_SECRET_KEY=<Stripe's Test Secret Key>
  2. Then type heroku config:add STRIPE_PUBLISHABLE_KEY=<Stripe's Test Publishable Key>

You have now finished setting up Stripe's server-side configuration. There is an additional step required to complete Stripe's configuration. There are a few steps that need to be completed before we get there.

Step 4: Freeze Requirements

Go back and update the requirements.txt file to have the Stripe requirement.

  1. Navigate to /.../crmeasy/
  2. Run pip freeze > requirements.txt

Step 4: Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "installed and configured Stripe"

Track your progress with a free account