Skip to content

Instantly share code, notes, and snippets.

@kiran
Created November 12, 2013 21:16
Show Gist options
  • Save kiran/7438868 to your computer and use it in GitHub Desktop.
Save kiran/7438868 to your computer and use it in GitHub Desktop.
stripe checkout flask implementation
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
formatted_address = _build_address(request.form)
customer = stripe.Customer.create(
email=request.form['stripeEmail'],
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, formatted_address=formatted_address)
def _build_address(form):
name = form['stripeShippingName']
line1 = form['stripeShippingAddressLine1']
zipcode = form['stripeShippingAddressZip']
city = form['stripeShippingAddressCity']
country = form['stripeShippingAddressCountry']
formatted_address = "{0} at {1}, {2}, {3} {4}".format(name, line1, city, country, zipcode)
return formatted_address
if __name__ == '__main__':
app.run(debug=True)
{% extends "layout.html" %}
{% block content %}
<h2>Thanks, you paid <strong>{{amount}}</strong>!</h2>
<p>Shipping to {{formatted_address}}</p>
{% endblock %}
{% extends "layout.html" %}
{% block content %}
<form action="/charge" method="post">
<article>
<label>
<span>Amount is $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ key }}"
data-description="A Flask Charge"
data-shippingAddress=true
data-amount="500"></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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment