Skip to content

Instantly share code, notes, and snippets.

@kaiserama
Last active November 24, 2021 06:54
Show Gist options
  • Save kaiserama/a458aabcc3b3067ce308 to your computer and use it in GitHub Desktop.
Save kaiserama/a458aabcc3b3067ce308 to your computer and use it in GitHub Desktop.
Flask + Stripe Connect
<h1>
Test Charges
</h1>
<p>Your credit card will charged later.</p>
<form action="{{ url_for('charge_test') }}" id="pay_form" class="pull-right" method="post">
<script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button" data-key="{{ key }}" data-name="Application Name Here" data-description="Test Reservation" data-amount="5000"></script>
</form>
STRIPE_SITE = 'https://connect.stripe.com'
STRIPE_AUTHORIZE_URI = '/oauth/authorize'
STRIPE_TOKEN_URI = '/oauth/token'
<h1>
Payment Information
</h1>
<p>In order to recieve payment, you must create and connect an account with Stripe.</p>
<a href="{{ url_for('authorize') }}" class="stripe-connect dark"><span>Connect with Stripe</span></a>
"""
Be sure you set your stripe keys as environment variables
Be sure to include your client id as a variable
To get a client id, create an application in Stripe, grab the client id appropriate for the environment
"""
@app.route('/charge_collect', methods=['POST', 'GET'])
def charge_test():
"""
Collects credit card information and saves a shared customer, you should save the returned stripe customer id to your model, then use later to charge
"""
if request.method == 'POST':
try:
customer = stripe.Customer.create(
email=request.form['stripeEmail'],
card=request.form['stripeToken']
)
customer_id = customer.id
# save this customer_id!
except stripe.CardError, e:
# The card has been declined, should show a decline page
pass
return "charged - %s" % customer_id
return render_template('charge.test.html' key=stripe_keys['publishable_key'])
@app.route('/paymentinformation')
def paymentinformation():
"""
paymentinformation - displays a stripe connect button so a provider can connect their stripe account to your stripe application
"""
return render_template('payment.html')
@app.route('/authorize')
def authorize():
"""
authorize - is called in payment.html
"""
import urllib
site = current_app.config['STRIPE_SITE'] + current_app.config['STRIPE_AUTHORIZE_URI']
params = {
'response_type': 'code',
'scope': 'read_write',
'client_id': stripe_keys['client_id']
}
# Redirect to Stripe /oauth/authorize endpoint
url = site + '?' + urllib.urlencode(params)
return redirect(url)
@app.route('/oauth/callback')
def callback():
"""
callback - requests access tokens for provider
"""
import requests
code = request.args.get('code')
data = {
'client_secret': stripe_keys['secret_key'],
'grant_type': 'authorization_code',
'client_id': stripe_keys['client_id'],
'code': code
}
# Make /oauth/token endpoint POST request
url = current_app.config['STRIPE_SITE'] + current_app.config['STRIPE_TOKEN_URI']
resp = requests.post(url, params=data)
# Grab access_token (use this as your user's API key)
token = resp.json().get('access_token')
refresh_token = resp.json().get('refresh_token')
# save these tokens to your model!
return 'Received Access Token'
@app.route('/charge')
def charge():
"""
charge - charge an existing customer on behalf of a connected users account
"""
customer_id = get_customer_id() # get this from your customer model
access_token = get_connect_stripe_access_token() # get this from your connect user model
token = stripe.Token.create(
customer=customer_id,
api_key=access_token # user's access token from the Stripe Connect flow
)
charge = stripe.Charge.create(
amount=1000, # amount in cents
currency="usd",
card=token.id,
description="Test Stripe Charge",
application_fee=123, # fee if any
api_key=access_token
)
return 'Charged'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment