Learn Django

Create Stripe Processing Code

We'll build the payment solution of this site using Stripe's Python tutorial code. While very basic, this code demonstrates well what is needed to process credit cards. Adhering to the fat model method, the processing code will be added as a method to the Subscriber class.

In the code below, locate the charge() method. This is the method used to process the payment. Here's a quick overview of how it works.

The method takes the request, email address, and fee to be charged. It then starts out by fetching your Stripe secret key. This was added to your settings file in a previous lesson. Next, it extracts the token from the request.POST dictionary. This token was automatically generated by Stripe and added as a hidden field in the FORM data. With those two values in hand, the method proceeds to create a new customer. With the new customer established it can then charge the user.

Notice that no credit card information is being passed. In fact, only Stripe is saving the customer's credit card. All you're doing is saving the customer ID that Stripe has set for the customer. With that ID you can then charge the customer using the credit card that Stripe has saved. This is handy because it reduces the amount of sensitive data that you have to store and secure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings

import stripe  


class Subscriber(models.Model):
    user_rec = models.ForeignKey(User)
    address_one = models.CharField(max_length=100)
    address_two = models.CharField(max_length=100, blank=True)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=2)
    stripe_id = models.CharField(max_length=30, blank=True)

    class Meta:
        verbose_name_plural = 'subscribers'

    def __unicode__(self):
        return u"%s's Subscription Info" % self.user_rec

    def charge(self, request, email, fee):
        # Set your secret key: remember to change this to your live secret key
        # in production. See your keys here https://manage.stripe.com/account
        stripe.api_key = settings.STRIPE_SECRET_KEY

        # Get the credit card details submitted by the form
        token = request.POST['stripeToken']

        # Create a Customer
        stripe_customer = stripe.Customer.create(
            card=token,
            description=email
        )

        # Save the Stripe ID to the customer's profile
        self.stripe_id = stripe_customer.id
        self.save()

        # Charge the Customer instead of the card
        stripe.Charge.create(
            amount=fee, # in cents
            currency="usd",
            customer=stripe_customer.id
        )

        return stripe_customer

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 "created payment processing code"

Track your progress with a free account