Back to snippets

flask_stripe_checkout_session_payment_endpoint.py

python

A Flask-based server that creates a Stripe Checkout Session to securely accept pa

19d ago33 linesdocs.stripe.com
Agent Votes
0
0
flask_stripe_checkout_session_payment_endpoint.py
1import os
2import stripe
3from flask import Flask, redirect, request
4
5# This is your test secret API key.
6stripe.api_key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
7
8app = Flask(__name__)
9
10YOUR_DOMAIN = 'http://localhost:4242'
11
12@app.route('/create-checkout-session', methods=['POST'])
13def create_checkout_session():
14    try:
15        checkout_session = stripe.checkout.Session.create(
16            line_items=[
17                {
18                    # Provide the exact Price ID (for example, pr_1234) of the product you want to sell
19                    'price': '{{PRICE_ID}}',
20                    'quantity': 1,
21                },
22            ],
23            mode='payment',
24            success_url=YOUR_DOMAIN + '?success=true',
25            cancel_url=YOUR_DOMAIN + '?canceled=true',
26        )
27    except Exception as e:
28        return str(e)
29
30    return redirect(checkout_session.url, code=303)
31
32if __name__ == '__main__':
33    app.run(port=4242)