Skip to content

Instantly share code, notes, and snippets.

@maccman
Created August 8, 2012 23:30
Show Gist options
  • Star 68 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save maccman/3299715 to your computer and use it in GitHub Desktop.
Save maccman/3299715 to your computer and use it in GitHub Desktop.
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__)
@app.route('/')
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(
email='customer@example.com',
card=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Flask Charge'
)
return render_template('charge.html', amount=amount)
if __name__ == '__main__':
app.run(debug=True)
{% 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">
<article>
<label>Amount: $5.00</label>
</article>
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button" data-key="{{ key }}"></script>
</form>
{% endblock %}
<!DOCTYPE html>
<html>
<head>
<title>Stripe</title>
<style type="text/css" media="screen">
form article label {
display: block;
margin: 5px;
}
form .submit {
margin: 15px 0;
}
</style>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
@NAThompson
Copy link

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

instead of

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

@tb123
Copy link

tb123 commented Mar 4, 2017

What about writing unit tests for all of this?

@emiranda04
Copy link

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 = customer.id, 
    amount = amount, 
    currency = 'usd', 
    description = description
 )

@thepythonist99
Copy link

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