A short Python script that creates a Stripe Checkout Session and prints the URL of its payment page. It shows the smallest working Stripe integration, with typed configuration from pydantic-settings.
Stripe Checkout is a payment page that Stripe hosts. Your server does not collect card details. It creates a Checkout Session with the items and the return URLs, then sends the buyer to the session's URL:
- The server creates a session with the line items, a success URL, and a cancel URL.
- The buyer pays on the Stripe page.
- Stripe redirects the buyer to the success URL, or to the cancel URL.
The script does step 1 for one product at 100 USD. Stripe amounts are integers in the smallest currency unit, so the amount is 10000 cents.
-
Install mise, and create a Stripe account.
-
Install the tools and dependencies, and set your test mode secret key in
.env:git clone git@github.com:tabmadi/python-stripe-example.git cd python-stripe-example mise install mise run setup cp .env.example .env -
Create a session, then open the logged URL to see the payment page:
mise run run
In test mode, the card number 4242 4242 4242 4242 pays with any future date and any CVC.
| Variable | Description | Default |
|---|---|---|
STRIPE_SECRET_KEY |
A Stripe secret key. Use a test mode key, which starts with sk_test_. It is required |
none |
PAYMENT_SUCCESS_URL |
Where Stripe sends the buyer after a payment | http://localhost:8000/success |
PAYMENT_CANCEL_URL |
Where Stripe sends the buyer after a cancel | http://localhost:8000/cancel |
| File | Holds |
|---|---|
src/main.py |
Builds the session request and logs the session URL |
src/config.py |
The settings, read from the environment and .env |
tests/test_main.py |
Checks the request without calling Stripe |
- A web server: the success and cancel URLs point to pages that this example does not serve.
- Webhooks: a real shop confirms the payment from the
checkout.session.completedwebhook, not from the redirect. A buyer can close the tab before the redirect. - Products and prices in Stripe: the price is inline in the request. A real catalog uses Price objects.
- Idempotency keys: a retry of the same request can create a second session.
| Task | Does |
|---|---|
mise run setup |
Installs the locked dependencies and the git hooks |
mise run run |
Creates a Checkout Session and logs its URL |
mise run lint |
Runs lint:py, lint:md, and lint:secrets |
mise run format |
Applies every formatter |
mise run test |
Runs the tests |
mise run check |
Runs lint and test. Run it before you finish a change |
mise run ci |
Runs exactly what CI runs. The pre-push hook runs it too |
Apache License 2.0. See LICENSE.