-
-
Save Hasimir/469597f5454e98bd273ba230977ed97d to your computer and use it in GitHub Desktop.
Stripe Flask Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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='[email protected]', | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "layout.html" %} | |
{% block content %} | |
<h2>Thanks, you payed <strong>$5.00</strong>!</h2> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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