This demo uses a simple Flask application as the server.
app.py
contains the routes that interface with Stripe to create charges and receive webhook events.setup.py
a simple setup script to make some fake Products and SKUs for our Stripe store.tests/tests.py
some unit tests that test the logic of our heavier APIs likeorders/<string:id>/pay
and/webhook
.test_data.py
contains some hardcoded mocked responses to test with.inventory_manager.py
a minimal wrapper over the Stripe Python SDK that handles creating/fetching orders and products. You can override this class with your own order management system code.
You’ll need the following:
- Python 3.6.5
- Modern browser that supports ES6 (Chrome to see the Payment Request, and Safari to see Apple Pay).
- Stripe account to accept payments (sign up for free!)
Before getting started, check to see that you have the right version of Python installed (3.6.5).
python3 --version
If your machine is running Python 2 as the default then you can follow these great guides on installing Python 3 for macOS, Windows, or Linux.
Once you have Homebrew and Python 3 set up, copy the example environment variables file .env.example
from the root of the repo into your own environment file called .env
:
cp .env.example .env
Update your .env
file with your own Stripe API keys and any other configuration details you might want to add. The env variables are managed via the python-dotenv package.
Create a virtual environment to manage the packages and state our application needs:
cd server/python
python3 -m venv env
source env/bin/activate
Run pip install
to fetch the Python packages we use:
pip install -r requirements.txt
Export our Flask app and run!
export FLASK_APP=app.py
flask run
You should now see it running on http://127.0.0.1:5000/
If you want to test receiving webhooks, we recommend using ngrok to expose your local server.
First download ngrok and start your Flask application.
Run ngrok. Assuming your Flask application is running on the default port 5000, you can simply run ngrok in your Terminal in the directory where you downloaded ngrok:
./ngrok http 5000
ngrok will display a UI in your terminal telling you the new forwarding address for your Flask app. Use this URL as the URL to be called in your developer webhooks panel.
Don't forget to append /webhook
when you set up your Stripe webhook URL in the Dashboard. Example URL to be called: http://75795038.ngrok.io/webhook
.
You can find tests for the API located in the tests
directory. Make sure you're running in the virtual environment you created earlier.
souce env/bin/activate
cd tests
python tests.py
- Code: Adrienne Dreyfus