Skip to content
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

89 lines (70 sloc) 2.94 KB
#! /usr/bin/env python3.6
Stripe Sample.
Python 3.6 or newer required.
import stripe
import json
import os
from flask import Flask, render_template, jsonify, request, send_from_directory
from dotenv import load_dotenv, find_dotenv
# Setup Stripe python client library
stripe.api_key = os.getenv('STRIPE_SECRET_KEY')
stripe.api_version = os.getenv('STRIPE_API_VERSION')
static_dir = str(os.path.abspath(os.path.join(__file__ , "..", os.getenv("STATIC_DIR"))))
app = Flask(__name__, static_folder=static_dir,
static_url_path="", template_folder=static_dir)
@app.route('/checkout', methods=['GET'])
def get_checkout_page():
# Display checkout page
return render_template('index.html')
def calculate_order_amount(items):
# Replace this constant with a calculation of the order's amount
# Calculate the order total on the server to prevent
# people from directly manipulating the amount on the client
return 1400
@app.route('/create-payment-intent', methods=['POST'])
def create_payment():
data = json.loads(
# Create a PaymentIntent with the order amount and currency
intent = stripe.PaymentIntent.create(
# Send publishable key and PaymentIntent details to client
return jsonify({'publishableKey': os.getenv('STRIPE_PUBLISHABLE_KEY'), 'clientSecret': intent.client_secret})
except Exception as e:
return jsonify(str(e)), 403
@app.route('/webhook', methods=['POST'])
def webhook_received():
# You can use webhooks to receive information about asynchronous payment events.
# For more about our webhook events check out
webhook_secret = os.getenv('STRIPE_WEBHOOK_SECRET')
request_data = json.loads(
if webhook_secret:
# Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured.
signature = request.headers.get('stripe-signature')
event = stripe.Webhook.construct_event(, sig_header=signature, secret=webhook_secret)
data = event['data']
except Exception as e:
return e
# Get the type of webhook event sent - used to check the status of PaymentIntents.
event_type = event['type']
data = request_data['data']
event_type = request_data['type']
data_object = data['object']
if event_type == 'payment_intent.succeeded':
print('💰 Payment received!')
# Fulfill any orders, e-mail receipts, etc
# To cancel the payment you will need to issue a Refund (
elif event_type == 'payment_intent.payment_failed':
print('❌ Payment failed.')
return jsonify({'status': 'success'})
if __name__ == '__main__':
You can’t perform that action at this time.