A simple Flask application that integrates Stripe Checkout Sessions for accepting donation payments.
- Flask backend
- Stripe Checkout payment flow
- Donation button
- Success page
- Cancel page
- Stripe webhook endpoint
- Environment variable configuration with
.env
flask_stripe_payment/
├── app.py
├── requirements.txt
├── .env.example
└── templates/
├── index.html
├── thankyou.html
└── cancel.html
- Python 3.10+
- A Stripe account
- Stripe test API keys
Clone the repository:
git clone https://github.com/chrispsk/flask_stripe_payment.git
cd flask_stripe_paymentCreate a virtual environment:
python -m venv .venvActivate the virtual environment:
# Windows
.venv\Scripts\activate# macOS / Linux
source .venv/bin/activateInstall the dependencies:
pip install -r requirements.txtCreate a .env file in the root of the project.
You can copy the example file:
# Windows
copy .env.example .env# macOS / Linux
cp .env.example .envThen update .env with your own Stripe keys:
STRIPE_SECRET_KEY=sk_test_your_secret_key_here
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here
DOMAIN=http://localhost:5000Do not commit your .env file to GitHub.
Start the Flask app:
python app.pyOpen your browser and go to:
http://localhost:5000
Click the donation button to start a Stripe Checkout payment.
Use Stripe's test card:
4242 4242 4242 4242
Use any future expiration date, any CVC, and any ZIP/postal code.
To test Stripe webhooks locally, install the Stripe CLI and run:
stripe listen --forward-to localhost:5000/webhookStripe will return a webhook signing secret that starts with:
whsec_
Copy that value into your .env file:
STRIPE_WEBHOOK_SECRET=whsec_your_real_webhook_secretThe app listens for the following event:
checkout.session.completed
This event is triggered when a Checkout payment is completed successfully.
This project is intended for learning and testing purposes.
Before using it in production, you should:
- Add proper database storage for payments
- Add logging
- Improve error handling
- Configure production Stripe keys
- Set
DOMAINto your production domain - Deploy over HTTPS
- Keep all secret keys out of GitHub
This project is open source and available for educational use.