Sample Flask + Stripe application.
Tutorial and explanation written up on medium.
Update your /etc/hosts
file by adding the following line:
0.0.0.0 mystripeapp.local
Clone down the repository.
The details in this readme are assuming you clone down the repo as mystripeapp
so you may experience issues or have to slightly alter commands if you clone it down as another directory name.
$ git clone https://github.com/holdenrehg/sample_flask_stripe_integration mystripeapp
There is a set of environment variables located under mystripeapp/utils/__init__.py
that need to be updated before running the application
The only two environment variables that you should need to change to get the application up and running are the stripe token and the stripe product code.
def environment():
"""
This is not how you would want to handle environments in a real project,
but for the sake of simplicity I'll create this function.
Look at using environment variables or dotfiles for these.
"""
return {
"app": {"name": "mystripeapp.local", "port": "5200", "secret": "my_super_secret_key"},
"billing": {"stripe": {"token": "****", "product": "****"}},
"database": {
"provider": "mysql",
"host": "mariadb",
"port": "3306",
"username": "stripeapp",
"password": "stripeapp",
"database": "stripeapp",
},
}
It's also information for you to add your public stripe key to the javascript on the register form. See the mystripeapp/ui/views/auth/register.html
file.
// Create a Stripe client.
var stripe = Stripe('****');
$ cd mystripeapp
$ docker-compose up
Make sure to leave the application running before migrating the database:
$ docker-compose exec app flask db upgrade
You'll be able to access at http://mystripeapp.local:5200 .
You can create an account using fake Stripe cards found at https://stripe.com/docs/testing . Use these on the http://mystripeapp.local:5200/register registration form.
$ python3 setup.py test --container $(docker ps -q --filter name=mystripeapp_app)
$ python3 setup.py test --container $(docker ps -q --filter name=mystripeapp_app) --no-coverage
Since we are running the application with docker, here are some useful commands to know:
# Run a command inside of the app container
$ docker exec -it $(docker ps -q --filter=mystripeapp_app) {command}
$ docker exec -it $(docker ps -q --filter=mystripeapp_app) ipython3
$ docker exec -it $(docker ps -q --filter=mystripeapp_app) bash
$ docker exec -it $(docker ps -q --filter=mystripeapp_app) flask init db
While developing you'll often need some simple knowledge of the docker-compose commands such as:
$ docker-compose stop
$ docker-compose restart