Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Stripe Flask Example
import os
from flask import Flask, render_template, request
import stripe
stripe_keys = {
'secret_key': os.environ['SECRET_KEY'],
'publishable_key': os.environ['PUBLISHABLE_KEY']
stripe.api_key = stripe_keys['secret_key']
app = Flask(__name__)
def index():
return render_template('index.html', key=stripe_keys['publishable_key'])
@app.route('/charge', methods=['POST'])
def charge():
amount = 500
customer = stripe.Customer.create(
charge = stripe.Charge.create(,
description='Flask Charge'
return render_template('charge.html', amount=amount)
if __name__ == '__main__':
{% extends "layout.html" %}
{% block content %}
<h2>Thanks, you payed <strong>$5.00</strong>!</h2>
{% endblock %}
{% extends "layout.html" %}
{% block content %}
<form action="/charge" method="post">
<label>Amount: $5.00</label>
<script src="" class="stripe-button" data-key="{{ key }}"></script>
{% endblock %}
<!DOCTYPE html>
<style type="text/css" media="screen">
form article label {
display: block;
margin: 5px;
form .submit {
margin: 15px 0;
{% block content %}{% endblock %}
Copy link

jgentes commented Apr 17, 2013

A few instructions for hosting this on heroku:

  1. Create a 'Procfile' file in the root folder that includes (without quotes): "web: python"
  2. Create a 'requirements.txt' file in the root folder that includes (without quotes): "flask"
  3. Adjust the last line of to read: "'', port=int(os.environ.get("PORT")))"
  4. If desired, you can add the secret key and publishable key as environment variables using "heroku config:add" using the heroku toolbelt

Also charge.html has a misspelling: should say "you paid" :)

Cheers, and thanks for the example!


Copy link

ecaldwell commented May 31, 2013

Indentation is off (slightly overindented) in on lines 22-25 and 27-32.

Copy link

octaflop commented Jun 25, 2013

To use the amount in the charge template, you'll want to use the format filter:

<h2>Thanks, you payed <strong>$
    {{ '%(amt).2f'|format(amt=amount/100) }}

Copy link

fyears commented Jun 4, 2014

Line #22 ~ #23 indention error. There should be 2 spaces in the front of each line.

Copy link

dhamaniasad commented Mar 2, 2015

If anyone is getting a TemplateNotFound exception with this example, just make a directory called templates and put the templates in that directory, and it'll work. This is because Flask looks for templates in the templates directory.

Copy link

coreybrett commented Mar 6, 2015

I've noticed that the email address entered on the checkout ends up as the “card holders name” and the hard-coded address is the customer's email address. Any way to fix that?

Copy link

NAThompson commented May 8, 2016

customer = stripe.Customer.create( email='', card=request.form['stripeToken'] )

instead of

customer = stripe.Customer.create( email=request.form['stripeEmail'], card=request.form['stripeToken'] )?

Copy link

tb123 commented Mar 4, 2017

What about writing unit tests for all of this?

Copy link

emiranda04 commented Oct 13, 2017

How will you pass values dynamically from the script form using the POST request? For example:

amount = request.form['amount'] 
description = request.form['description']

Then you can use them in the charge object thus avoiding the hard coded values like:

charge = stripe.Charge.create( 
    customer =, 
    amount = amount, 
    currency = 'usd', 
    description = description

Copy link

thepythonist99 commented Oct 30, 2017

Cool It worked just as I expected. Thanks !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment