No project description provided
Project description
Flask-Stripe-Checkout
Flask integration of Stripe Checkout.
Stripe Checkout allows you to quickly handle payments with minimal development. This Flask extension allows you to build a cart and launch Checkout sessions in less than 10 lines of code.
Check out the example folder for a full example.
Installation & setup
Install:
$ pip install flask-stripe-checkout
Setup:
from flask import Flask
from flask_stripe_checkout import StripeCheckout
app = Flask(__name__)
StripeCheckout(app)
Setting up your Stripe account
- Create a Stripe account
- Retrieve your API key
- Register your webhook (
https://yourdomain/stripe-webhook) and retrieve your endpoint secret
To use webhooks locally:
- Download stripe-cli
- Login with
stripe login - Run
stripe listen --forward-to localhost:5000/stripe-webhook
Configure your app before initializing StripeCheckout:
app.config.update({
"STRIPE_API_KEY": "sk_...",
"STRIPE_WEBHOOKS_ENDPOINT_SECRET": "whsec_..."
})
Usage
Introduction
Flask-Stripe-Checkout provides a current_cart object that allows you to add Stripe's "line items" to it.
Once your cart is ready, a Checkout session can be started. The cart is stored in the session.
Add an endpoint to add items to your cart:
from flask_stripe_checkout import current_cart
@app.post('/add-to-cart')
def add_to_cart():
product = Product.get(request.values["product_id"]) # your custom logic to find products
current_cart.add(product.name, product.amount)
Once ready, redirect to checkout:
@app.post('/checkout')
def checkout():
return current_cart.checkout(success_url="/success")
Check out the API section for all available current_cart methods.
To process the order on successful payment, listen to the Stripe webhook event checkout.session.completed:
from flask_stripe_checkout import webhook
@webhook.checkout_session_completed()
def checkout_completed(event, session):
# process order
Using the cart blueprint
The cart blueprint is registered by default and provides endpoints to manage the cart.
When requested normally, the cart/cart.html template is returned (feel free to override it).
When requested with application/json, an array of line items is returned.
This makes it very easy to use with htmx or the standard js fetch() function.
Endpoints may be accessible through 2 different URLs:
- a REST compatible one
- another one usable directly using html forms (only using POST)
For cart pages, you can edit the layout of the pages by overriding the cart/layout.html template.
A cart_content block must be used.
cart.view
Show the cart content
GET /cart
cart.update_item
Update an item's quantity
PUT /cart/<item_idx>
POST /cart/<item_idx>/update
Where item_idx is the item index in the cart.
Provide the quantity parameter (via query string or form values).
cart.remove_item
Remove an item from the cart
DELETE /cart/<item_idx>
POST /cart/<item_idx>/remove
Where item_idx is the item index in the cart.
cart.clear
Empty the cart
DELETE /cart
POST /cart/clear
cart.checkout
Redirect to the Stripe Checkout page
POST /cart/checkout
Webhooks
When received, signals will be sent. Connect to these signal using webhook():
from flask_stripe_checkout import webhook
@webhook("charge.succeeded")
def charge_succeeded(event):
# process event
You can also quickly retrieve a stripe object:
@webhook("charge.succeeded", retrieve=stripe.Charge)
def charge_succeeded(event, charge):
# process charge
Configuration
Available configuration options:
| Config key | Extension argument | Description | Default |
|---|---|---|---|
| STRIPE_API_KEY | set stripe.api_key |
||
| STRIPE_CHECKOUT_CURRENCY | currency | default currency to use when adding items using amounts | USD |
| STRIPE_CHECKOUT_SESSION_OPTIONS | session_options | a dict of options to use for the checkout session. See checkout api doc | {} |
| STRIPE_WEBHOOKS_ENDPOINT | webhook_endpoint | the stripe webhook endpoint url (use None to disable) |
/stripe-webhooks |
| STRIPE_WEBHOOKS_ENDPOINT_SECRET | webhook_endpoint_secret | your Stripe webhook endpoint secret to validate incoming hooks | app.secret_key |
| STRIPE_REGISTER_CART_BLUEPRINT | register_cart_blueprint | whether to register the cart blueprint | True |
API
The current_cart object is a session-backed Cart instance.
flask_stripe_checkout.cart.Cart
| Attribute | Description |
|---|---|
Cart.add() |
add a new CartItem. See CartItem constructor for available parameters |
Cart.update(idx, quantity) |
update the quantity for item at index |
Cart.remove(idx) |
remove item at index |
Cart.clear() |
empty the cart |
Cart.line_items |
the list of items |
Cart.total |
total cart amount in cents |
Cart.formatted_total |
totgal cart amount formatted using format_amount() |
Cart.create_checkout_session(**session_options) |
create the stripe.checkout.Session object |
Cart.checkout(**session_options) |
create the checkout session and returns a redirect response to the checkout url |
flask_stripe_checkout.cart.CartItem
CartItem is a dict representing a Stripe line item object.
Add an item using a Price:
current_cart.add(price=price_id, quantity=1)
Add an item using a Product and a custom amount:
current_cart.add(product=product_id, amount=amount, quantity=1, currency=None)
Add a product on the fly:
current_cart.add(product_name, amount, quantity=1, currency=None)
currency defaults to the one provided using STRIPE_CHECKOUT_CURRENCY.
You can provide any other line items options.
Special properties on CartItem objects:
| Attribute | Description |
|---|---|
CartItem.price |
retrieve the Price object associated to the line item or None |
CartItem.product |
retrieve the Product object associated to the line item or None |
CartItem.description |
retrieve the product description when associated to a product or the product name from price_data |
CartItem.currency |
either from the price or from the line item directly |
CartItem.quantity |
|
CartItem.unit_amount |
either from the price or from the line item directly |
CartItem.formatted_unit_amount |
the unit_amount formatted using format_amount() |
CartItem.amount |
the unit_amount multiplied by the quantity |
CartItem.formatted_amount |
the amount formatted using format_amount() |
flask_stripe_checkout.cart.format_amount
Format an amount with the currency sign.
format_amount(amount, currency)
Available as a filter in templates:
{{ amount|format_amount(currency) }}
A limited number of currencies are supported. Add support for your own currency:
from flask_stripe_checkout import CURRENCIES
CURRENCIES["USD"] = lambda amount: f"US${amount:,.2f}"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_stripe_checkout-0.1.0.tar.gz.
File metadata
- Download URL: flask_stripe_checkout-0.1.0.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-40-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c21fed489ad0fc44e735c54cdebc357bfa67f72f200fe663aaab91d7b7a35ae
|
|
| MD5 |
059056ca3c775765e19de323d667013a
|
|
| BLAKE2b-256 |
e971f5b4b815fe563500c094521e8dda580a94a75ed5d6391e918c26762599b6
|
File details
Details for the file flask_stripe_checkout-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flask_stripe_checkout-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-40-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b280804791e9b28a94118a1b61310b53804c7cd85ab25c4c51fd11adc38eec1b
|
|
| MD5 |
91af5446fa961b5e738047abad878f7a
|
|
| BLAKE2b-256 |
e2955e0da1bae855e16816347b35d1186e2d7088fa695f674aa5af5983ef630e
|